12// Server represents an HTTP server
18// ListenAndServe will serve traffic on HTTP or HTTPS depending on TLS options
19func (s *Server) ListenAndServe() {
20 if s.Opts.TLSKeyFile != "" || s.Opts.TLSCertFile != "" {
27// ServeHTTP constructs a net.Listener and starts handling HTTP requests
28func (s *Server) ServeHTTP() {
29 HTTPAddress := s.Opts.HTTPAddress
32 i := strings.Index(HTTPAddress, "://")
34 scheme = HTTPAddress[0:i]
37 var networkType string
45 slice := strings.SplitN(HTTPAddress, "//", 2)
46 listenAddr := slice[len(slice)-1]
48 listener, err := net.Listen(networkType, listenAddr)
50 log.Fatalf("FATAL: listen (%s, %s) failed - %s", networkType, listenAddr, err)
52 log.Printf("HTTP: listening on %s", listenAddr)
54 server := &http.Server{Handler: s.Handler}
55 err = server.Serve(listener)
56 if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
57 log.Printf("ERROR: http.Serve() - %s", err)
60 log.Printf("HTTP: closing %s", listener.Addr())
63// ServeHTTPS constructs a net.Listener and starts handling HTTPS requests
64func (s *Server) ServeHTTPS() {
65 addr := s.Opts.HTTPSAddress
66 config := &tls.Config{
67 MinVersion: tls.VersionTLS12,
68 MaxVersion: tls.VersionTLS12,
70 if config.NextProtos == nil {
71 config.NextProtos = []string{"http/1.1"}
75 config.Certificates = make([]tls.Certificate, 1)
76 config.Certificates[0], err = tls.LoadX509KeyPair(s.Opts.TLSCertFile, s.Opts.TLSKeyFile)
78 log.Fatalf("FATAL: loading tls config (%s, %s) failed - %s", s.Opts.TLSCertFile, s.Opts.TLSKeyFile, err)
81 ln, err := net.Listen("tcp", addr)
83 log.Fatalf("FATAL: listen (%s) failed - %s", addr, err)
85 log.Printf("HTTPS: listening on %s", ln.Addr())
87 tlsListener := tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, config)
88 srv := &http.Server{Handler: s.Handler}
89 err = srv.Serve(tlsListener)
91 if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
92 log.Printf("ERROR: https.Serve() - %s", err)
95 log.Printf("HTTPS: closing %s", tlsListener.Addr())
98// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
99// connections. It's used by ListenAndServe and ListenAndServeTLS so
100// dead TCP connections (e.g. closing laptop mid-download) eventually
102type tcpKeepAliveListener struct {
106func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
107 tc, err := ln.AcceptTCP()
111 tc.SetKeepAlive(true)
112 tc.SetKeepAlivePeriod(3 * time.Minute)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list