1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 var tls = require('tls');
23 var url = require('url');
24 var http = require('http');
25 var util = require('util');
26 var inherits = require('util').inherits;
27 var debug = util.debuglog('https');
29 function Server(opts, requestListener) {
30 if (!(this instanceof Server)) return new Server(opts, requestListener);
32 if (process.features.tls_npn && !opts.NPNProtocols) {
33 opts.NPNProtocols = ['http/1.1', 'http/1.0'];
36 tls.Server.call(this, opts, http._connectionListener);
38 this.httpAllowHalfOpen = false;
40 if (requestListener) {
41 this.addListener('request', requestListener);
44 this.addListener('clientError', function(err, conn) {
48 this.timeout = 2 * 60 * 1000;
50 inherits(Server, tls.Server);
51 exports.Server = Server;
53 Server.prototype.setTimeout = http.Server.prototype.setTimeout;
55 exports.createServer = function(opts, requestListener) {
56 return new Server(opts, requestListener);
62 function createConnection(port, host, options) {
63 if (util.isObject(port)) {
65 } else if (util.isObject(host)) {
67 } else if (util.isObject(options)) {
73 if (util.isNumber(port)) {
77 if (util.isString(host)) {
81 debug('createConnection', options);
82 return tls.connect(options);
86 function Agent(options) {
87 http.Agent.call(this, options);
88 this.defaultPort = 443;
89 this.protocol = 'https:';
91 inherits(Agent, http.Agent);
92 Agent.prototype.createConnection = createConnection;
94 Agent.prototype.getName = function(options) {
95 var name = http.Agent.prototype.getName.call(this, options);
103 name += options.cert;
107 name += options.ciphers;
118 if (!util.isUndefined(options.rejectUnauthorized))
119 name += options.rejectUnauthorized;
124 var globalAgent = new Agent();
126 exports.globalAgent = globalAgent;
127 exports.Agent = Agent;
129 exports.request = function(options, cb) {
130 if (util.isString(options)) {
131 options = url.parse(options);
133 options = util._extend({}, options);
135 options._defaultAgent = globalAgent;
136 return http.request(options, cb);
139 exports.get = function(options, cb) {
140 var req = exports.request(options, cb);