ResourceResponseCFNet.cpp [plain text]
#include "config.h"
#include "ResourceResponse.h"
#if USE(CFNETWORK)
#include "HTTPParsers.h"
#include "MIMETypeRegistry.h"
#include <CFNetwork/CFURLResponsePriv.h>
#include <wtf/RetainPtr.h>
#if PLATFORM(COCOA)
#include "WebCoreSystemInterface.h"
#endif
#define MAX_TIME_T ((time_t)INT_MAX)
namespace WebCore {
static CFStringRef const commonHeaderFields[] = {
CFSTR("Age"), CFSTR("Cache-Control"), CFSTR("Content-Type"), CFSTR("Date"), CFSTR("Etag"), CFSTR("Expires"), CFSTR("Last-Modified"), CFSTR("Pragma")
};
CFURLResponseRef ResourceResponse::cfURLResponse() const
{
if (!m_cfResponse && !m_isNull) {
#if PLATFORM(COCOA)
nsURLResponse();
#else
RetainPtr<CFURLRef> url = m_url.createCFURL();
m_cfResponse = adoptCF(CFURLResponseCreate(0, url.get(), m_mimeType.string().createCFString().get(), m_expectedContentLength, m_textEncodingName.string().createCFString().get(), kCFURLCacheStorageAllowed));
#endif
}
return m_cfResponse.get();
}
static void addToHTTPHeaderMap(const void* key, const void* value, void* context)
{
HTTPHeaderMap* httpHeaderMap = (HTTPHeaderMap*)context;
httpHeaderMap->set((CFStringRef)key, (CFStringRef)value);
}
void ResourceResponse::platformLazyInit(InitLevel initLevel)
{
if (m_initLevel > initLevel)
return;
if (m_isNull || !m_cfResponse.get())
return;
if (m_initLevel < CommonFieldsOnly && initLevel >= CommonFieldsOnly) {
m_url = CFURLResponseGetURL(m_cfResponse.get());
m_mimeType = CFURLResponseGetMIMEType(m_cfResponse.get());
m_expectedContentLength = CFURLResponseGetExpectedContentLength(m_cfResponse.get());
m_textEncodingName = CFURLResponseGetTextEncodingName(m_cfResponse.get());
unsigned textEncodingNameLength = m_textEncodingName.length();
if (textEncodingNameLength >= 2 && m_textEncodingName[0U] == '"' && m_textEncodingName[textEncodingNameLength - 1] == '"')
m_textEncodingName = m_textEncodingName.string().substring(1, textEncodingNameLength - 2);
CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(m_cfResponse.get());
if (httpResponse) {
m_httpStatusCode = CFHTTPMessageGetResponseStatusCode(httpResponse);
if (initLevel < AllFields) {
RetainPtr<CFDictionaryRef> headers = adoptCF(CFHTTPMessageCopyAllHeaderFields(httpResponse));
for (auto& commonHeader : commonHeaderFields) {
CFStringRef value;
if (CFDictionaryGetValueIfPresent(headers.get(), commonHeader, (const void **)&value))
m_httpHeaderFields.set(commonHeader, value);
}
}
} else
m_httpStatusCode = 0;
}
if (m_initLevel < AllFields && initLevel == AllFields) {
CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(m_cfResponse.get());
if (httpResponse) {
RetainPtr<CFStringRef> statusLine = adoptCF(CFHTTPMessageCopyResponseStatusLine(httpResponse));
m_httpStatusText = extractReasonPhraseFromHTTPStatusLine(statusLine.get());
RetainPtr<CFDictionaryRef> headers = adoptCF(CFHTTPMessageCopyAllHeaderFields(httpResponse));
CFDictionaryApplyFunction(headers.get(), addToHTTPHeaderMap, &m_httpHeaderFields);
}
}
m_initLevel = initLevel;
}
CertificateInfo ResourceResponse::platformCertificateInfo() const
{
#if PLATFORM(COCOA)
return CertificateInfo(adoptCF(wkCopyNSURLResponseCertificateChain(nsURLResponse())));
#endif
return CertificateInfo();
}
String ResourceResponse::platformSuggestedFilename() const
{
if (!cfURLResponse())
return String();
RetainPtr<CFStringRef> suggestedFilename = adoptCF(CFURLResponseCopySuggestedFilename(cfURLResponse()));
return suggestedFilename.get();
}
bool ResourceResponse::platformCompare(const ResourceResponse& a, const ResourceResponse& b)
{
if (!a.cfURLResponse() || !b.cfURLResponse())
return a.cfURLResponse() == b.cfURLResponse();
return CFEqual(a.cfURLResponse(), b.cfURLResponse());
}
}
#endif // USE(CFNETWORK)