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; }
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; }
CFCachedURLResponseRef SynchronousResourceHandleCFURLConnectionDelegate::willCacheResponse(CFCachedURLResponseRef cachedResponse) { #if PLATFORM(WIN) // Workaround for <rdar://problem/6300990> Caching does not respect Vary HTTP header. // FIXME: WebCore cache has issues with Vary, too (bug 58797, bug 71509). CFURLResponseRef wrappedResponse = CFCachedURLResponseGetWrappedResponse(cachedResponse); if (CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(wrappedResponse)) { ASSERT(CFHTTPMessageIsHeaderComplete(httpResponse)); RetainPtr<CFStringRef> varyValue = adoptCF(CFHTTPMessageCopyHeaderFieldValue(httpResponse, CFSTR("Vary"))); if (varyValue) return 0; } #endif // PLATFORM(WIN) #if PLATFORM(WIN) if (m_handle->client() && !m_handle->client()->shouldCacheResponse(m_handle, cachedResponse)) return 0; #else CFCachedURLResponseRef newResponse = m_handle->client()->willCacheResponse(m_handle, cachedResponse); if (newResponse != cachedResponse) return newResponse; #endif CFRetain(cachedResponse); return cachedResponse; }
ResourceRequest ResourceHandleCFURLConnectionDelegate::createResourceRequest(CFURLRequestRef cfRequest, CFURLResponseRef redirectResponse) { ResourceRequest request; CFHTTPMessageRef httpMessage = CFURLResponseGetHTTPResponse(redirectResponse); if (httpMessage && CFHTTPMessageGetResponseStatusCode(httpMessage) == 307) { RetainPtr<CFStringRef> lastHTTPMethod = m_handle->lastHTTPMethod().createCFString(); RetainPtr<CFStringRef> newMethod = adoptCF(CFURLRequestCopyHTTPRequestMethod(cfRequest)); if (CFStringCompareWithOptions(lastHTTPMethod.get(), newMethod.get(), CFRangeMake(0, CFStringGetLength(lastHTTPMethod.get())), kCFCompareCaseInsensitive)) { RetainPtr<CFMutableURLRequestRef> mutableRequest = adoptCF(CFURLRequestCreateMutableCopy(0, cfRequest)); wkSetRequestStorageSession(m_handle->storageSession(), mutableRequest.get()); CFURLRequestSetHTTPRequestMethod(mutableRequest.get(), lastHTTPMethod.get()); FormData* body = m_handle->firstRequest().httpBody(); if (!equalIgnoringCase(m_handle->firstRequest().httpMethod(), "GET") && body && !body->isEmpty()) WebCore::setHTTPBody(mutableRequest.get(), body); String originalContentType = m_handle->firstRequest().httpContentType(); if (!originalContentType.isEmpty()) CFURLRequestSetHTTPHeaderFieldValue(mutableRequest.get(), CFSTR("Content-Type"), originalContentType.createCFString().get()); request = mutableRequest.get(); } } if (request.isNull()) request = cfRequest; if (!request.url().protocolIs("https") && protocolIs(request.httpReferrer(), "https") && m_handle->context()->shouldClearReferrerOnHTTPSToHTTPRedirect()) request.clearHTTPReferrer(); return request; }
static CFCachedURLResponseRef willCacheResponse(CFURLConnectionRef, CFCachedURLResponseRef cachedResponse, const void* clientInfo) { ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo)); CFURLResponseRef wrappedResponse = CFCachedURLResponseGetWrappedResponse(cachedResponse); // Workaround for <rdar://problem/6300990> Caching does not respect Vary HTTP header. // FIXME: WebCore cache has issues with Vary, too (bug 58797, bug 71509). if (CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(wrappedResponse)) { ASSERT(CFHTTPMessageIsHeaderComplete(httpResponse)); RetainPtr<CFStringRef> varyValue = adoptCF(CFHTTPMessageCopyHeaderFieldValue(httpResponse, CFSTR("Vary"))); if (varyValue) return 0; } #if PLATFORM(WIN) if (handle->client() && !handle->client()->shouldCacheResponse(handle, cachedResponse)) return 0; #else CFCachedURLResponseRef newResponse = handle->client()->willCacheResponse(handle, cachedResponse); if (newResponse != cachedResponse) return newResponse; #endif CFRetain(cachedResponse); return cachedResponse; }
static CFURLRequestRef willSendRequest(CFURLConnectionRef conn, CFURLRequestRef cfRequest, CFURLResponseRef cfRedirectResponse, const void* clientInfo) { #if LOG_DISABLED UNUSED_PARAM(conn); #endif ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo)); if (!cfRedirectResponse) { CFRetain(cfRequest); return cfRequest; } LOG(Network, "CFNet - willSendRequest(conn=%p, handle=%p) (%s)", conn, handle, handle->firstRequest().url().string().utf8().data()); ResourceRequest request; if (cfRedirectResponse) { CFHTTPMessageRef httpMessage = CFURLResponseGetHTTPResponse(cfRedirectResponse); if (httpMessage && CFHTTPMessageGetResponseStatusCode(httpMessage) == 307) { RetainPtr<CFStringRef> lastHTTPMethod(AdoptCF, handle->lastHTTPMethod().createCFString()); RetainPtr<CFStringRef> newMethod(AdoptCF, CFURLRequestCopyHTTPRequestMethod(cfRequest)); if (CFStringCompareWithOptions(lastHTTPMethod.get(), newMethod.get(), CFRangeMake(0, CFStringGetLength(lastHTTPMethod.get())), kCFCompareCaseInsensitive)) { RetainPtr<CFMutableURLRequestRef> mutableRequest(AdoptCF, CFURLRequestCreateMutableCopy(0, cfRequest)); #if USE(CFURLSTORAGESESSIONS) wkSetRequestStorageSession(ResourceHandle::currentStorageSession(), mutableRequest.get()); #endif CFURLRequestSetHTTPRequestMethod(mutableRequest.get(), lastHTTPMethod.get()); FormData* body = handle->firstRequest().httpBody(); if (!equalIgnoringCase(handle->firstRequest().httpMethod(), "GET") && body && !body->isEmpty()) WebCore::setHTTPBody(mutableRequest.get(), body); String originalContentType = handle->firstRequest().httpContentType(); RetainPtr<CFStringRef> originalContentTypeCF(AdoptCF, originalContentType.createCFString()); if (!originalContentType.isEmpty()) CFURLRequestSetHTTPHeaderFieldValue(mutableRequest.get(), CFSTR("Content-Type"), originalContentTypeCF.get()); request = mutableRequest.get(); } } } if (request.isNull()) request = cfRequest; // Should not set Referer after a redirect from a secure resource to non-secure one. if (!request.url().protocolIs("https") && protocolIs(request.httpReferrer(), "https")) request.clearHTTPReferrer(); handle->willSendRequest(request, cfRedirectResponse); if (request.isNull()) return 0; cfRequest = request.cfURLRequest(); CFRetain(cfRequest); return cfRequest; }
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() { 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 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()); }
static CFCachedURLResponseRef willCacheResponse(CFURLConnectionRef, CFCachedURLResponseRef cachedResponse, const void* clientInfo) { ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo)); CFURLResponseRef wrappedResponse = CFCachedURLResponseGetWrappedResponse(cachedResponse); // Workaround for <rdar://problem/6300990> Caching does not respect Vary HTTP header. // FIXME: WebCore cache has issues with Vary, too (bug 58797, bug 71509). if (CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(wrappedResponse)) { ASSERT(CFHTTPMessageIsHeaderComplete(httpResponse)); RetainPtr<CFStringRef> varyValue = adoptCF(CFHTTPMessageCopyHeaderFieldValue(httpResponse, CFSTR("Vary"))); if (varyValue) return 0; } #if PLATFORM(WIN) if (handle->client() && !handle->client()->shouldCacheResponse(handle, cachedResponse)) return 0; #else CFCachedURLResponseRef newResponse = handle->client()->willCacheResponse(handle, cachedResponse); if (newResponse != cachedResponse) return newResponse; #endif CacheStoragePolicy policy = static_cast<CacheStoragePolicy>(CFCachedURLResponseGetStoragePolicy(cachedResponse)); if (handle->client()) handle->client()->willCacheResponse(handle, policy); if (static_cast<CFURLCacheStoragePolicy>(policy) != CFCachedURLResponseGetStoragePolicy(cachedResponse)) { RetainPtr<CFArrayRef> receiverData(AdoptCF, CFCachedURLResponseCopyReceiverDataArray(cachedResponse)); cachedResponse = CFCachedURLResponseCreateWithDataArray(kCFAllocatorDefault, wrappedResponse, receiverData.get(), CFCachedURLResponseGetUserInfo(cachedResponse), static_cast<CFURLCacheStoragePolicy>(policy)); } else CFRetain(cachedResponse); return cachedResponse; }