f9011648fb69a7aa2616c3d5c3a2f4b142a257f6
[platform/upstream/nodejs.git] / lib / https.js
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 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');
28
29 function Server(opts, requestListener) {
30   if (!(this instanceof Server)) return new Server(opts, requestListener);
31
32   if (process.features.tls_npn && !opts.NPNProtocols) {
33     opts.NPNProtocols = ['http/1.1', 'http/1.0'];
34   }
35
36   tls.Server.call(this, opts, http._connectionListener);
37
38   this.httpAllowHalfOpen = false;
39
40   if (requestListener) {
41     this.addListener('request', requestListener);
42   }
43
44   this.addListener('clientError', function(err, conn) {
45     conn.destroy();
46   });
47
48   this.timeout = 2 * 60 * 1000;
49 }
50 inherits(Server, tls.Server);
51 exports.Server = Server;
52
53 Server.prototype.setTimeout = http.Server.prototype.setTimeout;
54
55 exports.createServer = function(opts, requestListener) {
56   return new Server(opts, requestListener);
57 };
58
59
60 // HTTPS agents.
61
62 function createConnection(port, host, options) {
63   if (util.isObject(port)) {
64     options = port;
65   } else if (util.isObject(host)) {
66     options = host;
67   } else if (util.isObject(options)) {
68     options = options;
69   } else {
70     options = {};
71   }
72
73   if (util.isNumber(port)) {
74     options.port = port;
75   }
76
77   if (util.isString(host)) {
78     options.host = host;
79   }
80
81   debug('createConnection', options);
82   return tls.connect(options);
83 }
84
85
86 function Agent(options) {
87   http.Agent.call(this, options);
88   this.defaultPort = 443;
89   this.protocol = 'https:';
90 }
91 inherits(Agent, http.Agent);
92 Agent.prototype.createConnection = createConnection;
93
94 Agent.prototype.getName = function(options) {
95   var name = http.Agent.prototype.getName.call(this, options);
96
97   name += ':';
98   if (options.ca)
99     name += options.ca;
100
101   name += ':';
102   if (options.cert)
103     name += options.cert;
104
105   name += ':';
106   if (options.ciphers)
107     name += options.ciphers;
108
109   name += ':';
110   if (options.key)
111     name += options.key;
112
113   name += ':';
114   if (options.pfx)
115     name += options.pfx;
116
117   name += ':';
118   if (!util.isUndefined(options.rejectUnauthorized))
119     name += options.rejectUnauthorized;
120
121   return name;
122 };
123
124 var globalAgent = new Agent();
125
126 exports.globalAgent = globalAgent;
127 exports.Agent = Agent;
128
129 exports.request = function(options, cb) {
130   if (util.isString(options)) {
131     options = url.parse(options);
132   } else {
133     options = util._extend({}, options);
134   }
135   options._defaultAgent = globalAgent;
136   return http.request(options, cb);
137 };
138
139 exports.get = function(options, cb) {
140   var req = exports.request(options, cb);
141   req.end();
142   return req;
143 };