-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse.go
More file actions
105 lines (93 loc) · 3.84 KB
/
Copy pathresponse.go
File metadata and controls
105 lines (93 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package middleware
import (
"bufio"
"fmt"
"io"
"net"
"net/http"
)
// response is an interface used by an HTTP handler to construct an HTTP
// response. ResponseWriter may not be used after the Handler.ServeHTTP method
// has returned. Here it’s being used to include additional data for the logger
// such as the time spent responding to the HTTP request and the total size in
// bytes of the response.
type response struct {
http.ResponseWriter
Status int
Length int
}
// WriteHeader sends an HTTP response header with status code.
//
// If WriteHeader is not called explicitly, the first call to Write will
// trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to
// WriteHeader are mainly used to send error codes.
func (w *response) WriteHeader(status int) {
w.Status = status
w.ResponseWriter.WriteHeader(status)
}
// Write writes the data to the connection as part of an HTTP reply.
//
// If WriteHeader has not been called, Write calls WriteHeader(http.StatusOK)
// before writing the data. If the Header does not contain a Content-Type line,
// Write adds a Content-Type set to the result of passing the initial 512 bytes
// of written data to DetectContentType.
//
// Depending on the HTTP protocol version and the client, calling Write or
// WriteHeader may prevent future reads on the Request.Body. For HTTP/1.x
// requests, handlers should read any needed request body data before writing
// the response. Once the headers have been flushed (due to either an explicit
// Flusher.Flush call or writing enough data to trigger a flush), the request
// body may be unavailable. For HTTP/2 requests, the Go HTTP server permits
// handlers to continue to read the request body while concurrently writing the
// response. However, such behavior may not be supported by all HTTP/2 clients.
// Handlers should read before writing if possible to maximize compatibility.
func (w *response) Write(b []byte) (int, error) {
if w.Status == 0 {
w.Status = http.StatusOK
}
w.Length += len(b)
return w.ResponseWriter.Write(b)
}
// ReadFrom implements io.ReaderFrom by delegating to the underlying
// ResponseWriter when it supports it. The net/http server exposes io.ReaderFrom
// so that io.Copy (used by http.FileServer) can take the zero-copy sendfile(2)
// fast path when serving static files. Wrapping the ResponseWriter hides that
// method, so we forward it here to keep the optimization. When the underlying
// writer does not implement io.ReaderFrom, a regular copy is used instead. In
// both cases the recorded status and byte count stay in sync for the logger.
func (w *response) ReadFrom(src io.Reader) (int64, error) {
if w.Status == 0 {
w.Status = http.StatusOK
}
if rf, ok := w.ResponseWriter.(io.ReaderFrom); ok {
n, err := rf.ReadFrom(src)
w.Length += int(n)
return n, err
}
// The underlying writer cannot read directly from the source, so copy the
// data over. io.Copy writes straight to the wrapped writer to avoid double
// counting through the Write method above.
n, err := io.Copy(w.ResponseWriter, src)
w.Length += int(n)
return n, err
}
// Flush implements http.Flusher by delegating to the underlying ResponseWriter.
func (w *response) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
// Hijack implements http.Hijacker by delegating to the underlying ResponseWriter.
func (w *response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if h, ok := w.ResponseWriter.(http.Hijacker); ok {
return h.Hijack()
}
return nil, nil, fmt.Errorf("underlying ResponseWriter does not implement http.Hijacker")
}
// Push implements http.Pusher by delegating to the underlying ResponseWriter.
func (w *response) Push(target string, opts *http.PushOptions) error {
if p, ok := w.ResponseWriter.(http.Pusher); ok {
return p.Push(target, opts)
}
return fmt.Errorf("underlying ResponseWriter does not implement http.Pusher")
}