request: Add Conn and TLS methods

This commit is contained in:
Adnan Maolood 2021-02-23 17:27:54 -05:00
parent 83c904913f
commit c688defefd
2 changed files with 17 additions and 27 deletions

View File

@ -23,9 +23,6 @@ type Request struct {
// For international domain names, Host may be in Punycode or
// Unicode form. Use golang.org/x/net/idna to convert it to
// either format if needed.
//
// For server requests, Host specifies the host on which the URL
// is sought.
Host string
// For client requests, Certificate optionally specifies the
@ -33,20 +30,7 @@ type Request struct {
// This field is ignored by the Gemini server.
Certificate *tls.Certificate
// RemoteAddr allows Gemini servers and other software to record
// the network address that sent the request, usually for
// logging. This field is not filled in by ReadRequest.
// This field is ignored by the Gemini client.
RemoteAddr net.Addr
// TLS allows Gemini servers and other software to record
// information about the TLS connection on which the request
// was received. This field is not filled in by ReadRequest.
// The Gemini server in this package sets the field for
// TLS-enabled connections before invoking a handler;
// otherwise it leaves the field nil.
// This field is ignored by the Gemini client.
TLS *tls.ConnectionState
conn net.Conn
}
// NewRequest returns a new request.
@ -111,3 +95,18 @@ func (r *Request) Write(w io.Writer) error {
}
return bw.Flush()
}
// Conn returns the network connection on which the request was received.
func (r *Request) Conn() net.Conn {
return r.conn
}
// TLS returns information about the TLS connection on which the
// response was received.
func (r *Request) TLS() *tls.ConnectionState {
if tlsConn, ok := r.conn.(*tls.Conn); ok {
state := tlsConn.ConnectionState()
return &state
}
return nil
}

View File

@ -366,16 +366,7 @@ func (srv *Server) serveConn(ctx context.Context, conn net.Conn) error {
w.WriteHeader(StatusBadRequest, "Bad request")
return w.Flush()
}
// Store the TLS connection state
if tlsConn, ok := conn.(*tls.Conn); ok {
state := tlsConn.ConnectionState()
req.TLS = &state
req.Host = state.ServerName
}
// Store remote address
req.RemoteAddr = conn.RemoteAddr()
req.conn = conn
h := srv.Handler
if h == nil {