From 643ca7872b450ea4efacab6188849e5aac2ba161 Mon Sep 17 00:00:00 2001 From: Steve Block Date: Tue, 15 Dec 2009 10:12:09 +0000 Subject: Merge webkit.org at r51976 : Initial merge by git. Change-Id: Ib0e7e2f0fb4bee5a186610272edf3186f0986b43 --- WebCore/platform/KURLGoogle.cpp | 187 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 179 insertions(+), 8 deletions(-) (limited to 'WebCore/platform/KURLGoogle.cpp') diff --git a/WebCore/platform/KURLGoogle.cpp b/WebCore/platform/KURLGoogle.cpp index d0aae0c..76b5612 100644 --- a/WebCore/platform/KURLGoogle.cpp +++ b/WebCore/platform/KURLGoogle.cpp @@ -38,16 +38,21 @@ #include #endif +#include + #include "CString.h" +#include "StringHash.h" #include "NotImplemented.h" #include "TextEncoding.h" #include +#include #include #include using WTF::isASCIILower; using WTF::toASCIILower; +using std::binary_search; namespace WebCore { @@ -116,6 +121,16 @@ static bool lowerCaseEqualsASCII(const char* begin, const char* end, const char* return begin == end && !*str; } +static inline bool isSchemeFirstChar(char c) +{ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + +static inline bool isSchemeChar(char c) +{ + return isSchemeFirstChar(c) || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '*'; +} + // KURLGooglePrivate ----------------------------------------------------------- @@ -426,6 +441,11 @@ bool KURL::isValid() const return m_url.m_isValid; } +bool KURL::hasPort() const +{ + return hostEnd() < pathStart(); +} + bool KURL::protocolInHTTPFamily() const { return m_url.m_protocolInHTTPFamily; @@ -537,7 +557,10 @@ String KURL::query() const // Bug: https://bugs.webkit.org/show_bug.cgi?id=21015 this function returns // an empty string when the query is empty rather than a null (not sure // which is right). - return String("", 0); + // Returns a null if the query is not specified, instead of empty. + if (m_url.m_parsed.query.is_valid()) + return String("", 0); + return String(); } String KURL::path() const @@ -562,24 +585,36 @@ void KURL::setHost(const String& host) m_url.replaceComponents(replacements); } -// This function is used only in the JSC build. void KURL::setHostAndPort(const String& s) { - String newhost = s.left(s.find(":")); - String newport = s.substring(s.find(":") + 1); + String host = s; + String port; + int hostEnd = s.find(":"); + if (hostEnd != -1) { + host = s.left(hostEnd); + port = s.substring(hostEnd + 1); + } KURLGooglePrivate::Replacements replacements; // Host can't be removed, so we always set. - replacements.SetHost(CharactersOrEmpty(newhost), - url_parse::Component(0, newhost.length())); + replacements.SetHost(CharactersOrEmpty(host), + url_parse::Component(0, host.length())); - if (newport.isEmpty()) // Port may be removed, so we support clearing. + if (port.isEmpty()) // Port may be removed, so we support clearing. replacements.ClearPort(); else - replacements.SetPort(CharactersOrEmpty(newport), url_parse::Component(0, newport.length())); + replacements.SetPort(CharactersOrEmpty(port), url_parse::Component(0, port.length())); m_url.replaceComponents(replacements); } +void KURL::removePort() +{ + if (hasPort()) { + String urlWithoutPort = m_url.string().left(hostEnd()) + m_url.string().substring(pathStart()); + m_url.setUtf8(urlWithoutPort.utf8()); + } +} + void KURL::setPort(unsigned short i) { KURLGooglePrivate::Replacements replacements; @@ -698,6 +733,142 @@ 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; +} + +// We copied the KURL version here on Dec 4, 2009 while doing a WebKit +// merge. +// +// FIXME Somehow share this with KURL? Like we'd theoretically merge with +// decodeURLEscapeSequences below? +bool isDefaultPortForProtocol(unsigned short port, const String& protocol) +{ + if (protocol.isEmpty()) + return false; + + typedef HashMap 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; +} + +// We copied the KURL version here on Dec 4, 2009 while doing a WebKit +// merge. +// +// FIXME Somehow share this with KURL? Like we'd theoretically merge with +// decodeURLEscapeSequences below? +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; +} + // We copied the KURL version here on Sept 12, 2008 while doing a WebKit // merge. // -- cgit v1.1