Skip to content

Instantly share code, notes, and snippets.

@polotek
Created July 13, 2010 03:02
Show Gist options
  • Save polotek/473396 to your computer and use it in GitHub Desktop.
Save polotek/473396 to your computer and use it in GitHub Desktop.
var sys = require('sys');
var http = require('http');
function MyClient() {
http.Client.call(this);
this.setRequestCtor(MyClientRequest);
this.testProp = 'myclient test prop';
}
sys.inherits(MyClient, http.Client);
MyClient.prototype.tester = function() { sys.puts(this.testProp); }
exports.MyClient = MyClient;
function MyClientRequest() {
http.ClientRequest.apply(this, arguments);
this.testProp = 'myclientrequest test prop';
}
sys.inherits(MyClientRequest, http.ClientRequest);
MyClientRequest.prototype.tester = function() { sys.puts(this.testProp); }
diff --git a/lib/http.js b/lib/http.js
index 1f3537b..58506c7 100644
--- a/lib/http.js
+++ b/lib/http.js
@@ -835,6 +835,12 @@ function Client ( ) {
httpSocketSetup(self);
var parser;
+ var requestCtor = ClientRequest;
+ self.getRequestCtor = function() { return requestCtor; };
+ self.setRequestCtor = function(ctor) {
+ if(typeof ctor != 'function') throw new Error ('Request Ctor
+ requestCtor = ctor;
+ };
function initParser () {
if (!parser) parser = parsers.alloc();
@@ -1002,7 +1008,8 @@ Client.prototype.request = function (method,
url = method;
method = "GET";
}
- var req = new ClientRequest(this, method, url, headers);
+ var requestCtor = this.getRequestCtor();
+ var req = new requestCtor(this, method, url, headers);
this._outgoing.push(req);
if (this.readyState === 'closed') this._reconnect();
return req;
var sys = require('sys');
var http = require("myhttp");
var client = new http.MyClient();
client.port = 80;
client.host = 'www.google.com';
client.https = undefined;
client.credentials = undefined;
var request = client.request('GET', '/'
, {'host': 'www.google.com'});
request.addListener('response', function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.addListener('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
request.end();
client.tester();
request.tester();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment