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>
#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")
};
static const int numCommonHeaderFields = sizeof(commonHeaderFields) / sizeof(CFStringRef);
CFURLResponseRef ResourceResponse::cfURLResponse() const
{
if (!m_cfResponse && !m_isNull) {
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));
}
return m_cfResponse.get();
}
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);
RetainPtr<CFDictionaryRef> headers = adoptCF(CFHTTPMessageCopyAllHeaderFields(httpResponse));
for (int i = 0; i < numCommonHeaderFields; i++) {
CFStringRef value;
if (CFDictionaryGetValueIfPresent(headers.get(), commonHeaderFields[i], (const void **)&value))
m_httpHeaderFields.set(commonHeaderFields[i], value);
}
} else
m_httpStatusCode = 0;
}
if (m_initLevel < CommonAndUncommonFields && initLevel >= CommonAndUncommonFields) {
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));
CFIndex headerCount = CFDictionaryGetCount(headers.get());
Vector<const void*, 128> keys(headerCount);
Vector<const void*, 128> values(headerCount);
CFDictionaryGetKeysAndValues(headers.get(), keys.data(), values.data());
for (int i = 0; i < headerCount; ++i)
m_httpHeaderFields.set((CFStringRef)keys[i], (CFStringRef)values[i]);
}
}
if (m_initLevel < AllFields && initLevel >= AllFields) {
RetainPtr<CFStringRef> suggestedFilename = adoptCF(CFURLResponseCopySuggestedFilename(m_cfResponse.get()));
m_suggestedFilename = suggestedFilename.get();
}
m_initLevel = initLevel;
}
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)