#ifndef URL_h
#define URL_h
#include "PlatformExportMacros.h"
#include <wtf/Forward.h>
#include <wtf/HashMap.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 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&);
static URL fakeURLWithRelativePart(const String&);
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; }
String stringCenterEllipsizedToLength(unsigned length = 1024) const;
WEBCORE_EXPORT String protocol() const;
WEBCORE_EXPORT String host() const;
WEBCORE_EXPORT unsigned short port() const;
bool hasPort() 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 protocolIsBlob() const { return protocolIs("blob"); }
bool protocolIsData() const { return protocolIs("data"); }
bool protocolIsInHTTPFamily() const;
WEBCORE_EXPORT bool isLocalFile() const;
bool isBlankURL() const;
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&);
void setQuery(const String&);
void setFragmentIdentifier(const String&);
void removeFragmentIdentifier();
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
const URL* innerURL() const { return 0; }
#ifndef NDEBUG
void print() const;
#endif
bool isSafeToSendToAnotherThread() const;
template <class Encoder> void encode(Encoder&) const;
template <class Decoder> static bool decode(Decoder&, URL&);
String serialize(bool omitFragment = false) const;
private:
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;
void parse(const String&);
void parse(const char* url, const String* originalString = 0);
bool hasPath() const;
String m_string;
bool m_isValid : 1;
bool m_protocolIsInHTTPFamily : 1;
int m_schemeEnd;
int m_userStart;
int m_userEnd;
int m_passwordEnd;
int m_hostEnd;
int m_portEnd;
int m_pathAfterLastSlash;
int m_pathEnd;
int m_queryEnd;
int m_fragmentEnd;
};
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;
encoder << m_fragmentEnd;
}
template <class Decoder>
bool URL::decode(Decoder& decoder, URL& url)
{
if (!decoder.decode(url.m_string))
return false;
bool isValid;
if (!decoder.decode(isValid))
return false;
url.m_isValid = isValid;
if (!isValid)
return true;
bool protocolIsInHTTPFamily;
if (!decoder.decode(protocolIsInHTTPFamily))
return false;
url.m_protocolIsInHTTPFamily = protocolIsInHTTPFamily;
if (!decoder.decode(url.m_schemeEnd))
return false;
if (!decoder.decode(url.m_userStart))
return false;
if (!decoder.decode(url.m_userEnd))
return false;
if (!decoder.decode(url.m_passwordEnd))
return false;
if (!decoder.decode(url.m_hostEnd))
return false;
if (!decoder.decode(url.m_portEnd))
return false;
if (!decoder.decode(url.m_pathAfterLastSlash))
return false;
if (!decoder.decode(url.m_pathEnd))
return false;
if (!decoder.decode(url.m_queryEnd))
return false;
if (!decoder.decode(url.m_fragmentEnd))
return false;
return true;
}
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);
WEBCORE_EXPORT bool protocolIsInHTTPFamily(const String& url);
unsigned short defaultPortForProtocol(const String& protocol);
bool isDefaultPortForProtocol(unsigned short port, const String& protocol);
WEBCORE_EXPORT bool portAllowed(const URL&);
bool isValidProtocol(const String&);
String mimeTypeFromDataURL(const String& url);
WEBCORE_EXPORT String mimeTypeFromURL(const 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::hasPort() const
{
return m_hostEnd < 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_fragmentEnd > 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;
}
}
namespace WTF {
template<typename T> struct DefaultHash;
template<> struct DefaultHash<WebCore::URL> {
typedef WebCore::URLHash Hash;
};
}
#endif // URL_h