NetworkAgent.js
6.56 KB
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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule NetworkAgent
* @flow
*/
'use strict';
const InspectorAgent = require('InspectorAgent');
const JSInspector = require('JSInspector');
const Map = require('Map');
const XMLHttpRequest = require('XMLHttpRequest');
import type EventSender from 'InspectorAgent';
type RequestId = string;
type LoaderId = string;
type FrameId = string;
type Timestamp = number;
type Headers = Object;
// We don't currently care about this
type ResourceTiming = null;
type ResourceType =
'Document' |
'Stylesheet' |
'Image' |
'Media' |
'Font' |
'Script' |
'TextTrack' |
'XHR' |
'Fetch' |
'EventSource' |
'WebSocket' |
'Manifest' |
'Other';
type SecurityState =
'unknown' |
'neutral' |
'insecure' |
'warning' |
'secure' |
'info';
type BlockedReason =
'csp' |
'mixed-content' |
'origin' |
'inspector' |
'subresource-filter' |
'other';
type StackTrace = null;
type Initiator = {
type: 'script' | 'other',
stackTrace?: StackTrace,
url?: string,
lineNumber?: number
}
type ResourcePriority = 'VeryLow' | 'Low' | 'Medium' | 'High' | 'VeryHigh';
type Request = {
url: string,
method: string,
headers: Headers,
postData?: string,
mixedContentType?: 'blockable' | 'optionally-blockable' | 'none',
initialPriority: ResourcePriority,
};
type Response = {
url: string,
status: number,
statusText: string,
headers: Headers,
headersText?: string,
mimeType: string,
requestHeaders?: Headers,
requestHeadersText?: string,
connectionReused: boolean,
connectionId: number,
fromDiskCache?: boolean,
encodedDataLength: number,
timing?: ResourceTiming,
securityState: SecurityState,
};
type RequestWillBeSentEvent = {
requestId: RequestId,
frameId: FrameId,
loaderId: LoaderId,
documentURL: string,
request: Request,
timestamp: Timestamp,
initiator: Initiator,
redirectResponse?: Response,
// This is supposed to be optional but the inspector crashes without it,
// see https://bugs.chromium.org/p/chromium/issues/detail?id=653138
type: ResourceType,
};
type ResponseReceivedEvent = {
requestId: RequestId,
frameId: FrameId,
loaderId: LoaderId,
timestamp: Timestamp,
type: ResourceType,
response: Response,
};
type DataReceived = {
requestId: RequestId,
timestamp: Timestamp,
dataLength: number,
encodedDataLength: number,
};
type LoadingFinishedEvent = {
requestId: RequestId,
timestamp: Timestamp,
encodedDataLength: number,
};
type LoadingFailedEvent = {
requestId: RequestId,
timestamp: Timestamp,
type: ResourceType,
errorText: string,
canceled?: boolean,
blockedReason?: BlockedReason,
};
class Interceptor {
_agent: NetworkAgent;
_requests: Map<string, string>;
constructor(agent: NetworkAgent) {
this._agent = agent;
this._requests = new Map();
}
getData(requestId: string): ?string {
return this._requests.get(requestId);
}
requestSent(
id: number,
url: string,
method: string,
headers: Object) {
const requestId = String(id);
this._requests.set(requestId, '');
const request: Request = {
url,
method,
headers,
initialPriority: 'Medium',
};
const event: RequestWillBeSentEvent = {
requestId,
documentURL: '',
frameId: '1',
loaderId: '1',
request,
timestamp: JSInspector.getTimestamp(),
initiator: {
// TODO(blom): Get stack trace
// If type is 'script' the inspector will try to execute
// `stack.callFrames[0]`
type: 'other',
},
type: 'Other',
};
this._agent.sendEvent('requestWillBeSent', event);
}
responseReceived(
id: number,
url: string,
status: number,
headers: Object) {
const requestId = String(id);
const response: Response = {
url,
status,
statusText: String(status),
headers,
// TODO(blom) refined headers, can we get this?
requestHeaders: {},
mimeType: this._getMimeType(headers),
connectionReused: false,
connectionId: -1,
encodedDataLength: 0,
securityState: 'unknown',
};
const event: ResponseReceivedEvent = {
requestId,
frameId: '1',
loaderId: '1',
timestamp: JSInspector.getTimestamp(),
type: 'Other',
response,
};
this._agent.sendEvent('responseReceived', event);
}
dataReceived(
id: number,
data: string) {
const requestId = String(id);
const existingData = this._requests.get(requestId) || '';
this._requests.set(requestId, existingData.concat(data));
const event: DataReceived = {
requestId,
timestamp: JSInspector.getTimestamp(),
dataLength: data.length,
encodedDataLength: data.length,
};
this._agent.sendEvent('dataReceived', event);
}
loadingFinished(
id: number,
encodedDataLength: number) {
const event: LoadingFinishedEvent = {
requestId: String(id),
timestamp: JSInspector.getTimestamp(),
encodedDataLength: encodedDataLength,
};
this._agent.sendEvent('loadingFinished', event);
}
loadingFailed(
id: number,
error: string) {
const event: LoadingFailedEvent = {
requestId: String(id),
timestamp: JSInspector.getTimestamp(),
type: 'Other',
errorText: error,
};
this._agent.sendEvent('loadingFailed', event);
}
_getMimeType(headers: Object): string {
const contentType = headers['Content-Type'] || '';
return contentType.split(';')[0];
}
}
type EnableArgs = {
maxResourceBufferSize?: number,
maxTotalBufferSize?: number
};
class NetworkAgent extends InspectorAgent {
static DOMAIN = 'Network';
_sendEvent: EventSender;
_interceptor: ?Interceptor;
enable({ maxResourceBufferSize, maxTotalBufferSize }: EnableArgs) {
this._interceptor = new Interceptor(this);
XMLHttpRequest.setInterceptor(this._interceptor);
}
disable() {
XMLHttpRequest.setInterceptor(null);
this._interceptor = null;
}
getResponseBody({requestId}: {requestId: RequestId})
: {body: ?string, base64Encoded: boolean} {
return {body: this.interceptor().getData(requestId), base64Encoded: false};
}
interceptor(): Interceptor {
if (this._interceptor) {
return this._interceptor;
} else {
throw Error('_interceptor can not be null');
}
}
}
module.exports = NetworkAgent;