Make ResponseWriter a struct
Make ResponseWriter a struct again so that it can be extended in a backwards-compatible way.
This commit is contained in:
parent
64f9381bbc
commit
a65c3c3d4f
12
fs.go
12
fs.go
@ -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 *Reque
|
|||||||
//
|
//
|
||||||
// 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 b
|
|||||||
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)
|
||||||
|
10
handler.go
10
handler.go
@ -19,23 +19,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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -56,7 +56,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)) {
|
||||||
|
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
|
// 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)
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
82
response.go
82
response.go
@ -157,44 +157,7 @@ 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 interface {
|
type ResponseWriter struct {
|
||||||
// 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(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 Read or Write operations will be unblocked and return errors.
|
|
||||||
Close() error
|
|
||||||
}
|
|
||||||
|
|
||||||
type responseWriter struct {
|
|
||||||
b *bufio.Writer
|
b *bufio.Writer
|
||||||
closer io.Closer
|
closer io.Closer
|
||||||
mediatype string
|
mediatype string
|
||||||
@ -203,22 +166,30 @@ type responseWriter struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewResponseWriter returns a ResponseWriter that uses the provided io.WriteCloser.
|
// NewResponseWriter returns a ResponseWriter that uses the provided io.WriteCloser.
|
||||||
func NewResponseWriter(wc io.WriteCloser) ResponseWriter {
|
func NewResponseWriter(wc io.WriteCloser) *ResponseWriter {
|
||||||
return newResponseWriter(wc)
|
return &ResponseWriter{
|
||||||
}
|
|
||||||
|
|
||||||
func newResponseWriter(wc io.WriteCloser) *responseWriter {
|
|
||||||
return &responseWriter{
|
|
||||||
b: bufio.NewWriter(wc),
|
b: bufio.NewWriter(wc),
|
||||||
closer: wc,
|
closer: wc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *responseWriter) SetMediaType(mediatype string) {
|
// 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) {
|
||||||
w.mediatype = mediatype
|
w.mediatype = mediatype
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *responseWriter) Write(b []byte) (int, error) {
|
// 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) {
|
||||||
if !w.wroteHeader {
|
if !w.wroteHeader {
|
||||||
meta := w.mediatype
|
meta := w.mediatype
|
||||||
if meta == "" {
|
if meta == "" {
|
||||||
@ -233,7 +204,17 @@ func (w *responseWriter) Write(b []byte) (int, error) {
|
|||||||
return w.b.Write(b)
|
return w.b.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *responseWriter) WriteHeader(status Status, meta string) {
|
// 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) {
|
||||||
if w.wroteHeader {
|
if w.wroteHeader {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -249,7 +230,8 @@ func (w *responseWriter) WriteHeader(status Status, meta string) {
|
|||||||
w.wroteHeader = true
|
w.wroteHeader = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *responseWriter) Flush() error {
|
// Flush sends any buffered data to the client.
|
||||||
|
func (w *ResponseWriter) Flush() error {
|
||||||
if !w.wroteHeader {
|
if !w.wroteHeader {
|
||||||
w.WriteHeader(StatusTemporaryFailure, "Temporary failure")
|
w.WriteHeader(StatusTemporaryFailure, "Temporary failure")
|
||||||
}
|
}
|
||||||
@ -257,6 +239,8 @@ func (w *responseWriter) Flush() error {
|
|||||||
return w.b.Flush()
|
return w.b.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *responseWriter) Close() error {
|
// Close closes the connection.
|
||||||
|
// Any blocked Write operations will be unblocked and return errors.
|
||||||
|
func (w *ResponseWriter) Close() error {
|
||||||
return w.closer.Close()
|
return w.closer.Close()
|
||||||
}
|
}
|
||||||
|
@ -359,7 +359,7 @@ func (srv *Server) serveConn(ctx context.Context, conn net.Conn) error {
|
|||||||
rc: conn,
|
rc: conn,
|
||||||
}
|
}
|
||||||
|
|
||||||
w := newResponseWriter(cw)
|
w := NewResponseWriter(cw)
|
||||||
|
|
||||||
req, err := ReadRequest(r)
|
req, err := ReadRequest(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// +build ignore
|
||||||
|
|
||||||
package gemini
|
package gemini
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
Loading…
Reference in New Issue
Block a user