socketioMEANseed/www/node_modules/union/lib/request-stream.js
www/node_modules/union/lib/request-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 HttpStream = require('./http-stream');
13
14var RequestStream = module.exports = function (options) {
15 options = options || {};
16 HttpStream.call(this, options);
17
18 this.on('pipe', this.pipeRequest);
19 this.request = options.request;
20};
21
22util.inherits(RequestStream, HttpStream);
23
24//
25// ### function pipeRequest (source)
26// #### @source {ServerRequest|HttpStream} Source stream piping to this instance
27// Pipes additional HTTP request metadata from the `source` HTTP stream (either concrete or
28// abstract) to this instance. e.g. url, headers, query, etc.
29//
30// Remark: Is there anything else we wish to pipe?
31//
32RequestStream.prototype.pipeRequest = function (source) {
33 this.url = this.originalUrl = source.url;
34 this.method = source.method;
35 this.httpVersion = source.httpVersion;
36 this.httpVersionMajor = source.httpVersionMajor;
37 this.httpVersionMinor = source.httpVersionMinor;
38 this.setEncoding = source.setEncoding;
39 this.connection = source.connection;
40 this.socket = source.socket;
41
42 if (source.query) {
43 this.query = source.query;
44 }
45 else {
46 this.query = ~source.url.indexOf('?')
47 ? qs.parse(url.parse(source.url).query)
48 : {};
49 }
50};
51
52// http.serverRequest methods
53['setEncoding'].forEach(function (method) {
54 RequestStream.prototype[method] = function () {
55 return this.request[method].apply(this.request, arguments);
56 };
57});
58