void getResourceRequest(ResourceRequest& request, CFURLRequestRef cfRequest) { request = ResourceRequest(CFURLRequestGetURL(cfRequest)); request.setCachePolicy((ResourceRequestCachePolicy)CFURLRequestGetCachePolicy(cfRequest)); request.setTimeoutInterval(CFURLRequestGetTimeoutInterval(cfRequest)); request.setMainDocumentURL(KURL(CFURLRequestGetMainDocumentURL(cfRequest))); if (CFStringRef method = CFURLRequestCopyHTTPRequestMethod(cfRequest)) { request.setHTTPMethod(method); CFRelease(method); } request.setAllowHTTPCookies(CFURLRequestShouldHandleHTTPCookies(cfRequest)); if (CFDictionaryRef headers = CFURLRequestCopyAllHTTPHeaderFields(cfRequest)) { CFIndex headerCount = CFDictionaryGetCount(headers); Vector<const void*, 128> keys(headerCount); Vector<const void*, 128> values(headerCount); CFDictionaryGetKeysAndValues(headers, keys.data(), values.data()); for (int i = 0; i < headerCount; ++i) request.setHTTPHeaderField((CFStringRef)keys[i], (CFStringRef)values[i]); CFRelease(headers); } if (CFDataRef bodyData = CFURLRequestCopyHTTPRequestBody(cfRequest)) { request.setHTTPBody(new FormData(CFDataGetBytePtr(bodyData), CFDataGetLength(bodyData))); CFRelease(bodyData); } else if (CFReadStreamRef bodyStream = CFURLRequestCopyHTTPRequestBodyStream(cfRequest)) { if (FormData* formData = httpBodyFromStream(bodyStream)) request.setHTTPBody(formData); CFRelease(bodyStream); } // FIXME: what to do about arbitrary body streams? }
void ResourceRequest::doUpdatePlatformHTTPBody() { CFMutableURLRequestRef cfRequest; RetainPtr<CFURLRef> url = ResourceRequest::url().createCFURL(); RetainPtr<CFURLRef> firstPartyForCookies = ResourceRequest::firstPartyForCookies().createCFURL(); if (m_cfRequest) { cfRequest = CFURLRequestCreateMutableCopy(0, m_cfRequest.get()); CFURLRequestSetURL(cfRequest, url.get()); CFURLRequestSetMainDocumentURL(cfRequest, firstPartyForCookies.get()); CFURLRequestSetCachePolicy(cfRequest, (CFURLRequestCachePolicy)cachePolicy()); CFURLRequestSetTimeoutInterval(cfRequest, timeoutInterval()); } else cfRequest = CFURLRequestCreateMutable(0, url.get(), (CFURLRequestCachePolicy)cachePolicy(), timeoutInterval(), firstPartyForCookies.get()); FormData* formData = httpBody(); if (formData && !formData->isEmpty()) WebCore::setHTTPBody(cfRequest, formData); if (RetainPtr<CFReadStreamRef> bodyStream = adoptCF(CFURLRequestCopyHTTPRequestBodyStream(cfRequest))) { // For streams, provide a Content-Length to avoid using chunked encoding, and to get accurate total length in callbacks. if (RetainPtr<CFStringRef> lengthString = adoptCF(static_cast<CFStringRef>(CFReadStreamCopyProperty(bodyStream.get(), formDataStreamLengthPropertyName())))) { CFURLRequestSetHTTPHeaderFieldValue(cfRequest, CFSTR("Content-Length"), lengthString.get()); // Since resource request is already marked updated, we need to keep it up to date too. ASSERT(m_resourceRequestUpdated); m_httpHeaderFields.set(HTTPHeaderName::ContentLength, lengthString.get()); } } m_cfRequest = adoptCF(cfRequest); #if PLATFORM(COCOA) clearOrUpdateNSURLRequest(); #endif }
PassRefPtr<FormData> httpBodyFromRequest(CFURLRequestRef request) { if (RetainPtr<CFDataRef> bodyData = CFURLRequestCopyHTTPRequestBody(request)) return FormData::create(CFDataGetBytePtr(bodyData.get()), CFDataGetLength(bodyData.get())); if (wkCanAccessCFURLRequestHTTPBodyParts()) { if (RetainPtr<CFArrayRef> bodyParts = wkCFURLRequestCopyHTTPRequestBodyParts(request)) { RefPtr<FormData> formData = FormData::create(); CFIndex count = CFArrayGetCount(bodyParts.get()); for (CFIndex i = 0; i < count; i++) { CFTypeRef bodyPart = CFArrayGetValueAtIndex(bodyParts.get(), i); CFTypeID typeID = CFGetTypeID(bodyPart); if (typeID == CFStringGetTypeID()) { String filename = (CFStringRef)bodyPart; formData->appendFile(filename); } else if (typeID == CFDataGetTypeID()) { CFDataRef data = (CFDataRef)bodyPart; formData->appendData(CFDataGetBytePtr(data), CFDataGetLength(data)); } else ASSERT_NOT_REACHED(); } return formData.release(); } } else { if (RetainPtr<CFReadStreamRef> bodyStream = CFURLRequestCopyHTTPRequestBodyStream(request)) return getStreamFormDatas().get(bodyStream.get()); } // FIXME: what to do about arbitrary body streams? return 0; }
void ResourceRequest::doUpdateResourceHTTPBody() { if (!m_cfRequest) { m_httpBody = nullptr; return; } if (RetainPtr<CFDataRef> bodyData = adoptCF(CFURLRequestCopyHTTPRequestBody(m_cfRequest.get()))) m_httpBody = FormData::create(CFDataGetBytePtr(bodyData.get()), CFDataGetLength(bodyData.get())); else if (RetainPtr<CFReadStreamRef> bodyStream = adoptCF(CFURLRequestCopyHTTPRequestBodyStream(m_cfRequest.get()))) { FormData* formData = httpBodyFromStream(bodyStream.get()); // There is no FormData object if a client provided a custom data stream. // We shouldn't be looking at http body after client callbacks. ASSERT(formData); if (formData) m_httpBody = formData; } }