summaryrefslogtreecommitdiff
path: root/gemini/serve.go
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-01-24 19:59:47 -0700
committertjpcc <tjp@ctrl-c.club>2023-01-24 19:59:47 -0700
commit997514292a8492d2291643e91081f3e790eefbaf (patch)
treeed870f158c6a6adfd91e490a64276bb704b56619 /gemini/serve.go
parent23d705b93a89cb0aee582eda819a76257f42dffc (diff)
testing and linting and linter fixes
Diffstat (limited to 'gemini/serve.go')
-rw-r--r--gemini/serve.go21
1 files changed, 16 insertions, 5 deletions
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.")