| 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r |
| 2 | *\r |
| 3 | * ***** BEGIN LICENSE BLOCK *****\r |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1\r |
| 5 | *\r |
| 6 | * The contents of this file are subject to the Mozilla Public License Version\r |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with\r |
| 8 | * the License. You may obtain a copy of the License at\r |
| 9 | * http://www.mozilla.org/MPL/\r |
| 10 | *\r |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis,\r |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\r |
| 13 | * for the specific language governing rights and limitations under the\r |
| 14 | * License.\r |
| 15 | *\r |
| 16 | * The Original Code is JSIRC Library.\r |
| 17 | *\r |
| 18 | * The Initial Developer of the Original Code is\r |
| 19 | * New Dimensions Consulting, Inc.\r |
| 20 | * Portions created by the Initial Developer are Copyright (C) 1999\r |
| 21 | * the Initial Developer. All Rights Reserved.\r |
| 22 | *\r |
| 23 | * Contributor(s):\r |
| 24 | * Robert Ginda, rginda@ndcico.com, original author\r |
| 25 | * Peter Van der Beken, peter.vanderbeken@pandora.be, necko-only version\r |
| 26 | *\r |
| 27 | * Alternatively, the contents of this file may be used under the terms of\r |
| 28 | * either the GNU General Public License Version 2 or later (the "GPL"), or\r |
| 29 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),\r |
| 30 | * in which case the provisions of the GPL or the LGPL are applicable instead\r |
| 31 | * of those above. If you wish to allow use of your version of this file only\r |
| 32 | * under the terms of either the GPL or the LGPL, and not to allow others to\r |
| 33 | * use your version of this file under the terms of the MPL, indicate your\r |
| 34 | * decision by deleting the provisions above and replace them with the notice\r |
| 35 | * and other provisions required by the GPL or the LGPL. If you do not delete\r |
| 36 | * the provisions above, a recipient may use your version of this file under\r |
| 37 | * the terms of any one of the MPL, the GPL or the LGPL.\r |
| 38 | *\r |
| 39 | * ***** END LICENSE BLOCK ***** */\r |
| 40 | \r |
| 41 | const NS_ERROR_MODULE_NETWORK = 2152398848;\r |
| 42 | \r |
| 43 | const NS_ERROR_UNKNOWN_HOST = NS_ERROR_MODULE_NETWORK + 30;\r |
| 44 | const NS_ERROR_CONNECTION_REFUSED = NS_ERROR_MODULE_NETWORK + 13;\r |
| 45 | const NS_ERROR_NET_TIMEOUT = NS_ERROR_MODULE_NETWORK + 14;\r |
| 46 | const NS_ERROR_NET_RESET = NS_ERROR_MODULE_NETWORK + 20;\r |
| 47 | const NS_ERROR_UNKNOWN_PROXY_HOST = NS_ERROR_MODULE_NETWORK + 42;\r |
| 48 | const NS_ERROR_PROXY_CONNECTION_REFUSED = NS_ERROR_MODULE_NETWORK + 72;\r |
| 49 | \r |
| 50 | // Offline error constants:\r |
| 51 | const NS_ERROR_BINDING_ABORTED = NS_ERROR_MODULE_NETWORK + 2;\r |
| 52 | const NS_ERROR_ABORT = 0x80004004;\r |
| 53 | \r |
| 54 | const NS_NET_STATUS_RESOLVING_HOST = NS_ERROR_MODULE_NETWORK + 3;\r |
| 55 | const NS_NET_STATUS_CONNECTED_TO = NS_ERROR_MODULE_NETWORK + 4;\r |
| 56 | const NS_NET_STATUS_SENDING_TO = NS_ERROR_MODULE_NETWORK + 5;\r |
| 57 | const NS_NET_STATUS_RECEIVING_FROM = NS_ERROR_MODULE_NETWORK + 6;\r |
| 58 | const NS_NET_STATUS_CONNECTING_TO = NS_ERROR_MODULE_NETWORK + 7;\r |
| 59 | \r |
| 60 | // Security Constants.\r |
| 61 | const STATE_IS_BROKEN = 1;\r |
| 62 | const STATE_IS_SECURE = 2;\r |
| 63 | const STATE_IS_INSECURE = 3;\r |
| 64 | \r |
| 65 | const STATE_SECURE_LOW = 1;\r |
| 66 | const STATE_SECURE_HIGH = 2;\r |
| 67 | \r |
| 68 | const nsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream;\r |
| 69 | \r |
| 70 | const nsIBinaryInputStream = Components.interfaces.nsIBinaryInputStream;\r |
| 71 | const nsIBinaryOutputStream = Components.interfaces.nsIBinaryOutputStream;\r |
| 72 | \r |
| 73 | function toSInputStream(stream, binary)\r |
| 74 | {\r |
| 75 | var sstream;\r |
| 76 | \r |
| 77 | if (binary)\r |
| 78 | {\r |
| 79 | sstream = Components.classes["@mozilla.org/binaryinputstream;1"];\r |
| 80 | sstream = sstream.createInstance(nsIBinaryInputStream);\r |
| 81 | sstream.setInputStream(stream);\r |
| 82 | }\r |
| 83 | else\r |
| 84 | {\r |
| 85 | sstream = Components.classes["@mozilla.org/scriptableinputstream;1"];\r |
| 86 | sstream = sstream.createInstance(nsIScriptableInputStream);\r |
| 87 | sstream.init(stream);\r |
| 88 | }\r |
| 89 | \r |
| 90 | return sstream;\r |
| 91 | }\r |
| 92 | \r |
| 93 | function toSOutputStream(stream, binary)\r |
| 94 | {\r |
| 95 | var sstream;\r |
| 96 | \r |
| 97 | if (binary)\r |
| 98 | {\r |
| 99 | sstream = Components.classes["@mozilla.org/binaryoutputstream;1"];\r |
| 100 | sstream = sstream.createInstance(Components.interfaces.nsIBinaryOutputStream);\r |
| 101 | sstream.setOutputStream(stream);\r |
| 102 | }\r |
| 103 | else\r |
| 104 | {\r |
| 105 | sstream = stream;\r |
| 106 | }\r |
| 107 | \r |
| 108 | return sstream;\r |
| 109 | }\r |
| 110 | \r |
| 111 | function CBSConnection (binary)\r |
| 112 | {\r |
| 113 | /* Since 2003-01-17 18:14, Mozilla has had this contract ID for the STS.\r |
| 114 | * Prior to that it didn't have one, so we also include the CID for the\r |
| 115 | * STS back then - DO NOT UPDATE THE ID if it changes in Mozilla.\r |
| 116 | */\r |
| 117 | const sockClassByName =\r |
| 118 | Components.classes["@mozilla.org/network/socket-transport-service;1"];\r |
| 119 | const sockClassByID =\r |
| 120 | Components.classesByID["{c07e81e0-ef12-11d2-92b6-00105a1b0d64}"];\r |
| 121 | \r |
| 122 | var sockServiceClass = (sockClassByName || sockClassByID);\r |
| 123 | \r |
| 124 | if (!sockServiceClass)\r |
| 125 | throw ("Couldn't get socket service class.");\r |
| 126 | \r |
| 127 | var sockService = sockServiceClass.getService();\r |
| 128 | if (!sockService)\r |
| 129 | throw ("Couldn't get socket service.");\r |
| 130 | \r |
| 131 | this._sockService = sockService.QueryInterface\r |
| 132 | (Components.interfaces.nsISocketTransportService);\r |
| 133 | \r |
| 134 | /* Note: as part of the mess from bug 315288 and bug 316178, ChatZilla now\r |
| 135 | * uses the *binary* stream interfaces for all network\r |
| 136 | * communications.\r |
| 137 | *\r |
| 138 | * However, these interfaces do not exist prior to 1999-11-05. To\r |
| 139 | * make matters worse, an incompatible change to the "readBytes"\r |
| 140 | * method of this interface was made on 2003-03-13; luckly, this\r |
| 141 | * change also added a "readByteArray" method, which we will check\r |
| 142 | * for below, to determin if we can use the binary streams.\r |
| 143 | */\r |
| 144 | \r |
| 145 | // We want to check for working binary streams only the first time.\r |
| 146 | if (CBSConnection.prototype.workingBinaryStreams == -1)\r |
| 147 | {\r |
| 148 | CBSConnection.prototype.workingBinaryStreams = false;\r |
| 149 | \r |
| 150 | if (typeof nsIBinaryInputStream != "undefined")\r |
| 151 | {\r |
| 152 | var isCls = Components.classes["@mozilla.org/binaryinputstream;1"];\r |
| 153 | var inputStream = isCls.createInstance(nsIBinaryInputStream);\r |
| 154 | if ("readByteArray" in inputStream)\r |
| 155 | CBSConnection.prototype.workingBinaryStreams = true;\r |
| 156 | }\r |
| 157 | }\r |
| 158 | \r |
| 159 | this.wrappedJSObject = this;\r |
| 160 | if (typeof binary != "undefined")\r |
| 161 | this.binaryMode = binary;\r |
| 162 | else\r |
| 163 | this.binaryMode = this.workingBinaryStreams;\r |
| 164 | \r |
| 165 | if (!ASSERT(!this.binaryMode || this.workingBinaryStreams,\r |
| 166 | "Unable to use binary streams in this build."))\r |
| 167 | {\r |
| 168 | throw ("Unable to use binary streams in this build.");\r |
| 169 | }\r |
| 170 | }\r |
| 171 | \r |
| 172 | CBSConnection.prototype.workingBinaryStreams = -1;\r |
| 173 | \r |
| 174 | CBSConnection.prototype.connect =\r |
| 175 | function bc_connect(host, port, config, observer)\r |
| 176 | {\r |
| 177 | this.host = host.toLowerCase();\r |
| 178 | this.port = port;\r |
| 179 | \r |
| 180 | if (typeof config != "object")\r |
| 181 | config = {};\r |
| 182 | \r |
| 183 | // Lets get a transportInfo for this\r |
| 184 | var pps = getService("@mozilla.org/network/protocol-proxy-service;1",\r |
| 185 | "nsIProtocolProxyService");\r |
| 186 | if (!pps)\r |
| 187 | throw ("Couldn't get protocol proxy service");\r |
| 188 | \r |
| 189 | var ios = getService("@mozilla.org/network/io-service;1", "nsIIOService");\r |
| 190 | \r |
| 191 | function getProxyFor(uri)\r |
| 192 | {\r |
| 193 | uri = ios.newURI(uri, null, null);\r |
| 194 | // As of 2005-03-25, 'examineForProxy' was replaced by 'resolve'.\r |
| 195 | if ("resolve" in pps)\r |
| 196 | return pps.resolve(uri, 0);\r |
| 197 | if ("examineForProxy" in pps)\r |
| 198 | return pps.examineForProxy(uri);\r |
| 199 | return null;\r |
| 200 | };\r |
| 201 | \r |
| 202 | var proxyInfo = null;\r |
| 203 | var usingHTTPCONNECT = false;\r |
| 204 | if ("proxy" in config)\r |
| 205 | {\r |
| 206 | /* Force Necko to supply the HTTP proxy info if desired. For none,\r |
| 207 | * force no proxy. Other values will get default treatment.\r |
| 208 | */\r |
| 209 | if (config.proxy == "http")\r |
| 210 | proxyInfo = getProxyFor("http://" + host + ":" + port);\r |
| 211 | else if (config.proxy != "none")\r |
| 212 | proxyInfo = getProxyFor("irc://" + host + ":" + port);\r |
| 213 | \r |
| 214 | /* Since the proxy info is opaque, we need to check that we got\r |
| 215 | * something for our HTTP proxy - we can't just check proxyInfo.type.\r |
| 216 | */\r |
| 217 | usingHTTPCONNECT = ((config.proxy == "http") && proxyInfo);\r |
| 218 | }\r |
| 219 | else\r |
| 220 | {\r |
| 221 | proxyInfo = getProxyFor("irc://" + host + ":" + port);\r |
| 222 | }\r |
| 223 | \r |
| 224 | if (jsenv.HAS_STREAM_PROVIDER)\r |
| 225 | {\r |
| 226 | if (("isSecure" in config) && config.isSecure)\r |
| 227 | {\r |
| 228 | this._transport = this._sockService.\r |
| 229 | createTransportOfType("ssl", host, port,\r |
| 230 | proxyInfo, 0, 0);\r |
| 231 | }\r |
| 232 | else\r |
| 233 | {\r |
| 234 | this._transport = this._sockService.\r |
| 235 | createTransport(host, port, proxyInfo, 0, 0);\r |
| 236 | }\r |
| 237 | if (!this._transport)\r |
| 238 | throw ("Error creating transport.");\r |
| 239 | \r |
| 240 | if (jsenv.HAS_NSPR_EVENTQ)\r |
| 241 | { /* we've got an event queue, so start up an async write */\r |
| 242 | this._streamProvider = new StreamProvider (observer);\r |
| 243 | this._write_req =\r |
| 244 | this._transport.asyncWrite (this._streamProvider, this,\r |
| 245 | 0, -1, 0);\r |
| 246 | }\r |
| 247 | else\r |
| 248 | {\r |
| 249 | /* no nspr event queues in this environment, we can't use async\r |
| 250 | * calls, so set up the streams. */\r |
| 251 | this._outputStream = this._transport.openOutputStream(0, -1, 0);\r |
| 252 | if (!this._outputStream)\r |
| 253 | throw "Error getting output stream.";\r |
| 254 | this._sOutputStream = toSOutputStream(this._outputStream,\r |
| 255 | this.binaryMode);\r |
| 256 | \r |
| 257 | this._inputStream = this._transport.openInputStream(0, -1, 0);\r |
| 258 | if (!this._inputStream)\r |
| 259 | throw "Error getting input stream.";\r |
| 260 | this._sInputStream = toSInputStream(this._inputStream,\r |
| 261 | this.binaryMode);\r |
| 262 | }\r |
| 263 | }\r |
| 264 | else\r |
| 265 | {\r |
| 266 | /* use new necko interfaces */\r |
| 267 | if (("isSecure" in config) && config.isSecure)\r |
| 268 | {\r |
| 269 | this._transport = this._sockService.\r |
| 270 | createTransport(["ssl"], 1, host, port,\r |
| 271 | proxyInfo);\r |
| 272 | }\r |
| 273 | else\r |
| 274 | {\r |
| 275 | this._transport = this._sockService.\r |
| 276 | createTransport(null, 0, host, port, proxyInfo);\r |
| 277 | }\r |
| 278 | if (!this._transport)\r |
| 279 | throw ("Error creating transport.");\r |
| 280 | \r |
| 281 | /* if we don't have an event queue, then all i/o must be blocking */\r |
| 282 | var openFlags;\r |
| 283 | if (jsenv.HAS_NSPR_EVENTQ)\r |
| 284 | openFlags = 0;\r |
| 285 | else\r |
| 286 | openFlags = Components.interfaces.nsITransport.OPEN_BLOCKING;\r |
| 287 | \r |
| 288 | /* no limit on the output stream buffer */\r |
| 289 | this._outputStream =\r |
| 290 | this._transport.openOutputStream(openFlags, 4096, -1);\r |
| 291 | if (!this._outputStream)\r |
| 292 | throw "Error getting output stream.";\r |
| 293 | this._sOutputStream = toSOutputStream(this._outputStream,\r |
| 294 | this.binaryMode);\r |
| 295 | \r |
| 296 | this._inputStream = this._transport.openInputStream(openFlags, 0, 0);\r |
| 297 | if (!this._inputStream)\r |
| 298 | throw "Error getting input stream.";\r |
| 299 | this._sInputStream = toSInputStream(this._inputStream,\r |
| 300 | this.binaryMode);\r |
| 301 | }\r |
| 302 | \r |
| 303 | this.connectDate = new Date();\r |
| 304 | this.isConnected = true;\r |
| 305 | \r |
| 306 | // Bootstrap the connection if we're proxying via an HTTP proxy.\r |
| 307 | if (usingHTTPCONNECT)\r |
| 308 | {\r |
| 309 | this.sendData("CONNECT " + host + ":" + port + " HTTP/1.1\r\n\r\n");\r |
| 310 | }\r |
| 311 | \r |
| 312 | return this.isConnected;\r |
| 313 | \r |
| 314 | }\r |
| 315 | \r |
| 316 | CBSConnection.prototype.listen =\r |
| 317 | function bc_listen(port, observer)\r |
| 318 | {\r |
| 319 | var serverSockClass =\r |
| 320 | Components.classes["@mozilla.org/network/server-socket;1"];\r |
| 321 | \r |
| 322 | if (!serverSockClass)\r |
| 323 | throw ("Couldn't get server socket class.");\r |
| 324 | \r |
| 325 | var serverSock = serverSockClass.createInstance();\r |
| 326 | if (!serverSock)\r |
| 327 | throw ("Couldn't get server socket.");\r |
| 328 | \r |
| 329 | this._serverSock = serverSock.QueryInterface\r |
| 330 | (Components.interfaces.nsIServerSocket);\r |
| 331 | \r |
| 332 | this._serverSock.init(port, false, -1);\r |
| 333 | \r |
| 334 | this._serverSockListener = new SocketListener(this, observer);\r |
| 335 | \r |
| 336 | this._serverSock.asyncListen(this._serverSockListener);\r |
| 337 | \r |
| 338 | this.port = this._serverSock.port;\r |
| 339 | \r |
| 340 | return true;\r |
| 341 | }\r |
| 342 | \r |
| 343 | CBSConnection.prototype.accept =\r |
| 344 | function bc_accept(transport, observer)\r |
| 345 | {\r |
| 346 | this._transport = transport;\r |
| 347 | this.host = this._transport.host.toLowerCase();\r |
| 348 | this.port = this._transport.port;\r |
| 349 | \r |
| 350 | if (jsenv.HAS_STREAM_PROVIDER)\r |
| 351 | {\r |
| 352 | if (jsenv.HAS_NSPR_EVENTQ)\r |
| 353 | { /* we've got an event queue, so start up an async write */\r |
| 354 | this._streamProvider = new StreamProvider (observer);\r |
| 355 | this._write_req =\r |
| 356 | this._transport.asyncWrite (this._streamProvider, this,\r |
| 357 | 0, -1, 0);\r |
| 358 | }\r |
| 359 | else\r |
| 360 | {\r |
| 361 | /* no nspr event queues in this environment, we can't use async\r |
| 362 | * calls, so set up the streams. */\r |
| 363 | this._outputStream = this._transport.openOutputStream(0, -1, 0);\r |
| 364 | if (!this._outputStream)\r |
| 365 | throw "Error getting output stream.";\r |
| 366 | this._sOutputStream = toSOutputStream(this._outputStream,\r |
| 367 | this.binaryMode);\r |
| 368 | \r |
| 369 | //this._scriptableInputStream =\r |
| 370 | this._inputStream = this._transport.openInputStream(0, -1, 0);\r |
| 371 | if (!this._inputStream)\r |
| 372 | throw "Error getting input stream.";\r |
| 373 | this._sInputStream = toSInputStream(this._inputStream,\r |
| 374 | this.binaryMode);\r |
| 375 | }\r |
| 376 | }\r |
| 377 | else\r |
| 378 | {\r |
| 379 | /* if we don't have an event queue, then all i/o must be blocking */\r |
| 380 | var openFlags;\r |
| 381 | if (jsenv.HAS_NSPR_EVENTQ)\r |
| 382 | openFlags = 0;\r |
| 383 | else\r |
| 384 | openFlags = Components.interfaces.nsITransport.OPEN_BLOCKING;\r |
| 385 | \r |
| 386 | /* no limit on the output stream buffer */\r |
| 387 | this._outputStream =\r |
| 388 | this._transport.openOutputStream(openFlags, 4096, -1);\r |
| 389 | if (!this._outputStream)\r |
| 390 | throw "Error getting output stream.";\r |
| 391 | this._sOutputStream = toSOutputStream(this._outputStream,\r |
| 392 | this.binaryMode);\r |
| 393 | \r |
| 394 | this._inputStream = this._transport.openInputStream(openFlags, 0, 0);\r |
| 395 | if (!this._inputStream)\r |
| 396 | throw "Error getting input stream.";\r |
| 397 | this._sInputStream = toSInputStream(this._inputStream,\r |
| 398 | this.binaryMode);\r |
| 399 | }\r |
| 400 | \r |
| 401 | this.connectDate = new Date();\r |
| 402 | this.isConnected = true;\r |
| 403 | \r |
| 404 | // Clean up listening socket.\r |
| 405 | this.close();\r |
| 406 | \r |
| 407 | return this.isConnected;\r |
| 408 | }\r |
| 409 | \r |
| 410 | CBSConnection.prototype.close =\r |
| 411 | function bc_close()\r |
| 412 | {\r |
| 413 | if ("_serverSock" in this && this._serverSock)\r |
| 414 | this._serverSock.close();\r |
| 415 | }\r |
| 416 | \r |
| 417 | CBSConnection.prototype.disconnect =\r |
| 418 | function bc_disconnect()\r |
| 419 | {\r |
| 420 | if ("_inputStream" in this && this._inputStream)\r |
| 421 | this._inputStream.close();\r |
| 422 | if ("_outputStream" in this && this._outputStream)\r |
| 423 | this._outputStream.close();\r |
| 424 | this.isConnected = false;\r |
| 425 | /*\r |
| 426 | this._streamProvider.close();\r |
| 427 | if (this._streamProvider.isBlocked)\r |
| 428 | this._write_req.resume();\r |
| 429 | */\r |
| 430 | }\r |
| 431 | \r |
| 432 | CBSConnection.prototype.sendData =\r |
| 433 | function bc_senddata(str)\r |
| 434 | {\r |
| 435 | if (!this.isConnected)\r |
| 436 | throw "Not Connected.";\r |
| 437 | \r |
| 438 | if (jsenv.HAS_NSPR_EVENTQ && jsenv.HAS_STREAM_PROVIDER)\r |
| 439 | this.asyncWrite (str);\r |
| 440 | else\r |
| 441 | this.sendDataNow (str);\r |
| 442 | }\r |
| 443 | \r |
| 444 | CBSConnection.prototype.readData =\r |
| 445 | function bc_readdata(timeout, count)\r |
| 446 | {\r |
| 447 | if (!this.isConnected)\r |
| 448 | throw "Not Connected.";\r |
| 449 | \r |
| 450 | var rv;\r |
| 451 | \r |
| 452 | if (!("_sInputStream" in this)) {\r |
| 453 | this._sInputStream = toSInputStream(this._inputStream);\r |
| 454 | dump("OMG, setting up _sInputStream!\n");\r |
| 455 | }\r |
| 456 | \r |
| 457 | try\r |
| 458 | {\r |
| 459 | // XPCshell h4x\r |
| 460 | if (typeof count == "undefined")\r |
| 461 | count = this._sInputStream.available();\r |
| 462 | if (this.binaryMode)\r |
| 463 | rv = this._sInputStream.readBytes(count);\r |
| 464 | else\r |
| 465 | rv = this._sInputStream.read(count);\r |
| 466 | }\r |
| 467 | catch (ex)\r |
| 468 | {\r |
| 469 | dd ("*** Caught " + ex + " while reading.");\r |
| 470 | this.disconnect();\r |
| 471 | throw (ex);\r |
| 472 | }\r |
| 473 | \r |
| 474 | return rv;\r |
| 475 | }\r |
| 476 | \r |
| 477 | CBSConnection.prototype.startAsyncRead =\r |
| 478 | function bc_saread (observer)\r |
| 479 | {\r |
| 480 | if (jsenv.HAS_STREAM_PROVIDER)\r |
| 481 | {\r |
| 482 | this._transport.asyncRead (new StreamListener (observer),\r |
| 483 | this, 0, -1, 0);\r |
| 484 | }\r |
| 485 | else\r |
| 486 | {\r |
| 487 | var cls = Components.classes["@mozilla.org/network/input-stream-pump;1"];\r |
| 488 | var pump = cls.createInstance(Components.interfaces.nsIInputStreamPump);\r |
| 489 | pump.init(this._inputStream, -1, -1, 0, 0, false);\r |
| 490 | pump.asyncRead(new StreamListener(observer), this);\r |
| 491 | }\r |
| 492 | }\r |
| 493 | \r |
| 494 | CBSConnection.prototype.asyncWrite =\r |
| 495 | function bc_awrite (str)\r |
| 496 | {\r |
| 497 | this._streamProvider.pendingData += str;\r |
| 498 | if (this._streamProvider.isBlocked)\r |
| 499 | {\r |
| 500 | this._write_req.resume();\r |
| 501 | this._streamProvider.isBlocked = false;\r |
| 502 | }\r |
| 503 | }\r |
| 504 | \r |
| 505 | CBSConnection.prototype.hasPendingWrite =\r |
| 506 | function bc_haspwrite ()\r |
| 507 | {\r |
| 508 | if (jsenv.HAS_STREAM_PROVIDER)\r |
| 509 | return (this._streamProvider.pendingData != "");\r |
| 510 | else\r |
| 511 | return false; /* data already pushed to necko */\r |
| 512 | }\r |
| 513 | \r |
| 514 | CBSConnection.prototype.sendDataNow =\r |
| 515 | function bc_senddatanow(str)\r |
| 516 | {\r |
| 517 | var rv = false;\r |
| 518 | \r |
| 519 | try\r |
| 520 | {\r |
| 521 | if (this.binaryMode)\r |
| 522 | this._sOutputStream.writeBytes(str, str.length);\r |
| 523 | else\r |
| 524 | this._sOutputStream.write(str, str.length);\r |
| 525 | rv = true;\r |
| 526 | }\r |
| 527 | catch (ex)\r |
| 528 | {\r |
| 529 | dd ("*** Caught " + ex + " while sending.");\r |
| 530 | this.disconnect();\r |
| 531 | throw (ex);\r |
| 532 | }\r |
| 533 | \r |
| 534 | return rv;\r |
| 535 | }\r |
| 536 | \r |
| 537 | /* getSecurityState returns an array containing information about the security\r |
| 538 | * of the connection. The array always has at least one item, which contains a\r |
| 539 | * value from the STATE_IS_* enumeration at the top of this file. Iff this is\r |
| 540 | * STATE_IS_SECURE, the array has a second item indicating the level of\r |
| 541 | * security - a value from the STATE_SECURE_* enumeration.\r |
| 542 | *\r |
| 543 | * STATE_IS_BROKEN is returned if any errors occur, and STATE_IS_INSECURE is\r |
| 544 | * returned for disconnected sockets.\r |
| 545 | */\r |
| 546 | CBSConnection.prototype.getSecurityState =\r |
| 547 | function bc_getsecuritystate()\r |
| 548 | {\r |
| 549 | if (!this.isConnected || !this._transport.securityInfo)\r |
| 550 | return [STATE_IS_INSECURE];\r |
| 551 | \r |
| 552 | try\r |
| 553 | {\r |
| 554 | var sslSp = Components.interfaces.nsISSLStatusProvider;\r |
| 555 | var sslStatus = Components.interfaces.nsISSLStatus;\r |
| 556 | \r |
| 557 | // Get the actual SSL Status\r |
| 558 | sslSp = this._transport.securityInfo.QueryInterface(sslSp);\r |
| 559 | sslStatus = sslSp.SSLStatus.QueryInterface(sslStatus);\r |
| 560 | // Store appropriate status\r |
| 561 | if (!("keyLength" in sslStatus) || !sslStatus.keyLength)\r |
| 562 | return [STATE_IS_BROKEN];\r |
| 563 | else if (sslStatus.keyLength >= 90)\r |
| 564 | return [STATE_IS_SECURE, STATE_SECURE_HIGH];\r |
| 565 | else\r |
| 566 | return [STATE_IS_SECURE, STATE_SECURE_LOW];\r |
| 567 | }\r |
| 568 | catch (ex)\r |
| 569 | {\r |
| 570 | // Something goes wrong -> broken security icon\r |
| 571 | dd("Exception getting certificate for connection: " + ex.message);\r |
| 572 | return [STATE_IS_BROKEN];\r |
| 573 | }\r |
| 574 | }\r |
| 575 | \r |
| 576 | CBSConnection.prototype.getCertificate =\r |
| 577 | function bc_getcertificate()\r |
| 578 | {\r |
| 579 | if (!this.isConnected || !this._transport.securityInfo)\r |
| 580 | return null;\r |
| 581 | \r |
| 582 | var sslSp = Components.interfaces.nsISSLStatusProvider;\r |
| 583 | var sslStatus = Components.interfaces.nsISSLStatus;\r |
| 584 | \r |
| 585 | // Get the actual SSL Status\r |
| 586 | sslSp = this._transport.securityInfo.QueryInterface(sslSp);\r |
| 587 | sslStatus = sslSp.SSLStatus.QueryInterface(sslStatus);\r |
| 588 | \r |
| 589 | // return the certificate\r |
| 590 | return sslStatus.serverCert;\r |
| 591 | }\r |
| 592 | \r |
| 593 | function _notimpl ()\r |
| 594 | {\r |
| 595 | throw "Not Implemented.";\r |
| 596 | }\r |
| 597 | \r |
| 598 | if (!jsenv.HAS_NSPR_EVENTQ)\r |
| 599 | {\r |
| 600 | CBSConnection.prototype.startAsyncRead = _notimpl;\r |
| 601 | CBSConnection.prototype.asyncWrite = _notimpl;\r |
| 602 | }\r |
| 603 | else if (jsenv.HAS_STREAM_PROVIDER)\r |
| 604 | {\r |
| 605 | CBSConnection.prototype.sendDataNow = _notimpl;\r |
| 606 | }\r |
| 607 | else\r |
| 608 | {\r |
| 609 | CBSConnection.prototype.asyncWrite = _notimpl;\r |
| 610 | }\r |
| 611 | \r |
| 612 | delete _notimpl;\r |
| 613 | \r |
| 614 | function StreamProvider(observer)\r |
| 615 | {\r |
| 616 | this._observer = observer;\r |
| 617 | }\r |
| 618 | \r |
| 619 | StreamProvider.prototype.pendingData = "";\r |
| 620 | StreamProvider.prototype.isBlocked = true;\r |
| 621 | \r |
| 622 | StreamProvider.prototype.close =\r |
| 623 | function sp_close ()\r |
| 624 | {\r |
| 625 | this.isClosed = true;\r |
| 626 | }\r |
| 627 | \r |
| 628 | StreamProvider.prototype.onDataWritable =\r |
| 629 | function sp_datawrite (request, ctxt, ostream, offset, count)\r |
| 630 | {\r |
| 631 | //dd ("StreamProvider.prototype.onDataWritable");\r |
| 632 | \r |
| 633 | if ("isClosed" in this && this.isClosed)\r |
| 634 | throw Components.results.NS_BASE_STREAM_CLOSED;\r |
| 635 | \r |
| 636 | if (!this.pendingData)\r |
| 637 | {\r |
| 638 | this.isBlocked = true;\r |
| 639 | \r |
| 640 | /* this is here to support pre-XPCDOM builds (0.9.0 era), which\r |
| 641 | * don't have this result code mapped. */\r |
| 642 | if (!Components.results.NS_BASE_STREAM_WOULD_BLOCK)\r |
| 643 | throw 2152136711;\r |
| 644 | \r |
| 645 | throw Components.results.NS_BASE_STREAM_WOULD_BLOCK;\r |
| 646 | }\r |
| 647 | \r |
| 648 | var len = ostream.write (this.pendingData, this.pendingData.length);\r |
| 649 | this.pendingData = this.pendingData.substr (len);\r |
| 650 | }\r |
| 651 | \r |
| 652 | StreamProvider.prototype.onStartRequest =\r |
| 653 | function sp_startreq (request, ctxt)\r |
| 654 | {\r |
| 655 | //dd ("StreamProvider::onStartRequest: " + request + ", " + ctxt);\r |
| 656 | }\r |
| 657 | \r |
| 658 | \r |
| 659 | StreamProvider.prototype.onStopRequest =\r |
| 660 | function sp_stopreq (request, ctxt, status)\r |
| 661 | {\r |
| 662 | //dd ("StreamProvider::onStopRequest: " + request + ", " + ctxt + ", " +\r |
| 663 | // status);\r |
| 664 | if (this._observer)\r |
| 665 | this._observer.onStreamClose(status);\r |
| 666 | }\r |
| 667 | \r |
| 668 | function StreamListener(observer)\r |
| 669 | {\r |
| 670 | this._observer = observer;\r |
| 671 | }\r |
| 672 | \r |
| 673 | StreamListener.prototype.onStartRequest =\r |
| 674 | function sl_startreq (request, ctxt)\r |
| 675 | {\r |
| 676 | //dd ("StreamListener::onStartRequest: " + request + ", " + ctxt);\r |
| 677 | }\r |
| 678 | \r |
| 679 | StreamListener.prototype.onStopRequest =\r |
| 680 | function sl_stopreq (request, ctxt, status)\r |
| 681 | {\r |
| 682 | //dd ("StreamListener::onStopRequest: " + request + ", " + ctxt + ", " +\r |
| 683 | //status);\r |
| 684 | if (this._observer)\r |
| 685 | this._observer.onStreamClose(status);\r |
| 686 | }\r |
| 687 | \r |
| 688 | StreamListener.prototype.onDataAvailable =\r |
| 689 | function sl_dataavail (request, ctxt, inStr, sourceOffset, count)\r |
| 690 | {\r |
| 691 | ctxt = ctxt.wrappedJSObject;\r |
| 692 | if (!ctxt)\r |
| 693 | {\r |
| 694 | dd ("*** Can't get wrappedJSObject from ctxt in " +\r |
| 695 | "StreamListener.onDataAvailable ***");\r |
| 696 | return;\r |
| 697 | }\r |
| 698 | \r |
| 699 | if (!("_sInputStream" in ctxt))\r |
| 700 | ctxt._sInputStream = toSInputStream(inStr, false);\r |
| 701 | \r |
| 702 | if (this._observer)\r |
| 703 | this._observer.onStreamDataAvailable(request, inStr, sourceOffset,\r |
| 704 | count);\r |
| 705 | }\r |
| 706 | \r |
| 707 | function SocketListener(connection, observer)\r |
| 708 | {\r |
| 709 | this._connection = connection;\r |
| 710 | this._observer = observer;\r |
| 711 | }\r |
| 712 | \r |
| 713 | SocketListener.prototype.onSocketAccepted =\r |
| 714 | function sl_onSocketAccepted(socket, transport)\r |
| 715 | {\r |
| 716 | this._observer.onSocketAccepted(socket, transport);\r |
| 717 | }\r |
| 718 | SocketListener.prototype.onStopListening =\r |
| 719 | function sl_onStopListening(socket, status)\r |
| 720 | {\r |
| 721 | delete this._connection._serverSockListener;\r |
| 722 | delete this._connection._serverSock;\r |
| 723 | }\r |