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