Skip to content

Instantly share code, notes, and snippets.

@broofa
Created March 26, 2010 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save broofa/344786 to your computer and use it in GitHub Desktop.
Save broofa/344786 to your computer and use it in GitHub Desktop.
From d84a19d450ba485e5508241f96c6e504a78a8cdd Mon Sep 17 00:00:00 2001
From: Robert Kieffer <robert@broofa.com>
Date: Fri, 26 Mar 2010 04:04:06 -0700
Subject: [PATCH] - Add support for OutgoingMessage#statusCode
- OutgoingMessage#close() emits 'close' event
- Add missing semicolons in various spots
---
lib/http.js | 19 +++++++++++--------
1 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/lib/http.js b/lib/http.js
index 554f047..3ebc9e6 100644
--- a/lib/http.js
+++ b/lib/http.js
@@ -237,7 +237,8 @@ function OutgoingMessage (socket) {
this.useChunkedEncodingByDefault = true;
this.flushing = false;
- this.headWritten = false;
+ this.headWritten = false; // Deprecate? Can test statusCode instead
+ this.statusCode = null;
this.finished = false;
}
@@ -339,8 +340,8 @@ OutgoingMessage.prototype.sendBody = function () {
OutgoingMessage.prototype.write = function (chunk, encoding) {
- if ( (this instanceof ServerResponse) && !this.headWritten) {
- throw new Error("writeHead() must be called before write()")
+ if ( (this instanceof ServerResponse) && !this.statusCode) {
+ throw new Error("writeHead() must be called before write()");
}
encoding = encoding || "ascii";
@@ -376,6 +377,7 @@ OutgoingMessage.prototype.close = function () {
if (this.chunkedEncoding) this._send("0\r\n\r\n"); // last chunk
this.finished = true;
this.flush();
+ this.emit('close', this);
};
@@ -412,6 +414,7 @@ ServerResponse.prototype.writeHead = function (statusCode) {
+ reasonPhrase + CRLF;
this.sendHeaderLines(statusLine, headers);
this.headWritten = true;
+ this.statusCode = statusCode;
};
// TODO eventually remove sendHeader(), writeHeader()
@@ -573,7 +576,7 @@ function Client ( ) {
self.addListener("connect", function () {
parser.reinitialize('response');
debug('requests: ' + sys.inspect(requests));
- currentRequest = requests.shift()
+ currentRequest = requests.shift();
currentRequest.flush();
});
@@ -618,7 +621,7 @@ exports.createClient = function (port, host) {
c.port = port;
c.host = host;
return c;
-}
+};
Client.prototype.get = function () {
@@ -677,8 +680,8 @@ exports.cat = function (url, encoding_, headers_) {
}
}
- var url = require("url").parse(url);
-
+ url = require("url").parse(url);
+
var hasHost = false;
for (var i in headers) {
if (i.toLowerCase() === "host") {
@@ -689,7 +692,7 @@ exports.cat = function (url, encoding_, headers_) {
if (!hasHost) headers["Host"] = url.hostname;
var content = "";
-
+
var client = exports.createClient(url.port || 80, url.hostname);
var req = client.request((url.pathname || "/")+(url.search || "")+(url.hash || ""), headers);
--
1.7.0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment