Skip to content

Commit

Permalink
Use "url" module instead of "uri" module in http.js.
Browse files Browse the repository at this point in the history
Deprecate the URI module and remove tests for it.
- Rename "uri" to "url".
- Use the "url" module instead of the "uri" module.
- Remove the url parsing from http.js
- Update http.cat with the changed field names.
- Update tests for changes to http.js
- Update documentation for changes in http.js
  • Loading branch information
isaacs authored and ry committed Jan 5, 2010
1 parent 7ff04c1 commit 2b3d9e4
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 508 deletions.
41 changes: 33 additions & 8 deletions doc/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,8 @@ The request method as a string. Read only. Example:
+"GET"+, +"DELETE"+.


+request.uri+ ::
Request URI Object. This contains only the parameters that are
+request.url+ ::
Request URL string. This contains only the URL that is
present in the actual HTTP request. If the request is
+
----------------------------------------
Expand All @@ -828,16 +828,41 @@ Accept: text/plain\r\n
\r\n
----------------------------------------
+
Then +request.uri+ will be
Then +request.url+ will be
+
----------------------------------------
{ full: "/status?name=ryan",
path: "/status",
queryString: "name=ryan",
params: { "name": "ryan" },
fragment: ""
"/status?name=ryan"
----------------------------------------
+
If you would like to parse the URL into its parts, you can use
+require("url").parse(request.url)+. Example:
+
----------------------------------------
node> require("url").parse("/status?name=ryan")
{
"href": "/status?name=ryan",
"search": "?name=ryan",
"query": "name=ryan",
"pathname": "/status"
}
----------------------------------------
+
If you would like to extract the params from the query string,
you can use the +require("querystring").parse+ function, or pass
+true+ as the second argument to +require("url").parse+. Example:
+
----------------------------------------
node> require("url").parse("/status?name=ryan", true)
{
"href": "/status?name=ryan",
"search": "?name=ryan",
"query": {
"name": "ryan"
},
"pathname": "/status"
}
----------------------------------------
+


+request.headers+ ::
Expand Down
68 changes: 17 additions & 51 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ function IncomingMessage (connection) {
this.headers = {};

// request (server) only
this.uri = {
full: "",
queryString: "",
fragment: "",
path: "",
params: {}
};
this.url = "";

this.method = null;

Expand All @@ -74,20 +68,8 @@ function IncomingMessage (connection) {
sys.inherits(IncomingMessage, process.EventEmitter);
exports.IncomingMessage = IncomingMessage;

var decode = require("uri").decode;
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;
}
}
throw new Error("_parseQueryString is deprecated. Use require(\"querystring\") to parse query strings.\n");
};

IncomingMessage.prototype.setBodyEncoding = function (enc) {
Expand Down Expand Up @@ -267,7 +249,7 @@ ServerResponse.prototype.sendHeader = function (statusCode, headers) {
};


function ClientRequest (method, uri, headers) {
function ClientRequest (method, url, headers) {
OutgoingMessage.call(this);

this.should_keep_alive = false;
Expand All @@ -278,7 +260,7 @@ function ClientRequest (method, uri, headers) {
}
this.closeOnFinish = true;

this.sendHeaderLines(method + " " + uri + " HTTP/1.1\r\n", headers);
this.sendHeaderLines(method + " " + url + " HTTP/1.1\r\n", headers);
}
sys.inherits(ClientRequest, OutgoingMessage);
exports.ClientRequest = ClientRequest;
Expand All @@ -301,22 +283,10 @@ function createIncomingMessageStream (connection, incoming_listener) {
field = null;
value = null;
});

// Only servers will get URI events.
// Only servers will get URL events.
connection.addListener("url", function (data) {
incoming.uri.full += data;
});

connection.addListener("path", function (data) {
incoming.uri.path += data;
});

connection.addListener("fragment", function (data) {
incoming.uri.fragment += data;
});

connection.addListener("queryString", function (data) {
incoming.uri.queryString += data;
incoming.url += data;
});

connection.addListener("headerField", function (data) {
Expand Down Expand Up @@ -352,10 +322,6 @@ function createIncomingMessageStream (connection, incoming_listener) {
if (info.method) {
// server only
incoming.method = info.method;

if (incoming.uri.queryString.length > 0) {
incoming._parseQueryString();
}
} else {
// client only
incoming.statusCode = info.statusCode;
Expand Down Expand Up @@ -548,13 +514,13 @@ process.http.Client.prototype.put = function () {
throw new Error("client.put(...) is now client.request('PUT', ...)");
};

process.http.Client.prototype.request = function (method, uri, headers) {
if (typeof(uri) != "string") { // assume method was omitted, shift arguments
headers = uri;
uri = method;
process.http.Client.prototype.request = function (method, url, headers) {
if (typeof(url) != "string") { // assume method was omitted, shift arguments
headers = url;
url = method;
method = null;
}
var req = new ClientRequest(method || "GET", uri, headers);
var req = new ClientRequest(method || "GET", url, headers);
this._pushRequest(req);
return req;
};
Expand All @@ -565,7 +531,7 @@ exports.cat = function (url, encoding, headers) {

encoding = encoding || "utf8";

var uri = require("uri").parse(url);
var url = require("url").parse(url);
headers = headers || {};

var hasHost = false;
Expand All @@ -574,11 +540,11 @@ exports.cat = function (url, encoding, headers) {
break;
}
if (!hasHost) {
headers["Host"] = uri.domain;
headers["Host"] = url.hostname;
}

var client = exports.createClient(uri.port || 80, uri.domain);
var req = client.request(uri.path || "/", headers);
var client = exports.createClient(url.port || 80, url.hostname);
var req = client.request((url.pathname || "/")+(url.search || "")+(url.hash || ""), headers);

client.addListener("error", function () {
promise.emitError();
Expand Down
Loading

0 comments on commit 2b3d9e4

Please sign in to comment.