| 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 Base TestChannel implementation. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
goog.provide('goog.labs.net.webChannel.BaseTestChannel'); |
| 22 |
|
| 23 |
goog.require('goog.labs.net.webChannel.Channel'); |
| 24 |
goog.require('goog.labs.net.webChannel.ChannelRequest'); |
| 25 |
goog.require('goog.labs.net.webChannel.WebChannelDebug'); |
| 26 |
goog.require('goog.labs.net.webChannel.requestStats'); |
| 27 |
goog.require('goog.net.WebChannel'); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* A TestChannel is used during the first part of channel negotiation |
| 33 |
* with the server to create the channel. It helps us determine whether we're |
| 34 |
* behind a buffering proxy. |
| 35 |
* |
| 36 |
* @constructor |
| 37 |
* @struct |
| 38 |
* @param {!goog.labs.net.webChannel.Channel} channel The channel |
| 39 |
* that owns this test channel. |
| 40 |
* @param {!goog.labs.net.webChannel.WebChannelDebug} channelDebug A |
| 41 |
* WebChannelDebug instance to use for logging. |
| 42 |
* @implements {goog.labs.net.webChannel.Channel} |
| 43 |
*/ |
| 44 |
goog.labs.net.webChannel.BaseTestChannel = function(channel, channelDebug) { |
| 45 |
/** |
| 46 |
* The channel that owns this test channel |
| 47 |
* @private {!goog.labs.net.webChannel.Channel} |
| 48 |
*/ |
| 49 |
this.channel_ = channel; |
| 50 |
|
| 51 |
/** |
| 52 |
* The channel debug to use for logging |
| 53 |
* @private {!goog.labs.net.webChannel.WebChannelDebug} |
| 54 |
*/ |
| 55 |
this.channelDebug_ = channelDebug; |
| 56 |
|
| 57 |
/** |
| 58 |
* Extra HTTP headers to add to all the requests sent to the server. |
| 59 |
* @private {Object} |
| 60 |
*/ |
| 61 |
this.extraHeaders_ = null; |
| 62 |
|
| 63 |
/** |
| 64 |
* The test request. |
| 65 |
* @private {goog.labs.net.webChannel.ChannelRequest} |
| 66 |
*/ |
| 67 |
this.request_ = null; |
| 68 |
|
| 69 |
/** |
| 70 |
* Whether we have received the first result as an intermediate result. This |
| 71 |
* helps us determine whether we're behind a buffering proxy. |
| 72 |
* @private {boolean} |
| 73 |
*/ |
| 74 |
this.receivedIntermediateResult_ = false; |
| 75 |
|
| 76 |
/** |
| 77 |
* The relative path for test requests. |
| 78 |
* @private {?string} |
| 79 |
*/ |
| 80 |
this.path_ = null; |
| 81 |
|
| 82 |
/** |
| 83 |
* The last status code received. |
| 84 |
* @private {number} |
| 85 |
*/ |
| 86 |
this.lastStatusCode_ = -1; |
| 87 |
|
| 88 |
/** |
| 89 |
* A subdomain prefix for using a subdomain in IE for the backchannel |
| 90 |
* requests. |
| 91 |
* @private {?string} |
| 92 |
*/ |
| 93 |
this.hostPrefix_ = null; |
| 94 |
|
| 95 |
/** |
| 96 |
* The effective client protocol as indicated by the initial handshake |
| 97 |
* response via the x-client-wire-protocol header. |
| 98 |
* |
| 99 |
* @private {?string} |
| 100 |
*/ |
| 101 |
this.clientProtocol_ = null; |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
goog.scope(function() { |
| 106 |
var WebChannel = goog.net.WebChannel; |
| 107 |
var BaseTestChannel = goog.labs.net.webChannel.BaseTestChannel; |
| 108 |
var WebChannelDebug = goog.labs.net.webChannel.WebChannelDebug; |
| 109 |
var ChannelRequest = goog.labs.net.webChannel.ChannelRequest; |
| 110 |
var requestStats = goog.labs.net.webChannel.requestStats; |
| 111 |
var Channel = goog.labs.net.webChannel.Channel; |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Enum type for the test channel state machine |
| 116 |
* @enum {number} |
| 117 |
* @private |
| 118 |
*/ |
| 119 |
BaseTestChannel.State_ = { |
| 120 |
/** |
| 121 |
* The state for the TestChannel state machine where we making the |
| 122 |
* initial call to get the server configured parameters. |
| 123 |
*/ |
| 124 |
INIT: 0, |
| 125 |
|
| 126 |
/** |
| 127 |
* The state for the TestChannel state machine where we're checking to |
| 128 |
* se if we're behind a buffering proxy. |
| 129 |
*/ |
| 130 |
CONNECTION_TESTING: 1 |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* The state of the state machine for this object. |
| 136 |
* |
| 137 |
* @private {?BaseTestChannel.State_} |
| 138 |
*/ |
| 139 |
BaseTestChannel.prototype.state_ = null; |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Sets extra HTTP headers to add to all the requests sent to the server. |
| 144 |
* |
| 145 |
* @param {Object} extraHeaders The HTTP headers. |
| 146 |
*/ |
| 147 |
BaseTestChannel.prototype.setExtraHeaders = function(extraHeaders) { |
| 148 |
this.extraHeaders_ = extraHeaders; |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
/** |
| 153 |
* Starts the test channel. This initiates connections to the server. |
| 154 |
* |
| 155 |
* @param {string} path The relative uri for the test connection. |
| 156 |
*/ |
| 157 |
BaseTestChannel.prototype.connect = function(path) { |
| 158 |
this.path_ = path; |
| 159 |
var sendDataUri = this.channel_.getForwardChannelUri(this.path_); |
| 160 |
|
| 161 |
requestStats.notifyStatEvent(requestStats.Stat.TEST_STAGE_ONE_START); |
| 162 |
|
| 163 |
// If the channel already has the result of the handshake, then skip it. |
| 164 |
var handshakeResult = this.channel_.getConnectionState().handshakeResult; |
| 165 |
if (goog.isDefAndNotNull(handshakeResult)) { |
| 166 |
this.hostPrefix_ = this.channel_.correctHostPrefix(handshakeResult[0]); |
| 167 |
this.state_ = BaseTestChannel.State_.CONNECTION_TESTING; |
| 168 |
this.checkBufferingProxy_(); |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
// the first request returns server specific parameters |
| 173 |
sendDataUri.setParameterValues('MODE', 'init'); |
| 174 |
|
| 175 |
// http-session-id to be generated as the response |
| 176 |
if (!this.channel_.getBackgroundChannelTest() && |
| 177 |
this.channel_.getHttpSessionIdParam()) { |
| 178 |
sendDataUri.setParameterValues(WebChannel.X_HTTP_SESSION_ID, |
| 179 |
this.channel_.getHttpSessionIdParam()); |
| 180 |
} |
| 181 |
|
| 182 |
this.request_ = ChannelRequest.createChannelRequest(this, this.channelDebug_); |
| 183 |
|
| 184 |
this.request_.setExtraHeaders(this.extraHeaders_); |
| 185 |
|
| 186 |
this.request_.xmlHttpGet( |
| 187 |
sendDataUri, false /* decodeChunks */, null /* hostPrefix */); |
| 188 |
this.state_ = BaseTestChannel.State_.INIT; |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
/** |
| 193 |
* Begins the second stage of the test channel where we test to see if we're |
| 194 |
* behind a buffering proxy. The server sends back a multi-chunked response |
| 195 |
* with the first chunk containing the content '1' and then two seconds later |
| 196 |
* sending the second chunk containing the content '2'. Depending on how we |
| 197 |
* receive the content, we can tell if we're behind a buffering proxy. |
| 198 |
* @private |
| 199 |
*/ |
| 200 |
BaseTestChannel.prototype.checkBufferingProxy_ = function() { |
| 201 |
this.channelDebug_.debug('TestConnection: starting stage 2'); |
| 202 |
|
| 203 |
// If the test result is already available, skip its execution. |
| 204 |
var bufferingProxyResult = |
| 205 |
this.channel_.getConnectionState().bufferingProxyResult; |
| 206 |
if (goog.isDefAndNotNull(bufferingProxyResult)) { |
| 207 |
this.channelDebug_.debug(function() { |
| 208 |
return 'TestConnection: skipping stage 2, precomputed result is ' + |
| 209 |
bufferingProxyResult ? |
| 210 |
'Buffered' : |
| 211 |
'Unbuffered'; |
| 212 |
}); |
| 213 |
requestStats.notifyStatEvent(requestStats.Stat.TEST_STAGE_TWO_START); |
| 214 |
if (bufferingProxyResult) { // Buffered/Proxy connection |
| 215 |
requestStats.notifyStatEvent(requestStats.Stat.PROXY); |
| 216 |
this.channel_.testConnectionFinished(this, false); |
| 217 |
} else { // Unbuffered/NoProxy connection |
| 218 |
requestStats.notifyStatEvent(requestStats.Stat.NOPROXY); |
| 219 |
this.channel_.testConnectionFinished(this, true); |
| 220 |
} |
| 221 |
return; // Skip the test |
| 222 |
} |
| 223 |
this.request_ = ChannelRequest.createChannelRequest(this, this.channelDebug_); |
| 224 |
this.request_.setExtraHeaders(this.extraHeaders_); |
| 225 |
var recvDataUri = this.channel_.getBackChannelUri( |
| 226 |
this.hostPrefix_, |
| 227 |
/** @type {string} */ (this.path_)); |
| 228 |
|
| 229 |
requestStats.notifyStatEvent(requestStats.Stat.TEST_STAGE_TWO_START); |
| 230 |
recvDataUri.setParameterValues('TYPE', 'xmlhttp'); |
| 231 |
|
| 232 |
var param = this.channel_.getHttpSessionIdParam(); |
| 233 |
var value = this.channel_.getHttpSessionId(); |
| 234 |
if (param && value) { |
| 235 |
recvDataUri.setParameterValue(param, value); |
| 236 |
} |
| 237 |
|
| 238 |
this.request_.xmlHttpGet( |
| 239 |
recvDataUri, false /** decodeChunks */, this.hostPrefix_); |
| 240 |
}; |
| 241 |
|
| 242 |
|
| 243 |
/** |
| 244 |
* @override |
| 245 |
*/ |
| 246 |
BaseTestChannel.prototype.createXhrIo = function(hostPrefix) { |
| 247 |
return this.channel_.createXhrIo(hostPrefix); |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Aborts the test channel. |
| 253 |
*/ |
| 254 |
BaseTestChannel.prototype.abort = function() { |
| 255 |
if (this.request_) { |
| 256 |
this.request_.cancel(); |
| 257 |
this.request_ = null; |
| 258 |
} |
| 259 |
this.lastStatusCode_ = -1; |
| 260 |
}; |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* Returns whether the test channel is closed. The ChannelRequest object expects |
| 265 |
* this method to be implemented on its handler. |
| 266 |
* |
| 267 |
* @return {boolean} Whether the channel is closed. |
| 268 |
* @override |
| 269 |
*/ |
| 270 |
BaseTestChannel.prototype.isClosed = function() { |
| 271 |
return false; |
| 272 |
}; |
| 273 |
|
| 274 |
|
| 275 |
/** |
| 276 |
* Callback from ChannelRequest for when new data is received |
| 277 |
* |
| 278 |
* @param {ChannelRequest} req The request object. |
| 279 |
* @param {string} responseText The text of the response. |
| 280 |
* @override |
| 281 |
*/ |
| 282 |
BaseTestChannel.prototype.onRequestData = function(req, responseText) { |
| 283 |
this.lastStatusCode_ = req.getLastStatusCode(); |
| 284 |
if (this.state_ == BaseTestChannel.State_.INIT) { |
| 285 |
this.channelDebug_.debug('TestConnection: Got data for stage 1'); |
| 286 |
|
| 287 |
this.applyControlHeaders_(req); |
| 288 |
|
| 289 |
if (!responseText) { |
| 290 |
this.channelDebug_.debug('TestConnection: Null responseText'); |
| 291 |
// The server should always send text; something is wrong here |
| 292 |
this.channel_.testConnectionFailure(this, ChannelRequest.Error.BAD_DATA); |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
try { |
| 298 |
var channel = /** @type {!goog.labs.net.webChannel.WebChannelBase} */ ( |
| 299 |
this.channel_); |
| 300 |
var respArray = channel.getWireCodec().decodeMessage(responseText); |
| 301 |
} catch (e) { |
| 302 |
this.channelDebug_.dumpException(e); |
| 303 |
this.channel_.testConnectionFailure(this, ChannelRequest.Error.BAD_DATA); |
| 304 |
return; |
| 305 |
} |
| 306 |
this.hostPrefix_ = this.channel_.correctHostPrefix(respArray[0]); |
| 307 |
} else if (this.state_ == BaseTestChannel.State_.CONNECTION_TESTING) { |
| 308 |
if (this.receivedIntermediateResult_) { |
| 309 |
requestStats.notifyStatEvent(requestStats.Stat.TEST_STAGE_TWO_DATA_TWO); |
| 310 |
} else { |
| 311 |
// '11111' is used instead of '1' to prevent a small amount of buffering |
| 312 |
// by Safari. |
| 313 |
if (responseText == '11111') { |
| 314 |
requestStats.notifyStatEvent(requestStats.Stat.TEST_STAGE_TWO_DATA_ONE); |
| 315 |
this.receivedIntermediateResult_ = true; |
| 316 |
if (this.checkForEarlyNonBuffered_()) { |
| 317 |
// If early chunk detection is on, and we passed the tests, |
| 318 |
// assume HTTP_OK, cancel the test and turn on noproxy mode. |
| 319 |
this.lastStatusCode_ = 200; |
| 320 |
this.request_.cancel(); |
| 321 |
this.channelDebug_.debug( |
| 322 |
'Test connection succeeded; using streaming connection'); |
| 323 |
requestStats.notifyStatEvent(requestStats.Stat.NOPROXY); |
| 324 |
this.channel_.testConnectionFinished(this, true); |
| 325 |
} |
| 326 |
} else { |
| 327 |
requestStats.notifyStatEvent( |
| 328 |
requestStats.Stat.TEST_STAGE_TWO_DATA_BOTH); |
| 329 |
this.receivedIntermediateResult_ = false; |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
}; |
| 334 |
|
| 335 |
|
| 336 |
/** |
| 337 |
* Callback from ChannelRequest that indicates a request has completed. |
| 338 |
* |
| 339 |
* @param {!ChannelRequest} req The request object. |
| 340 |
* @override |
| 341 |
*/ |
| 342 |
BaseTestChannel.prototype.onRequestComplete = function(req) { |
| 343 |
this.lastStatusCode_ = this.request_.getLastStatusCode(); |
| 344 |
if (!this.request_.getSuccess()) { |
| 345 |
this.channelDebug_.debug( |
| 346 |
'TestConnection: request failed, in state ' + this.state_); |
| 347 |
if (this.state_ == BaseTestChannel.State_.INIT) { |
| 348 |
requestStats.notifyStatEvent(requestStats.Stat.TEST_STAGE_ONE_FAILED); |
| 349 |
} else if (this.state_ == BaseTestChannel.State_.CONNECTION_TESTING) { |
| 350 |
requestStats.notifyStatEvent(requestStats.Stat.TEST_STAGE_TWO_FAILED); |
| 351 |
} |
| 352 |
this.channel_.testConnectionFailure( |
| 353 |
this, |
| 354 |
/** @type {ChannelRequest.Error} */ |
| 355 |
(this.request_.getLastError())); |
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
if (this.state_ == BaseTestChannel.State_.INIT) { |
| 360 |
this.state_ = BaseTestChannel.State_.CONNECTION_TESTING; |
| 361 |
|
| 362 |
this.channelDebug_.debug( |
| 363 |
'TestConnection: request complete for initial check'); |
| 364 |
|
| 365 |
this.checkBufferingProxy_(); |
| 366 |
} else if (this.state_ == BaseTestChannel.State_.CONNECTION_TESTING) { |
| 367 |
this.channelDebug_.debug('TestConnection: request complete for stage 2'); |
| 368 |
|
| 369 |
var goodConn = this.receivedIntermediateResult_; |
| 370 |
if (goodConn) { |
| 371 |
this.channelDebug_.debug( |
| 372 |
'Test connection succeeded; using streaming connection'); |
| 373 |
requestStats.notifyStatEvent(requestStats.Stat.NOPROXY); |
| 374 |
this.channel_.testConnectionFinished(this, true); |
| 375 |
} else { |
| 376 |
this.channelDebug_.debug('Test connection failed; not using streaming'); |
| 377 |
requestStats.notifyStatEvent(requestStats.Stat.PROXY); |
| 378 |
this.channel_.testConnectionFinished(this, false); |
| 379 |
} |
| 380 |
} |
| 381 |
}; |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* Apply any control headers from the initial handshake response. |
| 386 |
* |
| 387 |
* @param {!ChannelRequest} req The request object. |
| 388 |
* @private |
| 389 |
*/ |
| 390 |
BaseTestChannel.prototype.applyControlHeaders_ = function(req) { |
| 391 |
if (this.channel_.getBackgroundChannelTest()) { |
| 392 |
return; |
| 393 |
} |
| 394 |
|
| 395 |
var xhr = req.getXhr(); |
| 396 |
if (xhr) { |
| 397 |
var protocolHeader = xhr.getStreamingResponseHeader( |
| 398 |
WebChannel.X_CLIENT_WIRE_PROTOCOL); |
| 399 |
this.clientProtocol_ = protocolHeader ? protocolHeader : null; |
| 400 |
|
| 401 |
if (this.channel_.getHttpSessionIdParam()) { |
| 402 |
var httpSessionIdHeader = xhr.getStreamingResponseHeader( |
| 403 |
WebChannel.X_HTTP_SESSION_ID); |
| 404 |
if (httpSessionIdHeader) { |
| 405 |
this.channel_.setHttpSessionId(httpSessionIdHeader); |
| 406 |
} else { |
| 407 |
this.channelDebug_.warning( |
| 408 |
'Missing X_HTTP_SESSION_ID in the handshake response'); |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
}; |
| 413 |
|
| 414 |
|
| 415 |
/** |
| 416 |
* @return {?string} The client protocol as recorded with the init handshake |
| 417 |
* request. |
| 418 |
*/ |
| 419 |
BaseTestChannel.prototype.getClientProtocol = function() { |
| 420 |
return this.clientProtocol_; |
| 421 |
}; |
| 422 |
|
| 423 |
|
| 424 |
/** |
| 425 |
* Returns the last status code received for a request. |
| 426 |
* @return {number} The last status code received for a request. |
| 427 |
*/ |
| 428 |
BaseTestChannel.prototype.getLastStatusCode = function() { |
| 429 |
return this.lastStatusCode_; |
| 430 |
}; |
| 431 |
|
| 432 |
|
| 433 |
/** |
| 434 |
* @return {boolean} Whether we should be using secondary domains when the |
| 435 |
* server instructs us to do so. |
| 436 |
* @override |
| 437 |
*/ |
| 438 |
BaseTestChannel.prototype.shouldUseSecondaryDomains = function() { |
| 439 |
return this.channel_.shouldUseSecondaryDomains(); |
| 440 |
}; |
| 441 |
|
| 442 |
|
| 443 |
/** |
| 444 |
* @override |
| 445 |
*/ |
| 446 |
BaseTestChannel.prototype.isActive = function() { |
| 447 |
return this.channel_.isActive(); |
| 448 |
}; |
| 449 |
|
| 450 |
|
| 451 |
/** |
| 452 |
* @return {boolean} True if test stage 2 detected a non-buffered |
| 453 |
* channel early and early no buffering detection is enabled. |
| 454 |
* @private |
| 455 |
*/ |
| 456 |
BaseTestChannel.prototype.checkForEarlyNonBuffered_ = function() { |
| 457 |
return ChannelRequest.supportsXhrStreaming(); |
| 458 |
}; |
| 459 |
|
| 460 |
|
| 461 |
/** |
| 462 |
* @override |
| 463 |
*/ |
| 464 |
BaseTestChannel.prototype.getForwardChannelUri = goog.abstractMethod; |
| 465 |
|
| 466 |
|
| 467 |
/** |
| 468 |
* @override |
| 469 |
*/ |
| 470 |
BaseTestChannel.prototype.getBackChannelUri = goog.abstractMethod; |
| 471 |
|
| 472 |
|
| 473 |
/** |
| 474 |
* @override |
| 475 |
*/ |
| 476 |
BaseTestChannel.prototype.correctHostPrefix = goog.abstractMethod; |
| 477 |
|
| 478 |
|
| 479 |
/** |
| 480 |
* @override |
| 481 |
*/ |
| 482 |
BaseTestChannel.prototype.createDataUri = goog.abstractMethod; |
| 483 |
|
| 484 |
|
| 485 |
/** |
| 486 |
* @override |
| 487 |
*/ |
| 488 |
BaseTestChannel.prototype.testConnectionFinished = goog.abstractMethod; |
| 489 |
|
| 490 |
|
| 491 |
/** |
| 492 |
* @override |
| 493 |
*/ |
| 494 |
BaseTestChannel.prototype.testConnectionFailure = goog.abstractMethod; |
| 495 |
|
| 496 |
|
| 497 |
/** |
| 498 |
* @override |
| 499 |
*/ |
| 500 |
BaseTestChannel.prototype.getConnectionState = goog.abstractMethod; |
| 501 |
|
| 502 |
|
| 503 |
/** |
| 504 |
* @override |
| 505 |
*/ |
| 506 |
BaseTestChannel.prototype.setHttpSessionIdParam = goog.abstractMethod; |
| 507 |
|
| 508 |
|
| 509 |
/** |
| 510 |
* @override |
| 511 |
*/ |
| 512 |
BaseTestChannel.prototype.getHttpSessionIdParam = goog.abstractMethod; |
| 513 |
|
| 514 |
|
| 515 |
/** |
| 516 |
* @override |
| 517 |
*/ |
| 518 |
BaseTestChannel.prototype.setHttpSessionId = goog.abstractMethod; |
| 519 |
|
| 520 |
|
| 521 |
/** |
| 522 |
* @override |
| 523 |
*/ |
| 524 |
BaseTestChannel.prototype.getHttpSessionId = goog.abstractMethod; |
| 525 |
|
| 526 |
|
| 527 |
/** |
| 528 |
* @override |
| 529 |
*/ |
| 530 |
BaseTestChannel.prototype.getBackgroundChannelTest = goog.abstractMethod; |
| 531 |
}); // goog.scope |