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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { defer, all } = require("promise");
const { LocalizationHelper } = require("devtools/shared/l10n");
const Services = require("Services");
const appInfo = Services.appinfo;
const { CurlUtils } = require("devtools/client/shared/curl");
const { getFormDataSections } = require("devtools/client/netmonitor/request-utils");
loader.lazyRequireGetter(this, "NetworkHelper", "devtools/shared/webconsole/network-helper");
loader.lazyGetter(this, "L10N", () => {
return new LocalizationHelper("devtools/client/locales/har.properties");
});
const HAR_VERSION = "1.1";
/**
* This object is responsible for building HAR file. See HAR spec:
* https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/HAR/Overview.html
* http://www.softwareishard.com/blog/har-12-spec/
*
* @param {Object} options configuration object
*
* The following options are supported:
*
* - items {Array}: List of Network requests to be exported. It is possible
* to use directly: NetMonitorView.RequestsMenu.items
*
* - id {String}: ID of the exported page.
*
* - title {String}: Title of the exported page.
*
* - includeResponseBodies {Boolean}: Set to true to include HTTP response
* bodies in the result data structure.
*/
var HarBuilder = function (options) {
this._options = options;
this._pageMap = [];
};
HarBuilder.prototype = {
// Public API
/**
* This is the main method used to build the entire result HAR data.
* The process is asynchronous since it can involve additional RDP
* communication (e.g. resolving long strings).
*
* @returns {Promise} A promise that resolves to the HAR object when
* the entire build process is done.
*/
build: function () {
this.promises = [];
// Build basic structure for data.
let log = this.buildLog();
// Build entries.
let items = this._options.items;
for (let i = 0; i < items.length; i++) {
let file = items[i].attachment;
log.entries.push(this.buildEntry(log, file));
}
// Some data needs to be fetched from the backend during the
// build process, so wait till all is done.
let { resolve, promise } = defer();
all(this.promises).then(results => resolve({ log: log }));
return promise;
},
// Helpers
buildLog: function () {
return {
version: HAR_VERSION,
creator: {
name: appInfo.name,
version: appInfo.version
},
browser: {
name: appInfo.name,
version: appInfo.version
},
pages: [],
entries: [],
};
},
buildPage: function (file) {
let page = {};
// Page start time is set when the first request is processed
// (see buildEntry)
page.startedDateTime = 0;
page.id = "page_" + this._options.id;
page.title = this._options.title;
return page;
},
getPage: function (log, file) {
let id = this._options.id;
let page = this._pageMap[id];
if (page) {
return page;
}
this._pageMap[id] = page = this.buildPage(file);
log.pages.push(page);
return page;
},
buildEntry: function (log, file) {
let page = this.getPage(log, file);
let entry = {};
entry.pageref = page.id;
entry.startedDateTime = dateToJSON(new Date(file.startedMillis));
entry.time = file.endedMillis - file.startedMillis;
entry.request = this.buildRequest(file);
entry.response = this.buildResponse(file);
entry.cache = this.buildCache(file);
entry.timings = file.eventTimings ? file.eventTimings.timings : {};
if (file.remoteAddress) {
entry.serverIPAddress = file.remoteAddress;
}
if (file.remotePort) {
entry.connection = file.remotePort + "";
}
// Compute page load start time according to the first request start time.
if (!page.startedDateTime) {
page.startedDateTime = entry.startedDateTime;
page.pageTimings = this.buildPageTimings(page, file);
}
return entry;
},
buildPageTimings: function (page, file) {
// Event timing info isn't available
let timings = {
onContentLoad: -1,
onLoad: -1
};
return timings;
},
buildRequest: function (file) {
let request = {
bodySize: 0
};
request.method = file.method;
request.url = file.url;
request.httpVersion = file.httpVersion || "";
request.headers = this.buildHeaders(file.requestHeaders);
request.headers = this.appendHeadersPostData(request.headers, file);
request.cookies = this.buildCookies(file.requestCookies);
request.queryString = NetworkHelper.parseQueryString(
NetworkHelper.nsIURL(file.url).query) || [];
request.postData = this.buildPostData(file);
request.headersSize = file.requestHeaders.headersSize;
// Set request body size, but make sure the body is fetched
// from the backend.
if (file.requestPostData) {
this.fetchData(file.requestPostData.postData.text).then(value => {
request.bodySize = value.length;
});
}
return request;
},
/**
* Fetch all header values from the backend (if necessary) and
* build the result HAR structure.
*
* @param {Object} input Request or response header object.
*/
buildHeaders: function (input) {
if (!input) {
return [];
}
return this.buildNameValuePairs(input.headers);
},
appendHeadersPostData: function (input = [], file) {
if (!file.requestPostData) {
return input;
}
this.fetchData(file.requestPostData.postData.text).then(value => {
let multipartHeaders = CurlUtils.getHeadersFromMultipartText(value);
for (let header of multipartHeaders) {
input.push(header);
}
});
return input;
},
buildCookies: function (input) {
if (!input) {
return [];
}
return this.buildNameValuePairs(input.cookies);
},
buildNameValuePairs: function (entries) {
let result = [];
// HAR requires headers array to be presented, so always
// return at least an empty array.
if (!entries) {
return result;
}
// Make sure header values are fully fetched from the server.
entries.forEach(entry => {
this.fetchData(entry.value).then(value => {
result.push({
name: entry.name,
value: value
});
});
});
return result;
},
buildPostData: function (file) {
let postData = {
mimeType: findValue(file.requestHeaders.headers, "content-type"),
params: [],
text: ""
};
if (!file.requestPostData) {
return postData;
}
if (file.requestPostData.postDataDiscarded) {
postData.comment = L10N.getStr("har.requestBodyNotIncluded");
return postData;
}
// Load request body from the backend.
this.fetchData(file.requestPostData.postData.text).then(postDataText => {
postData.text = postDataText;
// If we are dealing with URL encoded body, parse parameters.
let { headers } = file.requestHeaders;
if (CurlUtils.isUrlEncodedRequest({ headers, postDataText })) {
postData.mimeType = "application/x-www-form-urlencoded";
// Extract form parameters and produce nice HAR array.
getFormDataSections(
file.requestHeaders,
file.requestHeadersFromUploadStream,
file.requestPostData,
this._options.getString
).then(formDataSections => {
formDataSections.forEach(section => {
let paramsArray = NetworkHelper.parseQueryString(section);
if (paramsArray) {
postData.params = [...postData.params, ...paramsArray];
}
});
});
}
});
return postData;
},
buildResponse: function (file) {
let response = {
status: 0
};
// Arbitrary value if it's aborted to make sure status has a number
if (file.status) {
response.status = parseInt(file.status, 10);
}
let responseHeaders = file.responseHeaders;
response.statusText = file.statusText || "";
response.httpVersion = file.httpVersion || "";
response.headers = this.buildHeaders(responseHeaders);
response.cookies = this.buildCookies(file.responseCookies);
response.content = this.buildContent(file);
let headers = responseHeaders ? responseHeaders.headers : null;
let headersSize = responseHeaders ? responseHeaders.headersSize : -1;
response.redirectURL = findValue(headers, "Location");
response.headersSize = headersSize;
// 'bodySize' is size of the received response body in bytes.
// Set to zero in case of responses coming from the cache (304).
// Set to -1 if the info is not available.
if (typeof file.transferredSize != "number") {
response.bodySize = (response.status == 304) ? 0 : -1;
} else {
response.bodySize = file.transferredSize;
}
return response;
},
buildContent: function (file) {
let content = {
mimeType: file.mimeType,
size: -1
};
let responseContent = file.responseContent;
if (responseContent && responseContent.content) {
content.size = responseContent.content.size;
content.encoding = responseContent.content.encoding;
}
let includeBodies = this._options.includeResponseBodies;
let contentDiscarded = responseContent ?
responseContent.contentDiscarded : false;
// The comment is appended only if the response content
// is explicitly discarded.
if (!includeBodies || contentDiscarded) {
content.comment = L10N.getStr("har.responseBodyNotIncluded");
return content;
}
if (responseContent) {
let text = responseContent.content.text;
this.fetchData(text).then(value => {
content.text = value;
});
}
return content;
},
buildCache: function (file) {
let cache = {};
if (!file.fromCache) {
return cache;
}
// There is no such info yet in the Net panel.
// cache.beforeRequest = {};
if (file.cacheEntry) {
cache.afterRequest = this.buildCacheEntry(file.cacheEntry);
} else {
cache.afterRequest = null;
}
return cache;
},
buildCacheEntry: function (cacheEntry) {
let cache = {};
cache.expires = findValue(cacheEntry, "Expires");
cache.lastAccess = findValue(cacheEntry, "Last Fetched");
cache.eTag = "";
cache.hitCount = findValue(cacheEntry, "Fetch Count");
return cache;
},
getBlockingEndTime: function (file) {
if (file.resolveStarted && file.connectStarted) {
return file.resolvingTime;
}
if (file.connectStarted) {
return file.connectingTime;
}
if (file.sendStarted) {
return file.sendingTime;
}
return (file.sendingTime > file.startTime) ?
file.sendingTime : file.waitingForTime;
},
// RDP Helpers
fetchData: function (string) {
let promise = this._options.getString(string).then(value => {
return value;
});
// Building HAR is asynchronous and not done till all
// collected promises are resolved.
this.promises.push(promise);
return promise;
}
};
// Helpers
/**
* Find specified value within an array of name-value pairs
* (used for headers, cookies and cache entries)
*/
function findValue(arr, name) {
if (!arr) {
return "";
}
name = name.toLowerCase();
let result = arr.find(entry => entry.name.toLowerCase() == name);
return result ? result.value : "";
}
/**
* Generate HAR representation of a date.
* (YYYY-MM-DDThh:mm:ss.sTZD, e.g. 2009-07-24T19:20:30.45+01:00)
* See also HAR Schema: http://janodvarko.cz/har/viewer/
*
* Note: it would be great if we could utilize Date.toJSON(), but
* it doesn't return proper time zone offset.
*
* An example:
* This helper returns: 2015-05-29T16:10:30.424+02:00
* Date.toJSON() returns: 2015-05-29T14:10:30.424Z
*
* @param date {Date} The date object we want to convert.
*/
function dateToJSON(date) {
function f(n, c) {
if (!c) {
c = 2;
}
let s = new String(n);
while (s.length < c) {
s = "0" + s;
}
return s;
}
let result = date.getFullYear() + "-" +
f(date.getMonth() + 1) + "-" +
f(date.getDate()) + "T" +
f(date.getHours()) + ":" +
f(date.getMinutes()) + ":" +
f(date.getSeconds()) + "." +
f(date.getMilliseconds(), 3);
let offset = date.getTimezoneOffset();
let positive = offset > 0;
// Convert to positive number before using Math.floor (see issue 5512)
offset = Math.abs(offset);
let offsetHours = Math.floor(offset / 60);
let offsetMinutes = Math.floor(offset % 60);
let prettyOffset = (positive > 0 ? "-" : "+") + f(offsetHours) +
":" + f(offsetMinutes);
return result + prettyOffset;
}
// Exports from this module
exports.HarBuilder = HarBuilder;
|