diff options
Diffstat (limited to 'WebCore/platform/KURL.cpp')
-rw-r--r-- | WebCore/platform/KURL.cpp | 154 |
1 files changed, 142 insertions, 12 deletions
diff --git a/WebCore/platform/KURL.cpp b/WebCore/platform/KURL.cpp index ffacc19..a8f7969 100644 --- a/WebCore/platform/KURL.cpp +++ b/WebCore/platform/KURL.cpp @@ -30,7 +30,7 @@ #include "KURL.h" #include "CString.h" -#include "PlatformString.h" +#include "StringHash.h" #include "TextEncoding.h" #include <wtf/StdLibExtras.h> @@ -102,7 +102,7 @@ static const unsigned char characterClassTable[256] = { /* 42 * */ UserInfoChar, /* 43 + */ SchemeChar | UserInfoChar, /* 44 , */ UserInfoChar, /* 45 - */ SchemeChar | UserInfoChar | HostnameChar, - /* 46 . */ SchemeChar | UserInfoChar | HostnameChar, + /* 46 . */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char, /* 47 / */ PathSegmentEndChar, /* 48 0 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char, /* 49 1 */ SchemeChar | UserInfoChar | HostnameChar | IPv6Char, @@ -633,7 +633,7 @@ bool KURL::protocolIs(const char* protocol) const // JavaScript URLs are "valid" and should be executed even if KURL decides they are invalid. // The free function protocolIsJavaScript() should be used instead. - ASSERT(strcmp(protocol, "javascript") != 0); + ASSERT(!equalIgnoringCase(protocol, String("javascript"))); if (!m_isValid) return false; @@ -685,18 +685,22 @@ void KURL::setHost(const String& s) parse(m_string.left(hostStart()) + (slashSlashNeeded ? "//" : "") + s + m_string.substring(m_hostEnd)); } +void KURL::removePort() +{ + if (m_hostEnd == m_portEnd) + return; + parse(m_string.left(m_hostEnd) + m_string.substring(m_portEnd)); +} + void KURL::setPort(unsigned short i) { if (!m_isValid) return; - if (i) { - bool colonNeeded = m_portEnd == m_hostEnd; - int portStart = (colonNeeded ? m_hostEnd : m_hostEnd + 1); + bool colonNeeded = m_portEnd == m_hostEnd; + int portStart = (colonNeeded ? m_hostEnd : m_hostEnd + 1); - parse(m_string.left(portStart) + (colonNeeded ? ":" : "") + String::number(i) + m_string.substring(m_portEnd)); - } else - parse(m_string.left(m_hostEnd) + m_string.substring(m_portEnd)); + parse(m_string.left(portStart) + (colonNeeded ? ":" : "") + String::number(i) + m_string.substring(m_portEnd)); } void KURL::setHostAndPort(const String& hostAndPort) @@ -819,7 +823,7 @@ String KURL::prettyURL() const authority.append('@'); } append(authority, host()); - if (port() != 0) { + if (hasPort()) { authority.append(':'); append(authority, String::number(port())); } @@ -1269,8 +1273,8 @@ void KURL::parse(const char* url, const String* originalString) m_userStart = m_userEnd = m_passwordEnd = m_hostEnd = m_portEnd = p - buffer.data(); // For canonicalization, ensure we have a '/' for no path. - // Only do this for http and https. - if (m_protocolInHTTPFamily && pathEnd - pathStart == 0) + // Do this only for hierarchical URL with protocol http or https. + if (m_protocolInHTTPFamily && hierarchical && pathEnd == pathStart) *p++ = '/'; // add path, escaping bad characters @@ -1624,6 +1628,132 @@ bool protocolIsJavaScript(const String& url) return protocolIs(url, "javascript"); } +bool isValidProtocol(const String& protocol) +{ + if (!isSchemeFirstChar(protocol[0])) + return false; + unsigned protocolLength = protocol.length(); + for (unsigned i = 1; i < protocolLength; i++) { + if (!isSchemeChar(protocol[i])) + return false; + } + return true; +} + +bool isDefaultPortForProtocol(unsigned short port, const String& protocol) +{ + if (protocol.isEmpty()) + return false; + + typedef HashMap<String, unsigned, CaseFoldingHash> DefaultPortsMap; + DEFINE_STATIC_LOCAL(DefaultPortsMap, defaultPorts, ()); + if (defaultPorts.isEmpty()) { + defaultPorts.set("http", 80); + defaultPorts.set("https", 443); + defaultPorts.set("ftp", 21); + defaultPorts.set("ftps", 990); + } + return defaultPorts.get(protocol) == port; +} + +bool portAllowed(const KURL& url) +{ + unsigned short port = url.port(); + + // Since most URLs don't have a port, return early for the "no port" case. + if (!port) + return true; + + // This blocked port list matches the port blocking that Mozilla implements. + // See http://www.mozilla.org/projects/netlib/PortBanning.html for more information. + static const unsigned short blockedPortList[] = { + 1, // tcpmux + 7, // echo + 9, // discard + 11, // systat + 13, // daytime + 15, // netstat + 17, // qotd + 19, // chargen + 20, // FTP-data + 21, // FTP-control + 22, // SSH + 23, // telnet + 25, // SMTP + 37, // time + 42, // name + 43, // nicname + 53, // domain + 77, // priv-rjs + 79, // finger + 87, // ttylink + 95, // supdup + 101, // hostriame + 102, // iso-tsap + 103, // gppitnp + 104, // acr-nema + 109, // POP2 + 110, // POP3 + 111, // sunrpc + 113, // auth + 115, // SFTP + 117, // uucp-path + 119, // nntp + 123, // NTP + 135, // loc-srv / epmap + 139, // netbios + 143, // IMAP2 + 179, // BGP + 389, // LDAP + 465, // SMTP+SSL + 512, // print / exec + 513, // login + 514, // shell + 515, // printer + 526, // tempo + 530, // courier + 531, // Chat + 532, // netnews + 540, // UUCP + 556, // remotefs + 563, // NNTP+SSL + 587, // ESMTP + 601, // syslog-conn + 636, // LDAP+SSL + 993, // IMAP+SSL + 995, // POP3+SSL + 2049, // NFS + 3659, // apple-sasl / PasswordServer [Apple addition] + 4045, // lockd + 6000, // X11 + }; + const unsigned short* const blockedPortListEnd = blockedPortList + sizeof(blockedPortList) / sizeof(blockedPortList[0]); + +#ifndef NDEBUG + // The port list must be sorted for binary_search to work. + static bool checkedPortList = false; + if (!checkedPortList) { + for (const unsigned short* p = blockedPortList; p != blockedPortListEnd - 1; ++p) + ASSERT(*p < *(p + 1)); + checkedPortList = true; + } +#endif + + // If the port is not in the blocked port list, allow it. + if (!binary_search(blockedPortList, blockedPortListEnd, port)) + return true; + + // Allow ports 21 and 22 for FTP URLs, as Mozilla does. + if ((port == 21 || port == 22) && url.protocolIs("ftp")) + return true; + + // Allow any port number in a file URL, since the port number is ignored. + if (url.protocolIs("file")) + return true; + + return false; +} + String mimeTypeFromDataURL(const String& url) { ASSERT(protocolIs(url, "data")); |