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:
Adnan Maolood 2021-02-24 00:13:44 -05:00
parent 526d232ab0
commit 3660698a4b
5 changed files with 92 additions and 65 deletions

12
fs.go
View File

@ -33,7 +33,7 @@ type fileServer struct {
fs.FS 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) 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. // ServeContent tries to deduce the type from name's file extension.
// The name is otherwise unused; it is never sent in the response. // 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) 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 // Detect mimetype from file extension
ext := path.Ext(name) ext := path.Ext(name)
mimetype := mime.TypeByExtension(ext) 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 // 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 // selecting the file or directory to serve; only the file or directory
// provided in the name argument is used. // 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) { if containsDotDot(r.URL.Path) {
// Too many programs use r.URL.Path to construct the argument to // Too many programs use r.URL.Path to construct the argument to
// serveFile. Reject the request under the assumption that happened // 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 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" const indexPage = "/index.gmi"
// Redirect .../index.gmi to .../ // Redirect .../index.gmi to .../
@ -177,7 +177,7 @@ func serveFile(w *ResponseWriter, r *Request, fsys fs.FS, name string, redirect
serveContent(w, name, f) serveContent(w, name, f)
} }
func dirList(w *ResponseWriter, f fs.File) { func dirList(w ResponseWriter, f fs.File) {
var entries []fs.DirEntry var entries []fs.DirEntry
var err error var err error
d, ok := f.(fs.ReadDirFile) d, ok := f.(fs.ReadDirFile)

View File

@ -21,23 +21,23 @@ import (
// //
// Handlers should not modify the provided Request. // Handlers should not modify the provided Request.
type Handler interface { 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 // 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, // as Gemini handlers. If f is a function with the appropriate signature,
// HandlerFunc(f) is a Handler that calls f. // 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). // 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) f(ctx, w, r)
} }
// StatusHandler returns a request handler that responds to each request // StatusHandler returns a request handler that responds to each request
// with the provided status code and meta. // with the provided status code and meta.
func StatusHandler(status Status, meta string) Handler { 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) w.WriteHeader(status, meta)
}) })
} }
@ -58,7 +58,7 @@ func StripPrefix(prefix string, h Handler) Handler {
if prefix == "" { if prefix == "" {
return h 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) p := strings.TrimPrefix(r.URL.Path, prefix)
rp := strings.TrimPrefix(r.URL.RawPath, prefix) rp := strings.TrimPrefix(r.URL.RawPath, prefix)
if len(p) < len(r.URL.Path) && (r.URL.RawPath == "" || len(rp) < len(r.URL.RawPath)) { 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 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) ctx, cancel := context.WithTimeout(ctx, t.dt)
defer cancel() defer cancel()

2
mux.go
View File

@ -212,7 +212,7 @@ func (mux *ServeMux) Handler(r *Request) Handler {
// ServeGemini dispatches the request to the handler whose // ServeGemini dispatches the request to the handler whose
// pattern most closely matches the request URL. // 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 := mux.Handler(r)
h.ServeGemini(ctx, w, r) h.ServeGemini(ctx, w, r)
} }

View File

@ -8,7 +8,7 @@ import (
type nopHandler struct{} type nopHandler struct{}
func (*nopHandler) ServeGemini(context.Context, *ResponseWriter, *Request) {} func (*nopHandler) ServeGemini(context.Context, ResponseWriter, *Request) {}
func TestServeMuxMatch(t *testing.T) { func TestServeMuxMatch(t *testing.T) {
type Match struct { type Match struct {

View File

@ -120,7 +120,70 @@ func (r *Response) TLS() *tls.ConnectionState {
// //
// A ResponseWriter may not be used after the Handler.ServeGemini method // A ResponseWriter may not be used after the Handler.ServeGemini method
// has returned. // 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 bw *bufio.Writer
cl io.Closer cl io.Closer
mediatype string mediatype string
@ -130,38 +193,26 @@ type ResponseWriter struct {
conn net.Conn conn net.Conn
} }
func newResponseWriter(w io.WriteCloser) *ResponseWriter { func newResponseWriter(w io.WriteCloser) *responseWriter {
return &ResponseWriter{ return &responseWriter{
bw: bufio.NewWriter(w), bw: bufio.NewWriter(w),
cl: w, cl: w,
} }
} }
func (w *ResponseWriter) reset(wc io.WriteCloser) { func (w *responseWriter) reset(wc io.WriteCloser) {
w.bw.Reset(wc) w.bw.Reset(wc)
*w = ResponseWriter{ *w = responseWriter{
bw: w.bw, bw: w.bw,
cl: wc, cl: wc,
} }
} }
// SetMediaType sets the media type that will be sent by Write for a func (w *responseWriter) SetMediaType(mediatype string) {
// 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) {
w.mediatype = mediatype w.mediatype = mediatype
} }
// Write writes the data to the connection as part of a Gemini response. func (w *responseWriter) Write(b []byte) (int, error) {
//
// 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) {
if w.hijacked { if w.hijacked {
return 0, ErrHijacked return 0, ErrHijacked
} }
@ -179,17 +230,7 @@ func (w *ResponseWriter) Write(b []byte) (int, error) {
return w.bw.Write(b) return w.bw.Write(b)
} }
// WriteHeader sends a Gemini response header with the provided func (w *responseWriter) WriteHeader(status Status, meta string) {
// 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) {
if w.hijacked { if w.hijacked {
return return
} }
@ -208,8 +249,7 @@ func (w *ResponseWriter) WriteHeader(status Status, meta string) {
w.wroteHeader = true w.wroteHeader = true
} }
// Flush sends any buffered data to the client. func (w *responseWriter) Flush() error {
func (w *ResponseWriter) Flush() error {
if w.hijacked { if w.hijacked {
return ErrHijacked return ErrHijacked
} }
@ -220,23 +260,18 @@ func (w *ResponseWriter) Flush() error {
return w.bw.Flush() return w.bw.Flush()
} }
// Close closes the connection. func (w *responseWriter) Close() error {
// Any blocked Write operations will be unblocked and return errors.
func (w *ResponseWriter) Close() error {
if w.hijacked { if w.hijacked {
return ErrHijacked return ErrHijacked
} }
return w.cl.Close() return w.cl.Close()
} }
// Conn returns the underlying network connection. func (w *responseWriter) Conn() net.Conn {
// To take over the connection, use Hijack.
func (w *ResponseWriter) Conn() net.Conn {
return w.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 { if tlsConn, ok := w.conn.(*tls.Conn); ok {
state := tlsConn.ConnectionState() state := tlsConn.ConnectionState()
return &state return &state
@ -244,17 +279,9 @@ func (w *ResponseWriter) TLS() *tls.ConnectionState {
return nil return nil
} }
// Hijack lets the caller take over the connection. func (w *responseWriter) Hijack() net.Conn {
// 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 {
w.hijacked = true w.hijacked = true
return w.conn return w.conn
} }
func (w *responseWriter) unexported() {}