Move ResponseWriter.Flush to Flusher interface
This commit is contained in:
parent
fb9b50871c
commit
a3c1804395
|
@ -39,6 +39,12 @@ func main() {
|
||||||
|
|
||||||
// stream writes an infinite stream to w.
|
// stream writes an infinite stream to w.
|
||||||
func stream(w gemini.ResponseWriter, r *gemini.Request) {
|
func stream(w gemini.ResponseWriter, r *gemini.Request) {
|
||||||
|
flusher, ok := w.(gemini.Flusher)
|
||||||
|
if !ok {
|
||||||
|
w.Status(gemini.StatusTemporaryFailure)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ch := make(chan string)
|
ch := make(chan string)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
@ -63,7 +69,7 @@ func stream(w gemini.ResponseWriter, r *gemini.Request) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
fmt.Fprintln(w, s)
|
fmt.Fprintln(w, s)
|
||||||
if err := w.Flush(); err != nil {
|
if err := flusher.Flush(); err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
17
response.go
17
response.go
|
@ -156,11 +156,16 @@ type ResponseWriter interface {
|
||||||
// Write writes the response header if it has not already been written.
|
// Write writes the response header if it has not already been written.
|
||||||
// It writes a successful status code if one is not set.
|
// It writes a successful status code if one is not set.
|
||||||
Write([]byte) (int, error)
|
Write([]byte) (int, error)
|
||||||
|
}
|
||||||
|
|
||||||
// Flush writes any buffered data to the underlying io.Writer.
|
// The Flusher interface is implemented by ResponseWriters that allow a
|
||||||
//
|
// Gemini handler to flush buffered data to the client.
|
||||||
// Flush writes the response header if it has not already been written.
|
//
|
||||||
// It writes a failure status code if one is not set.
|
// The default Gemini ResponseWriter implementation supports Flusher,
|
||||||
|
// but ResponseWriter wrappers may not. Handlers should always test
|
||||||
|
// for this ability at runtime.
|
||||||
|
type Flusher interface {
|
||||||
|
// Flush sends any buffered data to the client.
|
||||||
Flush() error
|
Flush() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,6 +179,10 @@ type responseWriter struct {
|
||||||
|
|
||||||
// NewResponseWriter returns a ResponseWriter that uses the provided io.Writer.
|
// NewResponseWriter returns a ResponseWriter that uses the provided io.Writer.
|
||||||
func NewResponseWriter(w io.Writer) ResponseWriter {
|
func NewResponseWriter(w io.Writer) ResponseWriter {
|
||||||
|
return newResponseWriter(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newResponseWriter(w io.Writer) *responseWriter {
|
||||||
return &responseWriter{
|
return &responseWriter{
|
||||||
b: bufio.NewWriter(w),
|
b: bufio.NewWriter(w),
|
||||||
}
|
}
|
||||||
|
|
|
@ -376,7 +376,7 @@ func (srv *Server) respond(conn net.Conn) {
|
||||||
conn.SetWriteDeadline(time.Now().Add(d))
|
conn.SetWriteDeadline(time.Now().Add(d))
|
||||||
}
|
}
|
||||||
|
|
||||||
w := NewResponseWriter(conn)
|
w := newResponseWriter(conn)
|
||||||
|
|
||||||
req, err := ReadRequest(conn)
|
req, err := ReadRequest(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user