1 define("dojo/request/node", [
5 '../errors/RequestTimeoutError',
9 '../node!stream'/*=====,
11 '../_base/declare' =====*/
12 ], function(require, util, handlers, RequestTimeoutError, http, https, URL, stream/*=====, request, declare =====*/){
13 var Stream = stream.Stream,
16 var defaultOptions = {
22 function node(url, options){
23 var response = util.parseArgs(url, util.deepCreate(defaultOptions, options), options && options.data instanceof Stream);
25 options = response.options;
27 var def = util.deferred(
29 function(dfd, response){
30 response.clientRequest.abort();
36 var reqOptions = response.requestOptions = {
37 hostname: url.hostname,
39 socketPath: options.socketPath,
40 method: options.method,
41 headers: options.headers,
45 passphrase: options.passphrase,
48 ciphers: options.ciphers,
49 rejectUnauthorized: options.rejectUnauthorized === false ? false : true
52 reqOptions.path = url.path;
54 if(options.user || options.password){
55 reqOptions.auth = (options.user||'') + ':' + (options.password||'');
57 var req = response.clientRequest = (url.protocol === 'https:' ? https : http).request(reqOptions);
59 if(options.socketOptions){
60 if('timeout' in options.socketOptions){
61 req.setTimeout(options.socketOptions.timeout);
63 if('noDelay' in options.socketOptions){
64 req.setNoDelay(options.socketOptions.noDelay);
66 if('keepAlive' in options.socketOptions){
67 var initialDelay = options.socketOptions.keepAlive;
68 req.setKeepAlive(initialDelay >= 0, initialDelay || 0);
72 req.on('socket', function(){
73 response.hasSocket = true;
74 def.progress(response);
77 req.on('response', function(clientResponse){
78 response.clientResponse = clientResponse;
79 response.status = clientResponse.statusCode;
80 response.getHeader = function(headerName){
81 return clientResponse.headers[headerName.toLowerCase()] || null;
85 clientResponse.on('data', function(chunk){
88 // TODO: progress updates via the deferred
90 clientResponse.on('end', function(){
92 clearTimeout(timeout);
94 response.text = body.join('');
96 def.resolve(response);
100 req.on('error', def.reject);
103 if(typeof options.data === "string"){
104 req.end(options.data);
106 options.data.pipe(req);
113 var timeout = setTimeout(function(){
114 def.cancel(new RequestTimeoutError(response));
122 node = function(url, options){
124 // Sends a request using the included http or https interface from node.js
125 // with the given URL and options.
128 // options: dojo/request/node.__Options?
129 // Options for the request.
130 // returns: dojo/request.__Promise
132 node.__BaseOptions = declare(request.__BaseOptions, {
133 // data: String|Object|Stream?
134 // Data to transfer. This is ignored for GET and DELETE
137 // Headers to use for the request.
139 // Username to use during the request.
141 // Password to use during the request.
143 node.__MethodOptions = declare(null, {
145 // The HTTP method to use to make the request. Must be
146 // uppercase. Default is `"GET"`.
148 node.__Options = declare([node.__BaseOptions, node.__MethodOptions]);
150 node.get = function(url, options){
152 // Send an HTTP GET request using XMLHttpRequest with the given URL and options.
155 // options: dojo/request/node.__BaseOptions?
156 // Options for the request.
157 // returns: dojo/request.__Promise
159 node.post = function(url, options){
161 // Send an HTTP POST request using XMLHttpRequest with the given URL and options.
164 // options: dojo/request/node.__BaseOptions?
165 // Options for the request.
166 // returns: dojo/request.__Promise
168 node.put = function(url, options){
170 // Send an HTTP PUT request using XMLHttpRequest with the given URL and options.
173 // options: dojo/request/node.__BaseOptions?
174 // Options for the request.
175 // returns: dojo/request.__Promise
177 node.del = function(url, options){
179 // Send an HTTP DELETE request using XMLHttpRequest with the given URL and options.
182 // options: dojo/request/node.__BaseOptions?
183 // Options for the request.
184 // returns: dojo/request.__Promise
188 util.addCommonMethods(node);