forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_logger.go
166 lines (143 loc) · 4.18 KB
/
request_logger.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package wrapper
import (
"fmt"
"io/ioutil"
"net/http"
"sort"
"strings"
"time"
"code.cloudfoundry.org/cli/api/cloudcontroller"
)
//go:generate counterfeiter . RequestLoggerOutput
// RequestLoggerOutput is the interface for displaying logs
type RequestLoggerOutput interface {
DisplayHeader(name string, value string) error
DisplayHost(name string) error
DisplayJSONBody(body []byte) error
DisplayMessage(msg string) error
DisplayRequestHeader(method string, uri string, httpProtocol string) error
DisplayResponseHeader(httpProtocol string, status string) error
DisplayType(name string, requestDate time.Time) error
HandleInternalError(err error)
Start() error
Stop() error
}
// RequestLogger is the wrapper that logs requests to and responses from the
// Cloud Controller server
type RequestLogger struct {
connection cloudcontroller.Connection
output RequestLoggerOutput
}
// NewRequestLogger returns a pointer to a RequestLogger wrapper
func NewRequestLogger(output RequestLoggerOutput) *RequestLogger {
return &RequestLogger{
output: output,
}
}
// Make records the request and the response to UI
func (logger *RequestLogger) Make(request *cloudcontroller.Request, passedResponse *cloudcontroller.Response) error {
err := logger.displayRequest(request)
if err != nil {
logger.output.HandleInternalError(err)
}
err = logger.connection.Make(request, passedResponse)
if passedResponse.HTTPResponse != nil {
displayErr := logger.displayResponse(passedResponse)
if displayErr != nil {
logger.output.HandleInternalError(displayErr)
}
}
return err
}
// Wrap sets the connection on the RequestLogger and returns itself
func (logger *RequestLogger) Wrap(innerconnection cloudcontroller.Connection) cloudcontroller.Connection {
logger.connection = innerconnection
return logger
}
func (logger *RequestLogger) displayRequest(request *cloudcontroller.Request) error {
err := logger.output.Start()
if err != nil {
return err
}
defer logger.output.Stop()
err = logger.output.DisplayType("REQUEST", time.Now())
if err != nil {
return err
}
err = logger.output.DisplayRequestHeader(request.Method, request.URL.RequestURI(), request.Proto)
if err != nil {
return err
}
err = logger.output.DisplayHost(request.URL.Host)
if err != nil {
return err
}
err = logger.displaySortedHeaders(request.Header)
if err != nil {
return err
}
contentType := request.Header.Get("Content-Type")
if request.Body != nil {
if strings.Contains(contentType, "json") {
rawRequestBody, err := ioutil.ReadAll(request.Body)
if err != nil {
return err
}
defer request.ResetBody()
return logger.output.DisplayJSONBody(rawRequestBody)
} else if strings.Contains(contentType, "x-www-form-urlencoded") {
rawRequestBody, err := ioutil.ReadAll(request.Body)
if err != nil {
return err
}
defer request.ResetBody()
return logger.output.DisplayMessage(fmt.Sprintf("[application/x-www-form-urlencoded %s]", rawRequestBody))
}
}
if contentType != "" {
return logger.output.DisplayMessage(fmt.Sprintf("[%s Content Hidden]", strings.Split(contentType, ";")[0]))
}
return nil
}
func (logger *RequestLogger) displayResponse(passedResponse *cloudcontroller.Response) error {
err := logger.output.Start()
if err != nil {
return err
}
defer logger.output.Stop()
err = logger.output.DisplayType("RESPONSE", time.Now())
if err != nil {
return err
}
err = logger.output.DisplayResponseHeader(passedResponse.HTTPResponse.Proto, passedResponse.HTTPResponse.Status)
if err != nil {
return err
}
err = logger.displaySortedHeaders(passedResponse.HTTPResponse.Header)
if err != nil {
return err
}
return logger.output.DisplayJSONBody(passedResponse.RawResponse)
}
func (logger *RequestLogger) displaySortedHeaders(headers http.Header) error {
keys := []string{}
for key, _ := range headers {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
for _, value := range headers[key] {
err := logger.output.DisplayHeader(key, redactHeaders(key, value))
if err != nil {
return err
}
}
}
return nil
}
func redactHeaders(key string, value string) string {
if key == "Authorization" {
return "[PRIVATE DATA HIDDEN]"
}
return value
}