diff options
| author | Adnan Maolood <[email protected]> | 2020-12-18 01:41:14 -0500 |
|---|---|---|
| committer | Adnan Maolood <[email protected]> | 2020-12-18 01:42:05 -0500 |
| commit | d34d5df89e9a0d1cf8b3f8e95ce10d08ff91600b (patch) | |
| tree | ab893d372686e431241e599e2ce819929d37ffc4 /server.go | |
| parent | Expose Request.Write and Response.Read functions (diff) | |
| download | go-gemini-d34d5df89e9a0d1cf8b3f8e95ce10d08ff91600b.tar.xz go-gemini-d34d5df89e9a0d1cf8b3f8e95ce10d08ff91600b.zip | |
Add ReadRequest and ReadResponse functions
Diffstat (limited to 'server.go')
| -rw-r--r-- | server.go | 53 |
1 files changed, 14 insertions, 39 deletions
@@ -6,7 +6,6 @@ import ( "errors" "log" "net" - "net/url" "strconv" "strings" "time" @@ -174,6 +173,7 @@ func (s *Server) getCertificateFor(hostname string) (*tls.Certificate, error) { // respond responds to a connection. func (s *Server) respond(conn net.Conn) { + defer conn.Close() if d := s.ReadTimeout; d != 0 { conn.SetReadDeadline(time.Now().Add(d)) } @@ -181,58 +181,33 @@ func (s *Server) respond(conn net.Conn) { conn.SetWriteDeadline(time.Now().Add(d)) } - r := bufio.NewReader(conn) w := NewResponseWriter(conn) - // Read requested URL - rawurl, err := r.ReadString('\r') + defer w.b.Flush() + + req, err := ReadRequest(conn) if err != nil { - return - } - // Read terminating line feed - if b, err := r.ReadByte(); err != nil { - return - } else if b != '\n' { - w.WriteStatus(StatusBadRequest) - } - // Trim carriage return - rawurl = rawurl[:len(rawurl)-1] - // Ensure URL is valid - if len(rawurl) > 1024 { - w.WriteStatus(StatusBadRequest) - } else if url, err := url.Parse(rawurl); err != nil || url.User != nil { - // Note that we return an error status if User is specified in the URL w.WriteStatus(StatusBadRequest) } else { // Store information about the TLS connection - var connState tls.ConnectionState - var cert *tls.Certificate if tlsConn, ok := conn.(*tls.Conn); ok { - connState = tlsConn.ConnectionState() - if len(connState.PeerCertificates) > 0 { - peerCert := connState.PeerCertificates[0] + req.TLS = tlsConn.ConnectionState() + if len(req.TLS.PeerCertificates) > 0 { + peerCert := req.TLS.PeerCertificates[0] // Store the TLS certificate - cert = &tls.Certificate{ + req.Certificate = &tls.Certificate{ Certificate: [][]byte{peerCert.Raw}, Leaf: peerCert, } } } + } - req := &Request{ - URL: url, - RemoteAddr: conn.RemoteAddr(), - TLS: connState, - Certificate: cert, - } - resp := s.responder(req) - if resp != nil { - resp.Respond(w, req) - } else { - w.WriteStatus(StatusNotFound) - } + resp := s.responder(req) + if resp != nil { + resp.Respond(w, req) + } else { + w.WriteStatus(StatusNotFound) } - w.b.Flush() - conn.Close() } func (s *Server) responder(r *Request) Responder { |