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:
Adnan Maolood
2021-02-23 20:40:22 -05:00
parent 64f9381bbc
commit a65c3c3d4f
7 changed files with 49 additions and 63 deletions

View File

@@ -157,44 +157,7 @@ func (r *Response) TLS() *tls.ConnectionState {
//
// A ResponseWriter may not be used after the Handler.ServeGemini method
// has returned.
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(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 {
type ResponseWriter struct {
b *bufio.Writer
closer io.Closer
mediatype string
@@ -203,22 +166,30 @@ type responseWriter struct {
}
// NewResponseWriter returns a ResponseWriter that uses the provided io.WriteCloser.
func NewResponseWriter(wc io.WriteCloser) ResponseWriter {
return newResponseWriter(wc)
}
func newResponseWriter(wc io.WriteCloser) *responseWriter {
return &responseWriter{
func NewResponseWriter(wc io.WriteCloser) *ResponseWriter {
return &ResponseWriter{
b: bufio.NewWriter(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
}
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 {
meta := w.mediatype
if meta == "" {
@@ -233,7 +204,17 @@ func (w *responseWriter) Write(b []byte) (int, error) {
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 {
return
}
@@ -249,7 +230,8 @@ func (w *responseWriter) WriteHeader(status Status, meta string) {
w.wroteHeader = true
}
func (w *responseWriter) Flush() error {
// Flush sends any buffered data to the client.
func (w *ResponseWriter) Flush() error {
if !w.wroteHeader {
w.WriteHeader(StatusTemporaryFailure, "Temporary failure")
}
@@ -257,6 +239,8 @@ func (w *responseWriter) Flush() error {
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()
}