1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // TODO(eroman): put these methods into a namespace.
7 var printLogEntriesAsText;
8 var createLogEntryTablePrinter;
9 var proxySettingsToString;
10 var stripCookiesAndLoginInfo;
12 // Start of anonymous namespace.
16 function canCollapseBeginWithEnd(beginEntry) {
18 beginEntry.isBegin() &&
20 beginEntry.end.index == beginEntry.index + 1 &&
21 (!beginEntry.orig.params || !beginEntry.end.orig.params);
25 * Adds a child pre element to the end of |parent|, and writes the
26 * formatted contents of |logEntries| to it.
28 printLogEntriesAsText = function(logEntries, parent, privacyStripping,
30 var tablePrinter = createLogEntryTablePrinter(logEntries, privacyStripping,
33 // Format the table for fixed-width text.
34 tablePrinter.toText(0, parent);
37 * Creates a TablePrinter for use by the above two functions.
39 createLogEntryTablePrinter = function(logEntries, privacyStripping,
41 var entries = LogGroupEntry.createArrayFrom(logEntries);
42 var tablePrinter = new TablePrinter();
43 var parameterOutputter = new ParameterOutputter(tablePrinter);
45 if (entries.length == 0)
48 var startTime = timeutil.convertTimeTicksToTime(entries[0].orig.time);
50 for (var i = 0; i < entries.length; ++i) {
51 var entry = entries[i];
53 // Avoid printing the END for a BEGIN that was immediately before, unless
54 // both have extra parameters.
55 if (!entry.isEnd() || !canCollapseBeginWithEnd(entry.begin)) {
56 var entryTime = timeutil.convertTimeTicksToTime(entry.orig.time);
57 addRowWithTime(tablePrinter, entryTime, startTime);
59 for (var j = entry.getDepth(); j > 0; --j)
60 tablePrinter.addCell(' ');
62 var eventText = getTextForEvent(entry);
63 // Get the elapsed time, and append it to the event text.
64 if (entry.isBegin()) {
68 dt = entry.end.orig.time - entry.orig.time;
69 } else if (logCreationTime != undefined) {
70 dt = (logCreationTime - entryTime) + '+';
72 eventText += ' [dt=' + dt + ']';
75 var mainCell = tablePrinter.addCell(eventText);
76 mainCell.allowOverflow = true;
79 // Output the extra parameters.
80 if (typeof entry.orig.params == 'object') {
81 // Those 5 skipped cells are: two for "t=", and three for "st=".
82 tablePrinter.setNewRowCellIndent(5 + entry.getDepth());
83 writeParameters(entry.orig, privacyStripping, parameterOutputter);
85 tablePrinter.setNewRowCellIndent(0);
89 // If viewing a saved log file, add row with just the time the log was
90 // created, if the event never completed.
91 var lastEntry = entries[entries.length - 1];
92 // If the last entry has a non-zero depth or is a begin event, the source is
94 var isSourceActive = lastEntry.getDepth() != 0 || lastEntry.isBegin();
95 if (logCreationTime != undefined && isSourceActive)
96 addRowWithTime(tablePrinter, logCreationTime, startTime);
102 * Adds a new row to the given TablePrinter, and adds five cells containing
103 * information about the time an event occured.
104 * Format is '[t=<UTC time in ms>] [st=<ms since the source started>]'.
105 * @param {TablePrinter} tablePrinter The table printer to add the cells to.
106 * @param {number} eventTime The time the event occured, as a UTC time in
108 * @param {number} startTime The time the first event for the source occured,
109 * as a UTC time in milliseconds.
111 function addRowWithTime(tablePrinter, eventTime, startTime) {
112 tablePrinter.addRow();
113 tablePrinter.addCell('t=');
114 var tCell = tablePrinter.addCell(eventTime);
115 tCell.alignRight = true;
116 tablePrinter.addCell(' [st=');
117 var stCell = tablePrinter.addCell(eventTime - startTime);
118 stCell.alignRight = true;
119 tablePrinter.addCell('] ');
123 * |hexString| must be a string of hexadecimal characters with no whitespace,
124 * whose length is a multiple of two. Writes multiple lines to |out| with
125 * the hexadecimal characters from |hexString| on the left, in groups of
126 * two, and their corresponding ASCII characters on the right.
128 * |asciiCharsPerLine| specifies how many ASCII characters will be put on each
129 * line of the output string.
131 function writeHexString(hexString, asciiCharsPerLine, out) {
132 // Number of transferred bytes in a line of output. Length of a
133 // line is roughly 4 times larger.
134 var hexCharsPerLine = 2 * asciiCharsPerLine;
135 for (var i = 0; i < hexString.length; i += hexCharsPerLine) {
138 for (var j = i; j < i + hexCharsPerLine && j < hexString.length; j += 2) {
139 var hex = hexString.substr(j, 2);
140 hexLine += hex + ' ';
141 var charCode = parseInt(hex, 16);
142 // For ASCII codes 32 though 126, display the corresponding
143 // characters. Use a space for nulls, and a period for
145 if (charCode >= 0x20 && charCode <= 0x7E) {
146 asciiLine += String.fromCharCode(charCode);
147 } else if (charCode == 0x00) {
154 // Make the ASCII text for the last line of output align with the previous
156 hexLine += makeRepeatedString(' ', 3 * asciiCharsPerLine - hexLine.length);
157 out.writeLine(' ' + hexLine + ' ' + asciiLine);
162 * Wrapper around a TablePrinter to simplify outputting lines of text for event
165 var ParameterOutputter = (function() {
169 function ParameterOutputter(tablePrinter) {
170 this.tablePrinter_ = tablePrinter;
173 ParameterOutputter.prototype = {
175 * Outputs a single line.
177 writeLine: function(line) {
178 this.tablePrinter_.addRow();
179 var cell = this.tablePrinter_.addCell(line);
180 cell.allowOverflow = true;
185 * Outputs a key=value line which looks like:
189 writeArrowKeyValue: function(key, value, link) {
190 var cell = this.writeLine(kArrow + key + ' = ' + value);
195 * Outputs a key= line which looks like:
199 writeArrowKey: function(key) {
200 this.writeLine(kArrow + key + ' =');
204 * Outputs multiple lines, each indented by numSpaces.
205 * For instance if numSpaces=8 it might look like this:
211 writeSpaceIndentedLines: function(numSpaces, lines) {
212 var prefix = makeRepeatedString(' ', numSpaces);
213 for (var i = 0; i < lines.length; ++i)
214 this.writeLine(prefix + lines[i]);
218 * Outputs multiple lines such that the first line has
219 * an arrow pointing at it, and subsequent lines
220 * align with the first one. For example:
226 writeArrowIndentedLines: function(lines) {
227 if (lines.length == 0)
230 this.writeLine(kArrow + lines[0]);
232 for (var i = 1; i < lines.length; ++i)
233 this.writeLine(kArrowIndentation + lines[i]);
237 var kArrow = ' --> ';
238 var kArrowIndentation = ' ';
240 return ParameterOutputter;
241 })(); // end of ParameterOutputter
244 * Formats the parameters for |entry| and writes them to |out|.
245 * Certain event types have custom pretty printers. Everything else will
246 * default to a JSON-like format.
248 function writeParameters(entry, privacyStripping, out) {
249 if (privacyStripping) {
250 // If privacy stripping is enabled, remove data as needed.
251 entry = stripCookiesAndLoginInfo(entry);
253 // If headers are in an object, convert them to an array for better display.
254 entry = reformatHeaders(entry);
257 // Use any parameter writer available for this event type.
258 var paramsWriter = getParamaterWriterForEventType(entry.type);
259 var consumedParams = {};
261 paramsWriter(entry, out, consumedParams);
263 // Write any un-consumed parameters.
264 for (var k in entry.params) {
265 if (consumedParams[k])
267 defaultWriteParameter(k, entry.params[k], out);
272 * Finds a writer to format the parameters for events of type |eventType|.
274 * @return {function} The returned function "writer" can be invoked
275 * as |writer(entry, writer, consumedParams)|. It will
276 * output the parameters of |entry| to |out|, and fill
277 * |consumedParams| with the keys of the parameters
278 * consumed. If no writer is available for |eventType| then
281 function getParamaterWriterForEventType(eventType) {
283 case EventType.HTTP_TRANSACTION_SEND_REQUEST_HEADERS:
284 case EventType.HTTP_TRANSACTION_SEND_TUNNEL_HEADERS:
285 return writeParamsForRequestHeaders;
287 case EventType.PROXY_CONFIG_CHANGED:
288 return writeParamsForProxyConfigChanged;
290 case EventType.CERT_VERIFIER_JOB:
291 case EventType.SSL_CERTIFICATES_RECEIVED:
292 return writeParamsForCertificates;
294 case EventType.SSL_VERSION_FALLBACK:
295 return writeParamsForSSLVersionFallback;
301 * Default parameter writer that outputs a visualization of field named |key|
302 * with value |value| to |out|.
304 function defaultWriteParameter(key, value, out) {
305 if (key == 'headers' && value instanceof Array) {
306 out.writeArrowIndentedLines(value);
310 // For transferred bytes, display the bytes in hex and ASCII.
311 if (key == 'hex_encoded_bytes' && typeof value == 'string') {
312 out.writeArrowKey(key);
313 writeHexString(value, 20, out);
317 // Handle source_dependency entries - add link and map source type to
319 if (key == 'source_dependency' && typeof value == 'object') {
320 var link = '#events&s=' + value.id;
321 var valueStr = value.id + ' (' + EventSourceTypeNames[value.type] + ')';
322 out.writeArrowKeyValue(key, valueStr, link);
326 if (key == 'net_error' && typeof value == 'number') {
327 var valueStr = value + ' (' + netErrorToString(value) + ')';
328 out.writeArrowKeyValue(key, valueStr);
332 if (key == 'quic_error' && typeof value == 'number') {
333 var valueStr = value + ' (' + quicErrorToString(value) + ')';
334 out.writeArrowKeyValue(key, valueStr);
338 if (key == 'quic_crypto_handshake_message' && typeof value == 'string') {
339 var lines = value.split('\n');
340 out.writeArrowIndentedLines(lines);
344 if (key == 'quic_rst_stream_error' && typeof value == 'number') {
345 var valueStr = value + ' (' + quicRstStreamErrorToString(value) + ')';
346 out.writeArrowKeyValue(key, valueStr);
350 if (key == 'load_flags' && typeof value == 'number') {
351 var valueStr = value + ' (' + getLoadFlagSymbolicString(value) + ')';
352 out.writeArrowKeyValue(key, valueStr);
356 if (key == 'load_state' && typeof value == 'number') {
357 var valueStr = value + ' (' + getKeyWithValue(LoadState, value) + ')';
358 out.writeArrowKeyValue(key, valueStr);
362 // Otherwise just default to JSON formatting of the value.
363 out.writeArrowKeyValue(key, JSON.stringify(value));
367 * Returns the set of LoadFlags that make up the integer |loadFlag|.
368 * For example: getLoadFlagSymbolicString(
370 function getLoadFlagSymbolicString(loadFlag) {
371 // Load flag of 0 means "NORMAL". Special case this, since and-ing with
372 // 0 is always going to be false.
374 return getKeyWithValue(LoadFlag, loadFlag);
376 var matchingLoadFlagNames = [];
378 for (var k in LoadFlag) {
379 if (loadFlag & LoadFlag[k])
380 matchingLoadFlagNames.push(k);
383 return matchingLoadFlagNames.join(' | ');
387 * Converts an SSL version number to a textual representation.
388 * For instance, SSLVersionNumberToName(0x0301) returns 'TLS 1.0'.
390 function SSLVersionNumberToName(version) {
391 if ((version & 0xFFFF) != version) {
392 // If the version number is more than 2 bytes long something is wrong.
394 return 'SSL 0x' + version.toString(16);
397 // See if it is a known TLS name.
403 var name = kTLSNames[version];
407 // Otherwise label it as an SSL version.
408 var major = (version & 0xFF00) >> 8;
409 var minor = version & 0x00FF;
411 return 'SSL ' + major + '.' + minor;
415 * TODO(eroman): get rid of this, as it is only used by 1 callsite.
417 * Indent |lines| by |start|.
419 * For example, if |start| = ' -> ' and |lines| = ['line1', 'line2', 'line3']
420 * the output will be:
426 function indentLines(start, lines) {
427 return start + lines.join('\n' + makeRepeatedString(' ', start.length));
431 * If entry.param.headers exists and is an object other than an array, converts
432 * it into an array and returns a new entry. Otherwise, just returns the
435 function reformatHeaders(entry) {
436 // If there are no headers, or it is not an object other than an array,
437 // return |entry| without modification.
438 if (!entry.params || entry.params.headers === undefined ||
439 typeof entry.params.headers != 'object' ||
440 entry.params.headers instanceof Array) {
444 // Duplicate the top level object, and |entry.params|, so the original object
445 // will not be modified.
446 entry = shallowCloneObject(entry);
447 entry.params = shallowCloneObject(entry.params);
449 // Convert headers to an array.
451 for (var key in entry.params.headers)
452 headers.push(key + ': ' + entry.params.headers[key]);
453 entry.params.headers = headers;
459 * Removes a cookie or unencrypted login information from a single HTTP header
460 * line, if present, and returns the modified line. Otherwise, just returns
463 function stripCookieOrLoginInfo(line) {
470 // Unencrypted authentication patterns
471 /^authorization: \S*\s*/i,
472 /^proxy-authorization: \S*\s*/i];
474 // Prefix will hold the first part of the string that contains no private
475 // information. If null, no part of the string contains private information.
477 for (var i = 0; i < patterns.length; i++) {
478 var match = patterns[i].exec(line);
485 // Look for authentication information from data received from the server in
486 // multi-round Negotiate authentication.
487 if (prefix === null) {
488 var challengePatterns = [
489 /^www-authenticate: (\S*)\s*/i,
490 /^proxy-authenticate: (\S*)\s*/i];
491 for (var i = 0; i < challengePatterns.length; i++) {
492 var match = challengePatterns[i].exec(line);
496 // If there's no data after the scheme name, do nothing.
497 if (match[0].length == line.length)
500 // Ignore lines with commas, as they may contain lists of schemes, and
501 // the information we want to hide is Base64 encoded, so has no commas.
502 if (line.indexOf(',') >= 0)
505 // Ignore Basic and Digest authentication challenges, as they contain
506 // public information.
507 if (/^basic$/i.test(match[1]) || /^digest$/i.test(match[1]))
516 var suffix = line.slice(prefix.length);
517 // If private information has already been removed, keep the line as-is.
518 // This is often the case when viewing a loaded log.
519 // TODO(mmenke): Remove '[value was stripped]' check once M24 hits stable.
520 if (suffix.search(/^\[[0-9]+ bytes were stripped\]$/) == -1 &&
521 suffix != '[value was stripped]') {
522 return prefix + '[' + suffix.length + ' bytes were stripped]';
530 * If |entry| has headers, returns a copy of |entry| with all cookie and
531 * unencrypted login text removed. Otherwise, returns original |entry| object.
532 * This is needed so that JSON log dumps can be made without affecting the
533 * source data. Converts headers stored in objects to arrays.
535 stripCookiesAndLoginInfo = function(entry) {
536 if (!entry.params || entry.params.headers === undefined ||
537 !(entry.params.headers instanceof Object)) {
541 // Make sure entry's headers are in an array.
542 entry = reformatHeaders(entry);
544 // Duplicate the top level object, and |entry.params|. All other fields are
545 // just pointers to the original values, as they won't be modified, other than
546 // |entry.params.headers|.
547 entry = shallowCloneObject(entry);
548 entry.params = shallowCloneObject(entry.params);
550 entry.params.headers = entry.params.headers.map(stripCookieOrLoginInfo);
555 * Outputs the request header parameters of |entry| to |out|.
557 function writeParamsForRequestHeaders(entry, out, consumedParams) {
558 var params = entry.params;
560 if (!(typeof params.line == 'string') || !(params.headers instanceof Array)) {
561 // Unrecognized params.
565 // Strip the trailing CRLF that params.line contains.
566 var lineWithoutCRLF = params.line.replace(/\r\n$/g, '');
567 out.writeArrowIndentedLines([lineWithoutCRLF].concat(params.headers));
569 consumedParams.line = true;
570 consumedParams.headers = true;
574 * Outputs the certificate parameters of |entry| to |out|.
576 function writeParamsForCertificates(entry, out, consumedParams) {
577 if (!(entry.params.certificates instanceof Array)) {
578 // Unrecognized params.
582 var certs = entry.params.certificates.reduce(function(previous, current) {
583 return previous.concat(current.split('\n'));
586 out.writeArrowKey('certificates');
587 out.writeSpaceIndentedLines(8, certs);
589 consumedParams.certificates = true;
593 * Outputs the SSL version fallback parameters of |entry| to |out|.
595 function writeParamsForSSLVersionFallback(entry, out, consumedParams) {
596 var params = entry.params;
598 if (typeof params.version_before != 'number' ||
599 typeof params.version_after != 'number') {
600 // Unrecognized params.
604 var line = SSLVersionNumberToName(params.version_before) +
606 SSLVersionNumberToName(params.version_after);
607 out.writeArrowIndentedLines([line]);
609 consumedParams.version_before = true;
610 consumedParams.version_after = true;
613 function writeParamsForProxyConfigChanged(entry, out, consumedParams) {
614 var params = entry.params;
616 if (typeof params.new_config != 'object') {
617 // Unrecognized params.
621 if (typeof params.old_config == 'object') {
622 var oldConfigString = proxySettingsToString(params.old_config);
623 // The previous configuration may not be present in the case of
624 // the initial proxy settings fetch.
625 out.writeArrowKey('old_config');
627 out.writeSpaceIndentedLines(8, oldConfigString.split('\n'));
629 consumedParams.old_config = true;
632 var newConfigString = proxySettingsToString(params.new_config);
633 out.writeArrowKey('new_config');
634 out.writeSpaceIndentedLines(8, newConfigString.split('\n'));
636 consumedParams.new_config = true;
639 function getTextForEvent(entry) {
642 if (entry.isBegin() && canCollapseBeginWithEnd(entry)) {
643 // Don't prefix with '+' if we are going to collapse the END event.
645 } else if (entry.isBegin()) {
647 } else if (entry.isEnd()) {
653 text += EventTypeNames[entry.orig.type];
657 proxySettingsToString = function(config) {
661 // TODO(eroman): if |config| has unexpected properties, print it as JSON
662 // rather than hide them.
664 function getProxyListString(proxies) {
665 // Older versions of Chrome would set these values as strings, whereas newer
667 // TODO(eroman): This behavior changed in M27. Support for older logs can
668 // safely be removed circa M29.
669 if (Array.isArray(proxies)) {
670 var listString = proxies.join(', ');
671 if (proxies.length > 1)
672 return '[' + listString + ']';
678 // The proxy settings specify up to three major fallback choices
679 // (auto-detect, custom pac url, or manual settings).
680 // We enumerate these to a list so we can later number them.
683 // Output any automatic settings.
684 if (config.auto_detect)
685 modes.push(['Auto-detect']);
687 modes.push(['PAC script: ' + config.pac_url]);
689 // Output any manual settings.
690 if (config.single_proxy || config.proxy_per_scheme) {
693 if (config.single_proxy) {
694 lines.push('Proxy server: ' + getProxyListString(config.single_proxy));
695 } else if (config.proxy_per_scheme) {
696 for (var urlScheme in config.proxy_per_scheme) {
697 if (urlScheme != 'fallback') {
698 lines.push('Proxy server for ' + urlScheme.toUpperCase() + ': ' +
699 getProxyListString(config.proxy_per_scheme[urlScheme]));
702 if (config.proxy_per_scheme.fallback) {
703 lines.push('Proxy server for everything else: ' +
704 getProxyListString(config.proxy_per_scheme.fallback));
708 // Output any proxy bypass rules.
709 if (config.bypass_list) {
710 if (config.reverse_bypass) {
711 lines.push('Reversed bypass list: ');
713 lines.push('Bypass list: ');
716 for (var i = 0; i < config.bypass_list.length; ++i)
717 lines.push(' ' + config.bypass_list[i]);
724 if (modes.length < 1) {
725 // If we didn't find any proxy settings modes, we are using DIRECT.
726 result.push('Use DIRECT connections.');
727 } else if (modes.length == 1) {
728 // If there was just one mode, don't bother numbering it.
729 result.push(modes[0].join('\n'));
731 // Otherwise concatenate all of the modes into a numbered list
732 // (which correspond with the fallback order).
733 for (var i = 0; i < modes.length; ++i)
734 result.push(indentLines('(' + (i + 1) + ') ', modes[i]));
737 if (config.source != undefined && config.source != 'UNKNOWN')
738 result.push('Source: ' + config.source);
740 return result.join('\n');
743 // End of anonymous namespace.