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