CBSConnection.connect Member

Syntax

object.connect(host, port, config, observer);

Arguments

ArgumentSummary
host
port
config
observer

Returns

Remarks

See Also

Source Code

function bc_connect(host, port, config, observer)
{
this.host = host.toLowerCase();
this.port = port;
if (typeof config != "object")
config = {};
// Lets get a transportInfo for this
var pps = getService("@mozilla.org/network/protocol-proxy-service;1",
"nsIProtocolProxyService");
if (!pps)
throw ("Couldn't get protocol proxy service");
var ios = getService("@mozilla.org/network/io-service;1", "nsIIOService");
function getProxyFor(uri)
{
uri = ios.newURI(uri, null, null);
// As of 2005-03-25, 'examineForProxy' was replaced by 'resolve'.
if ("resolve" in pps)
return pps.resolve(uri, 0);
if ("examineForProxy" in pps)
return pps.examineForProxy(uri);
return null;
};
var proxyInfo = null;
var usingHTTPCONNECT = false;
if ("proxy" in config)
{
/* Force Necko to supply the HTTP proxy info if desired. For none,
* force no proxy. Other values will get default treatment.
*/
if (config.proxy == "http")
proxyInfo = getProxyFor("http://" + host + ":" + port);
else if (config.proxy != "none")
proxyInfo = getProxyFor("irc://" + host + ":" + port);
/* Since the proxy info is opaque, we need to check that we got
* something for our HTTP proxy - we can't just check proxyInfo.type.
*/
usingHTTPCONNECT = ((config.proxy == "http") && proxyInfo);
}
else
{
proxyInfo = getProxyFor("irc://" + host + ":" + port);
}
if (proxyInfo && ("type" in proxyInfo) && (proxyInfo.type == "unknown"))
throw JSIRC_ERR_PAC_LOADING;
if (jsenv.HAS_STREAM_PROVIDER)
{
if (("isSecure" in config) && config.isSecure)
{
this._transport = this._sockService.
createTransportOfType("ssl", host, port,
proxyInfo, 0, 0);
}
else
{
this._transport = this._sockService.
createTransport(host, port, proxyInfo, 0, 0);
}
if (!this._transport)
throw ("Error creating transport.");
if (jsenv.HAS_NSPR_EVENTQ)
{ /* we've got an event queue, so start up an async write */
this._streamProvider = new StreamProvider (observer);
this._write_req =
this._transport.asyncWrite (this._streamProvider, this,
0, -1, 0);
}
else
{
/* no nspr event queues in this environment, we can't use async
* calls, so set up the streams. */
this._outputStream = this._transport.openOutputStream(0, -1, 0);
if (!this._outputStream)
throw "Error getting output stream.";
this._sOutputStream = toSOutputStream(this._outputStream,
this.binaryMode);
this._inputStream = this._transport.openInputStream(0, -1, 0);
if (!this._inputStream)
throw "Error getting input stream.";
this._sInputStream = toSInputStream(this._inputStream,
this.binaryMode);
}
}
else
{
/* use new necko interfaces */
if (("isSecure" in config) && config.isSecure)
{
this._transport = this._sockService.
createTransport(["ssl"], 1, host, port,
proxyInfo);
if (this.strictSSL)
this._transport.securityCallbacks = new BadCertHandler();
}
else
{
this._transport = this._sockService.
createTransport(null, 0, host, port, proxyInfo);
}
if (!this._transport)
throw ("Error creating transport.");
/* if we don't have an event queue, then all i/o must be blocking */
var openFlags;
if (jsenv.HAS_NSPR_EVENTQ)
openFlags = 0;
else
openFlags = Components.interfaces.nsITransport.OPEN_BLOCKING;
/* no limit on the output stream buffer */
this._outputStream =
this._transport.openOutputStream(openFlags, 4096, -1);
if (!this._outputStream)
throw "Error getting output stream.";
this._sOutputStream = toSOutputStream(this._outputStream,
this.binaryMode);
this._inputStream = this._transport.openInputStream(openFlags, 0, 0);
if (!this._inputStream)
throw "Error getting input stream.";
this._sInputStream = toSInputStream(this._inputStream,
this.binaryMode);
}
this.connectDate = new Date();
this.isConnected = true;
// Bootstrap the connection if we're proxying via an HTTP proxy.
if (usingHTTPCONNECT)
{
this.sendData("CONNECT " + host + ":" + port + " HTTP/1.1\r\n\r\n");
}
return true;
}