This source file includes following definitions.
- assertProtocolIsGood
- asURLChar8Subtle
- charactersOrEmpty
- isSchemeFirstChar
- isSchemeChar
- isUnicodeEncoding
- ConvertFromUTF16
- isValidProtocol
- strippedForUseAsReferrer
- isLocalFile
- protocolIsJavaScript
- blankURL
- isBlankURL
- elidedString
- createIsolated
- m_string
- m_string
- m_string
- copy
- isNull
- isEmpty
- isValid
- hasPort
- protocolIsInHTTPFamily
- hasPath
- lastPathComponent
- protocol
- host
- port
- pass
- user
- fragmentIdentifier
- hasFragmentIdentifier
- baseAsString
- query
- path
- setProtocol
- setHost
- parsePortFromStringPosition
- setHostAndPort
- removePort
- setPort
- setPort
- setUser
- setPass
- setFragmentIdentifier
- removeFragmentIdentifier
- setQuery
- setPath
- decodeURLEscapeSequences
- decodeURLEscapeSequences
- encodeWithURLEscapeSequences
- isHierarchical
- print
- equalIgnoringFragmentIdentifier
- hostStart
- hostEnd
- pathStart
- pathEnd
- pathAfterLastSlash
- protocolIs
- init
- init
- initInnerURL
- internalProtocolIs
- checkIfProtocolIsInHTTPFamily
- initProtocolIsInHTTPFamily
- protocolIs
- stringForInvalidComponent
- componentString
- replaceComponents
- isSafeToSendToAnotherThread
#include "config.h"
#include "platform/weborigin/KURL.h"
#include "platform/weborigin/KnownPorts.h"
#include "wtf/StdLibExtras.h"
#include "wtf/text/CString.h"
#include "wtf/text/StringHash.h"
#include "wtf/text/StringUTF8Adaptor.h"
#include "wtf/text/TextEncoding.h"
#include <algorithm>
#include <url/url_util.h>
#ifndef NDEBUG
#include <stdio.h>
#endif
namespace WebCore {
static const int maximumValidPortNumber = 0xFFFE;
static const int invalidPortNumber = 0xFFFF;
static void assertProtocolIsGood(const char* protocol)
{
#ifndef NDEBUG
const char* p = protocol;
while (*p) {
ASSERT(*p > ' ' && *p < 0x7F && !(*p >= 'A' && *p <= 'Z'));
++p;
}
#endif
}
static const char* asURLChar8Subtle(const String& spec)
{
ASSERT(spec.is8Bit());
return reinterpret_cast<const char*>(spec.characters8());
}
static const char* charactersOrEmpty(const StringUTF8Adaptor& string)
{
static const char zero = 0;
return string.data() ? string.data() : &zero;
}
static bool isSchemeFirstChar(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static bool isSchemeChar(char c)
{
return isSchemeFirstChar(c) || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '+';
}
static bool isUnicodeEncoding(const WTF::TextEncoding* encoding)
{
return encoding->encodingForFormSubmission() == UTF8Encoding();
}
namespace {
class KURLCharsetConverter FINAL : public url_canon::CharsetConverter {
public:
explicit KURLCharsetConverter(const WTF::TextEncoding* encoding)
: m_encoding(encoding)
{
}
virtual void ConvertFromUTF16(const url_parse::UTF16Char* input, int inputLength, url_canon::CanonOutput* output) OVERRIDE
{
CString encoded = m_encoding->normalizeAndEncode(String(input, inputLength), WTF::URLEncodedEntitiesForUnencodables);
output->Append(encoded.data(), static_cast<int>(encoded.length()));
}
private:
const WTF::TextEncoding* m_encoding;
};
}
bool isValidProtocol(const String& protocol)
{
if (protocol.isEmpty())
return false;
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;
}
String KURL::strippedForUseAsReferrer() const
{
if (isBlankURL() || protocolIs("data") || protocolIs("javascript"))
return String();
KURL referrer(*this);
referrer.setUser(String());
referrer.setPass(String());
referrer.removeFragmentIdentifier();
return referrer.string();
}
bool KURL::isLocalFile() const
{
return protocolIs("file");
}
bool protocolIsJavaScript(const String& url)
{
return protocolIs(url, "javascript");
}
const KURL& blankURL()
{
DEFINE_STATIC_LOCAL(KURL, staticBlankURL, (ParsedURLString, "about:blank"));
return staticBlankURL;
}
bool KURL::isBlankURL() const
{
return protocolIs("about");
}
String KURL::elidedString() const
{
if (string().length() <= 1024)
return string();
return string().left(511) + "..." + string().right(510);
}
KURL::KURL(ParsedURLStringTag, const String& url)
{
if (!url.isNull())
init(KURL(), url, 0);
else {
m_isValid = false;
m_protocolIsInHTTPFamily = false;
}
}
KURL KURL::createIsolated(ParsedURLStringTag, const String& url)
{
return KURL(ParsedURLString, url).copy();
}
KURL::KURL(const KURL& base, const String& relative)
{
init(base, relative, 0);
}
KURL::KURL(const KURL& base, const String& relative, const WTF::TextEncoding& encoding)
{
init(base, relative, &encoding.encodingForFormSubmission());
}
KURL::KURL(const AtomicString& canonicalString, const url_parse::Parsed& parsed, bool isValid)
: m_isValid(isValid)
, m_protocolIsInHTTPFamily(false)
, m_parsed(parsed)
, m_string(canonicalString)
{
initProtocolIsInHTTPFamily();
initInnerURL();
}
KURL::KURL(WTF::HashTableDeletedValueType)
: m_isValid(false)
, m_protocolIsInHTTPFamily(false)
, m_string(WTF::HashTableDeletedValue)
{
}
KURL::KURL(const KURL& other)
: m_isValid(other.m_isValid)
, m_protocolIsInHTTPFamily(other.m_protocolIsInHTTPFamily)
, m_parsed(other.m_parsed)
, m_string(other.m_string)
{
if (other.m_innerURL.get())
m_innerURL = adoptPtr(new KURL(other.m_innerURL->copy()));
}
KURL& KURL::operator=(const KURL& other)
{
m_isValid = other.m_isValid;
m_protocolIsInHTTPFamily = other.m_protocolIsInHTTPFamily;
m_parsed = other.m_parsed;
m_string = other.m_string;
if (other.m_innerURL)
m_innerURL = adoptPtr(new KURL(other.m_innerURL->copy()));
else
m_innerURL.clear();
return *this;
}
KURL KURL::copy() const
{
KURL result;
result.m_isValid = m_isValid;
result.m_protocolIsInHTTPFamily = m_protocolIsInHTTPFamily;
result.m_parsed = m_parsed;
result.m_string = m_string.isolatedCopy();
if (result.m_innerURL)
result.m_innerURL = adoptPtr(new KURL(m_innerURL->copy()));
return result;
}
bool KURL::isNull() const
{
return m_string.isNull();
}
bool KURL::isEmpty() const
{
return m_string.isEmpty();
}
bool KURL::isValid() const
{
return m_isValid;
}
bool KURL::hasPort() const
{
return hostEnd() < pathStart();
}
bool KURL::protocolIsInHTTPFamily() const
{
return m_protocolIsInHTTPFamily;
}
bool KURL::hasPath() const
{
return m_parsed.path.len >= 0;
}
String KURL::lastPathComponent() const
{
if (!m_isValid)
return stringForInvalidComponent();
ASSERT(!m_string.isNull());
url_parse::Component path = m_parsed.path;
if (path.len > 0 && m_string[path.end() - 1] == '/')
path.len--;
url_parse::Component file;
if (m_string.is8Bit())
url_parse::ExtractFileName(asURLChar8Subtle(m_string), path, &file);
else
url_parse::ExtractFileName(m_string.characters16(), path, &file);
if (!file.is_nonempty())
return String();
return componentString(file);
}
String KURL::protocol() const
{
return componentString(m_parsed.scheme);
}
String KURL::host() const
{
return componentString(m_parsed.host);
}
unsigned short KURL::port() const
{
if (!m_isValid || m_parsed.port.len <= 0)
return 0;
ASSERT(!m_string.isNull());
int port = m_string.is8Bit() ?
url_parse::ParsePort(asURLChar8Subtle(m_string), m_parsed.port) :
url_parse::ParsePort(m_string.characters16(), 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_parsed.password.is_nonempty())
return String();
return componentString(m_parsed.password);
}
String KURL::user() const
{
return componentString(m_parsed.username);
}
String KURL::fragmentIdentifier() const
{
if (!m_parsed.ref.is_valid())
return String();
return componentString(m_parsed.ref);
}
bool KURL::hasFragmentIdentifier() const
{
return m_parsed.ref.len >= 0;
}
String KURL::baseAsString() const
{
return m_string.left(pathAfterLastSlash());
}
String KURL::query() const
{
if (m_parsed.query.len >= 0)
return componentString(m_parsed.query);
if (m_parsed.query.is_valid())
return emptyString();
return String();
}
String KURL::path() const
{
return componentString(m_parsed.path);
}
bool KURL::setProtocol(const String& protocol)
{
int separatorPosition = protocol.find(':');
String newProtocol = protocol.substring(0, separatorPosition);
StringUTF8Adaptor newProtocolUTF8(newProtocol);
url_canon::RawCanonOutputT<char> canonProtocol;
url_parse::Component protocolComponent;
if (!url_canon::CanonicalizeScheme(newProtocolUTF8.data(), url_parse::Component(0, newProtocolUTF8.length()), &canonProtocol, &protocolComponent)
|| !protocolComponent.is_nonempty())
return false;
url_canon::Replacements<char> replacements;
replacements.SetScheme(charactersOrEmpty(newProtocolUTF8), url_parse::Component(0, newProtocolUTF8.length()));
replaceComponents(replacements);
return true;
}
void KURL::setHost(const String& host)
{
StringUTF8Adaptor hostUTF8(host);
url_canon::Replacements<char> replacements;
replacements.SetHost(charactersOrEmpty(hostUTF8), url_parse::Component(0, hostUTF8.length()));
replaceComponents(replacements);
}
static String parsePortFromStringPosition(const String& value, unsigned portStart)
{
size_t length = value.length();
unsigned portEnd = portStart;
while (isASCIIDigit(value[portEnd]) && portEnd < length)
++portEnd;
while (value[portStart] == '0' && portStart < portEnd - 1)
++portStart;
if (portStart == portEnd)
return "0";
return value.substring(portStart, portEnd - portStart);
}
void KURL::setHostAndPort(const String& hostAndPort)
{
size_t separator = hostAndPort.find(':');
if (!separator)
return;
if (separator == kNotFound) {
url_canon::Replacements<char> replacements;
StringUTF8Adaptor hostUTF8(hostAndPort);
replacements.SetHost(charactersOrEmpty(hostUTF8), url_parse::Component(0, hostUTF8.length()));
replaceComponents(replacements);
return;
}
String host = hostAndPort.substring(0, separator);
String port = parsePortFromStringPosition(hostAndPort, separator + 1);
StringUTF8Adaptor hostUTF8(host);
StringUTF8Adaptor portUTF8(port);
url_canon::Replacements<char> replacements;
replacements.SetHost(charactersOrEmpty(hostUTF8), url_parse::Component(0, hostUTF8.length()));
replacements.SetPort(charactersOrEmpty(portUTF8), url_parse::Component(0, portUTF8.length()));
replaceComponents(replacements);
}
void KURL::removePort()
{
if (!hasPort())
return;
url_canon::Replacements<char> replacements;
replacements.ClearPort();
replaceComponents(replacements);
}
void KURL::setPort(const String& port)
{
String parsedPort = parsePortFromStringPosition(port, 0);
setPort(parsedPort.toUInt());
}
void KURL::setPort(unsigned short port)
{
if (isDefaultPortForProtocol(port, protocol())) {
removePort();
return;
}
String portString = String::number(port);
ASSERT(portString.is8Bit());
url_canon::Replacements<char> replacements;
replacements.SetPort(reinterpret_cast<const char*>(portString.characters8()), url_parse::Component(0, portString.length()));
replaceComponents(replacements);
}
void KURL::setUser(const String& user)
{
if (user.isEmpty() && !m_parsed.username.is_valid())
return;
StringUTF8Adaptor userUTF8(user);
url_canon::Replacements<char> replacements;
replacements.SetUsername(charactersOrEmpty(userUTF8), url_parse::Component(0, userUTF8.length()));
replaceComponents(replacements);
}
void KURL::setPass(const String& pass)
{
if (pass.isEmpty() && !m_parsed.password.is_valid())
return;
StringUTF8Adaptor passUTF8(pass);
url_canon::Replacements<char> replacements;
replacements.SetPassword(charactersOrEmpty(passUTF8), url_parse::Component(0, passUTF8.length()));
replaceComponents(replacements);
}
void KURL::setFragmentIdentifier(const String& fragment)
{
if (fragment.isNull() && !m_parsed.ref.is_valid())
return;
StringUTF8Adaptor fragmentUTF8(fragment);
url_canon::Replacements<char> replacements;
if (fragment.isNull())
replacements.ClearRef();
else
replacements.SetRef(charactersOrEmpty(fragmentUTF8), url_parse::Component(0, fragmentUTF8.length()));
replaceComponents(replacements);
}
void KURL::removeFragmentIdentifier()
{
url_canon::Replacements<char> replacements;
replacements.ClearRef();
replaceComponents(replacements);
}
void KURL::setQuery(const String& query)
{
StringUTF8Adaptor queryUTF8(query);
url_canon::Replacements<char> replacements;
if (query.isNull()) {
replacements.ClearQuery();
} else if (query.length() > 0 && query[0] == '?') {
replacements.SetQuery(charactersOrEmpty(queryUTF8), url_parse::Component(1, queryUTF8.length() - 1));
} else {
replacements.SetQuery(charactersOrEmpty(queryUTF8), url_parse::Component(0, queryUTF8.length()));
}
replaceComponents(replacements);
}
void KURL::setPath(const String& path)
{
StringUTF8Adaptor pathUTF8(path);
url_canon::Replacements<char> replacements;
replacements.SetPath(charactersOrEmpty(pathUTF8), url_parse::Component(0, pathUTF8.length()));
replaceComponents(replacements);
}
String decodeURLEscapeSequences(const String& string)
{
return decodeURLEscapeSequences(string, UTF8Encoding());
}
String decodeURLEscapeSequences(const String& string, const WTF::TextEncoding& encoding)
{
StringUTF8Adaptor stringUTF8(string);
url_canon::RawCanonOutputT<url_parse::UTF16Char> unescaped;
url_util::DecodeURLEscapeSequences(stringUTF8.data(), stringUTF8.length(), &unescaped);
return StringImpl::create8BitIfPossible(reinterpret_cast<UChar*>(unescaped.data()), unescaped.length());
}
String encodeWithURLEscapeSequences(const String& notEncodedString)
{
CString utf8 = UTF8Encoding().normalizeAndEncode(notEncodedString, WTF::URLEncodedEntitiesForUnencodables);
url_canon::RawCanonOutputT<char> buffer;
int inputLength = utf8.length();
if (buffer.length() < inputLength * 3)
buffer.Resize(inputLength * 3);
url_util::EncodeURIComponent(utf8.data(), inputLength, &buffer);
String escaped(buffer.data(), buffer.length());
escaped.replace("%2F", "/");
return escaped;
}
bool KURL::isHierarchical() const
{
if (m_string.isNull() || !m_parsed.scheme.is_nonempty())
return false;
return m_string.is8Bit() ?
url_util::IsStandard(asURLChar8Subtle(m_string), m_parsed.scheme) :
url_util::IsStandard(m_string.characters16(), m_parsed.scheme);
}
#ifndef NDEBUG
void KURL::print() const
{
printf("%s\n", m_string.utf8().data());
}
#endif
bool equalIgnoringFragmentIdentifier(const KURL& a, const KURL& b)
{
int aLength = a.m_string.length();
if (a.m_parsed.ref.len >= 0)
aLength = a.m_parsed.ref.begin - 1;
int bLength = b.m_string.length();
if (b.m_parsed.ref.len >= 0)
bLength = b.m_parsed.ref.begin - 1;
if (aLength != bLength)
return false;
const String& aString = a.m_string;
const String& bString = b.m_string;
for (int i = 0; i < aLength; ++i) {
if (aString[i] != bString[i])
return false;
}
return true;
}
unsigned KURL::hostStart() const
{
return m_parsed.CountCharactersBefore(url_parse::Parsed::HOST, false);
}
unsigned KURL::hostEnd() const
{
return m_parsed.CountCharactersBefore(url_parse::Parsed::PORT, true);
}
unsigned KURL::pathStart() const
{
return m_parsed.CountCharactersBefore(url_parse::Parsed::PATH, false);
}
unsigned KURL::pathEnd() const
{
return m_parsed.CountCharactersBefore(url_parse::Parsed::QUERY, true);
}
unsigned KURL::pathAfterLastSlash() const
{
if (m_string.isNull())
return 0;
if (!m_isValid || !m_parsed.path.is_valid())
return m_parsed.CountCharactersBefore(url_parse::Parsed::PATH, false);
url_parse::Component filename;
if (m_string.is8Bit())
url_parse::ExtractFileName(asURLChar8Subtle(m_string), m_parsed.path, &filename);
else
url_parse::ExtractFileName(m_string.characters16(), m_parsed.path, &filename);
return filename.begin;
}
bool protocolIs(const String& url, const char* protocol)
{
assertProtocolIsGood(protocol);
if (url.isNull())
return false;
if (url.is8Bit())
return url_util::FindAndCompareScheme(asURLChar8Subtle(url), url.length(), protocol, 0);
return url_util::FindAndCompareScheme(url.characters16(), url.length(), protocol, 0);
}
void KURL::init(const KURL& base, const String& relative, const WTF::TextEncoding* queryEncoding)
{
if (!relative.isNull() && relative.is8Bit()) {
StringUTF8Adaptor relativeUTF8(relative);
init(base, relativeUTF8.data(), relativeUTF8.length(), queryEncoding);
} else
init(base, relative.characters16(), relative.length(), queryEncoding);
initProtocolIsInHTTPFamily();
initInnerURL();
}
template <typename CHAR>
void KURL::init(const KURL& base, const CHAR* relative, int relativeLength, const WTF::TextEncoding* queryEncoding)
{
KURLCharsetConverter charsetConverterObject(queryEncoding);
KURLCharsetConverter* charsetConverter = (!queryEncoding || isUnicodeEncoding(queryEncoding)) ? 0 : &charsetConverterObject;
StringUTF8Adaptor baseUTF8(base.string());
url_canon::RawCanonOutputT<char> output;
m_isValid = url_util::ResolveRelative(baseUTF8.data(), baseUTF8.length(), base.m_parsed, relative, relativeLength, charsetConverter, &output, &m_parsed);
m_string = AtomicString::fromUTF8(output.data(), output.length());
}
void KURL::initInnerURL()
{
if (!m_isValid) {
m_innerURL.clear();
return;
}
if (url_parse::Parsed* innerParsed = m_parsed.inner_parsed())
m_innerURL = adoptPtr(new KURL(ParsedURLString, m_string.substring(innerParsed->scheme.begin, innerParsed->Length() - innerParsed->scheme.begin)));
else
m_innerURL.clear();
}
template<typename CHAR>
bool internalProtocolIs(const url_parse::Component& scheme, const CHAR* spec, const char* protocol)
{
const CHAR* begin = spec + scheme.begin;
const CHAR* end = begin + scheme.len;
while (begin != end && *protocol) {
ASSERT(toASCIILower(*protocol) == *protocol);
if (toASCIILower(*begin++) != *protocol++)
return false;
}
return begin == end && !*protocol;
}
template<typename CHAR>
bool checkIfProtocolIsInHTTPFamily(const url_parse::Component& scheme, const CHAR* spec)
{
if (scheme.len == 4)
return internalProtocolIs(scheme, spec, "http");
if (scheme.len == 5)
return internalProtocolIs(scheme, spec, "https");
return false;
}
void KURL::initProtocolIsInHTTPFamily()
{
if (!m_isValid) {
m_protocolIsInHTTPFamily = false;
return;
}
ASSERT(!m_string.isNull());
m_protocolIsInHTTPFamily = m_string.is8Bit() ?
checkIfProtocolIsInHTTPFamily(m_parsed.scheme, m_string.characters8()) :
checkIfProtocolIsInHTTPFamily(m_parsed.scheme, m_string.characters16());
}
bool KURL::protocolIs(const char* protocol) const
{
assertProtocolIsGood(protocol);
if (m_string.isNull() || m_parsed.scheme.len <= 0)
return *protocol == '\0';
return m_string.is8Bit() ?
internalProtocolIs(m_parsed.scheme, m_string.characters8(), protocol) :
internalProtocolIs(m_parsed.scheme, m_string.characters16(), protocol);
}
String KURL::stringForInvalidComponent() const
{
if (m_string.isNull())
return String();
return emptyString();
}
String KURL::componentString(const url_parse::Component& component) const
{
if (!m_isValid || component.len <= 0)
return stringForInvalidComponent();
return string().substring(component.begin, component.len);
}
template<typename CHAR>
void KURL::replaceComponents(const url_canon::Replacements<CHAR>& replacements)
{
url_canon::RawCanonOutputT<char> output;
url_parse::Parsed newParsed;
StringUTF8Adaptor utf8(m_string);
m_isValid = url_util::ReplaceComponents(utf8.data(), utf8.length(), m_parsed, replacements, 0, &output, &newParsed);
m_parsed = newParsed;
m_string = AtomicString::fromUTF8(output.data(), output.length());
}
bool KURL::isSafeToSendToAnotherThread() const
{
return m_string.isSafeToSendToAnotherThread()
&& (!m_innerURL || m_innerURL->isSafeToSendToAnotherThread());
}
}