From 997514292a8492d2291643e91081f3e790eefbaf Mon Sep 17 00:00:00 2001 From: tjpcc Date: Tue, 24 Jan 2023 19:59:47 -0700 Subject: testing and linting and linter fixes --- gemini/serve.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'gemini/serve.go') diff --git a/gemini/serve.go b/gemini/serve.go index dd7ad52..abed257 100644 --- a/gemini/serve.go +++ b/gemini/serve.go @@ -5,6 +5,7 @@ import ( "context" "crypto/tls" "errors" + "fmt" "io" "net" "strconv" @@ -15,11 +16,13 @@ import ( "tildegit.org/tjp/gus/logging" ) +type titanRequestBodyKey struct{} + // TitanRequestBody is the key set in a handler's context for titan requests. // // When this key is present in the context (request.URL.Scheme will be "titan"), the // corresponding value is a *bufio.Reader from which the request body can be read. -const TitanRequestBody = "titan_request_body" +var TitanRequestBody = titanRequestBodyKey{} type server struct { ctx context.Context @@ -70,7 +73,7 @@ func NewServer( // but be aware that Close() must still be called in that case to avoid // dangling goroutines. // -// On titan protocol requests, it sets a key/value pair in the context. The +// On titan protocol requests it sets a key/value pair in the context. The // key is TitanRequestBody, and the value is a *bufio.Reader from which the // request body can be read. func (s *server) Serve() error { @@ -88,7 +91,7 @@ func (s *server) Serve() error { if s.Closed() { err = nil } else { - s.errorLog.Log("msg", "accept_error", "error", err) + _ = s.errorLog.Log("msg", "accept error", "error", err) } return err @@ -135,7 +138,7 @@ func (s *server) handleConn(conn net.Conn) { } else { req.Server = s req.RemoteAddr = conn.RemoteAddr() - if tlsconn, ok := conn.(*tls.Conn); req != nil && ok { + if tlsconn, ok := conn.(*tls.Conn); ok { state := tlsconn.ConnectionState() req.TLSState = &state } @@ -146,12 +149,20 @@ func (s *server) handleConn(conn net.Conn) { if err == nil { ctx = context.WithValue( ctx, - "titan_request_body", + TitanRequestBody, io.LimitReader(buf, int64(len)), ) } } + defer func() { + if r := recover(); r != nil { + err := fmt.Errorf("%s", r) + _ = s.errorLog.Log("msg", "panic in handler", "err", err) + _, _ = io.Copy(conn, NewResponseReader(Failure(err))) + } + }() + response = s.handler(ctx, req) if response == nil { response = NotFound("Resource does not exist.") -- cgit v1.2.3