Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ncb000gt/744042 to your computer and use it in GitHub Desktop.
Save ncb000gt/744042 to your computer and use it in GitHub Desktop.
From 37f3ea740829e95e733951aab8def5d525a0bc29 Mon Sep 17 00:00:00 2001
From: Nick Campbell <ncampbell@siteworx.com>
Date: Thu, 16 Dec 2010 10:54:04 -0500
Subject: [PATCH] Match lowercased protocols so that protocols can be specified in any way.
Signed-off-by: Nick Campbell <ncampbell@siteworx.com>
---
lib/url.js | 18 +++++++++++-------
test/simple/test-url.js | 6 ++++++
2 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/lib/url.js b/lib/url.js
index a0f274e..aa0ae43 100644
--- a/lib/url.js
+++ b/lib/url.js
@@ -5,7 +5,7 @@ exports.format = urlFormat;
// define these here so at least they only have to be
// compiled once on the first module load.
-var protocolPattern = /^([a-z0-9]+:)/,
+var protocolPattern = /^([a-zA-Z0-9]+:)/,
portPattern = /:[0-9]+$/,
nonHostChars = ['/', '?', ';', '#'],
hostlessProtocol = {
@@ -34,8 +34,10 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
rest = url;
var proto = protocolPattern.exec(rest);
+ var lowerProto = undefined;
if (proto) {
proto = proto[0];
+ lowerProto = proto.toLowerCase();
out.protocol = proto;
rest = rest.substr(proto.length);
}
@@ -46,13 +48,13 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
// how the browser resolves relative URLs.
if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) {
var slashes = rest.substr(0, 2) === '//';
- if (slashes && !(proto && hostlessProtocol[proto])) {
+ if (slashes && !(proto && hostlessProtocol[lowerProto])) {
rest = rest.substr(2);
out.slashes = true;
}
}
- if (!hostlessProtocol[proto] &&
- (slashes || (proto && !slashedProtocol[proto]))) {
+ if (!hostlessProtocol[lowerProto] &&
+ (slashes || (proto && !slashedProtocol[lowerProto]))) {
// there's a hostname.
// the first instance of /, ?, ;, or # ends the host.
// don't enforce full RFC correctness, just be unstupid about it.
@@ -131,11 +133,12 @@ function urlFormat(obj) {
hash = obj.hash || '';
if (protocol && protocol.substr(-1) !== ':') protocol += ':';
+ var lowerProtocol = (protocol)?protocol.toLowerCase():undefined;
// only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
// unless they had them to begin with.
if (obj.slashes ||
- (!protocol || slashedProtocol[protocol]) && host !== false) {
+ (!protocol || slashedProtocol[lowerProtocol]) && host !== false) {
host = '//' + (host || '');
if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname;
} else if (!host) {
@@ -179,10 +182,11 @@ function urlResolveObject(source, relative) {
// because that's known to be hostless.
// anything else is assumed to be absolute.
- if (!slashedProtocol[relative.protocol]) return relative;
+ var lowerProtocol = (relative.protocol)?relative.protocol.toLowerCase():undefined;
+ if (!slashedProtocol[lowerProtocol]) return relative;
source.protocol = relative.protocol;
- if (!relative.host && !hostlessProtocol[relative.protocol]) {
+ if (!relative.host && !hostlessProtocol[lowerProtocol]) {
var relPath = (relative.pathname || '').split('/');
while (relPath.length && !(relative.host = relPath.shift()));
if (!relative.host) relative.host = '';
diff --git a/test/simple/test-url.js b/test/simple/test-url.js
index c59e92b..40ce011 100644
--- a/test/simple/test-url.js
+++ b/test/simple/test-url.js
@@ -11,6 +11,12 @@ var parseTests = {
'href': '//some_path',
'pathname': '//some_path'
},
+ 'HTTP://www.example.com' : {
+ 'href': 'HTTP://www.example.com',
+ 'protocol': 'HTTP:',
+ 'host': 'www.example.com',
+ 'hostname': 'www.example.com'
+ },
'http://www.narwhaljs.org/blog/categories?id=news' : {
'href': 'http://www.narwhaljs.org/blog/categories?id=news',
'protocol': 'http:',
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment