Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/labs/net/webchannel/webchanneldebug.js
1865 views
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
42
*/
43
this.logger_ = goog.log.getLogger('goog.labs.net.webChannel.WebChannelDebug');
44
};
45
46
47
goog.scope(function() {
48
var WebChannelDebug = goog.labs.net.webChannel.WebChannelDebug;
49
50
51
/**
52
* Gets the logger used by this ChannelDebug.
53
* @return {goog.debug.Logger} The logger used by this WebChannelDebug.
54
*/
55
WebChannelDebug.prototype.getLogger = function() {
56
return this.logger_;
57
};
58
59
60
/**
61
* Logs that the browser went offline during the lifetime of a request.
62
* @param {goog.Uri} url The URL being requested.
63
*/
64
WebChannelDebug.prototype.browserOfflineResponse = function(url) {
65
this.info('BROWSER_OFFLINE: ' + url);
66
};
67
68
69
/**
70
* Logs an XmlHttp request..
71
* @param {string} verb The request type (GET/POST).
72
* @param {goog.Uri} uri The request destination.
73
* @param {string|number|undefined} id The request id.
74
* @param {number} attempt Which attempt # the request was.
75
* @param {?string} postData The data posted in the request.
76
*/
77
WebChannelDebug.prototype.xmlHttpChannelRequest = function(
78
verb, uri, id, attempt, postData) {
79
this.info(
80
'XMLHTTP REQ (' + id + ') [attempt ' + attempt + ']: ' + verb + '\n' +
81
uri + '\n' + this.maybeRedactPostData_(postData));
82
};
83
84
85
/**
86
* Logs the meta data received from an XmlHttp request.
87
* @param {string} verb The request type (GET/POST).
88
* @param {goog.Uri} uri The request destination.
89
* @param {string|number|undefined} id The request id.
90
* @param {number} attempt Which attempt # the request was.
91
* @param {goog.net.XmlHttp.ReadyState} readyState The ready state.
92
* @param {number} statusCode The HTTP status code.
93
*/
94
WebChannelDebug.prototype.xmlHttpChannelResponseMetaData = function(
95
verb, uri, id, attempt, readyState, statusCode) {
96
this.info(
97
'XMLHTTP RESP (' + id + ') [ attempt ' + attempt + ']: ' + verb + '\n' +
98
uri + '\n' + readyState + ' ' + statusCode);
99
};
100
101
102
/**
103
* Logs the response data received from an XmlHttp request.
104
* @param {string|number|undefined} id The request id.
105
* @param {?string} responseText The response text.
106
* @param {?string=} opt_desc Optional request description.
107
*/
108
WebChannelDebug.prototype.xmlHttpChannelResponseText = function(
109
id, responseText, opt_desc) {
110
this.info(
111
'XMLHTTP TEXT (' + id + '): ' + this.redactResponse_(responseText) +
112
(opt_desc ? ' ' + opt_desc : ''));
113
};
114
115
116
/**
117
* Logs a request timeout.
118
* @param {goog.Uri} uri The uri that timed out.
119
*/
120
WebChannelDebug.prototype.timeoutResponse = function(uri) {
121
this.info('TIMEOUT: ' + uri);
122
};
123
124
125
/**
126
* Logs a debug message.
127
* @param {string} text The message.
128
*/
129
WebChannelDebug.prototype.debug = function(text) {
130
this.info(text);
131
};
132
133
134
/**
135
* Logs an exception
136
* @param {Error} e The error or error event.
137
* @param {string=} opt_msg The optional message, defaults to 'Exception'.
138
*/
139
WebChannelDebug.prototype.dumpException = function(e, opt_msg) {
140
this.severe((opt_msg || 'Exception') + e);
141
};
142
143
144
/**
145
* Logs an info message.
146
* @param {string} text The message.
147
*/
148
WebChannelDebug.prototype.info = function(text) {
149
goog.log.info(this.logger_, text);
150
};
151
152
153
/**
154
* Logs a warning message.
155
* @param {string} text The message.
156
*/
157
WebChannelDebug.prototype.warning = function(text) {
158
goog.log.warning(this.logger_, text);
159
};
160
161
162
/**
163
* Logs a severe message.
164
* @param {string} text The message.
165
*/
166
WebChannelDebug.prototype.severe = function(text) {
167
goog.log.error(this.logger_, text);
168
};
169
170
171
/**
172
* Removes potentially private data from a response so that we don't
173
* accidentally save private and personal data to the server logs.
174
* @param {?string} responseText A JSON response to clean.
175
* @return {?string} The cleaned response.
176
* @private
177
*/
178
WebChannelDebug.prototype.redactResponse_ = function(responseText) {
179
if (!responseText) {
180
return null;
181
}
182
183
try {
184
var responseArray = JSON.parse(responseText);
185
if (responseArray) {
186
for (var i = 0; i < responseArray.length; i++) {
187
if (goog.isArray(responseArray[i])) {
188
this.maybeRedactArray_(responseArray[i]);
189
}
190
}
191
}
192
193
return goog.json.serialize(responseArray);
194
} catch (e) {
195
this.debug('Exception parsing expected JS array - probably was not JS');
196
return responseText;
197
}
198
};
199
200
201
/**
202
* Removes data from a response array that may be sensitive.
203
* @param {!Array<?>} array The array to clean.
204
* @private
205
*/
206
WebChannelDebug.prototype.maybeRedactArray_ = function(array) {
207
if (array.length < 2) {
208
return;
209
}
210
var dataPart = array[1];
211
if (!goog.isArray(dataPart)) {
212
return;
213
}
214
if (dataPart.length < 1) {
215
return;
216
}
217
218
var type = dataPart[0];
219
if (type != 'noop' && type != 'stop' && type != 'close') {
220
// redact all fields in the array
221
for (var i = 1; i < dataPart.length; i++) {
222
dataPart[i] = '';
223
}
224
}
225
};
226
227
228
/**
229
* Removes potentially private data from a request POST body so that we don't
230
* accidentally save private and personal data to the server logs.
231
* @param {?string} data The data string to clean.
232
* @return {?string} The data string with sensitive data replaced by 'redacted'.
233
* @private
234
*/
235
WebChannelDebug.prototype.maybeRedactPostData_ = function(data) {
236
if (!data) {
237
return null;
238
}
239
var out = '';
240
var params = data.split('&');
241
for (var i = 0; i < params.length; i++) {
242
var param = params[i];
243
var keyValue = param.split('=');
244
if (keyValue.length > 1) {
245
var key = keyValue[0];
246
var value = keyValue[1];
247
248
var keyParts = key.split('_');
249
if (keyParts.length >= 2 && keyParts[1] == 'type') {
250
out += key + '=' + value + '&';
251
} else {
252
out += key + '=' +
253
'redacted' +
254
'&';
255
}
256
}
257
}
258
return out;
259
};
260
}); // goog.scope
261
262