request.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * request.js
  3. *
  4. * Request class contains server only options
  5. */
  6. var parse_url = require('url').parse;
  7. var Headers = require('./headers');
  8. var Body = require('./body');
  9. module.exports = Request;
  10. /**
  11. * Request class
  12. *
  13. * @param Mixed input Url or Request instance
  14. * @param Object init Custom options
  15. * @return Void
  16. */
  17. function Request(input, init) {
  18. var url, url_parsed;
  19. // normalize input
  20. if (!(input instanceof Request)) {
  21. url = input;
  22. url_parsed = parse_url(url);
  23. input = {};
  24. } else {
  25. url = input.url;
  26. url_parsed = parse_url(url);
  27. }
  28. // normalize init
  29. init = init || {};
  30. // fetch spec options
  31. this.method = init.method || input.method || 'GET';
  32. this.redirect = init.redirect || input.redirect || 'follow';
  33. this.headers = new Headers(init.headers || input.headers || {});
  34. this.url = url;
  35. // server only options
  36. this.follow = init.follow !== undefined ?
  37. init.follow : input.follow !== undefined ?
  38. input.follow : 20;
  39. this.compress = init.compress !== undefined ?
  40. init.compress : input.compress !== undefined ?
  41. input.compress : true;
  42. this.counter = init.counter || input.counter || 0;
  43. this.agent = init.agent || input.agent;
  44. Body.call(this, init.body || this._clone(input), {
  45. timeout: init.timeout || input.timeout || 0,
  46. size: init.size || input.size || 0
  47. });
  48. // server request options
  49. this.protocol = url_parsed.protocol;
  50. this.hostname = url_parsed.hostname;
  51. this.port = url_parsed.port;
  52. this.path = url_parsed.path;
  53. this.auth = url_parsed.auth;
  54. }
  55. Request.prototype = Object.create(Body.prototype);
  56. /**
  57. * Clone this request
  58. *
  59. * @return Request
  60. */
  61. Request.prototype.clone = function() {
  62. return new Request(this);
  63. };