#include "config.h"
#include "KURL.h"
#include "CString.h"
#include "PlatformString.h"
#include "RegularExpression.h"
#include "TextEncoding.h"
#include <wtf/Vector.h>
#if USE(ICU_UNICODE)
#include <unicode/uidna.h>
#elif USE(QT4_UNICODE)
#include <QUrl>
#endif
using namespace std;
using namespace WTF;
namespace WebCore {
enum URLCharacterClasses {
SchemeFirstChar = 1 << 0,
SchemeChar = 1 << 1,
UserInfoChar = 1 << 2,
HostnameChar = 1 << 3,
IPv6Char = 1 << 4,
PathSegmentEndChar = 1 << 5,
HexDigitChar = 1 << 6,
BadChar = 1 << 7
};
static const char hexDigits[17] = "0123456789ABCDEF";
static const unsigned char characterClassTable[256] = {
PathSegmentEndChar, BadChar,
BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, UserInfoChar,
BadChar, PathSegmentEndChar | BadChar,
UserInfoChar, UserInfoChar | HostnameChar | IPv6Char | BadChar,
UserInfoChar, UserInfoChar,
UserInfoChar, UserInfoChar,
UserInfoChar, SchemeChar | UserInfoChar,
UserInfoChar,
SchemeChar | UserInfoChar | HostnameChar,
SchemeChar | UserInfoChar | HostnameChar,
PathSegmentEndChar,
SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
UserInfoChar | IPv6Char, UserInfoChar,
BadChar, UserInfoChar,
BadChar, PathSegmentEndChar | BadChar,
0,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
0,
0, 0,
0,
UserInfoChar | HostnameChar,
0,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar | HexDigitChar | IPv6Char,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
SchemeFirstChar | SchemeChar | UserInfoChar | HostnameChar,
0,
0, 0, UserInfoChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar,
BadChar, BadChar, BadChar, BadChar
};
static int copyPathRemovingDots(char* dst, const char* src, int srcStart, int srcEnd);
static char* encodeRelativeString(const KURL &base, const DeprecatedString& rel, const TextEncoding&);
static DeprecatedString substituteBackslashes(const DeprecatedString &string);
static inline bool isSchemeFirstChar(unsigned char c) { return characterClassTable[c] & SchemeFirstChar; }
static inline bool isSchemeChar(unsigned char c) { return characterClassTable[c] & SchemeChar; }
static inline bool isUserInfoChar(unsigned char c) { return characterClassTable[c] & UserInfoChar; }
static inline bool isHostnameChar(unsigned char c) { return characterClassTable[c] & HostnameChar; }
static inline bool isIPv6Char(unsigned char c) { return characterClassTable[c] & IPv6Char; }
static inline bool isPathSegmentEndChar(unsigned char c) { return characterClassTable[c] & PathSegmentEndChar; }
static inline bool isBadChar(unsigned char c) { return characterClassTable[c] & BadChar; }
static inline bool isHexDigit(unsigned char c) { return characterClassTable[c] & HexDigitChar; }
static inline int hexDigitValue(unsigned char c)
{
ASSERT(isHexDigit(c));
if (c < 'A')
return c - '0';
return (c - 'A' + 10) & 0xF; }
inline bool KURL::protocolIs(const String& string, const char* protocol)
{
return WebCore::protocolIs(string, protocol);
}
KURL::KURL() : m_isValid(false)
{
}
KURL::KURL(const char *url)
{
if (url && url[0] == '/') {
size_t urlLength = strlen(url) + 1;
Vector<char, 2048> buffer(urlLength + 5);
buffer[0] = 'f';
buffer[1] = 'i';
buffer[2] = 'l';
buffer[3] = 'e';
buffer[4] = ':';
memcpy(&buffer[5], url, urlLength);
parse(buffer.data(), 0);
} else
parse(url, 0);
}
KURL::KURL(const DeprecatedString &url)
{
if (!url.isEmpty() && url[0] == '/') {
Vector<char, 2048> buffer(url.length() + 6);
buffer[0] = 'f';
buffer[1] = 'i';
buffer[2] = 'l';
buffer[3] = 'e';
buffer[4] = ':';
url.copyLatin1(&buffer[5]);
parse(buffer.data(), 0);
} else
parse(url.ascii(), &url);
}
KURL::KURL(const KURL& base, const DeprecatedString& relative)
{
init(base, relative, UTF8Encoding());
}
KURL::KURL(const KURL& base, const DeprecatedString& relative, const TextEncoding& encoding)
{
init(base, relative, encoding);
}
void KURL::init(const KURL &base, const DeprecatedString &relative, const TextEncoding& encoding)
{
if (!base.m_isValid && !base.isEmpty()) {
m_isValid = false;
return;
}
bool absolute = false;
DeprecatedString substitutedRelative;
bool shouldSubstituteBackslashes = relative.contains('\\') && !(relative.startsWith("javascript:", false) || relative.startsWith("data:", false));
if (shouldSubstituteBackslashes)
substitutedRelative = substituteBackslashes(relative);
const DeprecatedString &rel = shouldSubstituteBackslashes ? substitutedRelative : relative;
bool allASCII = rel.isAllASCII();
char *strBuffer;
const char *str;
if (allASCII) {
strBuffer = 0;
str = rel.ascii();
} else {
strBuffer = encodeRelativeString(base, rel, encoding);
str = strBuffer;
}
bool strippedStart = false;
while (*str == ' ') {
str++;
strippedStart = true;
}
int len = strlen(str);
int charsToChopOffEnd = 0;
for (int pos = len - 1; pos >= 0 && str[pos] == ' '; pos--) {
charsToChopOffEnd++;
}
if (charsToChopOffEnd > 0) {
char *newStrBuffer = (char *)fastMalloc((len + 1) - charsToChopOffEnd);
strncpy(newStrBuffer, str, len - charsToChopOffEnd);
newStrBuffer[len - charsToChopOffEnd] = '\0';
fastFree(strBuffer);
strBuffer = newStrBuffer;
str = strBuffer;
}
const char *p = str;
if (isSchemeFirstChar(*p)) {
++p;
while (isSchemeChar(*p)) {
++p;
}
if (*p == ':') {
if (p[1] != '/' && base.protocol().lower() == DeprecatedString(str, p - str).lower() && base.isHierarchical())
str = p + 1;
else
absolute = true;
}
}
if (absolute) {
parse(str, (allASCII && !strippedStart && (charsToChopOffEnd == 0)) ? &rel : 0);
} else {
if (!base.isHierarchical()) {
if (str[0] == '#') {
DeprecatedString newURL = base.urlString.left(base.queryEndPos) + str;
parse(newURL.ascii(), &newURL);
} else
m_isValid = false;
if (strBuffer)
fastFree(strBuffer);
return;
}
switch(str[0]) {
case '\0':
{
*this = base;
break;
}
case '#':
{
DeprecatedString newURL = base.urlString.left(base.queryEndPos) + str;
parse(newURL.ascii(), &newURL);
break;
}
case '?':
{
DeprecatedString newURL = base.urlString.left(base.pathEndPos) + str;
parse(newURL.ascii(), &newURL);
break;
}
case '/':
{
if (str[1] == '/') {
DeprecatedString newURL = base.urlString.left(base.schemeEndPos + 1) + str;
parse(newURL.ascii(), &newURL);
} else {
DeprecatedString newURL = base.urlString.left(base.portEndPos) + str;
parse(newURL.ascii(), &newURL);
}
break;
}
default:
{
Vector<char, 2048> buffer(base.pathEndPos + 1 + strlen(str) + 1);
char *bufferPos = buffer.data();
const char *baseString = base.urlString.ascii();
const char *baseStringStart = baseString;
const char *pathStart = baseStringStart + base.portEndPos;
while (baseStringStart < pathStart) {
*bufferPos++ = *baseStringStart++;
}
char *bufferPathStart = bufferPos;
const char *baseStringEnd = baseString + base.pathEndPos;
while (baseStringEnd > baseStringStart && baseStringEnd[-1] != '/') {
baseStringEnd--;
}
if (baseStringEnd == baseStringStart) {
if (base.schemeEndPos + 1 != base.pathEndPos && *str != '\0' && *str != '?' && *str != '#') {
*bufferPos++ = '/';
}
} else {
bufferPos += copyPathRemovingDots(bufferPos, baseStringStart, 0, baseStringEnd - baseStringStart);
}
const char *relStringStart = str;
const char *relStringPos = relStringStart;
while (*relStringPos != '\0' && *relStringPos != '?' && *relStringPos != '#') {
if (relStringPos[0] == '.' && bufferPos[-1] == '/') {
if (isPathSegmentEndChar(relStringPos[1])) {
relStringPos += 1;
if (relStringPos[0] == '/') {
relStringPos++;
}
continue;
} else if (relStringPos[1] == '.' && isPathSegmentEndChar(relStringPos[2])) {
relStringPos += 2;
if (relStringPos[0] == '/') {
relStringPos++;
}
if (bufferPos > bufferPathStart + 1) {
bufferPos--;
}
while (bufferPos > bufferPathStart + 1 && bufferPos[-1] != '/') {
bufferPos--;
}
continue;
}
}
*bufferPos = *relStringPos;
relStringPos++;
bufferPos++;
}
strcpy(bufferPos, relStringPos);
parse(buffer.data(), 0);
ASSERT(strlen(buffer.data()) + 1 <= buffer.size());
break;
}
}
}
if (strBuffer) {
fastFree(strBuffer);
}
}
bool KURL::hasPath() const
{
return m_isValid && pathEndPos != portEndPos;
}
DeprecatedString KURL::lastPathComponent() const
{
if (!hasPath())
return DeprecatedString();
int end = pathEndPos - 1;
if (urlString[end] == '/')
--end;
int start = urlString.findRev('/', end);
if (start < portEndPos)
return DeprecatedString();
++start;
return urlString.mid(start, end - start + 1);
}
DeprecatedString KURL::protocol() const
{
if (!m_isValid) {
return DeprecatedString();
}
return urlString.left(schemeEndPos);
}
DeprecatedString KURL::host() const
{
if (!m_isValid) {
return DeprecatedString();
}
int start = (passwordEndPos == userStartPos) ? passwordEndPos : passwordEndPos + 1;
return decode_string(urlString.mid(start, hostEndPos - start));
}
unsigned short int KURL::port() const
{
if (!m_isValid) {
return 0;
}
if (hostEndPos != portEndPos) {
bool ok;
unsigned short result = urlString.mid(hostEndPos + 1, portEndPos - hostEndPos - 1).toUShort(&ok);
if (!ok) {
result = 0;
}
return result;
}
return 0;
}
DeprecatedString KURL::pass() const
{
if (!m_isValid) {
return DeprecatedString();
}
if (passwordEndPos == userEndPos) {
return DeprecatedString();
}
return decode_string(urlString.mid(userEndPos + 1, passwordEndPos - userEndPos - 1));
}
DeprecatedString KURL::user() const
{
if (!m_isValid) {
return DeprecatedString();
}
return decode_string(urlString.mid(userStartPos, userEndPos - userStartPos));
}
DeprecatedString KURL::ref() const
{
if (!m_isValid || fragmentEndPos == queryEndPos) {
return DeprecatedString();
}
return urlString.mid(queryEndPos + 1, fragmentEndPos - (queryEndPos + 1));
}
bool KURL::hasRef() const
{
return m_isValid && fragmentEndPos != queryEndPos;
}
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
}
bool KURL::protocolIs(const char* protocol) const
{
assertProtocolIsGood(protocol);
if (!m_isValid)
return false;
String m_string = string(); for (int i = 0; i < schemeEndPos; ++i) {
if (!protocol[i] || toASCIILower(m_string[i]) != protocol[i])
return false;
}
return !protocol[schemeEndPos]; }
DeprecatedString KURL::query() const
{
if (!m_isValid) {
return DeprecatedString();
}
return urlString.mid(pathEndPos, queryEndPos - pathEndPos);
}
DeprecatedString KURL::path() const
{
if (!m_isValid) {
return DeprecatedString();
}
return decode_string(urlString.mid(portEndPos, pathEndPos - portEndPos));
}
void KURL::setProtocol(const DeprecatedString &s)
{
if (!m_isValid) {
DeprecatedString newURL = s + ":" + urlString;
parse(newURL.ascii(), &newURL);
return;
}
DeprecatedString newURL = s + urlString.mid(schemeEndPos);
parse(newURL.ascii(), &newURL);
}
void KURL::setHost(const DeprecatedString &s)
{
if (m_isValid) {
bool slashSlashNeeded = userStartPos == schemeEndPos + 1;
int hostStart = (passwordEndPos == userStartPos) ? passwordEndPos : passwordEndPos + 1;
DeprecatedString newURL = urlString.left(hostStart) + (slashSlashNeeded ? "//" : DeprecatedString()) + s + urlString.mid(hostEndPos);
parse(newURL.ascii(), &newURL);
}
}
void KURL::setPort(unsigned short i)
{
if (m_isValid) {
bool colonNeeded = portEndPos == hostEndPos;
int portStart = (colonNeeded ? hostEndPos : hostEndPos + 1);
DeprecatedString newURL = urlString.left(portStart) + (colonNeeded ? ":" : DeprecatedString()) + DeprecatedString::number(i) + urlString.mid(portEndPos);
parse(newURL.ascii(), &newURL);
}
}
void KURL::setHostAndPort(const DeprecatedString& hostAndPort)
{
if (m_isValid) {
bool slashSlashNeeded = userStartPos == schemeEndPos + 1;
int hostStart = (passwordEndPos == userStartPos) ? passwordEndPos : passwordEndPos + 1;
DeprecatedString newURL = urlString.left(hostStart) + (slashSlashNeeded ? "//" : DeprecatedString()) + hostAndPort + urlString.mid(portEndPos);
parse(newURL.ascii(), &newURL);
}
}
void KURL::setUser(const DeprecatedString &user)
{
if (m_isValid) {
DeprecatedString u;
int end = userEndPos;
if (!user.isEmpty()) {
u = user;
if (userStartPos == schemeEndPos + 1)
u = "//" + u;
if (end == hostEndPos || (end == passwordEndPos && urlString[end] != '@'))
u += '@';
} else {
if (userEndPos == passwordEndPos && end != hostEndPos && urlString[end] == '@')
end += 1;
}
const DeprecatedString newURL = urlString.left(userStartPos) + u + urlString.mid(end);
parse(newURL.ascii(), &newURL);
}
}
void KURL::setPass(const DeprecatedString &password)
{
if (m_isValid) {
DeprecatedString p;
int end = passwordEndPos;
if (!password.isEmpty()) {
p = ':' + password + '@';
if (userEndPos == schemeEndPos + 1)
p = "//" + p;
if (end != hostEndPos && urlString[end] == '@')
end += 1;
} else {
if (userStartPos == userEndPos && end != hostEndPos && urlString[end] == '@')
end += 1;
}
const DeprecatedString newURL = urlString.left(userEndPos) + p + urlString.mid(end);
parse(newURL.ascii(), &newURL);
}
}
void KURL::setRef(const DeprecatedString &s)
{
if (m_isValid) {
DeprecatedString newURL = urlString.left(queryEndPos) + (s.isEmpty() ? DeprecatedString() : "#" + s);
parse(newURL.ascii(), &newURL);
}
}
void KURL::setQuery(const DeprecatedString &query)
{
if (m_isValid) {
DeprecatedString q;
if (!query.isNull() && (query.isEmpty() || query[0] != '?')) {
q = "?" + query;
} else {
q = query;
}
DeprecatedString newURL = urlString.left(pathEndPos) + q + urlString.mid(queryEndPos);
parse(newURL.ascii(), &newURL);
}
}
void KURL::setPath(const DeprecatedString &s)
{
if (m_isValid) {
DeprecatedString newURL = urlString.left(portEndPos) + encode_string(s) + urlString.mid(pathEndPos);
parse(newURL.ascii(), &newURL);
}
}
DeprecatedString KURL::prettyURL() const
{
if (!m_isValid) {
return urlString;
}
DeprecatedString result = protocol() + ":";
DeprecatedString authority;
if (hostEndPos != passwordEndPos) {
if (userEndPos != userStartPos) {
authority += user();
authority += "@";
}
authority += host();
if (port() != 0) {
authority += ":";
authority += DeprecatedString::number(port());
}
}
if (!authority.isEmpty())
result += "//" + authority;
else if (protocol() == "file")
result += "//";
result += path();
result += query();
if (fragmentEndPos != queryEndPos) {
result += "#" + ref();
}
return result;
}
DeprecatedString KURL::decode_string(const DeprecatedString& urlString)
{
return decode_string(urlString, UTF8Encoding());
}
DeprecatedString KURL::decode_string(const DeprecatedString& urlString, const TextEncoding& encoding)
{
DeprecatedString result("");
Vector<char, 2048> buffer(0);
int length = urlString.length();
int decodedPosition = 0;
int searchPosition = 0;
int encodedRunPosition;
while ((encodedRunPosition = urlString.find('%', searchPosition)) >= 0) {
int encodedRunEnd = encodedRunPosition;
while (length - encodedRunEnd >= 3
&& urlString[encodedRunEnd] == '%'
&& isHexDigit(urlString[encodedRunEnd + 1].latin1())
&& isHexDigit(urlString[encodedRunEnd + 2].latin1()))
encodedRunEnd += 3;
if (encodedRunEnd == encodedRunPosition) {
++searchPosition;
continue;
}
searchPosition = encodedRunEnd;
int encodedRunLength = encodedRunEnd - encodedRunPosition;
buffer.clear();
buffer.grow(encodedRunLength + 1);
urlString.copyLatin1(buffer.data(), encodedRunPosition, encodedRunLength);
char *p = buffer.data();
const char *q = buffer.data();
while (*q) {
*p++ = (hexDigitValue(q[1]) << 4) | hexDigitValue(q[2]);
q += 3;
}
String decoded = (encoding.isValid() ? encoding : UTF8Encoding()).decode(buffer.data(), p - buffer.data());
if (decoded.isEmpty())
continue;
result.append(urlString.mid(decodedPosition, encodedRunPosition - decodedPosition));
result.append(reinterpret_cast<const DeprecatedChar*>(decoded.characters()), decoded.length());
decodedPosition = encodedRunEnd;
}
result.append(urlString.mid(decodedPosition, length - decodedPosition));
return result;
}
bool KURL::isLocalFile() const
{
return equalIgnoringCase(protocol(), "file");
}
static void appendEscapingBadChars(char*& buffer, const char *strStart, size_t length)
{
char *p = buffer;
const char *str = strStart;
const char *strEnd = strStart + length;
while (str < strEnd) {
unsigned char c = *str++;
if (isBadChar(c)) {
if (c == '%' || c == '?') {
*p++ = c;
} else if (c != 0x09 && c != 0x0a && c != 0x0d) {
*p++ = '%';
*p++ = hexDigits[c >> 4];
*p++ = hexDigits[c & 0xF];
}
} else {
*p++ = c;
}
}
buffer = p;
}
static int copyPathRemovingDots(char *dst, const char *src, int srcStart, int srcEnd)
{
char *bufferPathStart = dst;
if (srcStart != srcEnd) {
const char *baseStringStart = src + srcStart;
const char *baseStringEnd = src + srcEnd;
const char *baseStringPos = baseStringStart;
ASSERT(baseStringPos[0] == '/');
*dst = *baseStringPos;
baseStringPos++;
dst++;
while (baseStringPos < baseStringEnd) {
if (baseStringPos[0] == '.' && dst[-1] == '/') {
if (baseStringPos[1] == '/' || baseStringPos + 1 == baseStringEnd) {
baseStringPos += 2;
continue;
} else if (baseStringPos[1] == '.' && (baseStringPos[2] == '/' ||
baseStringPos + 2 == baseStringEnd)) {
baseStringPos += 3;
if (dst > bufferPathStart + 1) {
dst--;
}
while (dst > bufferPathStart + 1 && dst[-1] == '/') {
dst--;
}
while (dst > bufferPathStart && dst[-1] != '/') {
dst--;
}
continue;
}
}
*dst = *baseStringPos;
baseStringPos++;
dst++;
}
}
*dst = '\0';
return dst - bufferPathStart;
}
static inline bool hasSlashDotOrDotDot(const char *str)
{
const unsigned char *p = reinterpret_cast<const unsigned char *>(str);
if (!*p)
return false;
unsigned char pc = *p;
while (unsigned char c = *++p) {
if (c == '.' && (pc == '/' || pc == '.'))
return true;
pc = c;
}
return false;
}
static inline bool matchLetter(char c, char lowercaseLetter)
{
return (c | 0x20) == lowercaseLetter;
}
void KURL::parse(const char *url, const DeprecatedString *originalString)
{
m_isValid = true;
if (!url || url[0] == '\0') {
m_isValid = false;
urlString = url;
return;
}
if (!isSchemeFirstChar(url[0])) {
m_isValid = false;
urlString = url;
return;
}
int schemeEnd = 0;
while (isSchemeChar(url[schemeEnd])) {
schemeEnd++;
}
if (url[schemeEnd] != ':') {
m_isValid = false;
urlString = url;
return;
}
int userStart = schemeEnd + 1;
int userEnd;
int passwordStart;
int passwordEnd;
int hostStart;
int hostEnd;
int portStart;
int portEnd;
bool hierarchical = url[schemeEnd + 1] == '/';
if (hierarchical && url[schemeEnd + 2] == '/') {
userStart += 2;
userEnd = userStart;
int colonPos = 0;
while (isUserInfoChar(url[userEnd])) {
if (url[userEnd] == ':' && colonPos == 0) {
colonPos = userEnd;
}
userEnd++;
}
if (url[userEnd] == '@') {
if (colonPos != 0) {
passwordEnd = userEnd;
userEnd = colonPos;
passwordStart = colonPos + 1;
} else {
passwordStart = passwordEnd = userEnd;
}
hostStart = passwordEnd + 1;
} else if (url[userEnd] == '[' || isPathSegmentEndChar(url[userEnd])) {
userEnd = userStart;
passwordStart = passwordEnd = userEnd;
hostStart = userStart;
} else {
m_isValid = false;
urlString = url;
return;
}
hostEnd = hostStart;
if (url[hostEnd] == '[') {
hostEnd++;
while (isIPv6Char(url[hostEnd])) {
hostEnd++;
}
if (url[hostEnd] == ']') {
hostEnd++;
} else {
m_isValid = false;
urlString = url;
return;
}
} else {
while (isHostnameChar(url[hostEnd])) {
hostEnd++;
}
}
if (url[hostEnd] == ':') {
portStart = portEnd = hostEnd + 1;
portEnd = portStart;
while (isASCIIDigit(url[portEnd])) {
portEnd++;
}
} else {
portStart = portEnd = hostEnd;
}
if (!isPathSegmentEndChar(url[portEnd])) {
m_isValid = false;
urlString = url;
return;
}
} else {
userEnd = userStart;
passwordStart = passwordEnd = userEnd;
hostStart = hostEnd = passwordEnd;
portStart = portEnd = hostEnd;
}
int pathStart = portEnd;
int pathEnd = pathStart;
int queryStart;
int queryEnd;
int fragmentStart;
int fragmentEnd;
if (!hierarchical) {
while (url[pathEnd] != '\0' && url[pathEnd] != '?' && url[pathEnd] != '#')
pathEnd++;
queryStart = pathEnd;
queryEnd = queryStart;
if (url[queryStart] == '?') {
while (url[queryEnd] != '\0' && url[queryEnd] != '#')
queryEnd++;
}
fragmentStart = queryEnd;
fragmentEnd = fragmentStart;
if (url[fragmentStart] == '#') {
fragmentStart++;
fragmentEnd = fragmentStart;
while (url[fragmentEnd] != '\0')
fragmentEnd++;
}
}
else {
while (url[pathEnd] != '\0' && url[pathEnd] != '?' && url[pathEnd] != '#') {
pathEnd++;
}
queryStart = pathEnd;
queryEnd = queryStart;
if (url[queryStart] == '?') {
while (url[queryEnd] != '\0' && url[queryEnd] != '#') {
queryEnd++;
}
}
fragmentStart = queryEnd;
fragmentEnd = fragmentStart;
if (url[fragmentStart] == '#') {
fragmentStart++;
fragmentEnd = fragmentStart;
while (url[fragmentEnd] != '\0') {
fragmentEnd++;
}
}
}
Vector<char, 4096> buffer(fragmentEnd * 3 + 1);
char *p = buffer.data();
const char *strPtr = url;
const char *schemeEndPtr = url + schemeEnd;
while (strPtr < schemeEndPtr) {
*p++ = *strPtr++;
}
schemeEndPos = p - buffer.data();
bool isHTTPorHTTPS = matchLetter(url[0], 'h')
&& matchLetter(url[1], 't')
&& matchLetter(url[2], 't')
&& matchLetter(url[3], 'p')
&& (url[4] == ':'
|| (matchLetter(url[4], 's') && url[5] == ':'));
bool hostIsLocalHost = portEnd - userStart == 9
&& matchLetter(url[userStart], 'l')
&& matchLetter(url[userStart+1], 'o')
&& matchLetter(url[userStart+2], 'c')
&& matchLetter(url[userStart+3], 'a')
&& matchLetter(url[userStart+4], 'l')
&& matchLetter(url[userStart+5], 'h')
&& matchLetter(url[userStart+6], 'o')
&& matchLetter(url[userStart+7], 's')
&& matchLetter(url[userStart+8], 't');
bool isFile = matchLetter(url[0], 'f')
&& matchLetter(url[1], 'i')
&& matchLetter(url[2], 'l')
&& matchLetter(url[3], 'e')
&& url[4] == ':';
bool degenFilePath = pathStart == pathEnd
&& (hostStart == hostEnd
|| hostIsLocalHost);
bool haveNonHostAuthorityPart = userStart != userEnd || passwordStart != passwordEnd || portStart != portEnd;
*p++ = ':';
if (isFile ? !degenFilePath
: (haveNonHostAuthorityPart || hostStart != hostEnd)) {
*p++ = '/';
*p++ = '/';
userStartPos = p - buffer.data();
strPtr = url + userStart;
const char *userEndPtr = url + userEnd;
while (strPtr < userEndPtr) {
*p++ = *strPtr++;
}
userEndPos = p - buffer.data();
if (passwordEnd != passwordStart) {
*p++ = ':';
strPtr = url + passwordStart;
const char *passwordEndPtr = url + passwordEnd;
while (strPtr < passwordEndPtr) {
*p++ = *strPtr++;
}
}
passwordEndPos = p - buffer.data();
if (p - buffer.data() != userStartPos) {
*p++ = '@';
}
if (!(isFile && hostIsLocalHost && !haveNonHostAuthorityPart)) {
strPtr = url + hostStart;
const char *hostEndPtr = url + hostEnd;
while (strPtr < hostEndPtr) {
*p++ = *strPtr++;
}
}
hostEndPos = p - buffer.data();
if (hostEnd != portStart) {
*p++ = ':';
strPtr = url + portStart;
const char *portEndPtr = url + portEnd;
while (strPtr < portEndPtr) {
*p++ = *strPtr++;
}
}
portEndPos = p - buffer.data();
} else {
userStartPos = userEndPos = passwordEndPos = hostEndPos = portEndPos = p - buffer.data();
}
if (isHTTPorHTTPS && pathEnd - pathStart == 0) {
*p++ = '/';
}
if (hierarchical && hasSlashDotOrDotDot(url)) {
Vector<char, 4096> path_buffer(pathEnd - pathStart + 1);
copyPathRemovingDots(path_buffer.data(), url, pathStart, pathEnd);
appendEscapingBadChars(p, path_buffer.data(), strlen(path_buffer.data()));
} else
appendEscapingBadChars(p, url + pathStart, pathEnd - pathStart);
pathEndPos = p - buffer.data();
appendEscapingBadChars(p, url + queryStart, queryEnd - queryStart);
queryEndPos = p - buffer.data();
if (fragmentEnd != queryEnd) {
*p++ = '#';
appendEscapingBadChars(p, url + fragmentStart, fragmentEnd - fragmentStart);
}
fragmentEndPos = p - buffer.data();
if (originalString && strncmp(buffer.data(), url, fragmentEndPos) == 0) {
urlString = *originalString;
} else
urlString = DeprecatedString(buffer.data(), fragmentEndPos);
ASSERT(p - buffer.data() <= (int)buffer.size());
}
bool operator==(const KURL &a, const KURL &b)
{
return a.urlString == b.urlString;
}
bool equalIgnoringRef(const KURL& a, const KURL& b)
{
return a.urlString.left(a.queryEndPos) == b.urlString.left(b.queryEndPos);
}
DeprecatedString KURL::encode_string(const DeprecatedString& notEncodedString)
{
DeprecatedCString asUTF8 = notEncodedString.utf8();
Vector<char, 4096> buffer(asUTF8.length() * 3 + 1);
char *p = buffer.data();
const char *str = asUTF8;
const char *strEnd = str + asUTF8.length();
while (str < strEnd) {
unsigned char c = *str++;
if (isBadChar(c)) {
*p++ = '%';
*p++ = hexDigits[c >> 4];
*p++ = hexDigits[c & 0xF];
} else
*p++ = c;
}
DeprecatedString result(buffer.data(), p - buffer.data());
ASSERT(p - buffer.data() <= (int)buffer.size());
return result;
}
static DeprecatedString encodeHostname(const DeprecatedString &s)
{
const unsigned hostnameBufferLength = 2048;
if (s.isAllASCII() || s.length() > hostnameBufferLength)
return s;
#if USE(ICU_UNICODE)
UChar buffer[hostnameBufferLength];
UErrorCode error = U_ZERO_ERROR;
int32_t numCharactersConverted = uidna_IDNToASCII
(reinterpret_cast<const UChar *>(s.unicode()), s.length(), buffer, hostnameBufferLength, UIDNA_ALLOW_UNASSIGNED, 0, &error);
if (error != U_ZERO_ERROR) {
return s;
}
return DeprecatedString(reinterpret_cast<DeprecatedChar *>(buffer), numCharactersConverted);
#elif USE(QT4_UNICODE)
QByteArray result = QUrl::toAce(s);
return DeprecatedString(result.constData(), result.length());
#endif
}
static Vector<pair<int, int> > findHostnamesInMailToURL(const DeprecatedString &s)
{
Vector<pair<int, int> > a;
int p = 0;
while (1) {
int hostnameOrStringStart = s.find(RegularExpression("[\"@?]"), p);
if (hostnameOrStringStart == -1) {
return a;
}
DeprecatedChar c = s[hostnameOrStringStart];
p = hostnameOrStringStart + 1;
if (c == '?') {
return a;
}
if (c == '@') {
int hostnameStart = p;
int hostnameEnd = s.find(RegularExpression("[>,?]"), p);
bool done;
if (hostnameEnd == -1) {
hostnameEnd = s.length();
done = true;
} else {
p = hostnameEnd;
done = false;
}
a.append(make_pair(hostnameStart, hostnameEnd));
if (done)
return a;
} else {
ASSERT(c == '"');
while (1) {
int escapedCharacterOrStringEnd = s.find(RegularExpression("[\"\\]"), p);
if (escapedCharacterOrStringEnd == -1)
return a;
c = s[escapedCharacterOrStringEnd];
p = escapedCharacterOrStringEnd + 1;
if (c == '"')
break;
ASSERT(c == '\\');
if (p == static_cast<int>(s.length()))
return a;
++p;
}
}
}
}
static bool findHostnameInHierarchicalURL(const DeprecatedString &s, int &startOffset, int &endOffset)
{
int separator = s.find("://");
if (separator <= 0) {
return false;
}
if (!isSchemeFirstChar(s[0].latin1())) {
return false;
}
for (int i = 1; i < separator; ++i) {
if (!isSchemeChar(s[i].latin1())) {
return false;
}
}
int authorityStart = separator + 3;
int length = s.length();
int hostnameEnd = length;
for (int i = authorityStart; i < length; ++i) {
char c = s[i].latin1();
if (c == ':' || (isPathSegmentEndChar(c) && c != '\0')) {
hostnameEnd = i;
break;
}
}
int userInfoTerminator = s.find('@', authorityStart);
int hostnameStart;
if (userInfoTerminator == -1 || userInfoTerminator > hostnameEnd) {
hostnameStart = authorityStart;
} else {
hostnameStart = userInfoTerminator + 1;
}
startOffset = hostnameStart;
endOffset = hostnameEnd;
return true;
}
static DeprecatedString encodeHostnames(const DeprecatedString &s)
{
if (s.startsWith("mailto:", false)) {
const Vector<pair<int, int> > hostnameRanges = findHostnamesInMailToURL(s);
int n = hostnameRanges.size();
if (n != 0) {
DeprecatedString result;
unsigned p = 0;
for (int i = 0; i < n; ++i) {
const pair<int, int> &r = hostnameRanges[i];
result += s.mid(p, r.first);
result += encodeHostname(s.mid(r.first, r.second - r.first));
p = r.second;
}
result += s.mid(p);
return result;
}
} else {
int hostStart, hostEnd;
if (findHostnameInHierarchicalURL(s, hostStart, hostEnd)) {
return s.left(hostStart) + encodeHostname(s.mid(hostStart, hostEnd - hostStart)) + s.mid(hostEnd);
}
}
return s;
}
static char *encodeRelativeString(const KURL &base, const DeprecatedString &rel, const TextEncoding& encoding)
{
DeprecatedString s = encodeHostnames(rel);
char *strBuffer;
TextEncoding pathEncoding(UTF8Encoding());
TextEncoding otherEncoding = (encoding.isValid() && !rel.startsWith("mailto:", false)) ? encoding : UTF8Encoding();
int pathEnd = -1;
if (pathEncoding != otherEncoding) {
pathEnd = s.find(RegularExpression("[?#]"));
}
if (pathEnd == -1) {
CString decoded = pathEncoding.encode(reinterpret_cast<const UChar*>(s.unicode()), s.length());
int decodedLength = decoded.length();
strBuffer = static_cast<char *>(fastMalloc(decodedLength + 1));
memcpy(strBuffer, decoded.data(), decodedLength);
strBuffer[decodedLength] = 0;
} else {
int length = s.length();
CString pathDecoded = pathEncoding.encode(reinterpret_cast<const UChar*>(s.unicode()), pathEnd);
CString otherDecoded = otherEncoding.encode(reinterpret_cast<const UChar*>(s.unicode()) + pathEnd, length - pathEnd);
int pathDecodedLength = pathDecoded.length();
int otherDecodedLength = otherDecoded.length();
strBuffer = static_cast<char *>(fastMalloc(pathDecodedLength + otherDecodedLength + 1));
memcpy(strBuffer, pathDecoded.data(), pathDecodedLength);
memcpy(strBuffer + pathDecodedLength, otherDecoded.data(), otherDecodedLength);
strBuffer[pathDecodedLength + otherDecodedLength] = 0;
}
return strBuffer;
}
static DeprecatedString substituteBackslashes(const DeprecatedString &string)
{
int questionPos = string.find('?');
int hashPos = string.find('#');
unsigned pathEnd;
if (hashPos >= 0 && (questionPos < 0 || questionPos > hashPos)) {
pathEnd = hashPos;
} else if (questionPos >= 0) {
pathEnd = questionPos;
} else {
pathEnd = string.length();
}
return string.left(pathEnd).replace('\\','/') + string.mid(pathEnd);
}
bool KURL::isHierarchical() const
{
if (!m_isValid)
return false;
ASSERT(urlString[schemeEndPos] == ':');
return urlString[schemeEndPos + 1] == '/';
}
bool protocolIs(const String& url, const char* protocol)
{
assertProtocolIsGood(protocol);
for (int i = 0; ; ++i) {
if (!protocol[i])
return url[i] == ':';
if (toASCIILower(url[i]) != protocol[i])
return false;
}
}
bool protocolHostAndPortAreEqual(const KURL& a, const KURL& b)
{
if (a.schemeEndPos != b.schemeEndPos)
return false;
int hostStartA = a.hostStart();
int hostStartB = b.hostStart();
if (a.hostEndPos - hostStartA != b.hostEndPos - hostStartB)
return false;
for (int i = 0; i < a.schemeEndPos; ++i)
if (a.string()[i] != b.string()[i])
return false;
for (int i = hostStartA; i < a.hostEndPos; ++i)
if (a.string()[i] != b.string()[i])
return false;
if (a.port() != b.port())
return false;
return true;
}
#ifndef NDEBUG
void KURL::print() const
{
printf("%s\n", urlString.ascii());
}
#endif
}