webchanneldebug.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. * @fileoverview Provides a utility for tracing and debugging WebChannel
  16. * requests.
  17. *
  18. * @visibility {:internal}
  19. */
  20. goog.provide('goog.labs.net.webChannel.WebChannelDebug');
  21. goog.require('goog.json');
  22. goog.require('goog.log');
  23. /**
  24. * Logs and keeps a buffer of debugging info for the Channel.
  25. *
  26. * @constructor
  27. * @struct
  28. * @final
  29. */
  30. goog.labs.net.webChannel.WebChannelDebug = function() {
  31. /**
  32. * The logger instance.
  33. * @const
  34. * @private
  35. */
  36. this.logger_ = goog.log.getLogger('goog.labs.net.webChannel.WebChannelDebug');
  37. };
  38. goog.scope(function() {
  39. var WebChannelDebug = goog.labs.net.webChannel.WebChannelDebug;
  40. /**
  41. * Gets the logger used by this ChannelDebug.
  42. * @return {goog.debug.Logger} The logger used by this WebChannelDebug.
  43. */
  44. WebChannelDebug.prototype.getLogger = function() {
  45. return this.logger_;
  46. };
  47. /**
  48. * Logs that the browser went offline during the lifetime of a request.
  49. * @param {goog.Uri} url The URL being requested.
  50. */
  51. WebChannelDebug.prototype.browserOfflineResponse = function(url) {
  52. this.info('BROWSER_OFFLINE: ' + url);
  53. };
  54. /**
  55. * Logs an XmlHttp request..
  56. * @param {string} verb The request type (GET/POST).
  57. * @param {goog.Uri} uri The request destination.
  58. * @param {string|number|undefined} id The request id.
  59. * @param {number} attempt Which attempt # the request was.
  60. * @param {?string} postData The data posted in the request.
  61. */
  62. WebChannelDebug.prototype.xmlHttpChannelRequest = function(
  63. verb, uri, id, attempt, postData) {
  64. this.info(
  65. 'XMLHTTP REQ (' + id + ') [attempt ' + attempt + ']: ' + verb + '\n' +
  66. uri + '\n' + this.maybeRedactPostData_(postData));
  67. };
  68. /**
  69. * Logs the meta data received from an XmlHttp request.
  70. * @param {string} verb The request type (GET/POST).
  71. * @param {goog.Uri} uri The request destination.
  72. * @param {string|number|undefined} id The request id.
  73. * @param {number} attempt Which attempt # the request was.
  74. * @param {goog.net.XmlHttp.ReadyState} readyState The ready state.
  75. * @param {number} statusCode The HTTP status code.
  76. */
  77. WebChannelDebug.prototype.xmlHttpChannelResponseMetaData = function(
  78. verb, uri, id, attempt, readyState, statusCode) {
  79. this.info(
  80. 'XMLHTTP RESP (' + id + ') [ attempt ' + attempt + ']: ' + verb + '\n' +
  81. uri + '\n' + readyState + ' ' + statusCode);
  82. };
  83. /**
  84. * Logs the response data received from an XmlHttp request.
  85. * @param {string|number|undefined} id The request id.
  86. * @param {?string} responseText The response text.
  87. * @param {?string=} opt_desc Optional request description.
  88. */
  89. WebChannelDebug.prototype.xmlHttpChannelResponseText = function(
  90. id, responseText, opt_desc) {
  91. this.info(
  92. 'XMLHTTP TEXT (' + id + '): ' + this.redactResponse_(responseText) +
  93. (opt_desc ? ' ' + opt_desc : ''));
  94. };
  95. /**
  96. * Logs a request timeout.
  97. * @param {goog.Uri} uri The uri that timed out.
  98. */
  99. WebChannelDebug.prototype.timeoutResponse = function(uri) {
  100. this.info('TIMEOUT: ' + uri);
  101. };
  102. /**
  103. * Logs a debug message.
  104. * @param {string} text The message.
  105. */
  106. WebChannelDebug.prototype.debug = function(text) {
  107. this.info(text);
  108. };
  109. /**
  110. * Logs an exception
  111. * @param {Error} e The error or error event.
  112. * @param {string=} opt_msg The optional message, defaults to 'Exception'.
  113. */
  114. WebChannelDebug.prototype.dumpException = function(e, opt_msg) {
  115. this.severe((opt_msg || 'Exception') + e);
  116. };
  117. /**
  118. * Logs an info message.
  119. * @param {string} text The message.
  120. */
  121. WebChannelDebug.prototype.info = function(text) {
  122. goog.log.info(this.logger_, text);
  123. };
  124. /**
  125. * Logs a warning message.
  126. * @param {string} text The message.
  127. */
  128. WebChannelDebug.prototype.warning = function(text) {
  129. goog.log.warning(this.logger_, text);
  130. };
  131. /**
  132. * Logs a severe message.
  133. * @param {string} text The message.
  134. */
  135. WebChannelDebug.prototype.severe = function(text) {
  136. goog.log.error(this.logger_, text);
  137. };
  138. /**
  139. * Removes potentially private data from a response so that we don't
  140. * accidentally save private and personal data to the server logs.
  141. * @param {?string} responseText A JSON response to clean.
  142. * @return {?string} The cleaned response.
  143. * @private
  144. */
  145. WebChannelDebug.prototype.redactResponse_ = function(responseText) {
  146. if (!responseText) {
  147. return null;
  148. }
  149. try {
  150. var responseArray = JSON.parse(responseText);
  151. if (responseArray) {
  152. for (var i = 0; i < responseArray.length; i++) {
  153. if (goog.isArray(responseArray[i])) {
  154. this.maybeRedactArray_(responseArray[i]);
  155. }
  156. }
  157. }
  158. return goog.json.serialize(responseArray);
  159. } catch (e) {
  160. this.debug('Exception parsing expected JS array - probably was not JS');
  161. return responseText;
  162. }
  163. };
  164. /**
  165. * Removes data from a response array that may be sensitive.
  166. * @param {!Array<?>} array The array to clean.
  167. * @private
  168. */
  169. WebChannelDebug.prototype.maybeRedactArray_ = function(array) {
  170. if (array.length < 2) {
  171. return;
  172. }
  173. var dataPart = array[1];
  174. if (!goog.isArray(dataPart)) {
  175. return;
  176. }
  177. if (dataPart.length < 1) {
  178. return;
  179. }
  180. var type = dataPart[0];
  181. if (type != 'noop' && type != 'stop' && type != 'close') {
  182. // redact all fields in the array
  183. for (var i = 1; i < dataPart.length; i++) {
  184. dataPart[i] = '';
  185. }
  186. }
  187. };
  188. /**
  189. * Removes potentially private data from a request POST body so that we don't
  190. * accidentally save private and personal data to the server logs.
  191. * @param {?string} data The data string to clean.
  192. * @return {?string} The data string with sensitive data replaced by 'redacted'.
  193. * @private
  194. */
  195. WebChannelDebug.prototype.maybeRedactPostData_ = function(data) {
  196. if (!data) {
  197. return null;
  198. }
  199. var out = '';
  200. var params = data.split('&');
  201. for (var i = 0; i < params.length; i++) {
  202. var param = params[i];
  203. var keyValue = param.split('=');
  204. if (keyValue.length > 1) {
  205. var key = keyValue[0];
  206. var value = keyValue[1];
  207. var keyParts = key.split('_');
  208. if (keyParts.length >= 2 && keyParts[1] == 'type') {
  209. out += key + '=' + value + '&';
  210. } else {
  211. out += key + '=' +
  212. 'redacted' +
  213. '&';
  214. }
  215. }
  216. }
  217. return out;
  218. };
  219. }); // goog.scope