OutgoingMessage.prototype._send = function (data, encoding) {
var length = this.output.length;
- if (length === 0) {
+ if (length === 0 || typeof data != 'string') {
this.output.push(data);
encoding = encoding || "ascii";
this.outputEncodings.push(encoding);
if ((lastEncoding === encoding) ||
(!encoding && data.constructor === lastData.constructor)) {
- if (lastData.constructor === String) {
- this.output[length-1] = lastData + data;
- } else {
- this.output[length-1] = lastData.concat(data);
- }
+ this.output[length-1] = lastData + data;
return;
}
encoding = encoding || "ascii";
if (this.chunked_encoding) {
- this._send(process._byteLength(chunk, encoding).toString(16));
+ if (typeof chunk == 'string') {
+ this._send(process._byteLength(chunk, encoding).toString(16));
+ } else {
+ this._send(chunk.length.toString(16));
+ }
this._send(CRLF);
this._send(chunk, encoding);
this._send(CRLF);
self._reconnect = function () {
if (self.readyState != "opening") {
- //sys.debug("HTTP CLIENT: reconnecting readyState = " + self.readyState);
+ sys.debug("HTTP CLIENT: reconnecting readyState = " + self.readyState);
self.connect(self.port, self.host);
}
};
self._pushRequest = function (req) {
req.addListener("flush", function () {
- /*
if (self.readyState == "closed") {
- //sys.debug("HTTP CLIENT request flush. reconnect. readyState = " + self.readyState);
+ sys.debug("HTTP CLIENT request flush. reconnect. readyState = " + self.readyState);
self._reconnect();
return;
}
- */
- //sys.debug("self flush readyState = " + self.readyState);
+
+ sys.debug("self flush readyState = " + self.readyState);
if (req == currentRequest) flushMessageQueue(self, [req]);
});
requests.push(req);
self.addListener("connect", function () {
parser.reinitialize('response');
- currentRequest = requests.shift();
+ sys.puts('requests: ' + sys.inspect(requests));
+ currentRequest = requests.shift()
currentRequest.flush();
});
return;
}
- //sys.debug("HTTP CLIENT onClose. readyState = " + self.readyState);
+ sys.debug("HTTP CLIENT onClose. readyState = " + self.readyState);
// If there are more requests to handle, reconnect.
if (requests.length > 0) {
var c = new Client;
c.port = port;
c.host = host;
- c.connect(port, host);
return c;
}
var BACKEND_PORT = PORT+1;
var backend = http.createServer(function (req, res) {
- // debug("backend");
+ debug("backend request");
res.writeHead(200, {"content-type": "text/plain"});
res.write("hello world\n");
res.close();
});
-// debug("listen backend")
+debug("listen backend")
backend.listen(BACKEND_PORT);
var proxy_client = http.createClient(BACKEND_PORT);
});
proxy_res.addListener("end", function() {
res.close();
- // debug("proxy res");
+ debug("proxy res");
});
});
proxy_req.close();
});
-// debug("listen proxy")
+debug("listen proxy")
proxy.listen(PROXY_PORT);
var body = "";
-var client = http.createClient(PROXY_PORT);
-var req = client.request("/test");
-// debug("client req")
-req.addListener('response', function (res) {
- // debug("got res");
- assert.equal(200, res.statusCode);
- res.setBodyEncoding("utf8");
- res.addListener('data', function (chunk) { body += chunk; });
- res.addListener('end', function () {
- proxy.close();
- backend.close();
- // debug("closed both");
+nlistening = 0;
+function startReq () {
+ nlistening++;
+ if (nlistening < 2) return;
+
+ var client = http.createClient(PROXY_PORT);
+ var req = client.request("/test");
+ debug("client req")
+ req.addListener('response', function (res) {
+ debug("got res");
+ assert.equal(200, res.statusCode);
+ res.setBodyEncoding("utf8");
+ res.addListener('data', function (chunk) { body += chunk; });
+ res.addListener('end', function () {
+ proxy.close();
+ backend.close();
+ debug("closed both");
+ });
});
-});
-req.close();
+ req.close();
+}
+
+proxy.addListener('listening', startReq);
+backend.addListener('listening', startReq);
process.addListener("exit", function () {
assert.equal(body, "hello world\n");