From f6d3c478165d8f92c951073a38abb3bbade2db51 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 26 May 2023 00:38:12 -0400 Subject: [PATCH] Export TLS & remove conn in request struct This makes it possible to fully create another request outside of this module (which Hnakra will need) and has better parity with net/http. --- examples/auth.go | 4 ++-- request.go | 25 ++----------------------- server.go | 5 ++++- 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/examples/auth.go b/examples/auth.go index 2a76612..e808afa 100644 --- a/examples/auth.go +++ b/examples/auth.go @@ -52,7 +52,7 @@ func fingerprint(cert *x509.Certificate) string { } func profile(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) { - tls := r.TLS() + tls := r.TLS if len(tls.PeerCertificates) == 0 { w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required") return @@ -68,7 +68,7 @@ func profile(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) { } func changeUsername(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) { - tls := r.TLS() + tls := r.TLS if len(tls.PeerCertificates) == 0 { w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required") return diff --git a/request.go b/request.go index 0613716..59bfb13 100644 --- a/request.go +++ b/request.go @@ -4,7 +4,6 @@ import ( "bufio" "crypto/tls" "io" - "net" "net/url" ) @@ -28,8 +27,7 @@ type Request struct { // This field is ignored by the Gemini server. Certificate *tls.Certificate - conn net.Conn - tls *tls.ConnectionState + TLS *tls.ConnectionState } // NewRequest returns a new request. @@ -98,30 +96,11 @@ func (r *Request) WriteTo(w io.Writer) (int64, error) { return wrote, bw.Flush() } -// Conn returns the network connection on which the request was received. -// Conn returns nil for client requests. -func (r *Request) Conn() net.Conn { - return r.conn -} - -// TLS returns information about the TLS connection on which the -// request was received. -// TLS returns nil for client requests. -func (r *Request) TLS() *tls.ConnectionState { - if r.tls == nil { - if tlsConn, ok := r.conn.(*tls.Conn); ok { - state := tlsConn.ConnectionState() - r.tls = &state - } - } - return r.tls -} - // ServerName returns the value of the TLS Server Name Indication extension // sent by the client. // ServerName returns an empty string for client requests. func (r *Request) ServerName() string { - if tls := r.TLS(); tls != nil { + if tls := r.TLS; tls != nil { return tls.ServerName } return "" diff --git a/server.go b/server.go index a8ff77b..766e849 100644 --- a/server.go +++ b/server.go @@ -371,7 +371,10 @@ func (srv *Server) goServeConn(ctx context.Context, conn net.Conn) error { w.WriteHeader(StatusBadRequest, "Bad request") return w.Flush() } - req.conn = conn + if tlsConn, ok := conn.(*tls.Conn); ok { + state := tlsConn.ConnectionState() + req.TLS = &state + } h := srv.Handler if h == nil {