void SynchronousResourceHandleCFURLConnectionDelegate::didReceiveResponse(CFURLResponseRef cfResponse) { LOG(Network, "CFNet - SynchronousResourceHandleCFURLConnectionDelegate::didReceiveResponse(handle=%p) (%s)", m_handle, m_handle->firstRequest().url().string().utf8().data()); if (!m_handle->client()) return; #if PLATFORM(COCOA) // Avoid MIME type sniffing if the response comes back as 304 Not Modified. CFHTTPMessageRef msg = wkGetCFURLResponseHTTPResponse(cfResponse); int statusCode = msg ? CFHTTPMessageGetResponseStatusCode(msg) : 0; if (statusCode != 304) adjustMIMETypeIfNecessary(cfResponse); #if !PLATFORM(IOS) if (_CFURLRequestCopyProtocolPropertyForKey(m_handle->firstRequest().cfURLRequest(DoNotUpdateHTTPBody), CFSTR("ForceHTMLMIMEType"))) wkSetCFURLResponseMIMEType(cfResponse, CFSTR("text/html")); #endif // !PLATFORM(IOS) #else if (!CFURLResponseGetMIMEType(cfResponse)) { // We should never be applying the default MIMEType if we told the networking layer to do content sniffing for handle. ASSERT(!m_handle->shouldContentSniff()); setDefaultMIMEType(cfResponse); } #endif #if USE(QUICK_LOOK) m_handle->setQuickLookHandle(QuickLookHandle::create(m_handle, this, cfResponse)); if (m_handle->quickLookHandle()) cfResponse = m_handle->quickLookHandle()->cfResponse(); #endif m_handle->client()->didReceiveResponse(m_handle, cfResponse); }
static void didReceiveResponse(CFURLConnectionRef conn, CFURLResponseRef cfResponse, const void* clientInfo) { ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo)); LOG(Network, "CFNet - didReceiveResponse(conn=%p, handle=%p) (%s)", conn, handle, handle->firstRequest().url().string().utf8().data()); if (!handle->client()) return; #if PLATFORM(MAC) // Avoid MIME type sniffing if the response comes back as 304 Not Modified. CFHTTPMessageRef msg = wkGetCFURLResponseHTTPResponse(cfResponse); int statusCode = msg ? CFHTTPMessageGetResponseStatusCode(msg) : 0; if (statusCode != 304) adjustMIMETypeIfNecessary(cfResponse); #else if (!CFURLResponseGetMIMEType(cfResponse)) { // We should never be applying the default MIMEType if we told the networking layer to do content sniffing for handle. ASSERT(!handle->shouldContentSniff()); setDefaultMIMEType(cfResponse); } #endif handle->setQuickLookHandle(QuickLookHandle::create(handle, conn, cfResponse)); if (handle->quickLookHandle()) cfResponse = handle->quickLookHandle()->cfResponse(); handle->client()->didReceiveResponse(handle, cfResponse); }
static void didReceiveResponse(CFURLConnectionRef conn, CFURLResponseRef cfResponse, const void* clientInfo) { #if LOG_DISABLED UNUSED_PARAM(conn); #endif ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo)); LOG(Network, "CFNet - didReceiveResponse(conn=%p, handle=%p) (%s)", conn, handle, handle->firstRequest().url().string().utf8().data()); if (!handle->client()) return; #if PLATFORM(MAC) // Avoid MIME type sniffing if the response comes back as 304 Not Modified. CFHTTPMessageRef msg = wkGetCFURLResponseHTTPResponse(cfResponse); int statusCode = msg ? CFHTTPMessageGetResponseStatusCode(msg) : 0; if (statusCode != 304) adjustMIMETypeIfNecessary(cfResponse); if (_CFURLRequestCopyProtocolPropertyForKey(handle->firstRequest().cfURLRequest(), CFSTR("ForceHTMLMIMEType"))) wkSetCFURLResponseMIMEType(cfResponse, CFSTR("text/html")); #else if (!CFURLResponseGetMIMEType(cfResponse)) { // We should never be applying the default MIMEType if we told the networking layer to do content sniffing for handle. ASSERT(!handle->shouldContentSniff()); setDefaultMIMEType(cfResponse); } #endif handle->client()->didReceiveResponse(handle, cfResponse); }
void SynchronousResourceHandleCFURLConnectionDelegate::didReceiveResponse(CFURLConnectionRef connection, CFURLResponseRef cfResponse) { LOG(Network, "CFNet - SynchronousResourceHandleCFURLConnectionDelegate::didReceiveResponse(handle=%p) (%s)", m_handle, m_handle->firstRequest().url().string().utf8().data()); if (!m_handle->client()) return; #if PLATFORM(COCOA) // Avoid MIME type sniffing if the response comes back as 304 Not Modified. auto msg = CFURLResponseGetHTTPResponse(cfResponse); int statusCode = msg ? CFHTTPMessageGetResponseStatusCode(msg) : 0; if (statusCode != 304) { bool isMainResourceLoad = m_handle->firstRequest().requester() == ResourceRequest::Requester::Main; adjustMIMETypeIfNecessary(cfResponse, isMainResourceLoad); } #if !PLATFORM(IOS) if (_CFURLRequestCopyProtocolPropertyForKey(m_handle->firstRequest().cfURLRequest(DoNotUpdateHTTPBody), CFSTR("ForceHTMLMIMEType"))) CFURLResponseSetMIMEType(cfResponse, CFSTR("text/html")); #endif // !PLATFORM(IOS) #else if (!CFURLResponseGetMIMEType(cfResponse)) adjustMIMETypeIfNecessary(cfResponse); if (!CFURLResponseGetMIMEType(cfResponse)) { // We should never be applying the default MIMEType if we told the networking layer to do content sniffing for handle. ASSERT(!m_handle->shouldContentSniff()); setDefaultMIMEType(cfResponse); } #endif #if USE(QUICK_LOOK) m_handle->setQuickLookHandle(QuickLookHandle::create(m_handle, this, cfResponse)); if (m_handle->quickLookHandle()) cfResponse = m_handle->quickLookHandle()->cfResponse(); #endif ResourceResponse resourceResponse(cfResponse); #if PLATFORM(COCOA) && ENABLE(WEB_TIMING) ResourceHandle::getConnectionTimingData(connection, resourceResponse.networkLoadTiming()); #else UNUSED_PARAM(connection); #endif m_handle->client()->didReceiveResponse(m_handle, WTFMove(resourceResponse)); }
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()); // Workaround for <rdar://problem/8757088>, can be removed once that is fixed. unsigned textEncodingNameLength = m_textEncodingName.length(); if (textEncodingNameLength >= 2 && m_textEncodingName[0U] == '"' && m_textEncodingName[textEncodingNameLength - 1] == '"') m_textEncodingName = m_textEncodingName.substring(1, textEncodingNameLength - 2); m_lastModifiedDate = toTimeT(CFURLResponseGetLastModifiedDate(m_cfResponse.get())); 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; }
String WebPage::cachedResponseMIMETypeForURL(const KURL& url) { #if USE(CFNETWORK) RetainPtr<CFCachedURLResponseRef> cachedResponse = cachedResponseForURL(this, url); CFURLResponseRef response = CFCachedURLResponseGetWrappedResponse(cachedResponse.get()); return response ? CFURLResponseGetMIMEType(response) : String(); #else return String(); #endif }
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()); // Workaround for <rdar://problem/8757088>, can be removed once that is fixed. 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; }
void ResourceResponse::platformLazyInit() { if (m_isUpToDate) return; m_isUpToDate = true; if (m_isNull) { ASSERT(!m_cfResponse.get()); return; } // FIXME: We may need to do MIME type sniffing here (unless that is done in CFURLResponseGetMIMEType). m_url = CFURLResponseGetURL(m_cfResponse.get()); m_mimeType = CFURLResponseGetMIMEType(m_cfResponse.get()); m_expectedContentLength = CFURLResponseGetExpectedContentLength(m_cfResponse.get()); m_textEncodingName = CFURLResponseGetTextEncodingName(m_cfResponse.get()); m_lastModifiedDate = toTimeT(CFURLResponseGetLastModifiedDate(m_cfResponse.get())); RetainPtr<CFStringRef> suggestedFilename(AdoptCF, CFURLResponseCopySuggestedFilename(m_cfResponse.get())); m_suggestedFilename = suggestedFilename.get(); CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(m_cfResponse.get()); if (httpResponse) { m_httpStatusCode = CFHTTPMessageGetResponseStatusCode(httpResponse); RetainPtr<CFStringRef> statusLine(AdoptCF, CFHTTPMessageCopyResponseStatusLine(httpResponse)); String statusText(statusLine.get()); int spacePos = statusText.find(' '); // Remove the status code from the status text. spacePos = statusText.find(' ', spacePos + 1); statusText = statusText.substring(spacePos + 1); m_httpStatusText = statusText; 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]); } else m_httpStatusCode = 0; }
void ResourceResponse::platformLazyInit() { if (m_isUpToDate) return; m_isUpToDate = true; if (m_isNull) { ASSERT(!m_cfResponse.get()); return; } // FIXME: We may need to do MIME type sniffing here (unless that is done in CFURLResponseGetMIMEType). m_url = CFURLResponseGetURL(m_cfResponse.get()); m_mimeType = CFURLResponseGetMIMEType(m_cfResponse.get()); m_expectedContentLength = CFURLResponseGetExpectedContentLength(m_cfResponse.get()); m_textEncodingName = CFURLResponseGetTextEncodingName(m_cfResponse.get()); // Workaround for <rdar://problem/8757088>, can be removed once that is fixed. unsigned textEncodingNameLength = m_textEncodingName.length(); if (textEncodingNameLength >= 2 && m_textEncodingName[0U] == '"' && m_textEncodingName[textEncodingNameLength - 1] == '"') m_textEncodingName = m_textEncodingName.substring(1, textEncodingNameLength - 2); m_lastModifiedDate = toTimeT(CFURLResponseGetLastModifiedDate(m_cfResponse.get())); RetainPtr<CFStringRef> suggestedFilename(AdoptCF, CFURLResponseCopySuggestedFilename(m_cfResponse.get())); m_suggestedFilename = suggestedFilename.get(); CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(m_cfResponse.get()); if (httpResponse) { m_httpStatusCode = CFHTTPMessageGetResponseStatusCode(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]); } else m_httpStatusCode = 0; }
static void adjustMIMETypeIfNecessary(CFURLResponseRef cfResponse) { RetainPtr<CFStringRef> result = CFURLResponseGetMIMEType(cfResponse); RetainPtr<CFStringRef> originalResult = result; if (!result) { CFURLRef cfURL = CFURLResponseGetURL(cfResponse); URL url(cfURL); if (url.isLocalFile()) { String mimeType = mimeTypeFromURL(url); result = mimeType.createCFString().leakRef(); } } if (!result) { static CFStringRef defaultMIMETypeString = WebCore::defaultMIMEType().createCFString().leakRef(); result = defaultMIMETypeString; } if (result != originalResult) CFURLResponseSetMIMEType(cfResponse, result.get()); }
static void convertWebResourceResponseToDictionary(CFMutableDictionaryRef propertyList) { CFDataRef responseData = static_cast<CFDataRef>(CFDictionaryGetValue(propertyList, CFSTR("WebResourceResponse"))); // WebResourceResponseKey in WebResource.m if (CFGetTypeID(responseData) != CFDataGetTypeID()) return; RetainPtr<CFURLResponseRef> response = adoptCF(createCFURLResponseFromResponseData(responseData)); if (!response) return; RetainPtr<CFMutableDictionaryRef> responseDictionary = adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); RetainPtr<CFMutableStringRef> urlString = adoptCF(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, CFURLGetString(CFURLResponseGetURL(response.get())))); normalizeWebResourceURL(urlString.get()); CFDictionarySetValue(responseDictionary.get(), CFSTR("URL"), urlString.get()); RetainPtr<CFMutableStringRef> mimeTypeString = adoptCF(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, CFURLResponseGetMIMEType(response.get()))); convertMIMEType(mimeTypeString.get()); CFDictionarySetValue(responseDictionary.get(), CFSTR("MIMEType"), mimeTypeString.get()); CFStringRef textEncodingName = CFURLResponseGetTextEncodingName(response.get()); if (textEncodingName) CFDictionarySetValue(responseDictionary.get(), CFSTR("textEncodingName"), textEncodingName); SInt64 expectedContentLength = CFURLResponseGetExpectedContentLength(response.get()); RetainPtr<CFNumberRef> expectedContentLengthNumber = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &expectedContentLength)); CFDictionarySetValue(responseDictionary.get(), CFSTR("expectedContentLength"), expectedContentLengthNumber.get()); if (CFHTTPMessageRef httpMessage = CFURLResponseGetHTTPResponse(response.get())) { RetainPtr<CFDictionaryRef> allHeaders = adoptCF(CFHTTPMessageCopyAllHeaderFields(httpMessage)); RetainPtr<CFMutableDictionaryRef> allHeaderFields = adoptCF(CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, allHeaders.get())); normalizeHTTPResponseHeaderFields(allHeaderFields.get()); CFDictionarySetValue(responseDictionary.get(), CFSTR("allHeaderFields"), allHeaderFields.get()); CFIndex statusCode = CFHTTPMessageGetResponseStatusCode(httpMessage); RetainPtr<CFNumberRef> statusCodeNumber = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &statusCode)); CFDictionarySetValue(responseDictionary.get(), CFSTR("statusCode"), statusCodeNumber.get()); } CFDictionarySetValue(propertyList, CFSTR("WebResourceResponse"), responseDictionary.get()); }