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
|
/*
* 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 "NetworkCaptureRecorder.h"
#if ENABLE(NETWORK_CAPTURE)
#include "NetworkCaptureLogging.h"
#include "NetworkCaptureManager.h"
#include <WebCore/ResourceResponse.h>
#include <WebCore/SharedBuffer.h>
#define DEBUG_CLASS Recorder
namespace WebKit {
namespace NetworkCapture {
void Recorder::recordRequestSent(const WebCore::ResourceRequest& request)
{
// This class records NetworkLoad's process of loading a network resource.
// NetworkLoad does this by creating a NetworkDataTask and calling resume
// on it. This call to resume can be called immediately after the task has
// been created, or -- if the loading is marked as deferred -- it can be
// called later in NetworkLoad::setDeferred(true). In fact, the latter can
// be called multiple times if the loading is suspended and resumed
// multiple times.
//
// This method is called in both places where resume is called. Our task is
// to catch the call to NetworkDataTask::resume that starts the network
// loading process. We want to ignore the other calls to resume. Our
// approach to knowing which one is the first is to check our collection of
// recorded events. If it is empty, then this is the first call into the
// recorder, and we want to record the event. Otherwise, ignore it.
if (m_events.size())
return;
DEBUG_LOG("Sent request for URL = " STRING_SPECIFIER, DEBUG_STR(request.url().string()));
m_initialRequest = request;
recordEvent(RequestSentEvent(request));
}
void Recorder::recordResponseReceived(const WebCore::ResourceResponse& response)
{
// Called when receiving a response other than a redirect or error.
DEBUG_LOG("Received response from URL = " STRING_SPECIFIER, DEBUG_STR(response.url().string()));
ASSERT(m_events.size());
// TODO: Is there a better response to receiving a multi-part resource?
// Learn more about multi-part resources. Why don't we record these? (Note,
// this decision is based on some NetworkCache code.)
if (!response.isMultipart())
recordEvent(ResponseReceivedEvent(response));
else
m_events.clear();
}
void Recorder::recordRedirectReceived(const WebCore::ResourceRequest& request, const WebCore::ResourceResponse& response)
{
DEBUG_LOG("Received redirect to URL = " STRING_SPECIFIER, DEBUG_STR(request.url().string()));
ASSERT(m_events.size());
recordEvent(RedirectReceivedEvent(request, response));
}
void Recorder::recordRedirectSent(const WebCore::ResourceRequest& request)
{
DEBUG_LOG("Sent redirect for URL = " STRING_SPECIFIER, DEBUG_STR(request.url().string()));
ASSERT(m_events.size());
recordEvent(RedirectSentEvent(request));
}
void Recorder::recordDataReceived(WebCore::SharedBuffer& buffer)
{
DEBUG_LOG("Received %u bytes of data", buffer.size());
if (!m_events.size())
return;
// Prevent memory growth in case of streaming data. TODO: Is there a better
// response to this? If we encounter this condition, all of our recording
// silently goes out the window. Replay will not work, and the user doesn't
// know that.
constexpr size_t kMaximumCacheBufferSize = 10 * 1024 * 1024;
m_dataLength += buffer.size();
if (m_dataLength <= kMaximumCacheBufferSize)
recordEvent(DataReceivedEvent(buffer));
else
m_events.clear();
}
void Recorder::recordFinish(const WebCore::ResourceError& error)
{
DEBUG_LOG("Finished");
if (!m_events.size())
return;
recordEvent(FinishedEvent(error));
writeEvents();
}
void Recorder::writeEvents()
{
auto path = Manager::singleton().requestToPath(m_initialRequest);
auto handle = Manager::singleton().openCacheFile(path, WebCore::OpenForWrite);
if (!handle)
return;
for (auto const& event : m_events) {
auto asString = eventToString(event);
// Write out the JSON string with the terminating NUL. This allows us
// to better find the separate JSON objects that we write to a single
// file. It also works better with JSON parsers that expect to find a
// NUL at the end of their input.
if (handle.write(asString.c_str(), asString.size() + 1) == -1) {
DEBUG_LOG_ERROR("Error trying to write to file for URL = " STRING_SPECIFIER, DEBUG_STR(m_initialRequest.url().string()));
return;
}
}
Manager::singleton().logRecordedResource(m_initialRequest);
}
} // namespace NetworkCapture
} // namespace WebKit
#endif // ENABLE(NETWORK_CAPTURE)
|