summaryrefslogtreecommitdiff
path: root/lib/dojo/request/node.js.uncompressed.js
blob: 8c44b1988bab11eabe51e6615ac24f4bd9c0158d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
define("dojo/request/node", [
   'require',
   './util',
   './handlers',
   '../errors/RequestTimeoutError',
   '../node!http',
   '../node!https',
   '../node!url',
   '../node!stream'/*=====,
	'../request',
	'../_base/declare' =====*/
], function(require, util, handlers, RequestTimeoutError, http, https, URL, stream/*=====, request, declare =====*/){
	var Stream = stream.Stream,
		undefined;

	var defaultOptions = {
		method: 'GET',
		query: null,
		data: undefined,
		headers: {}
	};
	function node(url, options){
		var response = util.parseArgs(url, util.deepCreate(defaultOptions, options), options && options.data instanceof Stream);
		url = response.url;
		options = response.options;

		var def = util.deferred(
			response,
			function(dfd, response){
				response.clientRequest.abort();
			}
		);

		url = URL.parse(url);

		var reqOptions = response.requestOptions = {
			hostname: url.hostname,
			port: url.port,
			socketPath: options.socketPath,
			method: options.method,
			headers: options.headers,
			agent: options.agent,
			pfx: options.pfx,
			key: options.key,
			passphrase: options.passphrase,
			cert: options.cert,
			ca: options.ca,
			ciphers: options.ciphers,
			rejectUnauthorized: options.rejectUnauthorized === false ? false : true
		};
		if(url.path){
			reqOptions.path = url.path;
		}
		if(options.user || options.password){
			reqOptions.auth = (options.user||'') + ':' + (options.password||'');
		}
		var req = response.clientRequest = (url.protocol === 'https:' ? https : http).request(reqOptions);

		if(options.socketOptions){
			if('timeout' in options.socketOptions){
				req.setTimeout(options.socketOptions.timeout);
			}
			if('noDelay' in options.socketOptions){
				req.setNoDelay(options.socketOptions.noDelay);
			}
			if('keepAlive' in options.socketOptions){
				var initialDelay = options.socketOptions.keepAlive;
				req.setKeepAlive(initialDelay >= 0, initialDelay || 0);
			}
		}

		req.on('socket', function(){
			response.hasSocket = true;
			def.progress(response);
		});

		req.on('response', function(clientResponse){
			response.clientResponse = clientResponse;
			response.status = clientResponse.statusCode;
			response.getHeader = function(headerName){
				return clientResponse.headers[headerName.toLowerCase()] || null;
			};

			var body = [];
			clientResponse.on('data', function(chunk){
				body.push(chunk);

				// TODO: progress updates via the deferred
			});
			clientResponse.on('end', function(){
				if(timeout){
					clearTimeout(timeout);
				}
				response.text = body.join('');
				handlers(response);
				def.resolve(response);
			});
		});

		req.on('error', def.reject);

		if(options.data){
			if(typeof options.data === "string"){
				req.end(options.data);
			}else{
				options.data.pipe(req);
			}
		}else{
			req.end();
		}

		if(options.timeout){
			var timeout = setTimeout(function(){
				def.cancel(new RequestTimeoutError(response));
			}, options.timeout);
		}

		return def.promise;
	}

	/*=====
	node = function(url, options){
		// summary:
		//		Sends a request using the included http or https interface from node.js
		//		with the given URL and options.
		// url: String
		//		URL to request
		// options: dojo/request/node.__Options?
		//		Options for the request.
		// returns: dojo/request.__Promise
	};
	node.__BaseOptions = declare(request.__BaseOptions, {
		// data: String|Object|Stream?
		//		Data to transfer. This is ignored for GET and DELETE
		//		requests.
		// headers: Object?
		//		Headers to use for the request.
		// user: String?
		//		Username to use during the request.
		// password: String?
		//		Password to use during the request.
	});
	node.__MethodOptions = declare(null, {
		// method: String?
		//		The HTTP method to use to make the request. Must be
		//		uppercase. Default is `"GET"`.
	});
	node.__Options = declare([node.__BaseOptions, node.__MethodOptions]);

	node.get = function(url, options){
		// summary:
		//		Send an HTTP GET request using XMLHttpRequest with the given URL and options.
		// url: String
		//		URL to request
		// options: dojo/request/node.__BaseOptions?
		//		Options for the request.
		// returns: dojo/request.__Promise
	};
	node.post = function(url, options){
		// summary:
		//		Send an HTTP POST request using XMLHttpRequest with the given URL and options.
		// url: String
		//		URL to request
		// options: dojo/request/node.__BaseOptions?
		//		Options for the request.
		// returns: dojo/request.__Promise
	};
	node.put = function(url, options){
		// summary:
		//		Send an HTTP PUT request using XMLHttpRequest with the given URL and options.
		// url: String
		//		URL to request
		// options: dojo/request/node.__BaseOptions?
		//		Options for the request.
		// returns: dojo/request.__Promise
	};
	node.del = function(url, options){
		// summary:
		//		Send an HTTP DELETE request using XMLHttpRequest with the given URL and options.
		// url: String
		//		URL to request
		// options: dojo/request/node.__BaseOptions?
		//		Options for the request.
		// returns: dojo/request.__Promise
	};
	=====*/

	util.addCommonMethods(node);

	return node;
});