PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/node_modules/telehash/node_modules/telehash-cs2a/node_modules/node-forge/js/http.js

https://github.com/ImyarekRu/Paradox
JavaScript | 1413 lines | 799 code | 97 blank | 517 comment | 176 complexity | 0983f19e74e1bf57d71aafa3dd3e9ae1 MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, BSD-3-Clause, GPL-2.0, GPL-3.0, LGPL-3.0, LGPL-2.1, MIT, 0BSD, JSON, CC-BY-SA-3.0, MPL-2.0-no-copyleft-exception
  1. /**
  2. * HTTP client-side implementation that uses forge.net sockets.
  3. *
  4. * @author Dave Longley
  5. *
  6. * Copyright (c) 2010-2012 Digital Bazaar, Inc. All rights reserved.
  7. */
  8. (function() {
  9. // define http namespace
  10. var http = {};
  11. // logging category
  12. var cat = 'forge.http';
  13. // add array of clients to debug storage
  14. if(forge.debug) {
  15. forge.debug.set('forge.http', 'clients', []);
  16. }
  17. // normalizes an http header field name
  18. var _normalize = function(name) {
  19. return name.toLowerCase().replace(/(^.)|(-.)/g,
  20. function(a){return a.toUpperCase();});
  21. };
  22. /**
  23. * Gets the local storage ID for the given client.
  24. *
  25. * @param client the client to get the local storage ID for.
  26. *
  27. * @return the local storage ID to use.
  28. */
  29. var _getStorageId = function(client) {
  30. // TODO: include browser in ID to avoid sharing cookies between
  31. // browsers (if this is undesirable)
  32. // navigator.userAgent
  33. return 'forge.http.' +
  34. client.url.scheme + '.' +
  35. client.url.host + '.' +
  36. client.url.port;
  37. };
  38. /**
  39. * Loads persistent cookies from disk for the given client.
  40. *
  41. * @param client the client.
  42. */
  43. var _loadCookies = function(client) {
  44. if(client.persistCookies) {
  45. try {
  46. var cookies = forge.util.getItem(
  47. client.socketPool.flashApi,
  48. _getStorageId(client), 'cookies');
  49. client.cookies = cookies || {};
  50. }
  51. catch(ex) {
  52. // no flash storage available, just silently fail
  53. // TODO: i assume we want this logged somewhere or
  54. // should it actually generate an error
  55. //forge.log.error(cat, ex);
  56. }
  57. }
  58. };
  59. /**
  60. * Saves persistent cookies on disk for the given client.
  61. *
  62. * @param client the client.
  63. */
  64. var _saveCookies = function(client) {
  65. if(client.persistCookies) {
  66. try {
  67. forge.util.setItem(
  68. client.socketPool.flashApi,
  69. _getStorageId(client), 'cookies', client.cookies);
  70. }
  71. catch(ex) {
  72. // no flash storage available, just silently fail
  73. // TODO: i assume we want this logged somewhere or
  74. // should it actually generate an error
  75. //forge.log.error(cat, ex);
  76. }
  77. }
  78. // FIXME: remove me
  79. _loadCookies(client);
  80. };
  81. /**
  82. * Clears persistent cookies on disk for the given client.
  83. *
  84. * @param client the client.
  85. */
  86. var _clearCookies = function(client) {
  87. if(client.persistCookies) {
  88. try {
  89. // only thing stored is 'cookies', so clear whole storage
  90. forge.util.clearItems(
  91. client.socketPool.flashApi,
  92. _getStorageId(client));
  93. }
  94. catch(ex) {
  95. // no flash storage available, just silently fail
  96. // TODO: i assume we want this logged somewhere or
  97. // should it actually generate an error
  98. //forge.log.error(cat, ex);
  99. }
  100. }
  101. };
  102. /**
  103. * Connects and sends a request.
  104. *
  105. * @param client the http client.
  106. * @param socket the socket to use.
  107. */
  108. var _doRequest = function(client, socket) {
  109. if(socket.isConnected()) {
  110. // already connected
  111. socket.options.request.connectTime = +new Date();
  112. socket.connected({
  113. type: 'connect',
  114. id: socket.id
  115. });
  116. }
  117. else {
  118. // connect
  119. socket.options.request.connectTime = +new Date();
  120. socket.connect({
  121. host: client.url.host,
  122. port: client.url.port,
  123. policyPort: client.policyPort,
  124. policyUrl: client.policyUrl
  125. });
  126. }
  127. };
  128. /**
  129. * Handles the next request or marks a socket as idle.
  130. *
  131. * @param client the http client.
  132. * @param socket the socket.
  133. */
  134. var _handleNextRequest = function(client, socket) {
  135. // clear buffer
  136. socket.buffer.clear();
  137. // get pending request
  138. var pending = null;
  139. while(pending === null && client.requests.length > 0) {
  140. pending = client.requests.shift();
  141. if(pending.request.aborted) {
  142. pending = null;
  143. }
  144. }
  145. // mark socket idle if no pending requests
  146. if(pending === null) {
  147. if(socket.options !== null) {
  148. socket.options = null;
  149. }
  150. client.idle.push(socket);
  151. }
  152. // handle pending request
  153. else {
  154. // allow 1 retry
  155. socket.retries = 1;
  156. socket.options = pending;
  157. _doRequest(client, socket);
  158. }
  159. };
  160. /**
  161. * Sets up a socket for use with an http client.
  162. *
  163. * @param client the parent http client.
  164. * @param socket the socket to set up.
  165. * @param tlsOptions if the socket must use TLS, the TLS options.
  166. */
  167. var _initSocket = function(client, socket, tlsOptions) {
  168. // no socket options yet
  169. socket.options = null;
  170. // set up handlers
  171. socket.connected = function(e) {
  172. // socket primed by caching TLS session, handle next request
  173. if(socket.options === null) {
  174. _handleNextRequest(client, socket);
  175. }
  176. // socket in use
  177. else {
  178. var request = socket.options.request;
  179. request.connectTime = +new Date() - request.connectTime;
  180. e.socket = socket;
  181. socket.options.connected(e);
  182. if(request.aborted) {
  183. socket.close();
  184. }
  185. else {
  186. var out = request.toString();
  187. if(request.body) {
  188. out += request.body;
  189. }
  190. request.time = +new Date();
  191. socket.send(out);
  192. request.time = +new Date() - request.time;
  193. socket.options.response.time = +new Date();
  194. socket.sending = true;
  195. }
  196. }
  197. };
  198. socket.closed = function(e) {
  199. if(socket.sending) {
  200. socket.sending = false;
  201. if(socket.retries > 0) {
  202. --socket.retries;
  203. _doRequest(client, socket);
  204. }
  205. else {
  206. // error, closed during send
  207. socket.error({
  208. id: socket.id,
  209. type: 'ioError',
  210. message: 'Connection closed during send. Broken pipe.',
  211. bytesAvailable: 0
  212. });
  213. }
  214. }
  215. else {
  216. // handle unspecified content-length transfer
  217. var response = socket.options.response;
  218. if(response.readBodyUntilClose) {
  219. response.time = +new Date() - response.time;
  220. response.bodyReceived = true;
  221. socket.options.bodyReady({
  222. request: socket.options.request,
  223. response: response,
  224. socket: socket
  225. });
  226. }
  227. socket.options.closed(e);
  228. _handleNextRequest(client, socket);
  229. }
  230. };
  231. socket.data = function(e) {
  232. socket.sending = false;
  233. var request = socket.options.request;
  234. if(request.aborted) {
  235. socket.close();
  236. }
  237. else {
  238. // receive all bytes available
  239. var response = socket.options.response;
  240. var bytes = socket.receive(e.bytesAvailable);
  241. if(bytes !== null) {
  242. // receive header and then body
  243. socket.buffer.putBytes(bytes);
  244. if(!response.headerReceived) {
  245. response.readHeader(socket.buffer);
  246. if(response.headerReceived) {
  247. socket.options.headerReady({
  248. request: socket.options.request,
  249. response: response,
  250. socket: socket
  251. });
  252. }
  253. }
  254. if(response.headerReceived && !response.bodyReceived) {
  255. response.readBody(socket.buffer);
  256. }
  257. if(response.bodyReceived) {
  258. socket.options.bodyReady({
  259. request: socket.options.request,
  260. response: response,
  261. socket: socket
  262. });
  263. // close connection if requested or by default on http/1.0
  264. var value = response.getField('Connection') || '';
  265. if(value.indexOf('close') != -1 ||
  266. (response.version === 'HTTP/1.0' &&
  267. response.getField('Keep-Alive') === null)) {
  268. socket.close();
  269. }
  270. else {
  271. _handleNextRequest(client, socket);
  272. }
  273. }
  274. }
  275. }
  276. };
  277. socket.error = function(e) {
  278. // do error callback, include request
  279. socket.options.error({
  280. type: e.type,
  281. message: e.message,
  282. request: socket.options.request,
  283. response: socket.options.response,
  284. socket: socket
  285. });
  286. socket.close();
  287. };
  288. // wrap socket for TLS
  289. if(tlsOptions) {
  290. socket = forge.tls.wrapSocket({
  291. sessionId: null,
  292. sessionCache: {},
  293. caStore: tlsOptions.caStore,
  294. cipherSuites: tlsOptions.cipherSuites,
  295. socket: socket,
  296. virtualHost: tlsOptions.virtualHost,
  297. verify: tlsOptions.verify,
  298. getCertificate: tlsOptions.getCertificate,
  299. getPrivateKey: tlsOptions.getPrivateKey,
  300. getSignature: tlsOptions.getSignature,
  301. deflate: tlsOptions.deflate || null,
  302. inflate: tlsOptions.inflate || null
  303. });
  304. socket.options = null;
  305. socket.buffer = forge.util.createBuffer();
  306. client.sockets.push(socket);
  307. if(tlsOptions.prime) {
  308. // prime socket by connecting and caching TLS session, will do
  309. // next request from there
  310. socket.connect({
  311. host: client.url.host,
  312. port: client.url.port,
  313. policyPort: client.policyPort,
  314. policyUrl: client.policyUrl
  315. });
  316. }
  317. else {
  318. // do not prime socket, just add as idle
  319. client.idle.push(socket);
  320. }
  321. }
  322. else {
  323. // no need to prime non-TLS sockets
  324. socket.buffer = forge.util.createBuffer();
  325. client.sockets.push(socket);
  326. client.idle.push(socket);
  327. }
  328. };
  329. /**
  330. * Checks to see if the given cookie has expired. If the cookie's max-age
  331. * plus its created time is less than the time now, it has expired, unless
  332. * its max-age is set to -1 which indicates it will never expire.
  333. *
  334. * @param cookie the cookie to check.
  335. *
  336. * @return true if it has expired, false if not.
  337. */
  338. var _hasCookieExpired = function(cookie) {
  339. var rval = false;
  340. if(cookie.maxAge !== -1) {
  341. var now = _getUtcTime(new Date());
  342. var expires = cookie.created + cookie.maxAge;
  343. if(expires <= now) {
  344. rval = true;
  345. }
  346. }
  347. return rval;
  348. };
  349. /**
  350. * Adds cookies in the given client to the given request.
  351. *
  352. * @param client the client.
  353. * @param request the request.
  354. */
  355. var _writeCookies = function(client, request) {
  356. var expired = [];
  357. var url = client.url;
  358. var cookies = client.cookies;
  359. for(var name in cookies) {
  360. // get cookie paths
  361. var paths = cookies[name];
  362. for(var p in paths) {
  363. var cookie = paths[p];
  364. if(_hasCookieExpired(cookie)) {
  365. // store for clean up
  366. expired.push(cookie);
  367. }
  368. // path or path's ancestor must match cookie.path
  369. else if(request.path.indexOf(cookie.path) === 0) {
  370. request.addCookie(cookie);
  371. }
  372. }
  373. }
  374. // clean up expired cookies
  375. for(var i = 0; i < expired.length; ++i) {
  376. var cookie = expired[i];
  377. client.removeCookie(cookie.name, cookie.path);
  378. }
  379. };
  380. /**
  381. * Gets cookies from the given response and adds the to the given client.
  382. *
  383. * @param client the client.
  384. * @param response the response.
  385. */
  386. var _readCookies = function(client, response) {
  387. var cookies = response.getCookies();
  388. for(var i = 0; i < cookies.length; ++i) {
  389. try {
  390. client.setCookie(cookies[i]);
  391. }
  392. catch(ex) {
  393. // ignore failure to add other-domain, etc. cookies
  394. }
  395. }
  396. };
  397. /**
  398. * Creates an http client that uses forge.net sockets as a backend and
  399. * forge.tls for security.
  400. *
  401. * @param options:
  402. * url: the url to connect to (scheme://host:port).
  403. * socketPool: the flash socket pool to use.
  404. * policyPort: the flash policy port to use (if other than the
  405. * socket pool default), use 0 for flash default.
  406. * policyUrl: the flash policy file URL to use (if provided will
  407. * be used instead of a policy port).
  408. * connections: number of connections to use to handle requests.
  409. * caCerts: an array of certificates to trust for TLS, certs may
  410. * be PEM-formatted or cert objects produced via forge.pki.
  411. * cipherSuites: an optional array of cipher suites to use,
  412. * see forge.tls.CipherSuites.
  413. * virtualHost: the virtual server name to use in a TLS SNI
  414. * extension, if not provided the url host will be used.
  415. * verify: a custom TLS certificate verify callback to use.
  416. * getCertificate: an optional callback used to get a client-side
  417. * certificate (see forge.tls for details).
  418. * getPrivateKey: an optional callback used to get a client-side
  419. * private key (see forge.tls for details).
  420. * getSignature: an optional callback used to get a client-side
  421. * signature (see forge.tls for details).
  422. * persistCookies: true to use persistent cookies via flash local
  423. * storage, false to only keep cookies in javascript.
  424. * primeTlsSockets: true to immediately connect TLS sockets on
  425. * their creation so that they will cache TLS sessions for reuse.
  426. *
  427. * @return the client.
  428. */
  429. http.createClient = function(options) {
  430. // create CA store to share with all TLS connections
  431. var caStore = null;
  432. if(options.caCerts) {
  433. caStore = forge.pki.createCaStore(options.caCerts);
  434. }
  435. // get scheme, host, and port from url
  436. options.url = (options.url ||
  437. window.location.protocol + '//' + window.location.host);
  438. var url = http.parseUrl(options.url);
  439. if(!url) {
  440. throw {
  441. message: 'Invalid url.',
  442. details: {url: options.url}
  443. };
  444. }
  445. // default to 1 connection
  446. options.connections = options.connections || 1;
  447. // create client
  448. var sp = options.socketPool;
  449. var client = {
  450. // url
  451. url: url,
  452. // socket pool
  453. socketPool: sp,
  454. // the policy port to use
  455. policyPort: options.policyPort,
  456. // policy url to use
  457. policyUrl: options.policyUrl,
  458. // queue of requests to service
  459. requests: [],
  460. // all sockets
  461. sockets: [],
  462. // idle sockets
  463. idle: [],
  464. // whether or not the connections are secure
  465. secure: (url.scheme === 'https'),
  466. // cookie jar (key'd off of name and then path, there is only 1 domain
  467. // and one setting for secure per client so name+path is unique)
  468. cookies: {},
  469. // default to flash storage of cookies
  470. persistCookies: (typeof(options.persistCookies) === 'undefined') ?
  471. true : options.persistCookies
  472. };
  473. // add client to debug storage
  474. if(forge.debug) {
  475. forge.debug.get('forge.http', 'clients').push(client);
  476. }
  477. // load cookies from disk
  478. _loadCookies(client);
  479. /**
  480. * A default certificate verify function that checks a certificate common
  481. * name against the client's URL host.
  482. *
  483. * @param c the TLS connection.
  484. * @param verified true if cert is verified, otherwise alert number.
  485. * @param depth the chain depth.
  486. * @param certs the cert chain.
  487. *
  488. * @return true if verified and the common name matches the host, error
  489. * otherwise.
  490. */
  491. var _defaultCertificateVerify = function(c, verified, depth, certs) {
  492. if(depth === 0 && verified === true) {
  493. // compare common name to url host
  494. var cn = certs[depth].subject.getField('CN');
  495. if(cn === null || client.url.host !== cn.value) {
  496. verified = {
  497. message: 'Certificate common name does not match url host.'
  498. };
  499. }
  500. }
  501. return verified;
  502. };
  503. // determine if TLS is used
  504. var tlsOptions = null;
  505. if(client.secure) {
  506. tlsOptions = {
  507. caStore: caStore,
  508. cipherSuites: options.cipherSuites || null,
  509. virtualHost: options.virtualHost || url.host,
  510. verify: options.verify || _defaultCertificateVerify,
  511. getCertificate: options.getCertificate || null,
  512. getPrivateKey: options.getPrivateKey || null,
  513. getSignature: options.getSignature || null,
  514. prime: options.primeTlsSockets || false
  515. };
  516. // if socket pool uses a flash api, then add deflate support to TLS
  517. if(sp.flashApi !== null) {
  518. tlsOptions.deflate = function(bytes) {
  519. // strip 2 byte zlib header and 4 byte trailer
  520. return forge.util.deflate(sp.flashApi, bytes, true);
  521. };
  522. tlsOptions.inflate = function(bytes) {
  523. return forge.util.inflate(sp.flashApi, bytes, true);
  524. };
  525. }
  526. }
  527. // create and initialize sockets
  528. for(var i = 0; i < options.connections; ++i) {
  529. _initSocket(client, sp.createSocket(), tlsOptions);
  530. }
  531. /**
  532. * Sends a request. A method 'abort' will be set on the request that
  533. * can be called to attempt to abort the request.
  534. *
  535. * @param options:
  536. * request: the request to send.
  537. * connected: a callback for when the connection is open.
  538. * closed: a callback for when the connection is closed.
  539. * headerReady: a callback for when the response header arrives.
  540. * bodyReady: a callback for when the response body arrives.
  541. * error: a callback for if an error occurs.
  542. */
  543. client.send = function(options) {
  544. // add host header if not set
  545. if(options.request.getField('Host') === null) {
  546. options.request.setField('Host', client.url.fullHost);
  547. }
  548. // set default dummy handlers
  549. var opts = {};
  550. opts.request = options.request;
  551. opts.connected = options.connected || function(){};
  552. opts.closed = options.close || function(){};
  553. opts.headerReady = function(e) {
  554. // read cookies
  555. _readCookies(client, e.response);
  556. if(options.headerReady) {
  557. options.headerReady(e);
  558. }
  559. };
  560. opts.bodyReady = options.bodyReady || function(){};
  561. opts.error = options.error || function(){};
  562. // create response
  563. opts.response = http.createResponse();
  564. opts.response.time = 0;
  565. opts.response.flashApi = client.socketPool.flashApi;
  566. opts.request.flashApi = client.socketPool.flashApi;
  567. // create abort function
  568. opts.request.abort = function() {
  569. // set aborted, clear handlers
  570. opts.request.aborted = true;
  571. opts.connected = function(){};
  572. opts.closed = function(){};
  573. opts.headerReady = function(){};
  574. opts.bodyReady = function(){};
  575. opts.error = function(){};
  576. };
  577. // add cookies to request
  578. _writeCookies(client, opts.request);
  579. // queue request options if there are no idle sockets
  580. if(client.idle.length === 0) {
  581. client.requests.push(opts);
  582. }
  583. // use an idle socket
  584. else {
  585. // prefer an idle *connected* socket first
  586. var socket = null;
  587. var len = client.idle.length;
  588. for(var i = 0; socket === null && i < len; ++i) {
  589. socket = client.idle[i];
  590. if(socket.isConnected()) {
  591. client.idle.splice(i, 1);
  592. }
  593. else {
  594. socket = null;
  595. }
  596. }
  597. // no connected socket available, get unconnected socket
  598. if(socket === null) {
  599. socket = client.idle.pop();
  600. }
  601. socket.options = opts;
  602. _doRequest(client, socket);
  603. }
  604. };
  605. /**
  606. * Destroys this client.
  607. */
  608. client.destroy = function() {
  609. // clear pending requests, close and destroy sockets
  610. client.requests = [];
  611. for(var i = 0; i < client.sockets.length; ++i) {
  612. client.sockets[i].close();
  613. client.sockets[i].destroy();
  614. }
  615. client.socketPool = null;
  616. client.sockets = [];
  617. client.idle = [];
  618. };
  619. /**
  620. * Sets a cookie for use with all connections made by this client. Any
  621. * cookie with the same name will be replaced. If the cookie's value
  622. * is undefined, null, or the blank string, the cookie will be removed.
  623. *
  624. * If the cookie's domain doesn't match this client's url host or the
  625. * cookie's secure flag doesn't match this client's url scheme, then
  626. * setting the cookie will fail with an exception.
  627. *
  628. * @param cookie the cookie with parameters:
  629. * name: the name of the cookie.
  630. * value: the value of the cookie.
  631. * comment: an optional comment string.
  632. * maxAge: the age of the cookie in seconds relative to created time.
  633. * secure: true if the cookie must be sent over a secure protocol.
  634. * httpOnly: true to restrict access to the cookie from javascript
  635. * (inaffective since the cookies are stored in javascript).
  636. * path: the path for the cookie.
  637. * domain: optional domain the cookie belongs to (must start with dot).
  638. * version: optional version of the cookie.
  639. * created: creation time, in UTC seconds, of the cookie.
  640. */
  641. client.setCookie = function(cookie) {
  642. if(typeof(cookie.name) !== 'undefined') {
  643. if(cookie.value === null || typeof(cookie.value) === 'undefined' ||
  644. cookie.value === '') {
  645. // remove cookie
  646. rval = client.removeCookie(cookie.name, cookie.path);
  647. }
  648. else {
  649. // set cookie defaults
  650. cookie.comment = cookie.comment || '';
  651. cookie.maxAge = cookie.maxAge || 0;
  652. cookie.secure = (typeof(cookie.secure) === 'undefined') ?
  653. true : cookie.secure;
  654. cookie.httpOnly = cookie.httpOnly || true;
  655. cookie.path = cookie.path || '/';
  656. cookie.domain = cookie.domain || null;
  657. cookie.version = cookie.version || null;
  658. cookie.created = _getUtcTime(new Date());
  659. // do secure check
  660. if(cookie.secure !== client.secure) {
  661. throw {
  662. message: 'Http client url scheme is incompatible ' +
  663. 'with cookie secure flag.',
  664. url: client.url,
  665. cookie: cookie
  666. };
  667. }
  668. // make sure url host is within cookie.domain
  669. if(!http.withinCookieDomain(client.url, cookie)) {
  670. throw {
  671. message: 'Http client url host is incompatible with ' +
  672. 'cookie domain.',
  673. url: client.url,
  674. cookie: cookie
  675. };
  676. }
  677. // add new cookie
  678. if(!(cookie.name in client.cookies)) {
  679. client.cookies[cookie.name] = {};
  680. }
  681. client.cookies[cookie.name][cookie.path] = cookie;
  682. rval = true;
  683. // save cookies
  684. _saveCookies(client);
  685. }
  686. }
  687. return rval;
  688. };
  689. /**
  690. * Gets a cookie by its name.
  691. *
  692. * @param name the name of the cookie to retrieve.
  693. * @param path an optional path for the cookie (if there are multiple
  694. * cookies with the same name but different paths).
  695. *
  696. * @return the cookie or null if not found.
  697. */
  698. client.getCookie = function(name, path) {
  699. var rval = null;
  700. if(name in client.cookies) {
  701. var paths = client.cookies[name];
  702. // get path-specific cookie
  703. if(path) {
  704. if(path in paths) {
  705. rval = paths[path];
  706. }
  707. }
  708. // get first cookie
  709. else {
  710. for(var p in paths) {
  711. rval = paths[p];
  712. break;
  713. }
  714. }
  715. }
  716. return rval;
  717. };
  718. /**
  719. * Removes a cookie.
  720. *
  721. * @param name the name of the cookie to remove.
  722. * @param path an optional path for the cookie (if there are multiple
  723. * cookies with the same name but different paths).
  724. *
  725. * @return true if a cookie was removed, false if not.
  726. */
  727. client.removeCookie = function(name, path) {
  728. var rval = false;
  729. if(name in client.cookies) {
  730. // delete the specific path
  731. if(path) {
  732. var paths = client.cookies[name];
  733. if(path in paths) {
  734. rval = true;
  735. delete client.cookies[name][path];
  736. // clean up entry if empty
  737. var empty = true;
  738. for(var i in client.cookies[name]) {
  739. empty = false;
  740. break;
  741. }
  742. if(empty) {
  743. delete client.cookies[name];
  744. }
  745. }
  746. }
  747. // delete all cookies with the given name
  748. else {
  749. rval = true;
  750. delete client.cookies[name];
  751. }
  752. }
  753. if(rval) {
  754. // save cookies
  755. _saveCookies(client);
  756. }
  757. return rval;
  758. };
  759. /**
  760. * Clears all cookies stored in this client.
  761. */
  762. client.clearCookies = function() {
  763. client.cookies = {};
  764. _clearCookies(client);
  765. };
  766. if(forge.log) {
  767. forge.log.debug('forge.http', 'created client', options);
  768. }
  769. return client;
  770. };
  771. /**
  772. * Trims the whitespace off of the beginning and end of a string.
  773. *
  774. * @param str the string to trim.
  775. *
  776. * @return the trimmed string.
  777. */
  778. var _trimString = function(str) {
  779. return str.replace(/^\s*/, '').replace(/\s*$/, '');
  780. };
  781. /**
  782. * Creates an http header object.
  783. *
  784. * @return the http header object.
  785. */
  786. var _createHeader = function() {
  787. var header = {
  788. fields: {},
  789. setField: function(name, value) {
  790. // normalize field name, trim value
  791. header.fields[_normalize(name)] = [_trimString('' + value)];
  792. },
  793. appendField: function(name, value) {
  794. name = _normalize(name);
  795. if(!(name in header.fields)) {
  796. header.fields[name] = [];
  797. }
  798. header.fields[name].push(_trimString('' + value));
  799. },
  800. getField: function(name, index) {
  801. var rval = null;
  802. name = _normalize(name);
  803. if(name in header.fields) {
  804. index = index || 0;
  805. rval = header.fields[name][index];
  806. }
  807. return rval;
  808. }
  809. };
  810. return header;
  811. };
  812. /**
  813. * Gets the time in utc seconds given a date.
  814. *
  815. * @param d the date to use.
  816. *
  817. * @return the time in utc seconds.
  818. */
  819. var _getUtcTime = function(d) {
  820. var utc = +d + d.getTimezoneOffset() * 60000;
  821. return Math.floor(+new Date() / 1000);
  822. };
  823. /**
  824. * Creates an http request.
  825. *
  826. * @param options:
  827. * version: the version.
  828. * method: the method.
  829. * path: the path.
  830. * body: the body.
  831. * headers: custom header fields to add,
  832. * eg: [{'Content-Length': 0}].
  833. *
  834. * @return the http request.
  835. */
  836. http.createRequest = function(options) {
  837. options = options || {};
  838. var request = _createHeader();
  839. request.version = options.version || 'HTTP/1.1';
  840. request.method = options.method || null;
  841. request.path = options.path || null;
  842. request.body = options.body || null;
  843. request.bodyDeflated = false;
  844. request.flashApi = null;
  845. // add custom headers
  846. var headers = options.headers || [];
  847. if(!forge.util.isArray(headers)) {
  848. headers = [headers];
  849. }
  850. for(var i = 0; i < headers.length; ++i) {
  851. for(var name in headers[i]) {
  852. request.appendField(name, headers[i][name]);
  853. }
  854. }
  855. /**
  856. * Adds a cookie to the request 'Cookie' header.
  857. *
  858. * @param cookie a cookie to add.
  859. */
  860. request.addCookie = function(cookie) {
  861. var value = '';
  862. var field = request.getField('Cookie');
  863. if(field !== null) {
  864. // separate cookies by semi-colons
  865. value = field + '; ';
  866. }
  867. // get current time in utc seconds
  868. var now = _getUtcTime(new Date());
  869. // output cookie name and value
  870. value += cookie.name + '=' + cookie.value;
  871. request.setField('Cookie', value);
  872. };
  873. /**
  874. * Converts an http request into a string that can be sent as an
  875. * HTTP request. Does not include any data.
  876. *
  877. * @return the string representation of the request.
  878. */
  879. request.toString = function() {
  880. /* Sample request header:
  881. GET /some/path/?query HTTP/1.1
  882. Host: www.someurl.com
  883. Connection: close
  884. Accept-Encoding: deflate
  885. Accept: image/gif, text/html
  886. User-Agent: Mozilla 4.0
  887. */
  888. // set default headers
  889. if(request.getField('User-Agent') === null) {
  890. request.setField('User-Agent', 'forge.http 1.0');
  891. }
  892. if(request.getField('Accept') === null) {
  893. request.setField('Accept', '*/*');
  894. }
  895. if(request.getField('Connection') === null) {
  896. request.setField('Connection', 'keep-alive');
  897. request.setField('Keep-Alive', '115');
  898. }
  899. // add Accept-Encoding if not specified
  900. if(request.flashApi !== null &&
  901. request.getField('Accept-Encoding') === null) {
  902. request.setField('Accept-Encoding', 'deflate');
  903. }
  904. // if the body isn't null, deflate it if its larger than 100 bytes
  905. if(request.flashApi !== null && request.body !== null &&
  906. request.getField('Content-Encoding') === null &&
  907. !request.bodyDeflated && request.body.length > 100) {
  908. // use flash to compress data
  909. request.body = forge.util.deflate(request.flashApi, request.body);
  910. request.bodyDeflated = true;
  911. request.setField('Content-Encoding', 'deflate');
  912. request.setField('Content-Length', request.body.length);
  913. }
  914. else if(request.body !== null) {
  915. // set content length for body
  916. request.setField('Content-Length', request.body.length);
  917. }
  918. // build start line
  919. var rval =
  920. request.method.toUpperCase() + ' ' + request.path + ' ' +
  921. request.version + '\r\n';
  922. // add each header
  923. for(var name in request.fields) {
  924. var fields = request.fields[name];
  925. for(var i = 0; i < fields.length; ++i) {
  926. rval += name + ': ' + fields[i] + '\r\n';
  927. }
  928. }
  929. // final terminating CRLF
  930. rval += '\r\n';
  931. return rval;
  932. };
  933. return request;
  934. };
  935. /**
  936. * Creates an empty http response header.
  937. *
  938. * @return the empty http response header.
  939. */
  940. http.createResponse = function() {
  941. // private vars
  942. var _first = true;
  943. var _chunkSize = 0;
  944. var _chunksFinished = false;
  945. // create response
  946. var response = _createHeader();
  947. response.version = null;
  948. response.code = 0;
  949. response.message = null;
  950. response.body = null;
  951. response.headerReceived = false;
  952. response.bodyReceived = false;
  953. response.flashApi = null;
  954. /**
  955. * Reads a line that ends in CRLF from a byte buffer.
  956. *
  957. * @param b the byte buffer.
  958. *
  959. * @return the line or null if none was found.
  960. */
  961. var _readCrlf = function(b) {
  962. var line = null;
  963. var i = b.data.indexOf('\r\n', b.read);
  964. if(i != -1) {
  965. // read line, skip CRLF
  966. line = b.getBytes(i - b.read);
  967. b.getBytes(2);
  968. }
  969. return line;
  970. };
  971. /**
  972. * Parses a header field and appends it to the response.
  973. *
  974. * @param line the header field line.
  975. */
  976. var _parseHeader = function(line) {
  977. var tmp = line.indexOf(':');
  978. var name = line.substring(0, tmp++);
  979. response.appendField(
  980. name, (tmp < line.length) ? line.substring(tmp) : '');
  981. };
  982. /**
  983. * Reads an http response header from a buffer of bytes.
  984. *
  985. * @param b the byte buffer to parse the header from.
  986. *
  987. * @return true if the whole header was read, false if not.
  988. */
  989. response.readHeader = function(b) {
  990. // read header lines (each ends in CRLF)
  991. var line = '';
  992. while(!response.headerReceived && line !== null) {
  993. line = _readCrlf(b);
  994. if(line !== null) {
  995. // parse first line
  996. if(_first) {
  997. _first = false;
  998. var tmp = line.split(' ');
  999. if(tmp.length >= 3) {
  1000. response.version = tmp[0];
  1001. response.code = parseInt(tmp[1], 10);
  1002. response.message = tmp.slice(2).join(' ');
  1003. }
  1004. else {
  1005. // invalid header
  1006. throw {
  1007. message: 'Invalid http response header.',
  1008. details: {'line': line}
  1009. };
  1010. }
  1011. }
  1012. // handle final line
  1013. else if(line.length === 0) {
  1014. // end of header
  1015. response.headerReceived = true;
  1016. }
  1017. // parse header
  1018. else {
  1019. _parseHeader(line);
  1020. }
  1021. }
  1022. }
  1023. return response.headerReceived;
  1024. };
  1025. /**
  1026. * Reads some chunked http response entity-body from the given buffer of
  1027. * bytes.
  1028. *
  1029. * @param b the byte buffer to read from.
  1030. *
  1031. * @return true if the whole body was read, false if not.
  1032. */
  1033. var _readChunkedBody = function(b) {
  1034. /* Chunked transfer-encoding sends data in a series of chunks,
  1035. followed by a set of 0-N http trailers.
  1036. The format is as follows:
  1037. chunk-size (in hex) CRLF
  1038. chunk data (with "chunk-size" many bytes) CRLF
  1039. ... (N many chunks)
  1040. chunk-size (of 0 indicating the last chunk) CRLF
  1041. N many http trailers followed by CRLF
  1042. blank line + CRLF (terminates the trailers)
  1043. If there are no http trailers, then after the chunk-size of 0,
  1044. there is still a single CRLF (indicating the blank line + CRLF
  1045. that terminates the trailers). In other words, you always terminate
  1046. the trailers with blank line + CRLF, regardless of 0-N trailers. */
  1047. /* From RFC-2616, section 3.6.1, here is the pseudo-code for
  1048. implementing chunked transfer-encoding:
  1049. length := 0
  1050. read chunk-size, chunk-extension (if any) and CRLF
  1051. while (chunk-size > 0) {
  1052. read chunk-data and CRLF
  1053. append chunk-data to entity-body
  1054. length := length + chunk-size
  1055. read chunk-size and CRLF
  1056. }
  1057. read entity-header
  1058. while (entity-header not empty) {
  1059. append entity-header to existing header fields
  1060. read entity-header
  1061. }
  1062. Content-Length := length
  1063. Remove "chunked" from Transfer-Encoding
  1064. */
  1065. var line = '';
  1066. while(line !== null && b.length() > 0) {
  1067. // if in the process of reading a chunk
  1068. if(_chunkSize > 0) {
  1069. // if there are not enough bytes to read chunk and its
  1070. // trailing CRLF, we must wait for more data to be received
  1071. if(_chunkSize + 2 > b.length()) {
  1072. break;
  1073. }
  1074. // read chunk data, skip CRLF
  1075. response.body += b.getBytes(_chunkSize);
  1076. b.getBytes(2);
  1077. _chunkSize = 0;
  1078. }
  1079. // if chunks not finished, read the next chunk size
  1080. else if(!_chunksFinished) {
  1081. // read chunk-size line
  1082. line = _readCrlf(b);
  1083. if(line !== null) {
  1084. // parse chunk-size (ignore any chunk extension)
  1085. _chunkSize = parseInt(line.split(';', 1)[0], 16);
  1086. _chunksFinished = (_chunkSize === 0);
  1087. }
  1088. }
  1089. // chunks finished, read trailers
  1090. else {
  1091. // read next trailer
  1092. line = _readCrlf(b);
  1093. while(line !== null) {
  1094. if(line.length > 0) {
  1095. // parse trailer
  1096. _parseHeader(line);
  1097. // read next trailer
  1098. line = _readCrlf(b);
  1099. }
  1100. else {
  1101. // body received
  1102. response.bodyReceived = true;
  1103. line = null;
  1104. }
  1105. }
  1106. }
  1107. }
  1108. return response.bodyReceived;
  1109. };
  1110. /**
  1111. * Reads an http response body from a buffer of bytes.
  1112. *
  1113. * @param b the byte buffer to read from.
  1114. *
  1115. * @return true if the whole body was read, false if not.
  1116. */
  1117. response.readBody = function(b) {
  1118. var contentLength = response.getField('Content-Length');
  1119. var transferEncoding = response.getField('Transfer-Encoding');
  1120. if(contentLength !== null) {
  1121. contentLength = parseInt(contentLength);
  1122. }
  1123. // read specified length
  1124. if(contentLength !== null && contentLength >= 0) {
  1125. response.body = response.body || '';
  1126. response.body += b.getBytes(contentLength);
  1127. response.bodyReceived = (response.body.length === contentLength);
  1128. }
  1129. // read chunked encoding
  1130. else if(transferEncoding !== null) {
  1131. if(transferEncoding.indexOf('chunked') != -1) {
  1132. response.body = response.body || '';
  1133. _readChunkedBody(b);
  1134. }
  1135. else {
  1136. throw {
  1137. message: 'Unknown Transfer-Encoding.',
  1138. details: {'transferEncoding' : transferEncoding}
  1139. };
  1140. }
  1141. }
  1142. // read all data in the buffer
  1143. else if((contentLength !== null && contentLength < 0) ||
  1144. (contentLength === null &&
  1145. response.getField('Content-Type') !== null)) {
  1146. response.body = response.body || '';
  1147. response.body += b.getBytes();
  1148. response.readBodyUntilClose = true;
  1149. }
  1150. else {
  1151. // no body
  1152. response.body = null;
  1153. response.bodyReceived = true;
  1154. }
  1155. if(response.bodyReceived) {
  1156. response.time = +new Date() - response.time;
  1157. }
  1158. if(response.flashApi !== null &&
  1159. response.bodyReceived && response.body !== null &&
  1160. response.getField('Content-Encoding') === 'deflate') {
  1161. // inflate using flash api
  1162. response.body = forge.util.inflate(
  1163. response.flashApi, response.body);
  1164. }
  1165. return response.bodyReceived;
  1166. };
  1167. /**
  1168. * Parses an array of cookies from the 'Set-Cookie' field, if present.
  1169. *
  1170. * @return the array of cookies.
  1171. */
  1172. response.getCookies = function() {
  1173. var rval = [];
  1174. // get Set-Cookie field
  1175. if('Set-Cookie' in response.fields) {
  1176. var field = response.fields['Set-Cookie'];
  1177. // get current local time in seconds
  1178. var now = +new Date() / 1000;
  1179. // regex for parsing 'name1=value1; name2=value2; name3'
  1180. var regex = /\s*([^=]*)=?([^;]*)(;|$)/g;
  1181. // examples:
  1182. // Set-Cookie: cookie1_name=cookie1_value; max-age=0; path=/
  1183. // Set-Cookie: c2=v2; expires=Thu, 21-Aug-2008 23:47:25 GMT; path=/
  1184. for(var i = 0; i < field.length; ++i) {
  1185. var fv = field[i];
  1186. var m;
  1187. regex.lastIndex = 0;
  1188. var first = true;
  1189. var cookie = {};
  1190. do {
  1191. m = regex.exec(fv);
  1192. if(m !== null) {
  1193. var name = _trimString(m[1]);
  1194. var value = _trimString(m[2]);
  1195. // cookie_name=value
  1196. if(first) {
  1197. cookie.name = name;
  1198. cookie.value = value;
  1199. first = false;
  1200. }
  1201. // property_name=value
  1202. else {
  1203. name = name.toLowerCase();
  1204. switch(name) {
  1205. case 'expires':
  1206. // replace hyphens w/spaces so date will parse
  1207. value = value.replace(/-/g, ' ');
  1208. var secs = Date.parse(value) / 1000;
  1209. cookie.maxAge = Math.max(0, secs - now);
  1210. break;
  1211. case 'max-age':
  1212. cookie.maxAge = parseInt(value, 10);
  1213. break;
  1214. case 'secure':
  1215. cookie.secure = true;
  1216. break;
  1217. case 'httponly':
  1218. cookie.httpOnly = true;
  1219. break;
  1220. default:
  1221. if(name !== '') {
  1222. cookie[name] = value;
  1223. }
  1224. }
  1225. }
  1226. }
  1227. }
  1228. while(m !== null && m[0] !== '');
  1229. rval.push(cookie);
  1230. }
  1231. }
  1232. return rval;
  1233. };
  1234. /**
  1235. * Converts an http response into a string that can be sent as an
  1236. * HTTP response. Does not include any data.
  1237. *
  1238. * @return the string representation of the response.
  1239. */
  1240. response.toString = function() {
  1241. /* Sample response header:
  1242. HTTP/1.0 200 OK
  1243. Host: www.someurl.com
  1244. Connection: close
  1245. */
  1246. // build start line
  1247. var rval =
  1248. response.version + ' ' + response.code + ' ' + response.message + '\r\n';
  1249. // add each header
  1250. for(var name in response.fields) {
  1251. var fields = response.fields[name];
  1252. for(var i = 0; i < fields.length; ++i) {
  1253. rval += name + ': ' + fields[i] + '\r\n';
  1254. }
  1255. }
  1256. // final terminating CRLF
  1257. rval += '\r\n';
  1258. return rval;
  1259. };
  1260. return response;
  1261. };
  1262. /**
  1263. * Parses the scheme, host, and port from an http(s) url.
  1264. *
  1265. * @param str the url string.
  1266. *
  1267. * @return the parsed url object or null if the url is invalid.
  1268. */
  1269. http.parseUrl = forge.util.parseUrl;
  1270. /**
  1271. * Returns true if the given url is within the given cookie's domain.
  1272. *
  1273. * @param url the url to check.
  1274. * @param cookie the cookie or cookie domain to check.
  1275. */
  1276. http.withinCookieDomain = function(url, cookie) {
  1277. var rval = false;
  1278. // cookie may be null, a cookie object, or a domain string
  1279. var domain = (cookie === null || typeof cookie === 'string') ?
  1280. cookie : cookie.domain;
  1281. // any domain will do
  1282. if(domain === null) {
  1283. rval = true;
  1284. }
  1285. // ensure domain starts with a '.'
  1286. else if(domain.charAt(0) === '.') {
  1287. // parse URL as necessary
  1288. if(typeof url === 'string') {
  1289. url = http.parseUrl(url);
  1290. }
  1291. // add '.' to front of URL host to match against domain
  1292. var host = '.' + url.host;
  1293. // if the host ends with domain then it falls within it
  1294. var idx = host.lastIndexOf(domain);
  1295. if(idx !== -1 && (idx + domain.length === host.length)) {
  1296. rval = true;
  1297. }
  1298. }
  1299. return rval;
  1300. };
  1301. // public access to http namespace
  1302. if(typeof forge === 'undefined') {
  1303. forge = {};
  1304. }
  1305. forge.http = http;
  1306. })();