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.
This commit is contained in:
Sasha Koshka 2023-05-26 00:38:12 -04:00
parent 24d70951c9
commit f6d3c47816
3 changed files with 8 additions and 26 deletions

View File

@ -52,7 +52,7 @@ func fingerprint(cert *x509.Certificate) string {
} }
func profile(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) { func profile(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
tls := r.TLS() tls := r.TLS
if len(tls.PeerCertificates) == 0 { if len(tls.PeerCertificates) == 0 {
w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required") w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required")
return 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) { func changeUsername(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
tls := r.TLS() tls := r.TLS
if len(tls.PeerCertificates) == 0 { if len(tls.PeerCertificates) == 0 {
w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required") w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required")
return return

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"crypto/tls" "crypto/tls"
"io" "io"
"net"
"net/url" "net/url"
) )
@ -28,8 +27,7 @@ type Request struct {
// This field is ignored by the Gemini server. // This field is ignored by the Gemini server.
Certificate *tls.Certificate Certificate *tls.Certificate
conn net.Conn TLS *tls.ConnectionState
tls *tls.ConnectionState
} }
// NewRequest returns a new request. // NewRequest returns a new request.
@ -98,30 +96,11 @@ func (r *Request) WriteTo(w io.Writer) (int64, error) {
return wrote, bw.Flush() 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 // ServerName returns the value of the TLS Server Name Indication extension
// sent by the client. // sent by the client.
// ServerName returns an empty string for client requests. // ServerName returns an empty string for client requests.
func (r *Request) ServerName() string { func (r *Request) ServerName() string {
if tls := r.TLS(); tls != nil { if tls := r.TLS; tls != nil {
return tls.ServerName return tls.ServerName
} }
return "" return ""

View File

@ -371,7 +371,10 @@ func (srv *Server) goServeConn(ctx context.Context, conn net.Conn) error {
w.WriteHeader(StatusBadRequest, "Bad request") w.WriteHeader(StatusBadRequest, "Bad request")
return w.Flush() return w.Flush()
} }
req.conn = conn if tlsConn, ok := conn.(*tls.Conn); ok {
state := tlsConn.ConnectionState()
req.TLS = &state
}
h := srv.Handler h := srv.Handler
if h == nil { if h == nil {