aboutsummaryrefslogtreecommitdiff
path: root/handler.go
diff options
context:
space:
mode:
authorAdnan Maolood <[email protected]>2021-02-20 15:49:07 -0500
committerAdnan Maolood <[email protected]>2021-02-20 15:49:14 -0500
commite9a68917c9f47245ee4107157cff0a6dab83690b (patch)
treedfcbb06b1e6ab70b8fd816f7e134a414f90d8cbe /handler.go
parentexamples/client: Remove dependency on go-xdg (diff)
downloadgo-gemini-e9a68917c9f47245ee4107157cff0a6dab83690b.tar.xz
go-gemini-e9a68917c9f47245ee4107157cff0a6dab83690b.zip
handler: Make ServeGemini accept a Context
Diffstat (limited to 'handler.go')
-rw-r--r--handler.go21
1 files changed, 11 insertions, 10 deletions
diff --git a/handler.go b/handler.go
index 1b553ec..a85ba3c 100644
--- a/handler.go
+++ b/handler.go
@@ -1,6 +1,7 @@
package gemini
import (
+ "context"
"net/url"
"strings"
)
@@ -21,17 +22,17 @@ import (
// response but the server doesn't log an error, panic with the value
// ErrAbortHandler.
type Handler interface {
- ServeGemini(ResponseWriter, *Request)
+ ServeGemini(context.Context, ResponseWriter, *Request)
}
// The HandlerFunc type is an adapter to allow the use of ordinary functions
// as Gemini handlers. If f is a function with the appropriate signature,
// HandlerFunc(f) is a Handler that calls f.
-type HandlerFunc func(ResponseWriter, *Request)
+type HandlerFunc func(context.Context, ResponseWriter, *Request)
-// ServeGemini calls f(w, r).
-func (f HandlerFunc) ServeGemini(w ResponseWriter, r *Request) {
- f(w, r)
+// ServeGemini calls f(ctx, w, r).
+func (f HandlerFunc) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
+ f(ctx, w, r)
}
// RedirectHandler returns a request handler that redirects each request it
@@ -48,12 +49,12 @@ type redirectHandler struct {
url string
}
-func (h *redirectHandler) ServeGemini(w ResponseWriter, r *Request) {
+func (h *redirectHandler) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
w.WriteHeader(h.code, h.url)
}
// NotFound replies to the request with a Gemini 51 not found error.
-func NotFound(w ResponseWriter, r *Request) {
+func NotFound(ctx context.Context, w ResponseWriter, r *Request) {
w.WriteHeader(StatusNotFound, "Not found")
}
@@ -73,7 +74,7 @@ func StripPrefix(prefix string, h Handler) Handler {
if prefix == "" {
return h
}
- return HandlerFunc(func(w ResponseWriter, r *Request) {
+ return HandlerFunc(func(ctx context.Context, w ResponseWriter, r *Request) {
p := strings.TrimPrefix(r.URL.Path, prefix)
rp := strings.TrimPrefix(r.URL.RawPath, prefix)
if len(p) < len(r.URL.Path) && (r.URL.RawPath == "" || len(rp) < len(r.URL.RawPath)) {
@@ -83,9 +84,9 @@ func StripPrefix(prefix string, h Handler) Handler {
*r2.URL = *r.URL
r2.URL.Path = p
r2.URL.RawPath = rp
- h.ServeGemini(w, r2)
+ h.ServeGemini(ctx, w, r2)
} else {
- NotFound(w, r)
+ NotFound(ctx, w, r)
}
})
}