You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

650 lines
18 KiB

7 years ago
  1. /**
  2. * Wrapper for built-in http.js to emulate the browser XMLHttpRequest object.
  3. *
  4. * This can be used with JS designed for browsers to improve reuse of code and
  5. * allow the use of existing libraries.
  6. *
  7. * Usage: include("XMLHttpRequest.js") and use XMLHttpRequest per W3C specs.
  8. *
  9. * @author Dan DeFelippi <dan@driverdan.com>
  10. * @contributor David Ellis <d.f.ellis@ieee.org>
  11. * @license MIT
  12. */
  13. var fs = require('fs');
  14. var Url = require('url');
  15. var spawn = require('child_process').spawn;
  16. /**
  17. * Module exports.
  18. */
  19. module.exports = XMLHttpRequest;
  20. // backwards-compat
  21. XMLHttpRequest.XMLHttpRequest = XMLHttpRequest;
  22. /**
  23. * `XMLHttpRequest` constructor.
  24. *
  25. * Supported options for the `opts` object are:
  26. *
  27. * - `agent`: An http.Agent instance; http.globalAgent may be used; if 'undefined', agent usage is disabled
  28. *
  29. * @param {Object} opts optional "options" object
  30. */
  31. function XMLHttpRequest(opts) {
  32. "use strict";
  33. /**
  34. * Private variables
  35. */
  36. var self = this;
  37. var http = require('http');
  38. var https = require('https');
  39. // Holds http.js objects
  40. var request;
  41. var response;
  42. // Request settings
  43. var settings = {};
  44. // Disable header blacklist.
  45. // Not part of XHR specs.
  46. var disableHeaderCheck = false;
  47. // Set some default headers
  48. var defaultHeaders = {
  49. "User-Agent": "node-XMLHttpRequest",
  50. "Accept": "*/*"
  51. };
  52. var headers = defaultHeaders;
  53. // These headers are not user setable.
  54. // The following are allowed but banned in the spec:
  55. // * user-agent
  56. var forbiddenRequestHeaders = [
  57. "accept-charset",
  58. "accept-encoding",
  59. "access-control-request-headers",
  60. "access-control-request-method",
  61. "connection",
  62. "content-length",
  63. "content-transfer-encoding",
  64. "cookie",
  65. "cookie2",
  66. "date",
  67. "expect",
  68. "host",
  69. "keep-alive",
  70. "origin",
  71. "referer",
  72. "te",
  73. "trailer",
  74. "transfer-encoding",
  75. "upgrade",
  76. "via"
  77. ];
  78. // These request methods are not allowed
  79. var forbiddenRequestMethods = [
  80. "TRACE",
  81. "TRACK",
  82. "CONNECT"
  83. ];
  84. // Send flag
  85. var sendFlag = false;
  86. // Error flag, used when errors occur or abort is called
  87. var errorFlag = false;
  88. // Event listeners
  89. var listeners = {};
  90. /**
  91. * Constants
  92. */
  93. this.UNSENT = 0;
  94. this.OPENED = 1;
  95. this.HEADERS_RECEIVED = 2;
  96. this.LOADING = 3;
  97. this.DONE = 4;
  98. /**
  99. * Public vars
  100. */
  101. // Current state
  102. this.readyState = this.UNSENT;
  103. // default ready state change handler in case one is not set or is set late
  104. this.onreadystatechange = null;
  105. // Result & response
  106. this.responseText = "";
  107. this.responseXML = "";
  108. this.status = null;
  109. this.statusText = null;
  110. /**
  111. * Private methods
  112. */
  113. /**
  114. * Check if the specified header is allowed.
  115. *
  116. * @param string header Header to validate
  117. * @return boolean False if not allowed, otherwise true
  118. */
  119. var isAllowedHttpHeader = function(header) {
  120. return disableHeaderCheck || (header && forbiddenRequestHeaders.indexOf(header.toLowerCase()) === -1);
  121. };
  122. /**
  123. * Check if the specified method is allowed.
  124. *
  125. * @param string method Request method to validate
  126. * @return boolean False if not allowed, otherwise true
  127. */
  128. var isAllowedHttpMethod = function(method) {
  129. return (method && forbiddenRequestMethods.indexOf(method) === -1);
  130. };
  131. /**
  132. * Public methods
  133. */
  134. /**
  135. * Open the connection. Currently supports local server requests.
  136. *
  137. * @param string method Connection method (eg GET, POST)
  138. * @param string url URL for the connection.
  139. * @param boolean async Asynchronous connection. Default is true.
  140. * @param string user Username for basic authentication (optional)
  141. * @param string password Password for basic authentication (optional)
  142. */
  143. this.open = function(method, url, async, user, password) {
  144. this.abort();
  145. errorFlag = false;
  146. // Check for valid request method
  147. if (!isAllowedHttpMethod(method)) {
  148. throw "SecurityError: Request method not allowed";
  149. }
  150. settings = {
  151. "method": method,
  152. "url": url.toString(),
  153. "async": (typeof async !== "boolean" ? true : async),
  154. "user": user || null,
  155. "password": password || null
  156. };
  157. setState(this.OPENED);
  158. };
  159. /**
  160. * Disables or enables isAllowedHttpHeader() check the request. Enabled by default.
  161. * This does not conform to the W3C spec.
  162. *
  163. * @param boolean state Enable or disable header checking.
  164. */
  165. this.setDisableHeaderCheck = function(state) {
  166. disableHeaderCheck = state;
  167. };
  168. /**
  169. * Sets a header for the request.
  170. *
  171. * @param string header Header name
  172. * @param string value Header value
  173. * @return boolean Header added
  174. */
  175. this.setRequestHeader = function(header, value) {
  176. if (this.readyState != this.OPENED) {
  177. throw "INVALID_STATE_ERR: setRequestHeader can only be called when state is OPEN";
  178. return false;
  179. }
  180. if (!isAllowedHttpHeader(header)) {
  181. console.warn('Refused to set unsafe header "' + header + '"');
  182. return false;
  183. }
  184. if (sendFlag) {
  185. throw "INVALID_STATE_ERR: send flag is true";
  186. return false;
  187. }
  188. headers[header] = value;
  189. return true;
  190. };
  191. /**
  192. * Gets a header from the server response.
  193. *
  194. * @param string header Name of header to get.
  195. * @return string Text of the header or null if it doesn't exist.
  196. */
  197. this.getResponseHeader = function(header) {
  198. if (typeof header === "string"
  199. && this.readyState > this.OPENED
  200. && response.headers[header.toLowerCase()]
  201. && !errorFlag
  202. ) {
  203. return response.headers[header.toLowerCase()];
  204. }
  205. return null;
  206. };
  207. /**
  208. * Gets all the response headers.
  209. *
  210. * @return string A string with all response headers separated by CR+LF
  211. */
  212. this.getAllResponseHeaders = function() {
  213. if (this.readyState < this.HEADERS_RECEIVED || errorFlag) {
  214. return "";
  215. }
  216. var result = "";
  217. for (var i in response.headers) {
  218. // Cookie headers are excluded
  219. if (i !== "set-cookie" && i !== "set-cookie2") {
  220. result += i + ": " + response.headers[i] + "\r\n";
  221. }
  222. }
  223. return result.substr(0, result.length - 2);
  224. };
  225. /**
  226. * Gets a request header
  227. *
  228. * @param string name Name of header to get
  229. * @return string Returns the request header or empty string if not set
  230. */
  231. this.getRequestHeader = function(name) {
  232. // @TODO Make this case insensitive
  233. if (typeof name === "string" && headers[name]) {
  234. return headers[name];
  235. }
  236. return "";
  237. };
  238. /**
  239. * Sends the request to the server.
  240. *
  241. * @param string data Optional data to send as request body.
  242. */
  243. this.send = function(data) {
  244. if (this.readyState != this.OPENED) {
  245. throw "INVALID_STATE_ERR: connection must be opened before send() is called";
  246. }
  247. if (sendFlag) {
  248. throw "INVALID_STATE_ERR: send has already been called";
  249. }
  250. var ssl = false, local = false;
  251. var url = Url.parse(settings.url);
  252. var host;
  253. // Determine the server
  254. switch (url.protocol) {
  255. case 'https:':
  256. ssl = true;
  257. // SSL & non-SSL both need host, no break here.
  258. case 'http:':
  259. host = url.hostname;
  260. break;
  261. case 'file:':
  262. local = true;
  263. break;
  264. case undefined:
  265. case '':
  266. host = "localhost";
  267. break;
  268. default:
  269. throw "Protocol not supported.";
  270. }
  271. // Load files off the local filesystem (file://)
  272. if (local) {
  273. if (settings.method !== "GET") {
  274. throw "XMLHttpRequest: Only GET method is supported";
  275. }
  276. if (settings.async) {
  277. fs.readFile(url.pathname, 'utf8', function(error, data) {
  278. if (error) {
  279. self.handleError(error);
  280. } else {
  281. self.status = 200;
  282. self.responseText = data;
  283. setState(self.DONE);
  284. }
  285. });
  286. } else {
  287. try {
  288. this.responseText = fs.readFileSync(url.pathname, 'utf8');
  289. this.status = 200;
  290. setState(self.DONE);
  291. } catch(e) {
  292. this.handleError(e);
  293. }
  294. }
  295. return;
  296. }
  297. // Default to port 80. If accessing localhost on another port be sure
  298. // to use http://localhost:port/path
  299. var port = url.port || (ssl ? 443 : 80);
  300. // Add query string if one is used
  301. var uri = url.pathname + (url.search ? url.search : '');
  302. // Set the Host header or the server may reject the request
  303. headers["Host"] = host;
  304. if (!((ssl && port === 443) || port === 80)) {
  305. headers["Host"] += ':' + url.port;
  306. }
  307. // Set Basic Auth if necessary
  308. if (settings.user) {
  309. if (typeof settings.password == "undefined") {
  310. settings.password = "";
  311. }
  312. var authBuf = new Buffer(settings.user + ":" + settings.password);
  313. headers["Authorization"] = "Basic " + authBuf.toString("base64");
  314. }
  315. // Set content length header
  316. if (settings.method === "GET" || settings.method === "HEAD") {
  317. data = null;
  318. } else if (data) {
  319. headers["Content-Length"] = Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data);
  320. if (!headers["Content-Type"]) {
  321. headers["Content-Type"] = "text/plain;charset=UTF-8";
  322. }
  323. } else if (settings.method === "POST") {
  324. // For a post with no data set Content-Length: 0.
  325. // This is required by buggy servers that don't meet the specs.
  326. headers["Content-Length"] = 0;
  327. }
  328. var agent = false;
  329. if (opts && opts.agent) {
  330. agent = opts.agent;
  331. }
  332. var options = {
  333. host: host,
  334. port: port,
  335. path: uri,
  336. method: settings.method,
  337. headers: headers,
  338. agent: agent
  339. };
  340. if (ssl) {
  341. options.pfx = opts.pfx;
  342. options.key = opts.key;
  343. options.passphrase = opts.passphrase;
  344. options.cert = opts.cert;
  345. options.ca = opts.ca;
  346. options.ciphers = opts.ciphers;
  347. options.rejectUnauthorized = opts.rejectUnauthorized;
  348. }
  349. // Reset error flag
  350. errorFlag = false;
  351. // Handle async requests
  352. if (settings.async) {
  353. // Use the proper protocol
  354. var doRequest = ssl ? https.request : http.request;
  355. // Request is being sent, set send flag
  356. sendFlag = true;
  357. // As per spec, this is called here for historical reasons.
  358. self.dispatchEvent("readystatechange");
  359. // Handler for the response
  360. var responseHandler = function(resp) {
  361. // Set response var to the response we got back
  362. // This is so it remains accessable outside this scope
  363. response = resp;
  364. // Check for redirect
  365. // @TODO Prevent looped redirects
  366. if (response.statusCode === 302 || response.statusCode === 303 || response.statusCode === 307) {
  367. // Change URL to the redirect location
  368. settings.url = response.headers.location;
  369. var url = Url.parse(settings.url);
  370. // Set host var in case it's used later
  371. host = url.hostname;
  372. // Options for the new request
  373. var newOptions = {
  374. hostname: url.hostname,
  375. port: url.port,
  376. path: url.path,
  377. method: response.statusCode === 303 ? 'GET' : settings.method,
  378. headers: headers
  379. };
  380. if (ssl) {
  381. newOptions.pfx = opts.pfx;
  382. newOptions.key = opts.key;
  383. newOptions.passphrase = opts.passphrase;
  384. newOptions.cert = opts.cert;
  385. newOptions.ca = opts.ca;
  386. newOptions.ciphers = opts.ciphers;
  387. newOptions.rejectUnauthorized = opts.rejectUnauthorized;
  388. }
  389. // Issue the new request
  390. request = doRequest(newOptions, responseHandler).on('error', errorHandler);
  391. request.end();
  392. // @TODO Check if an XHR event needs to be fired here
  393. return;
  394. }
  395. if (response && response.setEncoding) {
  396. response.setEncoding("utf8");
  397. }
  398. setState(self.HEADERS_RECEIVED);
  399. self.status = response.statusCode;
  400. response.on('data', function(chunk) {
  401. // Make sure there's some data
  402. if (chunk) {
  403. self.responseText += chunk;
  404. }
  405. // Don't emit state changes if the connection has been aborted.
  406. if (sendFlag) {
  407. setState(self.LOADING);
  408. }
  409. });
  410. response.on('end', function() {
  411. if (sendFlag) {
  412. // Discard the 'end' event if the connection has been aborted
  413. setState(self.DONE);
  414. sendFlag = false;
  415. }
  416. });
  417. response.on('error', function(error) {
  418. self.handleError(error);
  419. });
  420. }
  421. // Error handler for the request
  422. var errorHandler = function(error) {
  423. self.handleError(error);
  424. }
  425. // Create the request
  426. request = doRequest(options, responseHandler).on('error', errorHandler);
  427. // Node 0.4 and later won't accept empty data. Make sure it's needed.
  428. if (data) {
  429. request.write(data);
  430. }
  431. request.end();
  432. self.dispatchEvent("loadstart");
  433. } else { // Synchronous
  434. // Create a temporary file for communication with the other Node process
  435. var contentFile = ".node-xmlhttprequest-content-" + process.pid;
  436. var syncFile = ".node-xmlhttprequest-sync-" + process.pid;
  437. fs.writeFileSync(syncFile, "", "utf8");
  438. // The async request the other Node process executes
  439. var execString = "var http = require('http'), https = require('https'), fs = require('fs');"
  440. + "var doRequest = http" + (ssl ? "s" : "") + ".request;"
  441. + "var options = " + JSON.stringify(options) + ";"
  442. + "var responseText = '';"
  443. + "var req = doRequest(options, function(response) {"
  444. + "response.setEncoding('utf8');"
  445. + "response.on('data', function(chunk) {"
  446. + " responseText += chunk;"
  447. + "});"
  448. + "response.on('end', function() {"
  449. + "fs.writeFileSync('" + contentFile + "', 'NODE-XMLHTTPREQUEST-STATUS:' + response.statusCode + ',' + responseText, 'utf8');"
  450. + "fs.unlinkSync('" + syncFile + "');"
  451. + "});"
  452. + "response.on('error', function(error) {"
  453. + "fs.writeFileSync('" + contentFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
  454. + "fs.unlinkSync('" + syncFile + "');"
  455. + "});"
  456. + "}).on('error', function(error) {"
  457. + "fs.writeFileSync('" + contentFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
  458. + "fs.unlinkSync('" + syncFile + "');"
  459. + "});"
  460. + (data ? "req.write('" + data.replace(/'/g, "\\'") + "');":"")
  461. + "req.end();";
  462. // Start the other Node Process, executing this string
  463. var syncProc = spawn(process.argv[0], ["-e", execString]);
  464. var statusText;
  465. while(fs.existsSync(syncFile)) {
  466. // Wait while the sync file is empty
  467. }
  468. self.responseText = fs.readFileSync(contentFile, 'utf8');
  469. // Kill the child process once the file has data
  470. syncProc.stdin.end();
  471. // Remove the temporary file
  472. fs.unlinkSync(contentFile);
  473. if (self.responseText.match(/^NODE-XMLHTTPREQUEST-ERROR:/)) {
  474. // If the file returned an error, handle it
  475. var errorObj = self.responseText.replace(/^NODE-XMLHTTPREQUEST-ERROR:/, "");
  476. self.handleError(errorObj);
  477. } else {
  478. // If the file returned okay, parse its data and move to the DONE state
  479. self.status = self.responseText.replace(/^NODE-XMLHTTPREQUEST-STATUS:([0-9]*),.*/, "$1");
  480. self.responseText = self.responseText.replace(/^NODE-XMLHTTPREQUEST-STATUS:[0-9]*,(.*)/, "$1");
  481. setState(self.DONE);
  482. }
  483. }
  484. };
  485. /**
  486. * Called when an error is encountered to deal with it.
  487. */
  488. this.handleError = function(error) {
  489. this.status = 503;
  490. this.statusText = error;
  491. this.responseText = error.stack;
  492. errorFlag = true;
  493. setState(this.DONE);
  494. };
  495. /**
  496. * Aborts a request.
  497. */
  498. this.abort = function() {
  499. if (request) {
  500. request.abort();
  501. request = null;
  502. }
  503. headers = defaultHeaders;
  504. this.responseText = "";
  505. this.responseXML = "";
  506. errorFlag = true;
  507. if (this.readyState !== this.UNSENT
  508. && (this.readyState !== this.OPENED || sendFlag)
  509. && this.readyState !== this.DONE) {
  510. sendFlag = false;
  511. setState(this.DONE);
  512. }
  513. this.readyState = this.UNSENT;
  514. };
  515. /**
  516. * Adds an event listener. Preferred method of binding to events.
  517. */
  518. this.addEventListener = function(event, callback) {
  519. if (!(event in listeners)) {
  520. listeners[event] = [];
  521. }
  522. // Currently allows duplicate callbacks. Should it?
  523. listeners[event].push(callback);
  524. };
  525. /**
  526. * Remove an event callback that has already been bound.
  527. * Only works on the matching funciton, cannot be a copy.
  528. */
  529. this.removeEventListener = function(event, callback) {
  530. if (event in listeners) {
  531. // Filter will return a new array with the callback removed
  532. listeners[event] = listeners[event].filter(function(ev) {
  533. return ev !== callback;
  534. });
  535. }
  536. };
  537. /**
  538. * Dispatch any events, including both "on" methods and events attached using addEventListener.
  539. */
  540. this.dispatchEvent = function(event) {
  541. if (typeof self["on" + event] === "function") {
  542. self["on" + event]();
  543. }
  544. if (event in listeners) {
  545. for (var i = 0, len = listeners[event].length; i < len; i++) {
  546. listeners[event][i].call(self);
  547. }
  548. }
  549. };
  550. /**
  551. * Changes readyState and calls onreadystatechange.
  552. *
  553. * @param int state New state
  554. */
  555. var setState = function(state) {
  556. if (self.readyState !== state) {
  557. self.readyState = state;
  558. if (settings.async || self.readyState < self.OPENED || self.readyState === self.DONE) {
  559. self.dispatchEvent("readystatechange");
  560. }
  561. if (self.readyState === self.DONE && !errorFlag) {
  562. self.dispatchEvent("load");
  563. // @TODO figure out InspectorInstrumentation::didLoadXHR(cookie)
  564. self.dispatchEvent("loadend");
  565. }
  566. }
  567. };
  568. };