mirror of
https://git.sr.ht/~adnano/go-gemini
synced 2024-11-23 12:42:13 +01:00
Make ResponseWriter an interface
Make ResponseWriter an interface with an unexported method. Implementors must embed a ResponseWriter from elsewhere. This gives us the flexibility of an interface while allowing us to add new methods in the future.
This commit is contained in:
parent
06dc7eed08
commit
36a67255e0
12
fs.go
12
fs.go
@ -33,7 +33,7 @@ type fileServer struct {
|
||||
fs.FS
|
||||
}
|
||||
|
||||
func (fs fileServer) ServeGemini(ctx context.Context, w *ResponseWriter, r *Request) {
|
||||
func (fs fileServer) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
|
||||
serveFile(w, r, fs, path.Clean(r.URL.Path), true)
|
||||
}
|
||||
|
||||
@ -43,11 +43,11 @@ func (fs fileServer) ServeGemini(ctx context.Context, w *ResponseWriter, r *Requ
|
||||
//
|
||||
// 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) {
|
||||
func ServeContent(w ResponseWriter, r *Request, name string, content io.Reader) {
|
||||
serveContent(w, name, content)
|
||||
}
|
||||
|
||||
func serveContent(w *ResponseWriter, name string, content io.Reader) {
|
||||
func serveContent(w ResponseWriter, name string, content io.Reader) {
|
||||
// Detect mimetype from file extension
|
||||
ext := path.Ext(name)
|
||||
mimetype := mime.TypeByExtension(ext)
|
||||
@ -75,7 +75,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(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
|
||||
@ -102,7 +102,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(w ResponseWriter, r *Request, fsys fs.FS, name string, redirect bool) {
|
||||
const indexPage = "/index.gmi"
|
||||
|
||||
// Redirect .../index.gmi to .../
|
||||
@ -177,7 +177,7 @@ func serveFile(w *ResponseWriter, r *Request, fsys fs.FS, name string, redirect
|
||||
serveContent(w, name, f)
|
||||
}
|
||||
|
||||
func dirList(w *ResponseWriter, f fs.File) {
|
||||
func dirList(w ResponseWriter, f fs.File) {
|
||||
var entries []fs.DirEntry
|
||||
var err error
|
||||
d, ok := f.(fs.ReadDirFile)
|
||||
|
12
handler.go
12
handler.go
@ -21,23 +21,23 @@ import (
|
||||
//
|
||||
// Handlers should not modify the provided Request.
|
||||
type Handler interface {
|
||||
ServeGemini(context.Context, *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(context.Context, *ResponseWriter, *Request)
|
||||
type HandlerFunc func(context.Context, ResponseWriter, *Request)
|
||||
|
||||
// ServeGemini calls f(ctx, w, r).
|
||||
func (f HandlerFunc) ServeGemini(ctx context.Context, w *ResponseWriter, r *Request) {
|
||||
func (f HandlerFunc) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
|
||||
f(ctx, w, r)
|
||||
}
|
||||
|
||||
// StatusHandler returns a request handler that responds to each request
|
||||
// with the provided status code and meta.
|
||||
func StatusHandler(status Status, meta string) Handler {
|
||||
return HandlerFunc(func(ctx context.Context, w *ResponseWriter, r *Request) {
|
||||
return HandlerFunc(func(ctx context.Context, w ResponseWriter, r *Request) {
|
||||
w.WriteHeader(status, meta)
|
||||
})
|
||||
}
|
||||
@ -58,7 +58,7 @@ func StripPrefix(prefix string, h Handler) Handler {
|
||||
if prefix == "" {
|
||||
return h
|
||||
}
|
||||
return HandlerFunc(func(ctx context.Context, 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)) {
|
||||
@ -92,7 +92,7 @@ type timeoutHandler struct {
|
||||
dt time.Duration
|
||||
}
|
||||
|
||||
func (t *timeoutHandler) ServeGemini(ctx context.Context, w *ResponseWriter, r *Request) {
|
||||
func (t *timeoutHandler) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
|
||||
ctx, cancel := context.WithTimeout(ctx, t.dt)
|
||||
defer cancel()
|
||||
|
||||
|
2
mux.go
2
mux.go
@ -212,7 +212,7 @@ func (mux *ServeMux) Handler(r *Request) Handler {
|
||||
|
||||
// ServeGemini dispatches the request to the handler whose
|
||||
// pattern most closely matches the request URL.
|
||||
func (mux *ServeMux) ServeGemini(ctx context.Context, w *ResponseWriter, r *Request) {
|
||||
func (mux *ServeMux) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
|
||||
h := mux.Handler(r)
|
||||
h.ServeGemini(ctx, w, r)
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
|
||||
type nopHandler struct{}
|
||||
|
||||
func (*nopHandler) ServeGemini(context.Context, *ResponseWriter, *Request) {}
|
||||
func (*nopHandler) ServeGemini(context.Context, ResponseWriter, *Request) {}
|
||||
|
||||
func TestServeMuxMatch(t *testing.T) {
|
||||
type Match struct {
|
||||
|
129
response.go
129
response.go
@ -120,7 +120,70 @@ func (r *Response) TLS() *tls.ConnectionState {
|
||||
//
|
||||
// A ResponseWriter may not be used after the Handler.ServeGemini method
|
||||
// has returned.
|
||||
type ResponseWriter struct {
|
||||
type ResponseWriter interface {
|
||||
// SetMediaType sets the media type that will be sent by Write for a
|
||||
// successful response. If no media type is set, a default of
|
||||
// "text/gemini; charset=utf-8" will be used.
|
||||
//
|
||||
// Setting the media type after a call to Write or WriteHeader has
|
||||
// no effect.
|
||||
SetMediaType(mediatype string)
|
||||
|
||||
// Write writes the data to the connection as part of a Gemini response.
|
||||
//
|
||||
// If WriteHeader has not yet been called, Write calls WriteHeader with
|
||||
// StatusSuccess and the media type set in SetMediaType before writing the data.
|
||||
// If no media type was set, Write uses a default media type of
|
||||
// "text/gemini; charset=utf-8".
|
||||
Write([]byte) (int, error)
|
||||
|
||||
// WriteHeader sends a Gemini response header with the provided
|
||||
// status code and meta.
|
||||
//
|
||||
// If WriteHeader is not called explicitly, the first call to Write
|
||||
// will trigger an implicit call to WriteHeader with a successful
|
||||
// status code and the media type set in SetMediaType.
|
||||
//
|
||||
// The provided code must be a valid Gemini status code.
|
||||
// The provided meta must not be longer than 1024 bytes.
|
||||
// Only one header may be written.
|
||||
WriteHeader(status Status, meta string)
|
||||
|
||||
// Flush sends any buffered data to the client.
|
||||
Flush() error
|
||||
|
||||
// Close closes the connection.
|
||||
// Any blocked Write operations will be unblocked and return errors.
|
||||
Close() error
|
||||
|
||||
// Conn returns the underlying network connection.
|
||||
// To take over the connection, use Hijack.
|
||||
Conn() net.Conn
|
||||
|
||||
// TLS returns information about the underlying TLS connection.
|
||||
TLS() *tls.ConnectionState
|
||||
|
||||
// Hijack lets the caller take over the connection.
|
||||
// After a call to Hijack the Gemini server library
|
||||
// will not do anything else with the connection.
|
||||
// It becomes the caller's responsibility to manage
|
||||
// and close the connection.
|
||||
//
|
||||
// The returned net.Conn may have read or write deadlines
|
||||
// already set, depending on the configuration of the
|
||||
// Server. It is the caller's responsibility to set
|
||||
// or clear those deadlines as needed.
|
||||
Hijack() net.Conn
|
||||
|
||||
reset(io.WriteCloser)
|
||||
|
||||
// unexported method so we can extend this interface over time
|
||||
// without breaking existing code. Implementers must embed a concrete
|
||||
// type from elsewhere.
|
||||
unexported()
|
||||
}
|
||||
|
||||
type responseWriter struct {
|
||||
bw *bufio.Writer
|
||||
cl io.Closer
|
||||
mediatype string
|
||||
@ -130,38 +193,26 @@ type ResponseWriter struct {
|
||||
conn net.Conn
|
||||
}
|
||||
|
||||
func newResponseWriter(w io.WriteCloser) *ResponseWriter {
|
||||
return &ResponseWriter{
|
||||
func newResponseWriter(w io.WriteCloser) *responseWriter {
|
||||
return &responseWriter{
|
||||
bw: bufio.NewWriter(w),
|
||||
cl: w,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *ResponseWriter) reset(wc io.WriteCloser) {
|
||||
func (w *responseWriter) reset(wc io.WriteCloser) {
|
||||
w.bw.Reset(wc)
|
||||
*w = ResponseWriter{
|
||||
*w = responseWriter{
|
||||
bw: w.bw,
|
||||
cl: wc,
|
||||
}
|
||||
}
|
||||
|
||||
// SetMediaType sets the media type that will be sent by Write for a
|
||||
// successful response. If no media type is set, a default of
|
||||
// "text/gemini; charset=utf-8" will be used.
|
||||
//
|
||||
// Setting the media type after a call to Write or WriteHeader has
|
||||
// no effect.
|
||||
func (w *ResponseWriter) SetMediaType(mediatype string) {
|
||||
func (w *responseWriter) SetMediaType(mediatype string) {
|
||||
w.mediatype = mediatype
|
||||
}
|
||||
|
||||
// Write writes the data to the connection as part of a Gemini response.
|
||||
//
|
||||
// If WriteHeader has not yet been called, Write calls WriteHeader with
|
||||
// StatusSuccess and the media type set in SetMediaType before writing the data.
|
||||
// If no media type was set, Write uses a default media type of
|
||||
// "text/gemini; charset=utf-8".
|
||||
func (w *ResponseWriter) Write(b []byte) (int, error) {
|
||||
func (w *responseWriter) Write(b []byte) (int, error) {
|
||||
if w.hijacked {
|
||||
return 0, ErrHijacked
|
||||
}
|
||||
@ -179,17 +230,7 @@ func (w *ResponseWriter) Write(b []byte) (int, error) {
|
||||
return w.bw.Write(b)
|
||||
}
|
||||
|
||||
// WriteHeader sends a Gemini response header with the provided
|
||||
// status code and meta.
|
||||
//
|
||||
// If WriteHeader is not called explicitly, the first call to Write
|
||||
// will trigger an implicit call to WriteHeader with a successful
|
||||
// status code and the media type set in SetMediaType.
|
||||
//
|
||||
// The provided code must be a valid Gemini status code.
|
||||
// The provided meta must not be longer than 1024 bytes.
|
||||
// Only one header may be written.
|
||||
func (w *ResponseWriter) WriteHeader(status Status, meta string) {
|
||||
func (w *responseWriter) WriteHeader(status Status, meta string) {
|
||||
if w.hijacked {
|
||||
return
|
||||
}
|
||||
@ -208,8 +249,7 @@ func (w *ResponseWriter) WriteHeader(status Status, meta string) {
|
||||
w.wroteHeader = true
|
||||
}
|
||||
|
||||
// Flush sends any buffered data to the client.
|
||||
func (w *ResponseWriter) Flush() error {
|
||||
func (w *responseWriter) Flush() error {
|
||||
if w.hijacked {
|
||||
return ErrHijacked
|
||||
}
|
||||
@ -220,23 +260,18 @@ func (w *ResponseWriter) Flush() error {
|
||||
return w.bw.Flush()
|
||||
}
|
||||
|
||||
// Close closes the connection.
|
||||
// Any blocked Write operations will be unblocked and return errors.
|
||||
func (w *ResponseWriter) Close() error {
|
||||
func (w *responseWriter) Close() error {
|
||||
if w.hijacked {
|
||||
return ErrHijacked
|
||||
}
|
||||
return w.cl.Close()
|
||||
}
|
||||
|
||||
// Conn returns the underlying network connection.
|
||||
// To take over the connection, use Hijack.
|
||||
func (w *ResponseWriter) Conn() net.Conn {
|
||||
func (w *responseWriter) Conn() net.Conn {
|
||||
return w.conn
|
||||
}
|
||||
|
||||
// TLS returns information about the underlying TLS connection.
|
||||
func (w *ResponseWriter) TLS() *tls.ConnectionState {
|
||||
func (w *responseWriter) TLS() *tls.ConnectionState {
|
||||
if tlsConn, ok := w.conn.(*tls.Conn); ok {
|
||||
state := tlsConn.ConnectionState()
|
||||
return &state
|
||||
@ -244,17 +279,9 @@ func (w *ResponseWriter) TLS() *tls.ConnectionState {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Hijack lets the caller take over the connection.
|
||||
// After a call to Hijack the Gemini server library
|
||||
// will not do anything else with the connection.
|
||||
// It becomes the caller's responsibility to manage
|
||||
// and close the connection.
|
||||
//
|
||||
// The returned net.Conn may have read or write deadlines
|
||||
// already set, depending on the configuration of the
|
||||
// Server. It is the caller's responsibility to set
|
||||
// or clear those deadlines as needed.
|
||||
func (w *ResponseWriter) Hijack() net.Conn {
|
||||
func (w *responseWriter) Hijack() net.Conn {
|
||||
w.hijacked = true
|
||||
return w.conn
|
||||
}
|
||||
|
||||
func (w *responseWriter) unexported() {}
|
||||
|
Loading…
Reference in New Issue
Block a user