Path: blob/trunk/third_party/closure/goog/net/channeldebug.js
2868 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Definition of the ChannelDebug class. ChannelDebug provides16* a utility for tracing and debugging the BrowserChannel requests.17*18*/192021/**22* Namespace for BrowserChannel23*/24goog.provide('goog.net.ChannelDebug');2526goog.require('goog.json');27goog.require('goog.log');28293031/**32* Logs and keeps a buffer of debugging info for the Channel.33*34* @constructor35*/36goog.net.ChannelDebug = function() {37/**38* The logger instance.39* @const40* @private {?goog.debug.Logger}41*/42this.logger_ = goog.log.getLogger('goog.net.BrowserChannel');43};444546/**47* Gets the logger used by this ChannelDebug.48* @return {goog.debug.Logger} The logger used by this ChannelDebug.49*/50goog.net.ChannelDebug.prototype.getLogger = function() {51return this.logger_;52};535455/**56* Logs that the browser went offline during the lifetime of a request.57* @param {goog.Uri} url The URL being requested.58*/59goog.net.ChannelDebug.prototype.browserOfflineResponse = function(url) {60this.info('BROWSER_OFFLINE: ' + url);61};626364/**65* Logs an XmlHttp request..66* @param {string} verb The request type (GET/POST).67* @param {goog.Uri} uri The request destination.68* @param {string|number|undefined} id The request id.69* @param {number} attempt Which attempt # the request was.70* @param {?string} postData The data posted in the request.71*/72goog.net.ChannelDebug.prototype.xmlHttpChannelRequest = function(73verb, uri, id, attempt, postData) {74this.info(75'XMLHTTP REQ (' + id + ') [attempt ' + attempt + ']: ' + verb + '\n' +76uri + '\n' + this.maybeRedactPostData_(postData));77};787980/**81* Logs the meta data received from an XmlHttp request.82* @param {string} verb The request type (GET/POST).83* @param {goog.Uri} uri The request destination.84* @param {string|number|undefined} id The request id.85* @param {number} attempt Which attempt # the request was.86* @param {goog.net.XmlHttp.ReadyState} readyState The ready state.87* @param {number} statusCode The HTTP status code.88*/89goog.net.ChannelDebug.prototype.xmlHttpChannelResponseMetaData = function(90verb, uri, id, attempt, readyState, statusCode) {91this.info(92'XMLHTTP RESP (' + id + ') [ attempt ' + attempt + ']: ' + verb + '\n' +93uri + '\n' + readyState + ' ' + statusCode);94};959697/**98* Logs the response data received from an XmlHttp request.99* @param {string|number|undefined} id The request id.100* @param {?string} responseText The response text.101* @param {?string=} opt_desc Optional request description.102*/103goog.net.ChannelDebug.prototype.xmlHttpChannelResponseText = function(104id, responseText, opt_desc) {105this.info(106'XMLHTTP TEXT (' + id + '): ' + this.redactResponse_(responseText) +107(opt_desc ? ' ' + opt_desc : ''));108};109110111/**112* Logs a Trident ActiveX request.113* @param {string} verb The request type (GET/POST).114* @param {goog.Uri} uri The request destination.115* @param {string|number|undefined} id The request id.116* @param {number} attempt Which attempt # the request was.117*/118goog.net.ChannelDebug.prototype.tridentChannelRequest = function(119verb, uri, id, attempt) {120this.info(121'TRIDENT REQ (' + id + ') [ attempt ' + attempt + ']: ' + verb + '\n' +122uri);123};124125126/**127* Logs the response text received from a Trident ActiveX request.128* @param {string|number|undefined} id The request id.129* @param {string} responseText The response text.130*/131goog.net.ChannelDebug.prototype.tridentChannelResponseText = function(132id, responseText) {133this.info('TRIDENT TEXT (' + id + '): ' + this.redactResponse_(responseText));134};135136137/**138* Logs the done response received from a Trident ActiveX request.139* @param {string|number|undefined} id The request id.140* @param {boolean} successful Whether the request was successful.141*/142goog.net.ChannelDebug.prototype.tridentChannelResponseDone = function(143id, successful) {144this.info('TRIDENT TEXT (' + id + '): ' + successful ? 'success' : 'failure');145};146147148/**149* Logs a request timeout.150* @param {goog.Uri} uri The uri that timed out.151*/152goog.net.ChannelDebug.prototype.timeoutResponse = function(uri) {153this.info('TIMEOUT: ' + uri);154};155156157/**158* Logs a debug message.159* @param {string} text The message.160*/161goog.net.ChannelDebug.prototype.debug = function(text) {162this.info(text);163};164165166/**167* Logs an exception168* @param {Error} e The error or error event.169* @param {string=} opt_msg The optional message, defaults to 'Exception'.170*/171goog.net.ChannelDebug.prototype.dumpException = function(e, opt_msg) {172this.severe((opt_msg || 'Exception') + e);173};174175176/**177* Logs an info message.178* @param {string} text The message.179*/180goog.net.ChannelDebug.prototype.info = function(text) {181goog.log.info(this.logger_, text);182};183184185/**186* Logs a warning message.187* @param {string} text The message.188*/189goog.net.ChannelDebug.prototype.warning = function(text) {190goog.log.warning(this.logger_, text);191};192193194/**195* Logs a severe message.196* @param {string} text The message.197*/198goog.net.ChannelDebug.prototype.severe = function(text) {199goog.log.error(this.logger_, text);200};201202203/**204* Removes potentially private data from a response so that we don't205* accidentally save private and personal data to the server logs.206* @param {?string} responseText A JSON response to clean.207* @return {?string} The cleaned response.208* @private209*/210goog.net.ChannelDebug.prototype.redactResponse_ = function(responseText) {211// first check if it's not JS - the only non-JS should be the magic cookie212if (!responseText ||213responseText == goog.net.ChannelDebug.MAGIC_RESPONSE_COOKIE) {214return responseText;215}216217try {218var responseArray = JSON.parse(responseText);219if (responseArray) {220for (var i = 0; i < responseArray.length; i++) {221if (goog.isArray(responseArray[i])) {222this.maybeRedactArray_(responseArray[i]);223}224}225}226227return goog.json.serialize(responseArray);228} catch (e) {229this.debug('Exception parsing expected JS array - probably was not JS');230return responseText;231}232};233234235/**236* Removes data from a response array that may be sensitive.237* @param {Array<?>} array The array to clean.238* @private239*/240goog.net.ChannelDebug.prototype.maybeRedactArray_ = function(array) {241if (array.length < 2) {242return;243}244var dataPart = array[1];245if (!goog.isArray(dataPart)) {246return;247}248if (dataPart.length < 1) {249return;250}251252var type = dataPart[0];253if (type != 'noop' && type != 'stop') {254// redact all fields in the array255for (var i = 1; i < dataPart.length; i++) {256dataPart[i] = '';257}258}259};260261262/**263* Removes potentially private data from a request POST body so that we don't264* accidentally save private and personal data to the server logs.265* @param {?string} data The data string to clean.266* @return {?string} The data string with sensitive data replaced by 'redacted'.267* @private268*/269goog.net.ChannelDebug.prototype.maybeRedactPostData_ = function(data) {270if (!data) {271return null;272}273var out = '';274var params = data.split('&');275for (var i = 0; i < params.length; i++) {276var param = params[i];277var keyValue = param.split('=');278if (keyValue.length > 1) {279var key = keyValue[0];280var value = keyValue[1];281282var keyParts = key.split('_');283if (keyParts.length >= 2 && keyParts[1] == 'type') {284out += key + '=' + value + '&';285} else {286out += key + '=' +287'redacted' +288'&';289}290}291}292return out;293};294295296/**297* The normal response for forward channel requests.298* Used only before version 8 of the protocol.299* @const300*/301goog.net.ChannelDebug.MAGIC_RESPONSE_COOKIE = 'y2f%';302303304