2016-03-08 00:15:17 -05:00
|
|
|
import HTTPResponse from './HTTPResponse';
|
|
|
|
|
import querystring from 'querystring';
|
2016-03-26 13:47:44 -04:00
|
|
|
import log from '../logger';
|
2018-09-23 12:31:08 -04:00
|
|
|
import { http, https } from 'follow-redirects';
|
2018-09-24 17:07:51 -04:00
|
|
|
import { parse } from 'url';
|
2016-02-03 12:00:16 -05:00
|
|
|
|
2018-09-23 12:31:08 -04:00
|
|
|
const clients = {
|
|
|
|
|
'http:': http,
|
|
|
|
|
'https:': https,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function makeCallback(resolve, reject) {
|
|
|
|
|
return function(response) {
|
|
|
|
|
const chunks = [];
|
|
|
|
|
response.on('data', chunk => {
|
|
|
|
|
chunks.push(chunk);
|
|
|
|
|
});
|
|
|
|
|
response.on('end', () => {
|
|
|
|
|
const body = Buffer.concat(chunks);
|
|
|
|
|
const httpResponse = new HTTPResponse(response, body);
|
|
|
|
|
|
|
|
|
|
// Consider <200 && >= 400 as errors
|
|
|
|
|
if (httpResponse.status < 200 || httpResponse.status >= 400) {
|
|
|
|
|
return reject(httpResponse);
|
|
|
|
|
} else {
|
|
|
|
|
return resolve(httpResponse);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
response.on('error', reject);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const encodeBody = function({ body, headers = {} }) {
|
2016-02-23 08:17:48 -05:00
|
|
|
if (typeof body !== 'object') {
|
2018-09-01 13:58:06 -04:00
|
|
|
return { body, headers };
|
2016-02-23 08:17:48 -05:00
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
var contentTypeKeys = Object.keys(headers).filter(key => {
|
2016-02-23 08:17:48 -05:00
|
|
|
return key.match(/content-type/i) != null;
|
|
|
|
|
});
|
|
|
|
|
|
2016-03-07 11:06:56 -05:00
|
|
|
if (contentTypeKeys.length == 0) {
|
|
|
|
|
// no content type
|
2016-03-08 08:08:48 -05:00
|
|
|
// As per https://parse.com/docs/cloudcode/guide#cloud-code-advanced-sending-a-post-request the default encoding is supposedly x-www-form-urlencoded
|
2016-03-26 13:47:44 -04:00
|
|
|
|
2016-03-08 08:08:48 -05:00
|
|
|
body = querystring.stringify(body);
|
|
|
|
|
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
2016-03-08 00:15:17 -05:00
|
|
|
} else {
|
|
|
|
|
/* istanbul ignore next */
|
|
|
|
|
if (contentTypeKeys.length > 1) {
|
2018-09-01 13:58:06 -04:00
|
|
|
log.error(
|
|
|
|
|
'Parse.Cloud.httpRequest',
|
|
|
|
|
'multiple content-type headers are set.'
|
|
|
|
|
);
|
2016-03-08 00:15:17 -05:00
|
|
|
}
|
|
|
|
|
// There maybe many, we'll just take the 1st one
|
2016-02-23 08:17:48 -05:00
|
|
|
var contentType = contentTypeKeys[0];
|
|
|
|
|
if (headers[contentType].match(/application\/json/i)) {
|
2016-03-08 00:15:17 -05:00
|
|
|
body = JSON.stringify(body);
|
2018-09-01 13:58:06 -04:00
|
|
|
} else if (
|
|
|
|
|
headers[contentType].match(/application\/x-www-form-urlencoded/i)
|
|
|
|
|
) {
|
2016-03-08 08:08:48 -05:00
|
|
|
body = querystring.stringify(body);
|
2016-02-23 08:17:48 -05:00
|
|
|
}
|
|
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
return { body, headers };
|
|
|
|
|
};
|
2016-02-23 08:17:48 -05:00
|
|
|
|
2018-08-09 16:20:13 -04:00
|
|
|
/**
|
|
|
|
|
* Makes an HTTP Request.
|
|
|
|
|
*
|
|
|
|
|
* **Available in Cloud Code only.**
|
|
|
|
|
*
|
|
|
|
|
* By default, Parse.Cloud.httpRequest does not follow redirects caused by HTTP 3xx response codes. You can use the followRedirects option in the {@link Parse.Cloud.HTTPOptions} object to change this behavior.
|
|
|
|
|
*
|
|
|
|
|
* Sample request:
|
|
|
|
|
* ```
|
|
|
|
|
* Parse.Cloud.httpRequest({
|
|
|
|
|
* url: 'http://www.parse.com/'
|
|
|
|
|
* }).then(function(httpResponse) {
|
|
|
|
|
* // success
|
|
|
|
|
* console.log(httpResponse.text);
|
|
|
|
|
* },function(httpResponse) {
|
|
|
|
|
* // error
|
|
|
|
|
* console.error('Request failed with response code ' + httpResponse.status);
|
|
|
|
|
* });
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* @method httpRequest
|
|
|
|
|
* @name Parse.Cloud.httpRequest
|
|
|
|
|
* @param {Parse.Cloud.HTTPOptions} options The Parse.Cloud.HTTPOptions object that makes the request.
|
|
|
|
|
* @return {Promise<Parse.Cloud.HTTPResponse>} A promise that will be resolved with a {@link Parse.Cloud.HTTPResponse} object when the request completes.
|
|
|
|
|
*/
|
2018-09-23 12:31:08 -04:00
|
|
|
module.exports = function httpRequest(options) {
|
|
|
|
|
let url;
|
|
|
|
|
try {
|
2018-09-24 17:07:51 -04:00
|
|
|
url = parse(options.url);
|
2018-09-23 12:31:08 -04:00
|
|
|
} catch (e) {
|
|
|
|
|
return Promise.reject(e);
|
|
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
options = Object.assign(options, encodeBody(options));
|
2016-03-08 20:23:55 +08:00
|
|
|
// support params options
|
|
|
|
|
if (typeof options.params === 'object') {
|
|
|
|
|
options.qs = options.params;
|
|
|
|
|
} else if (typeof options.params === 'string') {
|
|
|
|
|
options.qs = querystring.parse(options.params);
|
|
|
|
|
}
|
2018-09-23 12:31:08 -04:00
|
|
|
const client = clients[url.protocol];
|
|
|
|
|
if (!client) {
|
|
|
|
|
return Promise.reject(`Unsupported protocol ${url.protocol}`);
|
|
|
|
|
}
|
|
|
|
|
const requestOptions = {
|
|
|
|
|
method: options.method,
|
|
|
|
|
port: Number(url.port),
|
|
|
|
|
path: url.pathname,
|
|
|
|
|
hostname: url.hostname,
|
|
|
|
|
headers: options.headers,
|
|
|
|
|
encoding: null,
|
|
|
|
|
followRedirects: options.followRedirects === true,
|
|
|
|
|
};
|
2018-09-24 17:07:51 -04:00
|
|
|
if (requestOptions.headers) {
|
|
|
|
|
Object.keys(requestOptions.headers).forEach(key => {
|
|
|
|
|
if (typeof requestOptions.headers[key] === 'undefined') {
|
|
|
|
|
delete requestOptions.headers[key];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (url.search) {
|
|
|
|
|
options.qs = Object.assign({}, options.qs, querystring.parse(url.query));
|
|
|
|
|
}
|
|
|
|
|
if (url.auth) {
|
|
|
|
|
requestOptions.auth = url.auth;
|
|
|
|
|
}
|
2018-09-23 12:31:08 -04:00
|
|
|
if (options.qs) {
|
|
|
|
|
requestOptions.path += `?${querystring.stringify(options.qs)}`;
|
|
|
|
|
}
|
|
|
|
|
if (options.agent) {
|
|
|
|
|
requestOptions.agent = options.agent;
|
|
|
|
|
}
|
2018-08-05 13:58:07 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-09-23 12:31:08 -04:00
|
|
|
const req = client.request(
|
|
|
|
|
requestOptions,
|
|
|
|
|
makeCallback(resolve, reject, options)
|
|
|
|
|
);
|
|
|
|
|
if (options.body) {
|
|
|
|
|
req.write(options.body);
|
|
|
|
|
}
|
2018-09-24 17:07:51 -04:00
|
|
|
req.on('error', error => {
|
|
|
|
|
reject(error);
|
|
|
|
|
});
|
2018-09-23 12:31:08 -04:00
|
|
|
req.end();
|
2016-02-03 12:00:16 -05:00
|
|
|
});
|
2016-02-23 08:17:48 -05:00
|
|
|
};
|
|
|
|
|
|
2018-08-09 16:20:13 -04:00
|
|
|
/**
|
|
|
|
|
* @typedef Parse.Cloud.HTTPOptions
|
|
|
|
|
* @property {String|Object} body The body of the request. If it is a JSON object, then the Content-Type set in the headers must be application/x-www-form-urlencoded or application/json. You can also set this to a {@link Buffer} object to send raw bytes. If you use a Buffer, you should also set the Content-Type header explicitly to describe what these bytes represent.
|
|
|
|
|
* @property {function} error The function that is called when the request fails. It will be passed a Parse.Cloud.HTTPResponse object.
|
|
|
|
|
* @property {Boolean} followRedirects Whether to follow redirects caused by HTTP 3xx responses. Defaults to false.
|
|
|
|
|
* @property {Object} headers The headers for the request.
|
|
|
|
|
* @property {String} method The method of the request. GET, POST, PUT, DELETE, HEAD, and OPTIONS are supported. Will default to GET if not specified.
|
|
|
|
|
* @property {String|Object} params The query portion of the url. You can pass a JSON object of key value pairs like params: {q : 'Sean Plott'} or a raw string like params:q=Sean Plott.
|
|
|
|
|
* @property {function} success The function that is called when the request successfully completes. It will be passed a Parse.Cloud.HTTPResponse object.
|
|
|
|
|
* @property {string} url The url to send the request to.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-02-23 08:17:48 -05:00
|
|
|
module.exports.encodeBody = encodeBody;
|