-
Notifications
You must be signed in to change notification settings - Fork 1
/
response_proxy.go
44 lines (35 loc) · 1.06 KB
/
response_proxy.go
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
// Package launcher provides a launcher to start gRPC server, health server and grpc gateway server.
// response_proxy.go provides a response proxy to log the response.
package launcher
import "net/http"
// responseProxy wraps a http.ResponseWriter that implements the minimal
// http.ResponseWriter interface.
type responseProxy struct {
http.ResponseWriter
status int
body []byte
Len int
}
// WriteHeader writes the HTTP status code of the response.
func (p *responseProxy) WriteHeader(status int) {
p.status = status
p.ResponseWriter.WriteHeader(status)
}
// Write writes the body of the response.
func (p *responseProxy) Write(buf []byte) (int, error) {
if p.status == 0 {
p.WriteHeader(http.StatusOK)
}
n, err := p.ResponseWriter.Write(buf)
p.body = append(p.body, buf[:n]...)
p.Len += n
return n, err
}
// GetBody returns the body of the response.
func (p *responseProxy) GetBody() string {
return string(p.body)
}
// GETHTTPStatusCode returns the HTTP status code of the response.
func (p *responseProxy) GETHTTPStatusCode() int {
return p.status
}