📄 resourcehandlecfnet.cpp
字号:
/* * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#include "config.h"#include "ResourceHandle.h"#include "ResourceHandleClient.h"#include "ResourceHandleInternal.h"#include "AuthenticationCF.h"#include "AuthenticationChallenge.h"#include "CookieStorageWin.h"#include "CString.h"#include "DocLoader.h"#include "Frame.h"#include "FrameLoader.h"#include "Logging.h"#include "NotImplemented.h"#include "ResourceError.h"#include "ResourceResponse.h"#include <wtf/HashMap.h>#include <wtf/Threading.h>#include <sys/types.h>#include <sys/stat.h>#include <process.h> // for _beginthread()#include <CFNetwork/CFNetwork.h>#include <WebKitSystemInterface/WebKitSystemInterface.h>namespace WebCore {static HashSet<String>& allowsAnyHTTPSCertificateHosts(){ static HashSet<String> hosts; return hosts;}static HashMap<String, RetainPtr<CFDataRef> >& clientCerts(){ static HashMap<String, RetainPtr<CFDataRef> > certs; return certs;}CFURLRequestRef willSendRequest(CFURLConnectionRef conn, CFURLRequestRef cfRequest, CFURLResponseRef cfRedirectResponse, const void* clientInfo){ ResourceHandle* handle = (ResourceHandle*)clientInfo; if (!cfRedirectResponse) { CFRetain(cfRequest); return cfRequest; } LOG(Network, "CFNet - willSendRequest(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().string().utf8().data()); ResourceRequest request(cfRequest); if (handle->client()) handle->client()->willSendRequest(handle, request, cfRedirectResponse); cfRequest = request.cfURLRequest(); CFRetain(cfRequest); return cfRequest;}void didReceiveResponse(CFURLConnectionRef conn, CFURLResponseRef cfResponse, const void* clientInfo) { ResourceHandle* handle = (ResourceHandle*)clientInfo; LOG(Network, "CFNet - didReceiveResponse(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().string().utf8().data()); if (handle->client()) handle->client()->didReceiveResponse(handle, cfResponse);}void didReceiveData(CFURLConnectionRef conn, CFDataRef data, CFIndex originalLength, const void* clientInfo) { ResourceHandle* handle = (ResourceHandle*)clientInfo; const UInt8* bytes = CFDataGetBytePtr(data); CFIndex length = CFDataGetLength(data); LOG(Network, "CFNet - didReceiveData(conn=%p, handle=%p, bytes=%d) (%s)", conn, handle, length, handle->request().url().string().utf8().data()); if (handle->client()) handle->client()->didReceiveData(handle, (const char*)bytes, length, originalLength);}static void didSendBodyData(CFURLConnectionRef conn, CFIndex bytesWritten, CFIndex totalBytesWritten, CFIndex totalBytesExpectedToWrite, const void *clientInfo){ ResourceHandle* handle = (ResourceHandle*)clientInfo; if (!handle || !handle->client()) return; handle->client()->didSendData(handle, totalBytesWritten, totalBytesExpectedToWrite);}static Boolean shouldUseCredentialStorageCallback(CFURLConnectionRef conn, const void* clientInfo){ ResourceHandle* handle = const_cast<ResourceHandle*>(static_cast<const ResourceHandle*>(clientInfo)); LOG(Network, "CFNet - shouldUseCredentialStorage(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().string().utf8().data()); if (!handle) return false; return handle->shouldUseCredentialStorage();}void didFinishLoading(CFURLConnectionRef conn, const void* clientInfo) { ResourceHandle* handle = (ResourceHandle*)clientInfo; LOG(Network, "CFNet - didFinishLoading(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().string().utf8().data()); if (handle->client()) handle->client()->didFinishLoading(handle);}void didFail(CFURLConnectionRef conn, CFErrorRef error, const void* clientInfo) { ResourceHandle* handle = (ResourceHandle*)clientInfo; LOG(Network, "CFNet - didFail(conn=%p, handle=%p, error = %p) (%s)", conn, handle, error, handle->request().url().string().utf8().data()); if (handle->client()) handle->client()->didFail(handle, ResourceError(error));}CFCachedURLResponseRef willCacheResponse(CFURLConnectionRef conn, CFCachedURLResponseRef cachedResponse, const void* clientInfo) { ResourceHandle* handle = (ResourceHandle*)clientInfo; CacheStoragePolicy policy = static_cast<CacheStoragePolicy>(CFCachedURLResponseGetStoragePolicy(cachedResponse)); if (handle->client()) handle->client()->willCacheResponse(handle, policy); if (static_cast<CFURLCacheStoragePolicy>(policy) != CFCachedURLResponseGetStoragePolicy(cachedResponse)) cachedResponse = CFCachedURLResponseCreateWithUserInfo(kCFAllocatorDefault, CFCachedURLResponseGetWrappedResponse(cachedResponse), CFCachedURLResponseGetReceiverData(cachedResponse), CFCachedURLResponseGetUserInfo(cachedResponse), static_cast<CFURLCacheStoragePolicy>(policy)); CFRetain(cachedResponse); return cachedResponse;}void didReceiveChallenge(CFURLConnectionRef conn, CFURLAuthChallengeRef challenge, const void* clientInfo){ ResourceHandle* handle = (ResourceHandle*)clientInfo; ASSERT(handle); LOG(Network, "CFNet - didReceiveChallenge(conn=%p, handle=%p (%s)", conn, handle, handle->request().url().string().utf8().data()); handle->didReceiveAuthenticationChallenge(AuthenticationChallenge(challenge, handle));}void addHeadersFromHashMap(CFMutableURLRequestRef request, const HTTPHeaderMap& requestHeaders) { if (!requestHeaders.size()) return; HTTPHeaderMap::const_iterator end = requestHeaders.end(); for (HTTPHeaderMap::const_iterator it = requestHeaders.begin(); it != end; ++it) { CFStringRef key = it->first.createCFString(); CFStringRef value = it->second.createCFString(); CFURLRequestSetHTTPHeaderFieldValue(request, key, value); CFRelease(key); CFRelease(value); }}ResourceHandleInternal::~ResourceHandleInternal(){ if (m_connection) { LOG(Network, "CFNet - Cancelling connection %p (%s)", m_connection, m_request.url().string().utf8().data()); CFURLConnectionCancel(m_connection.get()); }}ResourceHandle::~ResourceHandle(){ LOG(Network, "CFNet - Destroying job %p (%s)", this, d->m_request.url().string().utf8().data());}CFArrayRef arrayFromFormData(const FormData& d){ size_t size = d.elements().size(); CFMutableArrayRef a = CFArrayCreateMutable(0, d.elements().size(), &kCFTypeArrayCallBacks); for (size_t i = 0; i < size; ++i) { const FormDataElement& e = d.elements()[i]; if (e.m_type == FormDataElement::data) { CFDataRef data = CFDataCreate(0, (const UInt8*)e.m_data.data(), e.m_data.size()); CFArrayAppendValue(a, data); CFRelease(data); } else { ASSERT(e.m_type == FormDataElement::encodedFile); CFStringRef filename = e.m_filename.createCFString(); CFArrayAppendValue(a, filename); CFRelease(filename); } } return a;}void emptyPerform(void* unused) {}static CFRunLoopRef loaderRL = 0;void* runLoaderThread(void *unused){ loaderRL = CFRunLoopGetCurrent(); // Must add a source to the run loop to prevent CFRunLoopRun() from exiting CFRunLoopSourceContext ctxt = {0, (void *)1 /*must be non-NULL*/, 0, 0, 0, 0, 0, 0, 0, emptyPerform}; CFRunLoopSourceRef bogusSource = CFRunLoopSourceCreate(0, 0, &ctxt); CFRunLoopAddSource(loaderRL, bogusSource,kCFRunLoopDefaultMode);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -