#pragma once
#include "PlatformExportMacros.h"
#include <wtf/Forward.h>
#include <wtf/RetainPtr.h>
#include <wtf/text/WTFString.h>
#if USE(CF)
typedef const struct __CFURL* CFURLRef;
#endif
#if USE(SOUP)
#include "GUniquePtrSoup.h"
#endif
#if USE(FOUNDATION)
OBJC_CLASS NSURL;
#endif
namespace WTF {
class TextStream;
}
namespace WebCore {
class TextEncoding;
struct URLHash;
enum ParsedURLStringTag { ParsedURLString };
class URL {
public:
URL() { invalidate(); }
WEBCORE_EXPORT URL(ParsedURLStringTag, const String&);
explicit URL(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { }
bool isHashTableDeletedValue() const { return string().isHashTableDeletedValue(); }
WEBCORE_EXPORT URL(const URL& base, const String& relative);
URL(const URL& base, const String& relative, const TextEncoding&);
WEBCORE_EXPORT static URL fakeURLWithRelativePart(const String&);
WEBCORE_EXPORT static URL fileURLWithFileSystemPath(const String&);
String strippedForUseAsReferrer() const;
WEBCORE_EXPORT URL isolatedCopy() const;
bool isNull() const;
bool isEmpty() const;
bool isValid() const;
bool canSetHostOrPort() const { return isHierarchical(); }
bool canSetPathname() const { return isHierarchical(); }
bool isHierarchical() const;
const String& string() const { return m_string; }
WEBCORE_EXPORT String stringCenterEllipsizedToLength(unsigned length = 1024) const;
WEBCORE_EXPORT StringView protocol() const;
WEBCORE_EXPORT String host() const;
WEBCORE_EXPORT std::optional<uint16_t> port() const;
WEBCORE_EXPORT String hostAndPort() const;
WEBCORE_EXPORT String protocolHostAndPort() const;
WEBCORE_EXPORT String user() const;
WEBCORE_EXPORT String pass() const;
WEBCORE_EXPORT String path() const;
WEBCORE_EXPORT String lastPathComponent() const;
WEBCORE_EXPORT String query() const;
WEBCORE_EXPORT String fragmentIdentifier() const;
WEBCORE_EXPORT bool hasFragmentIdentifier() const;
bool hasUsername() const;
bool hasPassword() const;
bool hasQuery() const;
bool hasFragment() const;
String encodedUser() const;
String encodedPass() const;
WEBCORE_EXPORT String baseAsString() const;
WEBCORE_EXPORT String fileSystemPath() const;
WEBCORE_EXPORT bool protocolIs(const char*) const;
bool protocolIs(StringView) const;
bool protocolIsBlob() const { return protocolIs("blob"); }
bool protocolIsData() const { return protocolIs("data"); }
bool protocolIsInHTTPFamily() const;
WEBCORE_EXPORT bool isLocalFile() const;
WEBCORE_EXPORT bool isBlankURL() const;
bool cannotBeABaseURL() const { return m_cannotBeABaseURL; }
WEBCORE_EXPORT bool setProtocol(const String&);
void setHost(const String&);
void removePort();
void setPort(unsigned short);
void setHostAndPort(const String&);
void setUser(const String&);
void setPass(const String&);
WEBCORE_EXPORT void setPath(const String&);
WEBCORE_EXPORT void setQuery(const String&);
WEBCORE_EXPORT void setFragmentIdentifier(StringView);
WEBCORE_EXPORT void removeFragmentIdentifier();
WEBCORE_EXPORT void removeQueryAndFragmentIdentifier();
WEBCORE_EXPORT friend bool equalIgnoringFragmentIdentifier(const URL&, const URL&);
WEBCORE_EXPORT friend bool protocolHostAndPortAreEqual(const URL&, const URL&);
unsigned hostStart() const;
unsigned hostEnd() const;
unsigned pathStart() const;
unsigned pathEnd() const;
unsigned pathAfterLastSlash() const;
operator const String&() const { return string(); }
#if USE(CF)
WEBCORE_EXPORT URL(CFURLRef);
WEBCORE_EXPORT RetainPtr<CFURLRef> createCFURL() const;
#endif
#if USE(SOUP)
URL(SoupURI*);
GUniquePtr<SoupURI> createSoupURI() const;
#endif
#if USE(FOUNDATION)
WEBCORE_EXPORT URL(NSURL*);
WEBCORE_EXPORT operator NSURL*() const;
#endif
#ifdef __OBJC__
operator NSString*() const { return string(); }
#endif
#ifndef NDEBUG
void print() const;
#endif
template <class Encoder> void encode(Encoder&) const;
template <class Decoder> static bool decode(Decoder&, URL&);
template <class Decoder> static std::optional<URL> decode(Decoder&);
private:
friend class URLParser;
WEBCORE_EXPORT void invalidate();
static bool protocolIs(const String&, const char*);
void init(const URL&, const String&, const TextEncoding&);
void copyToBuffer(Vector<char, 512>& buffer) const;
bool hasPath() const;
String m_string;
bool m_isValid : 1;
bool m_protocolIsInHTTPFamily : 1;
bool m_cannotBeABaseURL : 1;
unsigned m_schemeEnd;
unsigned m_userStart;
unsigned m_userEnd;
unsigned m_passwordEnd;
unsigned m_hostEnd;
unsigned m_portEnd;
unsigned m_pathAfterLastSlash;
unsigned m_pathEnd;
unsigned m_queryEnd;
};
template <class Encoder>
void URL::encode(Encoder& encoder) const
{
encoder << m_string;
encoder << static_cast<bool>(m_isValid);
if (!m_isValid)
return;
encoder << static_cast<bool>(m_protocolIsInHTTPFamily);
encoder << m_schemeEnd;
encoder << m_userStart;
encoder << m_userEnd;
encoder << m_passwordEnd;
encoder << m_hostEnd;
encoder << m_portEnd;
encoder << m_pathAfterLastSlash;
encoder << m_pathEnd;
encoder << m_queryEnd;
}
template <class Decoder>
bool URL::decode(Decoder& decoder, URL& url)
{
auto optionalURL = URL::decode(decoder);
if (!optionalURL)
return false;
url = WTFMove(*optionalURL);
return true;
}
template <class Decoder>
std::optional<URL> URL::decode(Decoder& decoder)
{
URL url;
if (!decoder.decode(url.m_string))
return std::nullopt;
bool isValid;
if (!decoder.decode(isValid))
return std::nullopt;
url.m_isValid = isValid;
if (!isValid)
return WTFMove(url);
bool protocolIsInHTTPFamily;
if (!decoder.decode(protocolIsInHTTPFamily))
return std::nullopt;
url.m_protocolIsInHTTPFamily = protocolIsInHTTPFamily;
if (!decoder.decode(url.m_schemeEnd))
return std::nullopt;
if (!decoder.decode(url.m_userStart))
return std::nullopt;
if (!decoder.decode(url.m_userEnd))
return std::nullopt;
if (!decoder.decode(url.m_passwordEnd))
return std::nullopt;
if (!decoder.decode(url.m_hostEnd))
return std::nullopt;
if (!decoder.decode(url.m_portEnd))
return std::nullopt;
if (!decoder.decode(url.m_pathAfterLastSlash))
return std::nullopt;
if (!decoder.decode(url.m_pathEnd))
return std::nullopt;
if (!decoder.decode(url.m_queryEnd))
return std::nullopt;
return WTFMove(url);
}
bool operator==(const URL&, const URL&);
bool operator==(const URL&, const String&);
bool operator==(const String&, const URL&);
bool operator!=(const URL&, const URL&);
bool operator!=(const URL&, const String&);
bool operator!=(const String&, const URL&);
WEBCORE_EXPORT bool equalIgnoringFragmentIdentifier(const URL&, const URL&);
WEBCORE_EXPORT bool protocolHostAndPortAreEqual(const URL&, const URL&);
WEBCORE_EXPORT bool hostsAreEqual(const URL&, const URL&);
WEBCORE_EXPORT const URL& blankURL();
WEBCORE_EXPORT bool protocolIs(const String& url, const char* protocol);
WEBCORE_EXPORT bool protocolIsJavaScript(const String& url);
bool protocolIsJavaScript(StringView url);
WEBCORE_EXPORT bool protocolIsInHTTPFamily(const String& url);
std::optional<uint16_t> defaultPortForProtocol(StringView protocol);
WEBCORE_EXPORT bool isDefaultPortForProtocol(uint16_t port, StringView protocol);
WEBCORE_EXPORT bool portAllowed(const URL&);
WEBCORE_EXPORT void registerDefaultPortForProtocolForTesting(uint16_t port, const String& protocol);
WEBCORE_EXPORT void clearDefaultPortForProtocolMapForTesting();
bool isValidProtocol(const String&);
String mimeTypeFromDataURL(const String& url);
WEBCORE_EXPORT String decodeURLEscapeSequences(const String&);
String decodeURLEscapeSequences(const String&, const TextEncoding&);
WEBCORE_EXPORT String encodeWithURLEscapeSequences(const String&);
#if PLATFORM(IOS)
WEBCORE_EXPORT void enableURLSchemeCanonicalization(bool);
#endif
inline bool operator==(const URL& a, const URL& b)
{
return a.string() == b.string();
}
inline bool operator==(const URL& a, const String& b)
{
return a.string() == b;
}
inline bool operator==(const String& a, const URL& b)
{
return a == b.string();
}
inline bool operator!=(const URL& a, const URL& b)
{
return a.string() != b.string();
}
inline bool operator!=(const URL& a, const String& b)
{
return a.string() != b;
}
inline bool operator!=(const String& a, const URL& b)
{
return a != b.string();
}
inline bool URL::isNull() const
{
return m_string.isNull();
}
inline bool URL::isEmpty() const
{
return m_string.isEmpty();
}
inline bool URL::isValid() const
{
return m_isValid;
}
inline bool URL::hasPath() const
{
return m_pathEnd != m_portEnd;
}
inline bool URL::hasUsername() const
{
return m_userEnd > m_userStart;
}
inline bool URL::hasPassword() const
{
return m_passwordEnd > (m_userEnd + 1);
}
inline bool URL::hasQuery() const
{
return m_queryEnd > m_pathEnd;
}
inline bool URL::hasFragment() const
{
return m_isValid && m_string.length() > m_queryEnd;
}
inline bool URL::protocolIsInHTTPFamily() const
{
return m_protocolIsInHTTPFamily;
}
inline unsigned URL::hostStart() const
{
return (m_passwordEnd == m_userStart) ? m_passwordEnd : m_passwordEnd + 1;
}
inline unsigned URL::hostEnd() const
{
return m_hostEnd;
}
inline unsigned URL::pathStart() const
{
return m_portEnd;
}
inline unsigned URL::pathEnd() const
{
return m_pathEnd;
}
inline unsigned URL::pathAfterLastSlash() const
{
return m_pathAfterLastSlash;
}
WTF::TextStream& operator<<(WTF::TextStream&, const URL&);
}
namespace WTF {
template<typename T> struct DefaultHash;
template<> struct DefaultHash<WebCore::URL> {
typedef WebCore::URLHash Hash;
};
}