mirror of https://github.com/nodejs/node.git
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().pull/22966/head
parent
c6affb64f9
commit
f8ba9c3bc9
|
@ -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++;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
39
lib/http.js
39
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();
|
||||
|
|
|
@ -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(); });
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
Loading…
Reference in New Issue