Created
August 27, 2010 16:37
-
-
Save ericf/553694 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sanitize URL — Validate it looks like a URL, then make it less dirty. | |
* | |
* Oddnut Software | |
* Copyright (c) 2010 Eric Ferraiuolo - http://eric.ferraiuolo.name | |
* MIT License - http://www.opensource.org/licenses/mit-license.php | |
* | |
* Examples: | |
* | |
* 'Http://WWW.example.com/' » 'http://www.example.com/' | |
* 'example.com' » 'http://example.com' | |
* 'example.c' » null | |
* 'https://foo:[email protected]' » 'https://foo:[email protected]' | |
* ' WWW.EXAMPLE.COM ' » 'http://www.example.com' | |
*/ | |
var sanitizeUrl = (function(){ | |
var URL_REGEXP = /^(?:(https?:)\/\/)?(?:([^:@\s]+:?[^:@\s]+?)@)?((?:[^;:@=\/\?\.\s]+\.)+[A-Za-z0-9\-]{2,})(?::(\d+))?(?=\/|$)(\/.*)?/i, | |
TRIM_REGEXP = /^\s+|\s+$/g, | |
HTTP_PROTOCOL = 'http:', | |
SLASH_SLASH = '//', | |
AT = '@', | |
COLON = ':', | |
EMPTY_STRING = ''; | |
function trim (s) { | |
try { | |
return s.replace(TRIM_REGEXP, EMPTY_STRING); | |
} catch(e) { | |
return s; | |
} | |
} | |
return function (url) { | |
url = trim(url); | |
if (url.length < 1) { return null; } | |
var urlParts = url.match(URL_REGEXP), | |
protocol, authorization, host, port, path; | |
if ( ! urlParts) { return null; } | |
protocol = (urlParts[1] ? urlParts[1].toLowerCase() : HTTP_PROTOCOL) + SLASH_SLASH; | |
authorization = (urlParts[2] ? (urlParts[2] + AT) : EMPTY_STRING); | |
host = (urlParts[3] ? urlParts[3].toLowerCase() : EMPTY_STRING); | |
port = (urlParts[4] ? (COLON + urlParts[4]) : EMPTY_STRING); | |
path = (urlParts[5] || EMPTY_STRING); | |
return ( protocol + authorization + host + port + path ); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment