This repository has been archived by the owner on Dec 3, 2019. It is now read-only.
mirrored from https://chromium.googlesource.com/catapult
-
Notifications
You must be signed in to change notification settings - Fork 562
/
proxy.go
271 lines (244 loc) · 8.41 KB
/
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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package webpagereplay
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
const errStatus = http.StatusInternalServerError
func makeLogger(req *http.Request, quietMode bool) func(msg string, args ...interface{}) {
if quietMode {
return func(string, ...interface{}) {}
}
prefix := fmt.Sprintf("ServeHTTP(%s): ", req.URL)
return func(msg string, args ...interface{}) {
log.Print(prefix + fmt.Sprintf(msg, args...))
}
}
// fixupRequestURL adds a scheme and host to req.URL.
// Adding the scheme is necessary since RoundTrip doesn't like an empty scheme.
// Adding the host is optional, but makes req.URL print more nicely.
func fixupRequestURL(req *http.Request, scheme string) {
req.URL.Scheme = scheme
if req.URL.Host == "" {
req.URL.Host = req.Host
}
}
// updateDate is the basic function for date adjustment.
func updateDate(h http.Header, name string, now, oldNow time.Time) {
val := h.Get(name)
if val == "" {
return
}
oldTime, err := http.ParseTime(val)
if err != nil {
return
}
newTime := now.Add(oldTime.Sub(oldNow))
h.Set(name, newTime.UTC().Format(http.TimeFormat))
}
// updateDates updates "Date" header as current time and adjusts "Last-Modified"/"Expires" against it.
func updateDates(h http.Header, now time.Time) {
oldNow, err := http.ParseTime(h.Get("Date"))
h.Set("Date", now.UTC().Format(http.TimeFormat))
if err != nil {
return
}
updateDate(h, "Last-Modified", now, oldNow)
updateDate(h, "Expires", now, oldNow)
}
// NewReplayingProxy constructs an HTTP proxy that replays responses from an archive.
// The proxy is listening for requests on a port that uses the given scheme (e.g., http, https).
func NewReplayingProxy(a *Archive, scheme string, transformers []ResponseTransformer, quietMode bool) http.Handler {
return &replayingProxy{a, scheme, transformers, quietMode}
}
type replayingProxy struct {
a *Archive
scheme string
transformers []ResponseTransformer
quietMode bool
}
func (proxy *replayingProxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/web-page-replay-generate-200" {
w.WriteHeader(200)
return
}
if req.URL.Path == "/web-page-replay-command-exit" {
log.Printf("Shutting down. Received /web-page-replay-command-exit")
os.Exit(0)
return
}
if req.URL.Path == "/web-page-replay-reset-replay-chronology" {
log.Printf("Received /web-page-replay-reset-replay-chronology")
log.Printf("Reset replay order to start.")
proxy.a.StartNewReplaySession()
return
}
fixupRequestURL(req, proxy.scheme)
logf := makeLogger(req, proxy.quietMode)
// Lookup the response in the archive.
_, storedResp, err := proxy.a.FindRequest(req)
if err != nil {
logf("couldn't find matching request: %v", err)
w.WriteHeader(http.StatusNotFound)
return
}
defer storedResp.Body.Close()
// Check if the stored Content-Encoding matches an encoding allowed by the client.
// If not, transform the response body to match the client's Accept-Encoding.
clientAE := strings.ToLower(req.Header.Get("Accept-Encoding"))
originCE := strings.ToLower(storedResp.Header.Get("Content-Encoding"))
if !strings.Contains(clientAE, originCE) {
logf("translating Content-Encoding [%s] -> [%s]", originCE, clientAE)
body, err := ioutil.ReadAll(storedResp.Body)
if err != nil {
logf("error reading response body from archive: %v", err)
w.WriteHeader(http.StatusNotFound)
return
}
body, err = decompressBody(originCE, body)
if err != nil {
logf("error decompressing response body: %v", err)
w.WriteHeader(http.StatusNotFound)
return
}
body, ce, err := CompressBody(clientAE, body)
if err != nil {
logf("error recompressing response body: %v", err)
w.WriteHeader(http.StatusNotFound)
return
}
storedResp.Header.Set("Content-Encoding", ce)
storedResp.Body = ioutil.NopCloser(bytes.NewReader(body))
// ContentLength has changed, so update the outgoing headers accordingly.
if storedResp.ContentLength >= 0 {
storedResp.ContentLength = int64(len(body))
storedResp.Header.Set("Content-Length", strconv.Itoa(len(body)))
}
}
// Update dates in response header.
updateDates(storedResp.Header, time.Now())
// Transform.
for _, t := range proxy.transformers {
t.Transform(req, storedResp)
}
// Forward the response.
logf("serving %v response", storedResp.StatusCode)
for k, v := range storedResp.Header {
w.Header()[k] = append([]string{}, v...)
}
w.WriteHeader(storedResp.StatusCode)
if _, err := io.Copy(w, storedResp.Body); err != nil {
logf("warning: client response truncated: %v", err)
}
}
// NewRecordingProxy constructs an HTTP proxy that records responses into an archive.
// The proxy is listening for requests on a port that uses the given scheme (e.g., http, https).
func NewRecordingProxy(a *WritableArchive, scheme string, transformers []ResponseTransformer) http.Handler {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
return &recordingProxy{http.DefaultTransport.(*http.Transport), a, scheme, transformers}
}
type recordingProxy struct {
tr *http.Transport
a *WritableArchive
scheme string
transformers []ResponseTransformer
}
func (proxy *recordingProxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/web-page-replay-generate-200" {
w.WriteHeader(200)
return
}
if req.URL.Path == "/web-page-replay-command-exit" {
log.Printf("Shutting down. Received /web-page-replay-command-exit")
if err := proxy.a.Close(); err != nil {
log.Printf("Error flushing archive: %v", err)
}
os.Exit(0)
return
}
fixupRequestURL(req, proxy.scheme)
logf := makeLogger(req, false)
// https://github.com/golang/go/issues/16036. Server requests always
// have non-nil body even for GET and HEAD. This prevents http.Transport
// from retrying requests on dead reused conns. Catapult Issue 3706.
if req.ContentLength == 0 {
req.Body = nil
}
// TODO(catapult:3742): Implement Brotli support. Remove br advertisement for now.
ce := req.Header.Get("Accept-Encoding")
req.Header.Set("Accept-Encoding", strings.TrimSuffix(ce, ", br"))
// Read the entire request body (for POST) before forwarding to the server
// so we can save the entire request in the archive.
var requestBody []byte
if req.Body != nil {
var err error
requestBody, err = ioutil.ReadAll(req.Body)
if err != nil {
logf("read request body failed: %v", err)
w.WriteHeader(errStatus)
return
}
req.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
}
// Make the external request.
// If RoundTrip fails, convert the response to a 500.
resp, err := proxy.tr.RoundTrip(req)
if err != nil {
logf("RoundTrip failed: %v", err)
resp = &http.Response{
Status: http.StatusText(errStatus),
StatusCode: errStatus,
Proto: req.Proto,
ProtoMajor: req.ProtoMajor,
ProtoMinor: req.ProtoMinor,
Body: ioutil.NopCloser(bytes.NewReader(nil)),
}
}
// Copy the entire response body.
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
logf("warning: origin response truncated: %v", err)
}
resp.Body.Close()
// Restore req body (which was consumed by RoundTrip) and record original response without transformation.
resp.Body = ioutil.NopCloser(bytes.NewReader(responseBody))
if req.Body != nil {
req.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
}
if err := proxy.a.RecordRequest(req, resp); err != nil {
logf("failed recording request: %v", err)
}
// Restore req and response body which are consumed by RecordRequest.
if req.Body != nil {
req.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
}
resp.Body = ioutil.NopCloser(bytes.NewReader(responseBody))
// Transform.
for _, t := range proxy.transformers {
t.Transform(req, resp)
}
responseBodyAfterTransform, err := ioutil.ReadAll(resp.Body)
if err != nil {
logf("warning: transformed response truncated: %v", err)
}
// Forward the response.
logf("serving %d, %d bytes", resp.StatusCode, len(responseBodyAfterTransform))
for k, v := range resp.Header {
w.Header()[k] = append([]string{}, v...)
}
w.WriteHeader(resp.StatusCode)
if n, err := io.Copy(w, bytes.NewReader(responseBodyAfterTransform)); err != nil {
logf("warning: client response truncated (%d/%d bytes): %v", n, len(responseBodyAfterTransform), err)
}
}