Parse queryString into req.uri.params
This commit is contained in:
parent
4c51af882d
commit
522909bcbf
@ -679,6 +679,7 @@ Then +request.uri+ will be
|
|||||||
{ full: "/status?name=ryan",
|
{ full: "/status?name=ryan",
|
||||||
path: "/status",
|
path: "/status",
|
||||||
queryString: "name=ryan",
|
queryString: "name=ryan",
|
||||||
|
params: { "name": "ryan" },
|
||||||
fragment: ""
|
fragment: ""
|
||||||
}
|
}
|
||||||
----------------------------------------
|
----------------------------------------
|
||||||
|
22
lib/http.js
22
lib/http.js
@ -129,7 +129,8 @@ function IncomingMessage (connection) {
|
|||||||
full: "",
|
full: "",
|
||||||
queryString: "",
|
queryString: "",
|
||||||
fragment: "",
|
fragment: "",
|
||||||
path: ""
|
path: "",
|
||||||
|
params: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.method = null;
|
this.method = null;
|
||||||
@ -140,6 +141,21 @@ function IncomingMessage (connection) {
|
|||||||
}
|
}
|
||||||
node.inherits(IncomingMessage, node.EventEmitter);
|
node.inherits(IncomingMessage, node.EventEmitter);
|
||||||
|
|
||||||
|
IncomingMessage.prototype._parseQueryString = function () {
|
||||||
|
var parts = this.uri.queryString.split('&');
|
||||||
|
for (var j = 0; j < parts.length; j++) {
|
||||||
|
var i = parts[j].indexOf('=');
|
||||||
|
if (i < 0) continue;
|
||||||
|
try {
|
||||||
|
var key = decode(parts[j].slice(0,i))
|
||||||
|
var value = decode(parts[j].slice(i+1));
|
||||||
|
this.uri.params[key] = value;
|
||||||
|
} catch (e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
IncomingMessage.prototype.setBodyEncoding = function (enc) {
|
IncomingMessage.prototype.setBodyEncoding = function (enc) {
|
||||||
// TODO: Find a cleaner way of doing this.
|
// TODO: Find a cleaner way of doing this.
|
||||||
this.connection.setEncoding(enc);
|
this.connection.setEncoding(enc);
|
||||||
@ -390,6 +406,10 @@ function createIncomingMessageStream (connection, incoming_listener) {
|
|||||||
if (info.method) {
|
if (info.method) {
|
||||||
// server only
|
// server only
|
||||||
incoming.method = info.method;
|
incoming.method = info.method;
|
||||||
|
|
||||||
|
if (incoming.uri.queryString.length > 0) {
|
||||||
|
incoming._parseQueryString();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// client only
|
// client only
|
||||||
incoming.statusCode = info.statusCode;
|
incoming.statusCode = info.statusCode;
|
||||||
|
@ -16,6 +16,8 @@ http.createServer(function (req, res) {
|
|||||||
if (req.id == 0) {
|
if (req.id == 0) {
|
||||||
assertEquals("GET", req.method);
|
assertEquals("GET", req.method);
|
||||||
assertEquals("/hello", req.uri.path);
|
assertEquals("/hello", req.uri.path);
|
||||||
|
assertEquals("world", req.uri.params["hello"]);
|
||||||
|
assertEquals("b==ar", req.uri.params["foo"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.id == 1) {
|
if (req.id == 1) {
|
||||||
@ -38,7 +40,7 @@ var c = tcp.createConnection(port);
|
|||||||
c.setEncoding("utf8");
|
c.setEncoding("utf8");
|
||||||
|
|
||||||
c.addListener("connect", function () {
|
c.addListener("connect", function () {
|
||||||
c.send( "GET /hello HTTP/1.1\r\n\r\n" );
|
c.send( "GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n" );
|
||||||
requests_sent += 1;
|
requests_sent += 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user