| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Provides a utility for tracing and debugging WebChannel |
| 17 |
* requests. |
| 18 |
* |
| 19 |
* @visibility {:internal} |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
goog.provide('goog.labs.net.webChannel.WebChannelDebug'); |
| 24 |
|
| 25 |
goog.require('goog.json'); |
| 26 |
goog.require('goog.log'); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Logs and keeps a buffer of debugging info for the Channel. |
| 32 |
* |
| 33 |
* @constructor |
| 34 |
* @struct |
| 35 |
* @final |
| 36 |
*/ |
| 37 |
goog.labs.net.webChannel.WebChannelDebug = function() { |
| 38 |
/** |
| 39 |
* The logger instance. |
| 40 |
* @const |
| 41 |
* @private {?goog.log.Logger} |
| 42 |
*/ |
| 43 |
this.logger_ = goog.log.getLogger('goog.labs.net.webChannel.WebChannelDebug'); |
| 44 |
|
| 45 |
/** |
| 46 |
* Whether to enable redact. Defaults to true. |
| 47 |
* @private {boolean} |
| 48 |
*/ |
| 49 |
this.redactEnabled_ = true; |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
goog.scope(function() { |
| 54 |
var WebChannelDebug = goog.labs.net.webChannel.WebChannelDebug; |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Turns off redact. |
| 59 |
*/ |
| 60 |
WebChannelDebug.prototype.disableRedact = function() { |
| 61 |
this.redactEnabled_ = false; |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* Logs that the browser went offline during the lifetime of a request. |
| 67 |
* @param {goog.Uri} url The URL being requested. |
| 68 |
*/ |
| 69 |
WebChannelDebug.prototype.browserOfflineResponse = function(url) { |
| 70 |
this.info(function() { |
| 71 |
return 'BROWSER_OFFLINE: ' + url; |
| 72 |
}); |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Logs an XmlHttp request.. |
| 78 |
* @param {string} verb The request type (GET/POST). |
| 79 |
* @param {goog.Uri} uri The request destination. |
| 80 |
* @param {string|number|undefined} id The request id. |
| 81 |
* @param {number} attempt Which attempt # the request was. |
| 82 |
* @param {?string} postData The data posted in the request. |
| 83 |
*/ |
| 84 |
WebChannelDebug.prototype.xmlHttpChannelRequest = function( |
| 85 |
verb, uri, id, attempt, postData) { |
| 86 |
var self = this; |
| 87 |
this.info(function() { |
| 88 |
return 'XMLHTTP REQ (' + id + ') [attempt ' + attempt + ']: ' + verb + |
| 89 |
'\n' + uri + '\n' + self.maybeRedactPostData_(postData); |
| 90 |
}); |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Logs the meta data received from an XmlHttp request. |
| 96 |
* @param {string} verb The request type (GET/POST). |
| 97 |
* @param {goog.Uri} uri The request destination. |
| 98 |
* @param {string|number|undefined} id The request id. |
| 99 |
* @param {number} attempt Which attempt # the request was. |
| 100 |
* @param {goog.net.XmlHttp.ReadyState} readyState The ready state. |
| 101 |
* @param {number} statusCode The HTTP status code. |
| 102 |
*/ |
| 103 |
WebChannelDebug.prototype.xmlHttpChannelResponseMetaData = function( |
| 104 |
verb, uri, id, attempt, readyState, statusCode) { |
| 105 |
this.info(function() { |
| 106 |
return 'XMLHTTP RESP (' + id + ') [ attempt ' + attempt + ']: ' + verb + |
| 107 |
'\n' + uri + '\n' + readyState + ' ' + statusCode; |
| 108 |
}); |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Logs the response data received from an XmlHttp request. |
| 114 |
* @param {string|number|undefined} id The request id. |
| 115 |
* @param {?string} responseText The response text. |
| 116 |
* @param {?string=} opt_desc Optional request description. |
| 117 |
*/ |
| 118 |
WebChannelDebug.prototype.xmlHttpChannelResponseText = function( |
| 119 |
id, responseText, opt_desc) { |
| 120 |
var self = this; |
| 121 |
this.info(function() { |
| 122 |
return 'XMLHTTP TEXT (' + id + '): ' + self.redactResponse_(responseText) + |
| 123 |
(opt_desc ? ' ' + opt_desc : ''); |
| 124 |
}); |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* Logs a request timeout. |
| 130 |
* @param {goog.Uri} uri The uri that timed out. |
| 131 |
*/ |
| 132 |
WebChannelDebug.prototype.timeoutResponse = function(uri) { |
| 133 |
this.info(function() { |
| 134 |
return 'TIMEOUT: ' + uri; |
| 135 |
}); |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
/** |
| 140 |
* Logs a debug message. |
| 141 |
* @param {!goog.debug.Loggable} text The message. |
| 142 |
*/ |
| 143 |
WebChannelDebug.prototype.debug = function(text) { |
| 144 |
goog.log.fine(this.logger_, text); |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* Logs an exception |
| 150 |
* @param {Error} e The error or error event. |
| 151 |
* @param {goog.debug.Loggable=} opt_msg The optional message, |
| 152 |
* defaults to 'Exception'. |
| 153 |
*/ |
| 154 |
WebChannelDebug.prototype.dumpException = function(e, opt_msg) { |
| 155 |
goog.log.error(this.logger_, opt_msg || 'Exception', e); |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Logs an info message. |
| 161 |
* @param {!goog.debug.Loggable} text The message. |
| 162 |
*/ |
| 163 |
WebChannelDebug.prototype.info = function(text) { |
| 164 |
goog.log.info(this.logger_, text); |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* Logs a warning message. |
| 170 |
* @param {!goog.debug.Loggable} text The message. |
| 171 |
*/ |
| 172 |
WebChannelDebug.prototype.warning = function(text) { |
| 173 |
goog.log.warning(this.logger_, text); |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* Logs a severe message. |
| 179 |
* @param {!goog.debug.Loggable} text The message. |
| 180 |
*/ |
| 181 |
WebChannelDebug.prototype.severe = function(text) { |
| 182 |
goog.log.error(this.logger_, text); |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* Removes potentially private data from a response so that we don't |
| 188 |
* accidentally save private and personal data to the server logs. |
| 189 |
* @param {?string} responseText A JSON response to clean. |
| 190 |
* @return {?string} The cleaned response. |
| 191 |
* @private |
| 192 |
*/ |
| 193 |
WebChannelDebug.prototype.redactResponse_ = function(responseText) { |
| 194 |
if (!this.redactEnabled_) { |
| 195 |
return responseText; |
| 196 |
} |
| 197 |
|
| 198 |
if (!responseText) { |
| 199 |
return null; |
| 200 |
} |
| 201 |
|
| 202 |
try { |
| 203 |
var responseArray = JSON.parse(responseText); |
| 204 |
if (responseArray) { |
| 205 |
for (var i = 0; i < responseArray.length; i++) { |
| 206 |
if (goog.isArray(responseArray[i])) { |
| 207 |
this.maybeRedactArray_(responseArray[i]); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
return goog.json.serialize(responseArray); |
| 213 |
} catch (e) { |
| 214 |
this.debug('Exception parsing expected JS array - probably was not JS'); |
| 215 |
return responseText; |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
/** |
| 221 |
* Removes data from a response array that may be sensitive. |
| 222 |
* @param {!Array<?>} array The array to clean. |
| 223 |
* @private |
| 224 |
*/ |
| 225 |
WebChannelDebug.prototype.maybeRedactArray_ = function(array) { |
| 226 |
if (array.length < 2) { |
| 227 |
return; |
| 228 |
} |
| 229 |
var dataPart = array[1]; |
| 230 |
if (!goog.isArray(dataPart)) { |
| 231 |
return; |
| 232 |
} |
| 233 |
if (dataPart.length < 1) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
var type = dataPart[0]; |
| 238 |
if (type != 'noop' && type != 'stop' && type != 'close') { |
| 239 |
// redact all fields in the array |
| 240 |
for (var i = 1; i < dataPart.length; i++) { |
| 241 |
dataPart[i] = ''; |
| 242 |
} |
| 243 |
} |
| 244 |
}; |
| 245 |
|
| 246 |
|
| 247 |
/** |
| 248 |
* Removes potentially private data from a request POST body so that we don't |
| 249 |
* accidentally save private and personal data to the server logs. |
| 250 |
* @param {?string} data The data string to clean. |
| 251 |
* @return {?string} The data string with sensitive data replaced by 'redacted'. |
| 252 |
* @private |
| 253 |
*/ |
| 254 |
WebChannelDebug.prototype.maybeRedactPostData_ = function(data) { |
| 255 |
if (!this.redactEnabled_) { |
| 256 |
return data; |
| 257 |
} |
| 258 |
|
| 259 |
if (!data) { |
| 260 |
return null; |
| 261 |
} |
| 262 |
var out = ''; |
| 263 |
var params = data.split('&'); |
| 264 |
for (var i = 0; i < params.length; i++) { |
| 265 |
var param = params[i]; |
| 266 |
var keyValue = param.split('='); |
| 267 |
if (keyValue.length > 1) { |
| 268 |
var key = keyValue[0]; |
| 269 |
var value = keyValue[1]; |
| 270 |
|
| 271 |
var keyParts = key.split('_'); |
| 272 |
if (keyParts.length >= 2 && keyParts[1] == 'type') { |
| 273 |
out += key + '=' + value + '&'; |
| 274 |
} else { |
| 275 |
out += key + '=' + |
| 276 |
'redacted' + |
| 277 |
'&'; |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
return out; |
| 282 |
}; |
| 283 |
}); // goog.scope |