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.

58 lines
1.6 KiB

8 years ago
  1. /*
  2. * http-stream.js: Idomatic buffered stream which pipes additional HTTP information.
  3. *
  4. * (C) 2011, Charlie Robbins & the Contributors
  5. * MIT LICENSE
  6. *
  7. */
  8. var url = require('url'),
  9. util = require('util'),
  10. qs = require('qs'),
  11. HttpStream = require('./http-stream');
  12. var RequestStream = module.exports = function (options) {
  13. options = options || {};
  14. HttpStream.call(this, options);
  15. this.on('pipe', this.pipeRequest);
  16. this.request = options.request;
  17. };
  18. util.inherits(RequestStream, HttpStream);
  19. //
  20. // ### function pipeRequest (source)
  21. // #### @source {ServerRequest|HttpStream} Source stream piping to this instance
  22. // Pipes additional HTTP request metadata from the `source` HTTP stream (either concrete or
  23. // abstract) to this instance. e.g. url, headers, query, etc.
  24. //
  25. // Remark: Is there anything else we wish to pipe?
  26. //
  27. RequestStream.prototype.pipeRequest = function (source) {
  28. this.url = this.originalUrl = source.url;
  29. this.method = source.method;
  30. this.httpVersion = source.httpVersion;
  31. this.httpVersionMajor = source.httpVersionMajor;
  32. this.httpVersionMinor = source.httpVersionMinor;
  33. this.setEncoding = source.setEncoding;
  34. this.connection = source.connection;
  35. this.socket = source.socket;
  36. if (source.query) {
  37. this.query = source.query;
  38. }
  39. else {
  40. this.query = ~source.url.indexOf('?')
  41. ? qs.parse(url.parse(source.url).query)
  42. : {};
  43. }
  44. };
  45. // http.serverRequest methods
  46. ['setEncoding'].forEach(function (method) {
  47. RequestStream.prototype[method] = function () {
  48. return this.request[method].apply(this.request, arguments);
  49. };
  50. });