aboutsummaryrefslogtreecommitdiff
path: root/fs.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 /fs.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 'fs.go')
-rw-r--r--fs.go19
1 files changed, 10 insertions, 9 deletions
diff --git a/fs.go b/fs.go
index 52a4f77..4942ccf 100644
--- a/fs.go
+++ b/fs.go
@@ -1,6 +1,7 @@
package gemini
import (
+ "context"
"fmt"
"io"
"io/fs"
@@ -31,8 +32,8 @@ type fileServer struct {
fs.FS
}
-func (fs fileServer) ServeGemini(w ResponseWriter, r *Request) {
- serveFile(w, r, fs, path.Clean(r.URL.Path), true)
+func (fs fileServer) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
+ serveFile(ctx, w, r, fs, path.Clean(r.URL.Path), true)
}
// ServeContent replies to the request using the content in the
@@ -41,11 +42,11 @@ func (fs fileServer) ServeGemini(w ResponseWriter, r *Request) {
//
// ServeContent tries to deduce the type from name's file extension.
// The name is otherwise unused; it is never sent in the response.
-func ServeContent(w ResponseWriter, r *Request, name string, content io.Reader) {
- serveContent(w, name, content)
+func ServeContent(ctx context.Context, w ResponseWriter, r *Request, name string, content io.Reader) {
+ serveContent(ctx, w, name, content)
}
-func serveContent(w ResponseWriter, name string, content io.Reader) {
+func serveContent(ctx context.Context, w ResponseWriter, name string, content io.Reader) {
// Detect mimetype from file extension
ext := path.Ext(name)
mimetype := mime.TypeByExtension(ext)
@@ -73,7 +74,7 @@ func serveContent(w ResponseWriter, name string, content io.Reader) {
// Outside of those two special cases, ServeFile does not use r.URL.Path for
// selecting the file or directory to serve; only the file or directory
// provided in the name argument is used.
-func ServeFile(w ResponseWriter, r *Request, fsys fs.FS, name string) {
+func ServeFile(ctx context.Context, w ResponseWriter, r *Request, fsys fs.FS, name string) {
if containsDotDot(r.URL.Path) {
// Too many programs use r.URL.Path to construct the argument to
// serveFile. Reject the request under the assumption that happened
@@ -83,7 +84,7 @@ func ServeFile(w ResponseWriter, r *Request, fsys fs.FS, name string) {
w.WriteHeader(StatusBadRequest, "invalid URL path")
return
}
- serveFile(w, r, fsys, name, false)
+ serveFile(ctx, w, r, fsys, name, false)
}
func containsDotDot(v string) bool {
@@ -100,7 +101,7 @@ func containsDotDot(v string) bool {
func isSlashRune(r rune) bool { return r == '/' || r == '\\' }
-func serveFile(w ResponseWriter, r *Request, fsys fs.FS, name string, redirect bool) {
+func serveFile(ctx context.Context, w ResponseWriter, r *Request, fsys fs.FS, name string, redirect bool) {
const indexPage = "/index.gmi"
// Redirect .../index.gmi to .../
@@ -172,7 +173,7 @@ func serveFile(w ResponseWriter, r *Request, fsys fs.FS, name string, redirect b
return
}
- serveContent(w, name, f)
+ serveContent(ctx, w, name, f)
}
func dirList(w ResponseWriter, f fs.File) {