From f8ba9c3bc9fbab6b822a8bb531c7a3c09035100a Mon Sep 17 00:00:00 2001 From: Christopher Lenz Date: Wed, 2 Dec 2009 23:25:56 +0100 Subject: [PATCH] Add http.Client.prototype.request() Change the http.Client API so that it provides a single request() method taking an optional parameter to specify the HTTP method (defaulting to "GET"), instead of the five methods get(), head(), post(), del() and put(). --- benchmark/static_http_server.js | 4 ++-- doc/api.txt | 7 ++++-- lib/http.js | 39 +++++++++++++++++---------------- test/mjsunit/test-http-client-race.js | 4 ++-- test/mjsunit/test-http-client-upload.js | 2 +- test/mjsunit/test-http-proxy.js | 4 ++-- test/mjsunit/test-http-tls.js | 4 ++-- test/mjsunit/test-http.js | 4 ++-- test/mjsunit/test-multipart.js | 2 +- 9 files changed, 37 insertions(+), 33 deletions(-) diff --git a/benchmark/static_http_server.js b/benchmark/static_http_server.js index 6d925b5..da62434 100644 --- a/benchmark/static_http_server.js +++ b/benchmark/static_http_server.js @@ -28,7 +28,7 @@ server.listen(port); function responseListener (res) { res.addListener("complete", function () { if (requests < n) { - res.client.get("/").finish(responseListener); + res.client.request("/").finish(responseListener); requests++; } @@ -41,6 +41,6 @@ function responseListener (res) { for (var i = 0; i < concurrency; i++) { var client = http.createClient(port); client.id = i; - client.get("/").finish(responseListener); + client.request("/").finish(responseListener); requests++; } diff --git a/doc/api.txt b/doc/api.txt index 0be8709..5852ca8 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -928,7 +928,7 @@ Example of connecting to +google.com+ var sys = require("sys"), http = require("http"); var google = http.createClient(80, "www.google.com"); -var request = google.get("/", {"host": "www.google.com"}); +var request = google.request("GET", "/", {"host": "www.google.com"}); request.finish(function (response) { sys.puts("STATUS: " + response.statusCode); sys.puts("HEADERS: " + JSON.stringify(response.headers)); @@ -945,9 +945,12 @@ Constructs a new HTTP client. +port+ and +host+ refer to the server to be connected to. A connection is not established until a request is issued. -+client.get(path, request_headers)+, +client.head(path, request_headers)+, +client.post(path, request_headers)+, +client.del(path, request_headers)+, +client.put(path, request_headers)+ :: ++client.request([method], path, [request_headers])+ :: Issues a request; if necessary establishes connection. Returns a +http.ClientRequest+ instance. + ++ ++method+ is optional and defaults to "GET" if omitted. + +request_headers+ is optional. Additional request headers might be added internally diff --git a/lib/http.js b/lib/http.js index 17846c1..e49ec5f 100644 --- a/lib/http.js +++ b/lib/http.js @@ -594,32 +594,33 @@ exports.createClient = function (port, host) { return client; }; -process.http.Client.prototype.get = function (uri, headers) { - var req = new ClientRequest("GET", uri, headers); - this._pushRequest(req); - return req; +process.http.Client.prototype.get = function () { + throw new Error("client.get(...) is now client.request('GET', ...)"); }; -process.http.Client.prototype.head = function (uri, headers) { - var req = new ClientRequest("HEAD", uri, headers); - this._pushRequest(req); - return req; +process.http.Client.prototype.head = function () { + throw new Error("client.head(...) is now client.request('HEAD', ...)"); }; -process.http.Client.prototype.post = function (uri, headers) { - var req = new ClientRequest("POST", uri, headers); - this._pushRequest(req); - return req; +process.http.Client.prototype.post = function () { + throw new Error("client.post(...) is now client.request('POST', ...)"); }; -process.http.Client.prototype.del = function (uri, headers) { - var req = new ClientRequest("DELETE", uri, headers); - this._pushRequest(req); - return req; +process.http.Client.prototype.del = function () { + throw new Error("client.del(...) is now client.request('DELETE', ...)"); }; -process.http.Client.prototype.put = function (uri, headers) { - var req = new ClientRequest("PUT", uri, headers); +process.http.Client.prototype.put = function () { + throw new Error("client.put(...) is now client.request('PUT', ...)"); +}; + +process.http.Client.prototype.request = function (method, uri, headers) { + if (typeof(uri) != "string") { // assume method was omitted, shift arguments + headers = uri; + uri = method; + method = null; + } + var req = new ClientRequest(method || "GET", uri, headers); this._pushRequest(req); return req; }; @@ -637,7 +638,7 @@ exports.cat = function (url, encoding, headers) { } var client = exports.createClient(uri.port || 80, uri.host); - var req = client.get(uri.path || "/", headers); + var req = client.request(uri.path || "/", headers); client.addListener("error", function () { promise.emitError(); diff --git a/test/mjsunit/test-http-client-race.js b/test/mjsunit/test-http-client-race.js index eedd276..1f33392 100644 --- a/test/mjsunit/test-http-client-race.js +++ b/test/mjsunit/test-http-client-race.js @@ -20,7 +20,7 @@ var client = http.createClient(PORT); var body1 = ""; var body2 = ""; -client.get("/1").finish(function (res1) { +client.request("/1").finish(function (res1) { res1.setBodyEncoding("utf8"); res1.addListener("body", function (chunk) { @@ -28,7 +28,7 @@ client.get("/1").finish(function (res1) { }); res1.addListener("complete", function () { - client.get("/2").finish(function (res2) { + client.request("/2").finish(function (res2) { res2.setBodyEncoding("utf8"); res2.addListener("body", function (chunk) { body2 += chunk; }); res2.addListener("complete", function () { server.close(); }); diff --git a/test/mjsunit/test-http-client-upload.js b/test/mjsunit/test-http-client-upload.js index 5760664..73d95d5 100644 --- a/test/mjsunit/test-http-client-upload.js +++ b/test/mjsunit/test-http-client-upload.js @@ -26,7 +26,7 @@ var server = http.createServer(function(req, res) { server.listen(PORT); var client = http.createClient(PORT); -var req = client.post('/'); +var req = client.request('POST', '/'); req.sendBody('1\n'); req.sendBody('2\n'); diff --git a/test/mjsunit/test-http-proxy.js b/test/mjsunit/test-http-proxy.js index f575fab..1a9025d 100644 --- a/test/mjsunit/test-http-proxy.js +++ b/test/mjsunit/test-http-proxy.js @@ -16,7 +16,7 @@ backend.listen(BACKEND_PORT); var proxy_client = http.createClient(BACKEND_PORT); var proxy = http.createServer(function (req, res) { debug("proxy req headers: " + JSON.stringify(req.headers)); - var proxy_req = proxy_client.get(req.uri.path); + var proxy_req = proxy_client.request(req.uri.path); proxy_req.finish(function(proxy_res) { res.sendHeader(proxy_res.statusCode, proxy_res.headers); proxy_res.addListener("body", function(chunk) { @@ -34,7 +34,7 @@ proxy.listen(PROXY_PORT); var body = ""; var client = http.createClient(PROXY_PORT); -var req = client.get("/test"); +var req = client.request("/test"); // debug("client req") req.finish(function (res) { // debug("got res"); diff --git a/test/mjsunit/test-http-tls.js b/test/mjsunit/test-http-tls.js index 0d80b9a..342b009 100644 --- a/test/mjsunit/test-http-tls.js +++ b/test/mjsunit/test-http-tls.js @@ -64,7 +64,7 @@ http_server.listen(PORT); var client = http.createClient(PORT, HOST); client.setSecure("x509_PEM", caPem, 0, keyPem, certPem); -var req = client.get("/hello", {"Accept": "*/*", "Foo": "bar"}); +var req = client.request("/hello", {"Accept": "*/*", "Foo": "bar"}); req.finish(function (res) { var verified = res.connection.verifyPeer(); var peerDN = res.connection.getPeerCertificate("DNstring"); @@ -79,7 +79,7 @@ req.finish(function (res) { }); setTimeout(function () { - req = client.post("/world"); + req = client.request("POST", "/world"); req.finish(function (res) { var verified = res.connection.verifyPeer(); var peerDN = res.connection.getPeerCertificate("DNstring"); diff --git a/test/mjsunit/test-http.js b/test/mjsunit/test-http.js index e41175f..ae67cb8 100644 --- a/test/mjsunit/test-http.js +++ b/test/mjsunit/test-http.js @@ -37,7 +37,7 @@ http.createServer(function (req, res) { }).listen(PORT); var client = http.createClient(PORT); -var req = client.get("/hello", {"Accept": "*/*", "Foo": "bar"}); +var req = client.request("/hello", {"Accept": "*/*", "Foo": "bar"}); req.finish(function (res) { assert.equal(200, res.statusCode); responses_recvd += 1; @@ -47,7 +47,7 @@ req.finish(function (res) { }); setTimeout(function () { - req = client.post("/world"); + req = client.request("POST", "/world"); req.finish(function (res) { assert.equal(200, res.statusCode); responses_recvd += 1; diff --git a/test/mjsunit/test-multipart.js b/test/mjsunit/test-multipart.js index ee41a66..ea80232 100644 --- a/test/mjsunit/test-multipart.js +++ b/test/mjsunit/test-multipart.js @@ -48,7 +48,7 @@ var server = http.createServer(function(req, res) { server.listen(port); var client = http.createClient(port); -var request = client.post('/', {'Content-Type': 'multipart/form-data; boundary=AaB03x', 'Content-Length': fixture.reply.length}); +var request = client.request('POST', '/', {'Content-Type': 'multipart/form-data; boundary=AaB03x', 'Content-Length': fixture.reply.length}); request.sendBody(fixture.reply, 'binary'); request.finish(); -- 2.7.4