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.

52 lines
1.3 KiB

7 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. BufferedStream = require('./buffered-stream');
  12. var HttpStream = module.exports = function (options) {
  13. options = options || {};
  14. BufferedStream.call(this, options.limit);
  15. if (options.buffer === false) {
  16. this.buffer = false;
  17. }
  18. this.on('pipe', this.pipeState);
  19. };
  20. util.inherits(HttpStream, BufferedStream);
  21. //
  22. // ### function pipeState (source)
  23. // #### @source {ServerRequest|HttpStream} Source stream piping to this instance
  24. // Pipes additional HTTP metadata from the `source` HTTP stream (either concrete or
  25. // abstract) to this instance. e.g. url, headers, query, etc.
  26. //
  27. // Remark: Is there anything else we wish to pipe?
  28. //
  29. HttpStream.prototype.pipeState = function (source) {
  30. this.headers = source.headers;
  31. this.trailers = source.trailers;
  32. this.method = source.method;
  33. if (source.url) {
  34. this.url = this.originalUrl = source.url;
  35. }
  36. if (source.query) {
  37. this.query = source.query;
  38. }
  39. else if (source.url) {
  40. this.query = ~source.url.indexOf('?')
  41. ? qs.parse(url.parse(source.url).query)
  42. : {};
  43. }
  44. };