aboutsummaryrefslogtreecommitdiff
path: root/server.go
diff options
context:
space:
mode:
authorHugo Wetterberg <[email protected]>2021-01-05 20:16:33 +0100
committerAdnan Maolood <[email protected]>2021-01-05 18:33:36 -0500
commitefef44c2f9939f8095247da56018bda711e14801 (patch)
treeaa6ad38d3ef9649bfaa6f9e6679b0f209dee0787 /server.go
parentclient: Close connection for unsuccessful responses (diff)
downloadgo-gemini-efef44c2f9939f8095247da56018bda711e14801.tar.xz
go-gemini-efef44c2f9939f8095247da56018bda711e14801.zip
server: abort request handling on bad requests
A request to a hostname that hasn't been registered with the server currently results in a nil pointer deref panic in server.go:215 as request handling continues even if ReadRequest() returns an error. This change changes all if-else error handling in Server.respond() to a WriteStatus-call and early return. This makes it clear when request handling is aborted (and actually aborts when ReadRequest() fails).
Diffstat (limited to 'server.go')
-rw-r--r--server.go30
1 files changed, 16 insertions, 14 deletions
diff --git a/server.go b/server.go
index 5643dcd..1f9078a 100644
--- a/server.go
+++ b/server.go
@@ -188,27 +188,29 @@ func (s *Server) respond(conn net.Conn) {
req, err := ReadRequest(conn)
if err != nil {
w.WriteStatus(StatusBadRequest)
- } else {
- // Store information about the TLS connection
- if tlsConn, ok := conn.(*tls.Conn); ok {
- req.TLS = tlsConn.ConnectionState()
- if len(req.TLS.PeerCertificates) > 0 {
- peerCert := req.TLS.PeerCertificates[0]
- // Store the TLS certificate
- req.Certificate = &tls.Certificate{
- Certificate: [][]byte{peerCert.Raw},
- Leaf: peerCert,
- }
+ return
+ }
+
+ // Store information about the TLS connection
+ if tlsConn, ok := conn.(*tls.Conn); ok {
+ req.TLS = tlsConn.ConnectionState()
+ if len(req.TLS.PeerCertificates) > 0 {
+ peerCert := req.TLS.PeerCertificates[0]
+ // Store the TLS certificate
+ req.Certificate = &tls.Certificate{
+ Certificate: [][]byte{peerCert.Raw},
+ Leaf: peerCert,
}
}
}
resp := s.responder(req)
- if resp != nil {
- resp.Respond(w, req)
- } else {
+ if resp == nil {
w.WriteStatus(StatusNotFound)
+ return
}
+
+ resp.Respond(w, req)
}
func (s *Server) responder(r *Request) Responder {