Merge WindowsMerge to trunk
[WebKit-https.git] / WebCore / platform / network / cf / ResourceRequestCFNet.cpp
1 /*
2  * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "ResourceRequestCFNet.h"
28
29 #include "FormDataStreamCFNet.h"
30 #include "ResourceRequest.h"
31
32 #include <CFNetwork/CFURLRequestPriv.h>
33
34 namespace WebCore {
35
36 CFURLRequestRef ResourceRequest::cfURLRequest() const
37 {
38     updatePlatformRequest();
39
40     return m_cfRequest.get();
41 }
42
43 static inline void addHeadersFromHashMap(CFMutableURLRequestRef request, const HTTPHeaderMap& requestHeaders) 
44 {
45     if (!requestHeaders.size())
46         return;
47         
48     HTTPHeaderMap::const_iterator end = requestHeaders.end();
49     for (HTTPHeaderMap::const_iterator it = requestHeaders.begin(); it != end; ++it) {
50         CFStringRef key = it->first.createCFString();
51         CFStringRef value = it->second.createCFString();
52         CFURLRequestSetHTTPHeaderFieldValue(request, key, value);
53         CFRelease(key);
54         CFRelease(value);
55     }
56 }
57
58 void ResourceRequest::doUpdatePlatformRequest()
59 {
60     RetainPtr<CFURLRef> url(AdoptCF, ResourceRequest::url().createCFURL());
61     RetainPtr<CFURLRef> mainDocumentURL(AdoptCF, ResourceRequest::mainDocumentURL().createCFURL());
62
63     CFMutableURLRequestRef cfRequest = CFURLRequestCreateMutable(0, url.get(), (CFURLRequestCachePolicy)cachePolicy(), timeoutInterval(), mainDocumentURL.get());
64
65     RetainPtr<CFStringRef> requestMethod(AdoptCF, httpMethod().createCFString());
66     CFURLRequestSetHTTPRequestMethod(cfRequest, requestMethod.get());
67
68     addHeadersFromHashMap(cfRequest, httpHeaderFields());
69     WebCore::setHTTPBody(cfRequest, httpBody());
70     CFURLRequestSetShouldHandleHTTPCookies(cfRequest, allowHTTPCookies());
71
72     if (m_cfRequest) {
73 #ifdef CFNETWORK_HAS_NEW_COOKIE_FUNCTIONS
74         RetainPtr<CFHTTPCookieStorageRef> cookieStorage(AdoptCF, CFURLRequestCopyHTTPCookieStorage(m_cfRequest.get()));
75         CFURLRequestSetHTTPCookieStorage(cfRequest, cookieStorage.get());
76         CFURLRequestSetHTTPCookieStorageAcceptPolicy(cfRequest, CFURLRequestGetHTTPCookieStorageAcceptPolicy(m_cfRequest.get()));
77 #endif
78         CFURLRequestSetSSLProperties(cfRequest, CFURLRequestGetSSLProperties(m_cfRequest.get()));
79     }
80
81     m_cfRequest.adoptCF(cfRequest);
82 }
83
84 void ResourceRequest::doUpdateResourceRequest()
85 {
86     m_url = CFURLRequestGetURL(m_cfRequest.get());
87
88     m_cachePolicy = (ResourceRequestCachePolicy)CFURLRequestGetCachePolicy(m_cfRequest.get());
89     m_timeoutInterval = CFURLRequestGetTimeoutInterval(m_cfRequest.get());
90     m_mainDocumentURL = CFURLRequestGetMainDocumentURL(m_cfRequest.get());
91     if (CFStringRef method = CFURLRequestCopyHTTPRequestMethod(m_cfRequest.get())) {
92         m_httpMethod = method;
93         CFRelease(method);
94     }
95     m_allowHTTPCookies = CFURLRequestShouldHandleHTTPCookies(m_cfRequest.get());
96
97     if (CFDictionaryRef headers = CFURLRequestCopyAllHTTPHeaderFields(m_cfRequest.get())) {
98         CFIndex headerCount = CFDictionaryGetCount(headers);
99         Vector<const void*, 128> keys(headerCount);
100         Vector<const void*, 128> values(headerCount);
101         CFDictionaryGetKeysAndValues(headers, keys.data(), values.data());
102         for (int i = 0; i < headerCount; ++i)
103             m_httpHeaderFields.set((CFStringRef)keys[i], (CFStringRef)values[i]);
104         CFRelease(headers);
105     }
106
107     if (CFDataRef bodyData = CFURLRequestCopyHTTPRequestBody(m_cfRequest.get())) {
108         m_httpBody = new FormData(CFDataGetBytePtr(bodyData), CFDataGetLength(bodyData));
109         CFRelease(bodyData);
110     } else if (CFReadStreamRef bodyStream = CFURLRequestCopyHTTPRequestBodyStream(m_cfRequest.get())) {
111         if (FormData* formData = httpBodyFromStream(bodyStream))
112             m_httpBody = formData;
113         CFRelease(bodyStream);
114     }
115     // FIXME: what to do about arbitrary body streams?
116 }
117
118 }