From f65b36eec1e3bf07307e926981b3ec57a0145c1f Mon Sep 17 00:00:00 2001 From: isaacs Date: Fri, 11 Dec 2009 15:46:32 -0800 Subject: [PATCH] Use the new uri parser. --- lib/http.js | 83 +++++++---------------------------------------------- 1 file changed, 11 insertions(+), 72 deletions(-) diff --git a/lib/http.js b/lib/http.js index 0450d9b44f..dfb59bb447 100644 --- a/lib/http.js +++ b/lib/http.js @@ -41,74 +41,6 @@ var STATUS_CODES = exports.STATUS_CODES = { 505 : 'HTTP Version not supported' }; -/* - parseUri 1.2.1 - (c) 2007 Steven Levithan - MIT License -*/ - -function decode (s) { - return decodeURIComponent(s.replace(/\+/g, ' ')); -} - -exports.parseUri = function (str) { - var o = exports.parseUri.options, - m = o.parser[o.strictMode ? "strict" : "loose"].exec(str), - uri = {}, - i = 14; - - while (i--) uri[o.key[i]] = m[i] || ""; - - uri[o.q.name] = {}; - uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) { - if ($1) { - try { - var key = decode($1); - var val = decode($2); - } catch (e) { - return; - } - uri[o.q.name][key] = val; - } - }); - uri.toString = function () { return str; }; - - for (i = o.key.length - 1; i >= 0; i--){ - if (uri[o.key[i]] == "") delete uri[o.key[i]]; - } - - return uri; -}; - -exports.parseUri.options = { - strictMode: false, - key: [ - "source", - "protocol", - "authority", - "userInfo", - "user", - "password", - "host", - "port", - "relative", - "path", - "directory", - "file", - "query", - "anchor" - ], - q: { - name: "params", - parser: /(?:^|&)([^&=]*)=?([^&]*)/g - }, - parser: { - strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, - loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ - } -}; - - var connection_expression = /Connection/i; var transfer_encoding_expression = /Transfer-Encoding/i; var close_expression = /close/i; @@ -142,6 +74,7 @@ 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++) { @@ -631,13 +564,19 @@ exports.cat = function (url, encoding, headers) { encoding = encoding || "utf8"; - var uri = exports.parseUri(url); + var uri = require("uri").parse(url); headers = headers || {}; - if (!headers["Host"] && uri.host) { - headers["Host"] = uri.host; + + var hasHost = false; + for (var i in headers) if (i.toLowerCase() === "host") { + hasHost = true; + break; + } + if (!hasHost) { + headers["Host"] = uri.domain; } - var client = exports.createClient(uri.port || 80, uri.host); + var client = exports.createClient(uri.port || 80, uri.domain); var req = client.request(uri.path || "/", headers); client.addListener("error", function () {