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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
/*
* Copyright (C) 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "NetworkCaptureEvent.h"
#if ENABLE(NETWORK_CAPTURE)
#include "NetworkCaptureLogging.h"
#include "json.hpp"
#include <WebCore/ResourceError.h>
#include <WebCore/ResourceRequest.h>
#include <WebCore/ResourceResponse.h>
#include <WebCore/URLParser.h>
#include <wtf/Assertions.h>
#include <wtf/Brigand.h>
#include <wtf/text/Base64.h>
namespace WebKit {
namespace NetworkCapture {
const char RequestSentEvent::typeName[] = "RequestSentEvent";
const char ResponseReceivedEvent::typeName[] = "ResponseReceivedEvent";
const char RedirectReceivedEvent::typeName[] = "RedirectReceivedEvent";
const char RedirectSentEvent::typeName[] = "RedirectSentEvent";
const char DataReceivedEvent::typeName[] = "DataReceivedEvent";
const char FinishedEvent::typeName[] = "FinishedEvent";
static Headers copyHeaders(const WebCore::HTTPHeaderMap& httpHeaders)
{
Headers headers;
for (const auto& header : httpHeaders)
headers.append(std::make_pair(header.key, header.value));
return headers;
}
// ----------
Request::Request(String&& url, String&& referrer, int policy, String&& method, Headers&& headers)
: url(WTFMove(url))
, referrer(WTFMove(referrer))
, policy(WTFMove(policy))
, method(WTFMove(method))
, headers(WTFMove(headers))
{
}
Request::Request(const WebCore::ResourceRequest& request)
: url(request.url().string())
, referrer(request.httpReferrer())
, policy(static_cast<int>(request.cachePolicy()))
, method(request.httpMethod())
, headers(copyHeaders(request.httpHeaderFields()))
{
}
Request::operator WebCore::ResourceRequest() const
{
WebCore::URLParser parser(url);
WebCore::ResourceRequest request(parser.result(), referrer, static_cast<WebCore::ResourceRequestCachePolicy>(policy));
request.setHTTPMethod(method);
for (const auto& header : headers)
request.setHTTPHeaderField(header.first, header.second);
return request;
}
// ----------
Response::Response(String&& url, String&& mimeType, long long expectedLength, String&& textEncodingName, String&& version, int status, String&& reason, Headers&& headers)
: url(WTFMove(url))
, mimeType(WTFMove(mimeType))
, expectedLength(WTFMove(expectedLength))
, textEncodingName(WTFMove(textEncodingName))
, status(WTFMove(status))
, reason(WTFMove(reason))
, headers(WTFMove(headers))
{
}
Response::Response(const WebCore::ResourceResponse& response)
: url(response.url().string())
, mimeType(response.mimeType())
, expectedLength(response.expectedContentLength())
, textEncodingName(response.textEncodingName())
, version(response.httpVersion())
, status(response.httpStatusCode())
, reason(response.httpStatusText())
, headers(copyHeaders(response.httpHeaderFields()))
{
}
Response::operator WebCore::ResourceResponse() const
{
WebCore::URLParser parser(url);
WebCore::ResourceResponse response(parser.result(), mimeType, expectedLength, textEncodingName);
response.setHTTPVersion(version);
response.setHTTPStatusCode(status);
response.setHTTPStatusText(reason);
for (const auto& header : headers)
response.setHTTPHeaderField(header.first, header.second);
return response;
}
// ----------
Error::Error(String&& domain, String&& failingURL, String&& localizedDescription, int errorCode, int type)
: domain(WTFMove(domain))
, failingURL(WTFMove(failingURL))
, localizedDescription(WTFMove(localizedDescription))
, errorCode(WTFMove(errorCode))
, type(WTFMove(type))
{
}
Error::Error(const WebCore::ResourceError& error)
: domain(error.domain())
, failingURL(error.failingURL().string())
, localizedDescription(error.localizedDescription())
, errorCode(error.errorCode())
, type(static_cast<int>(error.type()))
{
}
Error::operator WebCore::ResourceError() const
{
WebCore::URLParser parser(failingURL);
WebCore::ResourceError error(domain, errorCode, parser.result(), localizedDescription, static_cast<WebCore::ResourceError::Type>(type));
return error;
}
// ----------
// SEE THE NOTE IN json.hpp REGARDING ITS USE IN THIS PROJECT. IN SHORT, DO NOT
// USE json.hpp ANYWHERE ELSE. IT WILL BE GOING AWAY.
using json = nlohmann::basic_json<>;
template<typename Type>
struct JSONCoder {
static json encode(Type val)
{
return json(val);
}
static Type decode(const json& jVal)
{
return jVal.get<Type>();
}
};
template<>
struct JSONCoder<const char*> {
static json encode(const char* val)
{
return json(val);
}
};
template<>
struct JSONCoder<String> {
static json encode(const String& val)
{
return json(static_cast<const char*>(val.utf8().data()));
}
static String decode(const json& jVal)
{
return String(jVal.get_ref<const std::string&>().c_str());
}
};
template<>
struct JSONCoder<CaptureTimeType> {
static json encode(const CaptureTimeType& time)
{
return JSONCoder<double>::encode(time.secondsSinceEpoch().seconds());
}
static CaptureTimeType decode(const json& jTime)
{
return CaptureTimeType::fromRawSeconds(JSONCoder<double>::decode(jTime));
}
};
template<>
struct JSONCoder<KeyValuePair> {
static json encode(const KeyValuePair& pair)
{
return json {
JSONCoder<String>::encode(pair.first),
JSONCoder<String>::encode(pair.second)
};
}
static KeyValuePair decode(const json& jPair)
{
return KeyValuePair {
JSONCoder<String>::decode(jPair[0]),
JSONCoder<String>::decode(jPair[1])
};
}
};
template<typename T>
struct JSONCoder<Vector<T>> {
static json encode(const Vector<T>& vector)
{
json jVector;
for (const auto& element : vector)
jVector.push_back(JSONCoder<T>::encode(element));
return jVector;
}
static Vector<T> decode(const json& jVector)
{
Vector<T> vector;
for (const auto& element : jVector)
vector.append(JSONCoder<T>::decode(element));
return vector;
}
};
template<>
struct JSONCoder<Request> {
static json encode(const Request& request)
{
return json {
{ "url", JSONCoder<String>::encode(request.url) },
{ "referrer", JSONCoder<String>::encode(request.referrer) },
{ "policy", JSONCoder<int>::encode(request.policy) },
{ "method", JSONCoder<String>::encode(request.method) },
{ "headers", JSONCoder<Headers>::encode(request.headers) }
};
}
static Request decode(const json& jRequest)
{
return Request {
JSONCoder<String>::decode(jRequest["url"]),
JSONCoder<String>::decode(jRequest["referrer"]),
JSONCoder<int>::decode(jRequest["policy"]),
JSONCoder<String>::decode(jRequest["method"]),
JSONCoder<Headers>::decode(jRequest["headers"])
};
}
};
template<>
struct JSONCoder<Response> {
static json encode(const Response& response)
{
return json {
{ "url", JSONCoder<String>::encode(response.url) },
{ "mimeType", JSONCoder<String>::encode(response.mimeType) },
{ "expectedLength", JSONCoder<long long>::encode(response.expectedLength) },
{ "textEncodingName", JSONCoder<String>::encode(response.textEncodingName) },
{ "version", JSONCoder<String>::encode(response.version) },
{ "status", JSONCoder<int>::encode(response.status) },
{ "reason", JSONCoder<String>::encode(response.reason) },
{ "headers", JSONCoder<Headers>::encode(response.headers) }
};
}
static Response decode(const json& jResponse)
{
return Response {
JSONCoder<String>::decode(jResponse["url"]),
JSONCoder<String>::decode(jResponse["mimeType"]),
JSONCoder<long long>::decode(jResponse["expectedLength"]),
JSONCoder<String>::decode(jResponse["textEncodingName"]),
JSONCoder<String>::decode(jResponse["version"]),
JSONCoder<int>::decode(jResponse["status"]),
JSONCoder<String>::decode(jResponse["reason"]),
JSONCoder<Headers>::decode(jResponse["headers"])
};
}
};
template<>
struct JSONCoder<Error> {
static json encode(const Error& error)
{
return json {
{ "domain", JSONCoder<String>::encode(error.domain) },
{ "failingURL", JSONCoder<String>::encode(error.failingURL) },
{ "localizedDescription", JSONCoder<String>::encode(error.localizedDescription) },
{ "errorCode", JSONCoder<int>::encode(error.errorCode) },
{ "type", JSONCoder<int>::encode(error.type) }
};
}
static Error decode(const json& jError)
{
return Error {
JSONCoder<String>::decode(jError["domain"]),
JSONCoder<String>::decode(jError["failingURL"]),
JSONCoder<String>::decode(jError["localizedDescription"]),
JSONCoder<int>::decode(jError["errorCode"]),
JSONCoder<int>::decode(jError["type"])
};
}
};
template<>
struct JSONCoder<WebCore::SharedBuffer> {
static json encode(const WebCore::SharedBuffer& data)
{
Vector<char> buffer;
base64Encode(data.data(), data.size(), buffer);
return json(&buffer[0], buffer.size());
}
static Ref<WebCore::SharedBuffer> decode(const json& jData)
{
Vector<char> data;
const auto& str = jData.get_ref<const std::string&>();
auto result = base64Decode(str.c_str(), str.size(), data);
ASSERT_UNUSED(result, result);
return WebCore::SharedBuffer::adoptVector(data);
}
};
template<>
struct JSONCoder<RequestSentEvent> {
static json encode(const RequestSentEvent& event)
{
return json {
{ "type", JSONCoder<const char*>::encode(event.typeName) },
{ "time", JSONCoder<CaptureTimeType>::encode(event.time) },
{ "request", JSONCoder<Request>::encode(event.request) }
};
}
static RequestSentEvent decode(const json& jEvent)
{
return RequestSentEvent {
JSONCoder<CaptureTimeType>::decode(jEvent["time"]),
JSONCoder<Request>::decode(jEvent["request"])
};
}
};
template<>
struct JSONCoder<ResponseReceivedEvent> {
static json encode(const ResponseReceivedEvent& event)
{
return json {
{ "type", JSONCoder<const char*>::encode(event.typeName) },
{ "time", JSONCoder<CaptureTimeType>::encode(event.time) },
{ "response", JSONCoder<Response>::encode(event.response) }
};
}
static ResponseReceivedEvent decode(const json& jEvent)
{
return ResponseReceivedEvent {
JSONCoder<CaptureTimeType>::decode(jEvent["time"]),
JSONCoder<Response>::decode(jEvent["response"])
};
}
};
template<>
struct JSONCoder<RedirectReceivedEvent> {
static json encode(const RedirectReceivedEvent& event)
{
return json {
{ "type", JSONCoder<const char*>::encode(event.typeName) },
{ "time", JSONCoder<CaptureTimeType>::encode(event.time) },
{ "request", JSONCoder<Request>::encode(event.request) },
{ "response", JSONCoder<Response>::encode(event.response) }
};
}
static RedirectReceivedEvent decode(const json& jEvent)
{
return RedirectReceivedEvent {
JSONCoder<CaptureTimeType>::decode(jEvent["time"]),
JSONCoder<Request>::decode(jEvent["request"]),
JSONCoder<Response>::decode(jEvent["response"])
};
}
};
template<>
struct JSONCoder<RedirectSentEvent> {
static json encode(const RedirectSentEvent& event)
{
return json {
{ "type", JSONCoder<const char*>::encode(event.typeName) },
{ "time", JSONCoder<CaptureTimeType>::encode(event.time) },
{ "request", JSONCoder<Request>::encode(event.request) },
};
}
static RedirectSentEvent decode(const json& jEvent)
{
return RedirectSentEvent {
JSONCoder<CaptureTimeType>::decode(jEvent["time"]),
JSONCoder<Request>::decode(jEvent["request"])
};
}
};
template<>
struct JSONCoder<DataReceivedEvent> {
static json encode(const DataReceivedEvent& event)
{
return json {
{ "type", JSONCoder<const char*>::encode(event.typeName) },
{ "time", JSONCoder<CaptureTimeType>::encode(event.time) },
{ "data", JSONCoder<WebCore::SharedBuffer>::encode(event.data.get()) }
};
}
static DataReceivedEvent decode(const json& jEvent)
{
return DataReceivedEvent {
JSONCoder<CaptureTimeType>::decode(jEvent["time"]),
JSONCoder<WebCore::SharedBuffer>::decode(jEvent["data"])
};
}
};
template<>
struct JSONCoder<FinishedEvent> {
static json encode(const FinishedEvent& event)
{
return json {
{ "type", JSONCoder<const char*>::encode(event.typeName) },
{ "time", JSONCoder<CaptureTimeType>::encode(event.time) },
{ "error", JSONCoder<Error>::encode(event.error) }
};
}
static FinishedEvent decode(const json& jEvent)
{
return FinishedEvent {
JSONCoder<CaptureTimeType>::decode(jEvent["time"]),
JSONCoder<Error>::decode(jEvent["error"])
};
}
};
std::string eventToString(const CaptureEvent& event)
{
json result;
WTF::visit([&result](const auto& event) {
using EventType = std::decay_t<decltype(event)>;
result = JSONCoder<EventType>::encode(event);
}, event);
return result.dump(4);
}
OptionalCaptureEvent stringToEvent(const char* jsonStr)
{
auto jValue = json::parse(jsonStr);
const auto& type = jValue["type"].get_ref<const std::string&>();
OptionalCaptureEvent result { std::nullopt };
brigand::for_each<CaptureEvent>([&](auto T) {
using Type = typename decltype(T)::type;
if (!result && type == Type::typeName)
result = OptionalCaptureEvent(JSONCoder<Type>::decode(jValue));
});
return result;
}
} // namespace NetworkCapture
} // namespace WebKit
#endif // ENABLE(NETWORK_CAPTURE)
|