#include "config.h"
#include "CFURLExtras.h"
#include "URL.h"
#include <wtf/text/CString.h>
namespace WebCore {
RetainPtr<CFURLRef> createCFURLFromBuffer(const char* data, size_t size, CFURLRef baseURL)
{
RetainPtr<CFURLRef> result = adoptCF(CFURLCreateAbsoluteURLWithBytes(0, reinterpret_cast<const UInt8*>(data), size, kCFStringEncodingUTF8, baseURL, true));
if (!result)
result = adoptCF(CFURLCreateAbsoluteURLWithBytes(0, reinterpret_cast<const UInt8*>(data), size, kCFStringEncodingISOLatin1, baseURL, true));
return result;
}
void getURLBytes(CFURLRef url, URLCharBuffer& result)
{
CFIndex bytesLength = CFURLGetBytes(url, 0, 0);
result.resize(bytesLength);
CFIndex finalLength = CFURLGetBytes(url, reinterpret_cast<UInt8*>(result.data()), bytesLength);
ASSERT_UNUSED(finalLength, finalLength == bytesLength);
}
void getURLBytes(CFURLRef url, CString& result)
{
CFIndex bytesLength = CFURLGetBytes(url, 0, 0);
char* bytes;
result = CString::newUninitialized(bytesLength, bytes);
CFIndex finalLength = CFURLGetBytes(url, reinterpret_cast<UInt8*>(bytes), bytesLength);
ASSERT_UNUSED(finalLength, finalLength == bytesLength);
}
bool isCFURLSameOrigin(CFURLRef cfURL, const URL& url)
{
ASSERT(url.protocolIsInHTTPFamily());
if (url.hasUsername() || url.hasPassword())
return protocolHostAndPortAreEqual(url, URL { cfURL });
URLCharBuffer bytes;
getURLBytes(cfURL, bytes);
StringView cfURLString { reinterpret_cast<const LChar*>(bytes.data()), static_cast<unsigned>(bytes.size()) };
if (!url.hasPath())
return StringView { url.string() } == cfURLString;
auto urlWithoutPath = StringView { url.string() }.substring(0, url.pathStart() + 1);
return cfURLString.startsWith(urlWithoutPath);
}
}