Skip to content

Commit 005d607

Browse files
seebeeskoichik
authored andcommitted
http.request(url.parse(x))
http2.js protocols object to store defaults for http and https, and use as a switch for supported protocols. options.hostname > options.host > 'localhost' if I have an options.auth element and I do not have an Authorization header, I do basic auth. http.request collapses to new ClientRequest since the defaults are handled by the protocol object test-http-url.parse* Fixes nodejs#1390 Conflicts: lib/http2.js
1 parent be4576d commit 005d607

10 files changed

Lines changed: 453 additions & 3 deletions

lib/http.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,14 +980,15 @@ Agent.prototype.removeSocket = function(s, name, host, port) {
980980
var globalAgent = new Agent();
981981
exports.globalAgent = globalAgent;
982982

983+
983984
function ClientRequest(options, cb) {
984985
var self = this;
985986
OutgoingMessage.call(self);
986987
self.agent = options.agent;
987988
options.defaultPort = options.defaultPort || 80;
988989

989990
options.port = options.port || options.defaultPort;
990-
options.host = options.host || 'localhost';
991+
options.host = options.hostname || options.host || 'localhost';
991992

992993
if (options.setHost === undefined) {
993994
options.setHost = true;
@@ -1018,6 +1019,12 @@ function ClientRequest(options, cb) {
10181019
}
10191020
}
10201021

1022+
if (options.auth && !this.getHeader('Authorization')) {
1023+
//basic auth
1024+
this.setHeader('Authorization', 'Basic ' +
1025+
new Buffer(options.auth).toString('base64'));
1026+
}
1027+
10211028
if (method === 'GET' || method === 'HEAD') {
10221029
self.useChunkedEncodingByDefault = false;
10231030
} else {
@@ -1276,10 +1283,14 @@ ClientRequest.prototype.pause = function() {
12761283

12771284

12781285
exports.request = function(options, cb) {
1286+
if (options.protocol && options.protocol !== 'http:') {
1287+
throw new Error('Protocol:' + options.protocol + ' not supported.');
1288+
}
1289+
12791290
if (options.agent === undefined) {
12801291
options.agent = globalAgent;
12811292
}
1282-
options.defaultPort = options.defaultPort || 80;
1293+
12831294
return new ClientRequest(options, cb);
12841295
};
12851296

lib/https.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,16 @@ exports.globalAgent = globalAgent;
6868
exports.Agent = Agent;
6969

7070
exports.request = function(options, cb) {
71+
if (options.protocol && options.protocol !== 'https:') {
72+
throw new Error('Protocol:' + options.protocol + ' not supported.');
73+
}
74+
7175
if (options.agent === undefined) {
7276
options.agent = globalAgent;
7377
}
7478
options.createConnection = createConnection;
7579
options.defaultPort = options.defaultPort || 443;
76-
return http.request(options, cb);
80+
return new http.ClientRequest(options, cb);
7781
};
7882

7983
exports.get = function(options, cb) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
var https = require('https');
26+
var url = require('url');
27+
28+
var testURL = url.parse('http://asdf:qwer@localhost:' + common.PORT);
29+
// the test here is if you set a specific authorization header in the
30+
// request we should not override that with basic auth
31+
testURL.headers = {
32+
Authorization: 'NoAuthForYOU'
33+
};
34+
35+
function check(request) {
36+
// the correct authorization header is be passed
37+
assert.strictEqual(request.headers.authorization, 'NoAuthForYOU');
38+
}
39+
40+
var server = http.createServer(function(request, response) {
41+
// run the check function
42+
check.call(this, request, response);
43+
response.writeHead(200, {});
44+
response.end('ok');
45+
server.close();
46+
});
47+
48+
server.listen(common.PORT, function () {
49+
// make the request
50+
http.request(testURL).end();
51+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
var https = require('https');
26+
var url = require('url');
27+
28+
var testURL = url.parse('http://asdf:qwer@localhost:' + common.PORT);
29+
30+
function check(request) {
31+
// the correct authorization header is be passed
32+
assert.strictEqual(request.headers.authorization, 'Basic YXNkZjpxd2Vy');
33+
}
34+
35+
var server = http.createServer(function(request, response) {
36+
// run the check function
37+
check.call(this, request, response);
38+
response.writeHead(200, {});
39+
response.end('ok');
40+
server.close();
41+
});
42+
43+
server.listen(common.PORT, function () {
44+
// make the request
45+
http.request(testURL).end();
46+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
var url = require('url');
26+
27+
var testURL = url.parse('http://localhost:' + common.PORT);
28+
29+
// make sure the basics work
30+
function check(request) {
31+
// default method should still be get
32+
assert.strictEqual(request.method, 'GET');
33+
// there are no URL params, so you should not see any
34+
assert.strictEqual(request.url, '/');
35+
// the host header should use the url.parse.hostname
36+
assert.strictEqual(request.headers.host,
37+
testURL.hostname + ':' + testURL.port);
38+
}
39+
40+
var server = http.createServer(function(request, response) {
41+
// run the check function
42+
check.call(this, request, response);
43+
response.writeHead(200, {});
44+
response.end('ok');
45+
server.close();
46+
});
47+
48+
server.listen(common.PORT, function() {
49+
// make the request
50+
var clientRequest = http.request(testURL);
51+
// since there is a little magic with the agent
52+
// make sure that an http request uses the http.Agent
53+
assert.ok(clientRequest.agent instanceof http.Agent);
54+
clientRequest.end();
55+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var https = require('https');
25+
var url = require('url');
26+
var fs = require('fs');
27+
var clientRequest;
28+
29+
// https options
30+
var httpsOptions = {
31+
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
32+
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
33+
};
34+
35+
var testURL = url.parse('https://localhost:' + common.PORT);
36+
37+
function check(request) {
38+
// assert that I'm https
39+
assert.ok(request.socket.encrypted);
40+
}
41+
42+
var server = https.createServer(httpsOptions, function(request, response) {
43+
// run the check function
44+
check.call(this, request, response);
45+
response.writeHead(200, {});
46+
response.end('ok');
47+
server.close();
48+
});
49+
50+
server.listen(common.PORT, function () {
51+
// make the request
52+
var clientRequest = https.request(testURL);
53+
// since there is a little magic with the agent
54+
// make sure that the request uses the https.Agent
55+
assert.ok(clientRequest.agent instanceof https.Agent);
56+
clientRequest.end();
57+
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
var url = require('url');
26+
27+
28+
assert.throws(function() {
29+
http.request(url.parse('file:///whatever'));
30+
}, function(err) {
31+
if (err instanceof Error) {
32+
assert.strictEqual(err.message, 'Protocol:file: not supported.');
33+
return true;
34+
}
35+
});
36+
37+
assert.throws(function() {
38+
http.request(url.parse('mailto:asdf@asdf.com'));
39+
}, function(err) {
40+
if (err instanceof Error) {
41+
assert.strictEqual(err.message, 'Protocol:mailto: not supported.');
42+
return true;
43+
}
44+
});
45+
46+
assert.throws(function() {
47+
http.request(url.parse('ftp://www.example.com'));
48+
}, function(err) {
49+
if (err instanceof Error) {
50+
assert.strictEqual(err.message, 'Protocol:ftp: not supported.');
51+
return true;
52+
}
53+
});
54+
55+
assert.throws(function() {
56+
http.request(url.parse('javascript:alert(\'hello\');'));
57+
}, function(err) {
58+
if (err instanceof Error) {
59+
assert.strictEqual(err.message, 'Protocol:javascript: not supported.');
60+
return true;
61+
}
62+
});
63+
64+
assert.throws(function() {
65+
http.request(url.parse('xmpp:isaacschlueter@jabber.org'));
66+
}, function(err) {
67+
if (err instanceof Error) {
68+
assert.strictEqual(err.message, 'Protocol:xmpp: not supported.');
69+
return true;
70+
}
71+
});
72+
73+
assert.throws(function() {
74+
http.request(url.parse('f://some.host/path'));
75+
}, function(err) {
76+
if (err instanceof Error) {
77+
assert.strictEqual(err.message, 'Protocol:f: not supported.');
78+
return true;
79+
}
80+
});
81+
82+
//TODO do I need to test url.parse(notPrococol.example.com)?

0 commit comments

Comments
 (0)