#include "config.h"
#if USE(GOOGLEURL)
#include "KURL.h"
#ifndef NDEBUG
#include <stdio.h>
#endif
#include <algorithm>
#include "StringHash.h"
#include "NotImplemented.h"
#include "TextEncoding.h"
#include <wtf/HashMap.h>
#include <wtf/Vector.h>
#include <wtf/StdLibExtras.h>
#include <wtf/text/CString.h>
#include <googleurl/src/url_canon_internal.h>
#include <googleurl/src/url_util.h>
using WTF::isASCIILower;
using WTF::toASCIILower;
using std::binary_search;
namespace WebCore {
static const int maximumValidPortNumber = 0xFFFE;
static const int invalidPortNumber = 0xFFFF;
class KURLCharsetConverter : public url_canon::CharsetConverter {
public:
KURLCharsetConverter(const TextEncoding* encoding)
: m_encoding(encoding)
{
}
virtual void ConvertFromUTF16(const url_parse::UTF16Char* input, int inputLength,
url_canon::CanonOutput* output)
{
CString encoded = m_encoding->encode(input, inputLength, URLEncodedEntitiesForUnencodables);
output->Append(encoded.data(), static_cast<int>(encoded.length()));
}
private:
const TextEncoding* m_encoding;
};
static inline void assertProtocolIsGood(const char* protocol)
{
#ifndef NDEBUG
const char* p = protocol;
while (*p) {
ASSERT(*p > ' ' && *p < 0x7F && !(*p >= 'A' && *p <= 'Z'));
++p;
}
#endif
}
static inline const url_parse::UTF16Char* CharactersOrEmpty(const String& str)
{
static const url_parse::UTF16Char zero = 0;
return str.characters() ?
reinterpret_cast<const url_parse::UTF16Char*>(str.characters()) :
&zero;
}
static inline bool isUnicodeEncoding(const TextEncoding* encoding)
{
return encoding->encodingForFormSubmission() == UTF8Encoding();
}
static bool lowerCaseEqualsASCII(const char* begin, const char* end, const char* str)
{
while (begin != end && *str) {
ASSERT(toASCIILower(*str) == *str);
if (toASCIILower(*begin++) != *str++)
return false;
}
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::KURLGooglePrivate()
: m_isValid(false)
, m_protocolInHTTPFamily(false)
, m_utf8IsASCII(true)
, m_stringIsValid(false)
{
}
KURLGooglePrivate::KURLGooglePrivate(const url_parse::Parsed& parsed, bool isValid)
: m_isValid(isValid)
, m_protocolInHTTPFamily(false)
, m_parsed(parsed)
, m_utf8IsASCII(true)
, m_stringIsValid(false)
{
}
void KURLGooglePrivate::setUtf8(const CString& str)
{
const char* data = str.data();
unsigned dataLength = str.length();
m_utf8IsASCII = true;
for (unsigned i = 0; i < dataLength; i++) {
if (static_cast<unsigned char>(data[i]) >= 0x80) {
m_utf8IsASCII = false;
break;
}
}
m_utf8 = str;
m_stringIsValid = false;
initProtocolInHTTPFamily();
}
void KURLGooglePrivate::setAscii(const CString& str)
{
m_utf8 = str;
m_utf8IsASCII = true;
m_stringIsValid = false;
initProtocolInHTTPFamily();
}
void KURLGooglePrivate::init(const KURL& base,
const String& relative,
const TextEncoding* queryEncoding)
{
init(base, relative.characters(), relative.length(), queryEncoding);
}
void KURLGooglePrivate::init(const KURL& base, const char* rel, int relLength,
const TextEncoding* queryEncoding)
{
KURLCharsetConverter charsetConverterObject(queryEncoding);
KURLCharsetConverter* charsetConverter =
(!queryEncoding || isUnicodeEncoding(queryEncoding)) ? 0 :
&charsetConverterObject;
url_canon::RawCanonOutputT<char> output;
const CString& baseStr = base.m_url.utf8String();
m_isValid = url_util::ResolveRelative(baseStr.data(), baseStr.length(),
base.m_url.m_parsed, rel, relLength,
charsetConverter,
&output, &m_parsed);
if (m_isValid || output.length()) {
if (m_parsed.ref.is_nonempty())
setUtf8(CString(output.data(), output.length()));
else
setAscii(CString(output.data(), output.length()));
} else {
setUtf8(CString("", 0));
}
}
void KURLGooglePrivate::init(const KURL& base, const UChar* rel, int relLength,
const TextEncoding* queryEncoding)
{
KURLCharsetConverter charsetConverterObject(queryEncoding);
KURLCharsetConverter* charsetConverter =
(!queryEncoding || isUnicodeEncoding(queryEncoding)) ? 0 :
&charsetConverterObject;
url_canon::RawCanonOutputT<char> output;
const CString& baseStr = base.m_url.utf8String();
m_isValid = url_util::ResolveRelative(baseStr.data(), baseStr.length(),
base.m_url.m_parsed, rel, relLength,
charsetConverter,
&output, &m_parsed);
if (m_isValid || output.length()) {
if (m_parsed.ref.is_nonempty())
setUtf8(CString(output.data(), output.length()));
else
setAscii(CString(output.data(), output.length()));
} else
setUtf8(CString("", 0));
}
void KURLGooglePrivate::initProtocolInHTTPFamily()
{
if (!m_isValid) {
m_protocolInHTTPFamily = false;
return;
}
const char* scheme = m_utf8.data() + m_parsed.scheme.begin;
if (m_parsed.scheme.len == 4)
m_protocolInHTTPFamily = lowerCaseEqualsASCII(scheme, scheme + 4, "http");
else if (m_parsed.scheme.len == 5)
m_protocolInHTTPFamily = lowerCaseEqualsASCII(scheme, scheme + 5, "https");
else
m_protocolInHTTPFamily = false;
}
void KURLGooglePrivate::copyTo(KURLGooglePrivate* dest) const
{
dest->m_isValid = m_isValid;
dest->m_protocolInHTTPFamily = m_protocolInHTTPFamily;
dest->m_parsed = m_parsed;
dest->m_utf8 = CString(m_utf8.data(), m_utf8.length());
dest->m_utf8IsASCII = m_utf8IsASCII;
dest->m_stringIsValid = false;
}
String KURLGooglePrivate::componentString(const url_parse::Component& comp) const
{
if (!m_isValid || comp.len <= 0) {
if (utf8String().isNull())
return String();
return String("", 0);
}
return string().substring(comp.begin, comp.len);
}
void KURLGooglePrivate::replaceComponents(const Replacements& replacements)
{
url_canon::RawCanonOutputT<char> output;
url_parse::Parsed newParsed;
m_isValid = url_util::ReplaceComponents(utf8String().data(),
utf8String().length(), m_parsed, replacements, 0, &output, &newParsed);
m_parsed = newParsed;
if (m_parsed.ref.is_nonempty())
setUtf8(CString(output.data(), output.length()));
else
setAscii(CString(output.data(), output.length()));
}
const String& KURLGooglePrivate::string() const
{
if (!m_stringIsValid) {
if (m_utf8.isNull())
m_string = String();
else if (m_utf8IsASCII)
m_string = String(m_utf8.data(), m_utf8.length());
else
m_string = String::fromUTF8(m_utf8.data(), m_utf8.length());
m_stringIsValid = true;
}
return m_string;
}
KURL::KURL(ParsedURLStringTag, const char *url)
{
m_url.init(KURL(), url, strlen(url), 0);
if (m_url.utf8String().isNull())
m_url.setAscii(CString("", 0));
}
KURL::KURL(ParsedURLStringTag, const String& url)
{
if (!url.isNull())
m_url.init(KURL(), url, 0);
else {
m_url.m_isValid = false;
m_url.m_protocolInHTTPFamily = false;
}
}
KURL::KURL(const KURL& base, const String& relative)
{
m_url.init(base, relative, 0);
}
KURL::KURL(const KURL& base,
const String& relative,
const TextEncoding& encoding)
{
m_url.init(base, relative, &encoding.encodingForFormSubmission());
}
KURL::KURL(const CString& canonicalSpec,
const url_parse::Parsed& parsed, bool isValid)
: m_url(parsed, isValid)
{
if (parsed.ref.is_nonempty())
m_url.setUtf8(canonicalSpec);
else
m_url.setAscii(canonicalSpec);
}
#if PLATFORM(CF)
KURL::KURL(CFURLRef)
{
notImplemented();
invalidate();
}
CFURLRef KURL::createCFURL() const
{
notImplemented();
return 0;
}
#endif
KURL KURL::copy() const
{
KURL result = *this;
m_url.copyTo(&result.m_url);
return result;
}
bool KURL::isNull() const
{
return m_url.utf8String().isNull();
}
bool KURL::isEmpty() const
{
return !m_url.utf8String().length();
}
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;
}
bool KURL::hasPath() const
{
return m_url.m_parsed.path.len >= 0;
}
String KURL::lastPathComponent() const
{
url_parse::Component path = m_url.m_parsed.path;
if (path.len > 0 && m_url.utf8String().data()[path.end() - 1] == '/')
path.len--;
url_parse::Component file;
url_parse::ExtractFileName(m_url.utf8String().data(), path, &file);
if (!file.is_nonempty())
return String();
return m_url.componentString(file);
}
String KURL::protocol() const
{
return m_url.componentString(m_url.m_parsed.scheme);
}
String KURL::host() const
{
return m_url.componentString(m_url.m_parsed.host);
}
unsigned short KURL::port() const
{
if (!m_url.m_isValid || m_url.m_parsed.port.len <= 0)
return 0;
int port = url_parse::ParsePort(m_url.utf8String().data(), m_url.m_parsed.port);
ASSERT(port != url_parse::PORT_UNSPECIFIED);
if (port == url_parse::PORT_INVALID || port > maximumValidPortNumber) port = invalidPortNumber;
return static_cast<unsigned short>(port);
}
String KURL::pass() const
{
if (!m_url.m_parsed.password.is_nonempty())
return String();
return m_url.componentString(m_url.m_parsed.password);
}
String KURL::user() const
{
return m_url.componentString(m_url.m_parsed.username);
}
String KURL::fragmentIdentifier() const
{
if (!m_url.m_parsed.ref.is_valid())
return String();
return m_url.componentString(m_url.m_parsed.ref);
}
bool KURL::hasFragmentIdentifier() const
{
return m_url.m_parsed.ref.len >= 0;
}
String KURL::baseAsString() const
{
return string().left(pathAfterLastSlash());
}
String KURL::query() const
{
if (m_url.m_parsed.query.len >= 0)
return m_url.componentString(m_url.m_parsed.query);
if (m_url.m_parsed.query.is_valid())
return String("", 0);
return String();
}
String KURL::path() const
{
return m_url.componentString(m_url.m_parsed.path);
}
bool KURL::setProtocol(const String& protocol)
{
int separatorPosition = protocol.find(':');
String newProtocol = protocol.substring(0, separatorPosition);
url_canon::RawCanonOutputT<char> canonProtocol;
url_parse::Component protocolComponent;
if (!url_canon::CanonicalizeScheme(newProtocol.characters(),
url_parse::Component(0, newProtocol.length()),
&canonProtocol, &protocolComponent)
|| !protocolComponent.is_nonempty())
return false;
KURLGooglePrivate::Replacements replacements;
replacements.SetScheme(CharactersOrEmpty(newProtocol),
url_parse::Component(0, newProtocol.length()));
m_url.replaceComponents(replacements);
return true;
}
void KURL::setHost(const String& host)
{
KURLGooglePrivate::Replacements replacements;
replacements.SetHost(CharactersOrEmpty(host),
url_parse::Component(0, host.length()));
m_url.replaceComponents(replacements);
}
void KURL::setHostAndPort(const String& s)
{
String host = s;
String port;
int hostEnd = s.find(":");
if (hostEnd != -1) {
host = s.left(hostEnd);
port = s.substring(hostEnd + 1);
}
KURLGooglePrivate::Replacements replacements;
replacements.SetHost(CharactersOrEmpty(host),
url_parse::Component(0, host.length()));
if (port.isEmpty()) replacements.ClearPort();
else
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;
String portStr;
if (i) {
portStr = String::number(i);
replacements.SetPort(
reinterpret_cast<const url_parse::UTF16Char*>(portStr.characters()),
url_parse::Component(0, portStr.length()));
} else {
replacements.ClearPort();
}
m_url.replaceComponents(replacements);
}
void KURL::setUser(const String& user)
{
if (user.isEmpty() && !m_url.m_parsed.username.is_valid())
return;
KURLGooglePrivate::Replacements replacements;
replacements.SetUsername(CharactersOrEmpty(user),
url_parse::Component(0, user.length()));
m_url.replaceComponents(replacements);
}
void KURL::setPass(const String& pass)
{
if (pass.isEmpty() && !m_url.m_parsed.password.is_valid())
return;
KURLGooglePrivate::Replacements replacements;
replacements.SetPassword(CharactersOrEmpty(pass),
url_parse::Component(0, pass.length()));
m_url.replaceComponents(replacements);
}
void KURL::setFragmentIdentifier(const String& s)
{
if (s.isNull() && !m_url.m_parsed.ref.is_valid())
return;
KURLGooglePrivate::Replacements replacements;
if (s.isNull())
replacements.ClearRef();
else
replacements.SetRef(CharactersOrEmpty(s), url_parse::Component(0, s.length()));
m_url.replaceComponents(replacements);
}
void KURL::removeFragmentIdentifier()
{
KURLGooglePrivate::Replacements replacements;
replacements.ClearRef();
m_url.replaceComponents(replacements);
}
void KURL::setQuery(const String& query)
{
KURLGooglePrivate::Replacements replacements;
if (query.isNull()) {
replacements.ClearQuery();
} else if (query.length() > 0 && query[0] == '?') {
replacements.SetQuery(CharactersOrEmpty(query),
url_parse::Component(1, query.length() - 1));
} else {
replacements.SetQuery(CharactersOrEmpty(query),
url_parse::Component(0, query.length()));
}
m_url.replaceComponents(replacements);
}
void KURL::setPath(const String& path)
{
KURLGooglePrivate::Replacements replacements;
replacements.SetPath(CharactersOrEmpty(path),
url_parse::Component(0, path.length()));
m_url.replaceComponents(replacements);
}
String KURL::prettyURL() const
{
if (!m_url.m_isValid)
return String();
return m_url.string();
}
bool protocolIsJavaScript(const String& url)
{
return protocolIs(url, "javascript");
}
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();
if (!port)
return true;
static const unsigned short blockedPortList[] = {
1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42, 43, 53, 77, 79, 87, 95, 101, 102, 103, 104, 109, 110, 111, 113, 115, 117, 119, 123, 135, 139, 143, 179, 389, 465, 512, 513, 514, 515, 526, 530, 531, 532, 540, 556, 563, 587, 601, 636, 993, 995, 2049, 3659, 4045, 6000, 6665, 6666, 6667, 6668, 6669, invalidPortNumber, };
const unsigned short* const blockedPortListEnd = blockedPortList + sizeof(blockedPortList) / sizeof(blockedPortList[0]);
#ifndef NDEBUG
static bool checkedPortList = false;
if (!checkedPortList) {
for (const unsigned short* p = blockedPortList; p != blockedPortListEnd - 1; ++p)
ASSERT(*p < *(p + 1));
checkedPortList = true;
}
#endif
if (!binary_search(blockedPortList, blockedPortListEnd, port))
return true;
if ((port == 21 || port == 22) && url.protocolIs("ftp"))
return true;
if (url.protocolIs("file"))
return true;
return false;
}
String mimeTypeFromDataURL(const String& url)
{
ASSERT(protocolIs(url, "data"));
int index = url.find(';');
if (index == -1)
index = url.find(',');
if (index != -1) {
int len = index - 5;
if (len > 0)
return url.substring(5, len);
return "text/plain"; }
return "";
}
String decodeURLEscapeSequences(const String& str)
{
return decodeURLEscapeSequences(str, UTF8Encoding());
}
String decodeURLEscapeSequences(const String& str, const TextEncoding& encoding)
{
CString cstr = str.utf8();
const char* input = cstr.data();
int inputLength = cstr.length();
url_canon::RawCanonOutputT<char> unescaped;
for (int i = 0; i < inputLength; i++) {
if (input[i] == '%') {
unsigned char ch;
if (url_canon::DecodeEscaped(input, &i, inputLength, &ch))
unescaped.push_back(ch);
else {
unescaped.push_back('%');
}
} else {
unescaped.push_back(input[i]);
}
}
url_canon::RawCanonOutputT<url_parse::UTF16Char> utf16;
for (int i = 0; i < unescaped.length(); i++) {
unsigned char uch = static_cast<unsigned char>(unescaped.at(i));
if (uch < 0x80) {
utf16.push_back(uch);
} else {
int nextCharacter = i;
unsigned codePoint;
if (url_canon::ReadUTFChar(unescaped.data(), &nextCharacter,
unescaped.length(), &codePoint)) {
url_canon::AppendUTF16Value(codePoint, &utf16);
i = nextCharacter;
} else {
while (i < nextCharacter) {
utf16.push_back(static_cast<unsigned char>(unescaped.at(i)));
i++;
}
utf16.push_back(static_cast<unsigned char>(unescaped.at(i)));
}
}
}
return String(reinterpret_cast<UChar*>(utf16.data()), utf16.length());
}
bool KURL::protocolIs(const char* protocol) const
{
assertProtocolIsGood(protocol);
if (m_url.m_parsed.scheme.len <= 0)
return !protocol;
return lowerCaseEqualsASCII(
m_url.utf8String().data() + m_url.m_parsed.scheme.begin,
m_url.utf8String().data() + m_url.m_parsed.scheme.end(),
protocol);
}
bool KURL::isLocalFile() const
{
return protocolIs("file");
}
String encodeWithURLEscapeSequences(const String& notEncodedString)
{
CString utf8 = UTF8Encoding().encode(
reinterpret_cast<const UChar*>(notEncodedString.characters()),
notEncodedString.length(),
URLEncodedEntitiesForUnencodables);
const char* input = utf8.data();
int inputLength = utf8.length();
Vector<char, 2048> buffer;
for (int i = 0; i < inputLength; i++) {
if (!input[i])
buffer.append("%00", 3);
else
buffer.append(input[i]);
}
return String(buffer.data(), buffer.size());
}
bool KURL::isHierarchical() const
{
if (!m_url.m_parsed.scheme.is_nonempty())
return false;
return url_util::IsStandard(
&m_url.utf8String().data()[m_url.m_parsed.scheme.begin],
m_url.m_parsed.scheme);
}
#ifndef NDEBUG
void KURL::print() const
{
printf("%s\n", m_url.utf8String().data());
}
#endif
void KURL::invalidate()
{
m_url.m_isValid = false;
m_url.m_protocolInHTTPFamily = false;
}
bool equalIgnoringFragmentIdentifier(const KURL& a, const KURL& b)
{
int aLength = a.m_url.utf8String().length();
if (a.m_url.m_parsed.ref.len >= 0)
aLength = a.m_url.m_parsed.ref.begin - 1;
int bLength = b.m_url.utf8String().length();
if (b.m_url.m_parsed.ref.len >= 0)
bLength = b.m_url.m_parsed.ref.begin - 1;
return aLength == bLength
&& !strncmp(a.m_url.utf8String().data(), b.m_url.utf8String().data(), aLength);
}
unsigned KURL::hostStart() const
{
return m_url.m_parsed.CountCharactersBefore(url_parse::Parsed::HOST, false);
}
unsigned KURL::hostEnd() const
{
return m_url.m_parsed.CountCharactersBefore(url_parse::Parsed::PORT, true);
}
unsigned KURL::pathStart() const
{
return m_url.m_parsed.CountCharactersBefore(url_parse::Parsed::PATH, false);
}
unsigned KURL::pathEnd() const
{
return m_url.m_parsed.CountCharactersBefore(url_parse::Parsed::QUERY, true);
}
unsigned KURL::pathAfterLastSlash() const
{
if (!m_url.m_parsed.path.is_valid())
return m_url.m_parsed.CountCharactersBefore(url_parse::Parsed::PATH, false);
url_parse::Component filename;
url_parse::ExtractFileName(m_url.utf8String().data(), m_url.m_parsed.path,
&filename);
return filename.begin;
}
const KURL& blankURL()
{
static KURL staticBlankURL(ParsedURLString, "about:blank");
return staticBlankURL;
}
bool protocolIs(const String& url, const char* protocol)
{
assertProtocolIsGood(protocol);
return url_util::FindAndCompareScheme(url.characters(), url.length(),
protocol, NULL);
}
inline bool KURL::protocolIs(const String& string, const char* protocol)
{
return WebCore::protocolIs(string, protocol);
}
bool protocolHostAndPortAreEqual(const KURL& a, const KURL& b)
{
if (a.parsed().scheme.end() != b.parsed().scheme.end())
return false;
int hostStartA = a.hostStart();
int hostLengthA = a.hostEnd() - hostStartA;
int hostStartB = b.hostStart();
int hostLengthB = b.hostEnd() - b.hostStart();
if (hostLengthA != hostLengthB)
return false;
for (int i = 0; i < a.parsed().scheme.end(); ++i)
if (a.string()[i] != b.string()[i])
return false;
for (int i = 0; i < hostLengthA; ++i)
if (a.string()[hostStartA + i] != b.string()[hostStartB + i])
return false;
if (a.port() != b.port())
return false;
return true;
}
}
#endif // USE(GOOGLEURL)