ea408c277b0d62dbe32da12ada9b084935024c6a
[WebKit-https.git] / Source / WebCore / platform / network / blackberry / ResourceRequestBlackBerry.cpp
1 /*
2  * Copyright (C) 2009, 2010, 2011 Research In Motion Limited. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "config.h"
20 #include "ResourceRequest.h"
21
22 #include "BlobRegistryImpl.h"
23 #include "CookieManager.h"
24 #include <BlackBerryPlatformClient.h>
25 #include <network/NetworkRequest.h>
26 #include <wtf/text/CString.h>
27
28 using BlackBerry::Platform::NetworkRequest;
29
30 namespace WebCore {
31
32 unsigned initializeMaximumHTTPConnectionCountPerHost()
33 {
34     return 6;
35 }
36
37 static inline NetworkRequest::CachePolicy platformCachePolicyForRequest(const ResourceRequest& request)
38 {
39     switch (request.cachePolicy()) {
40     case WebCore::UseProtocolCachePolicy:
41         return NetworkRequest::UseProtocolCachePolicy;
42     case WebCore::ReloadIgnoringCacheData:
43         return NetworkRequest::ReloadIgnoringCacheData;
44     case WebCore::ReturnCacheDataElseLoad:
45         return NetworkRequest::ReturnCacheDataElseLoad;
46     case WebCore::ReturnCacheDataDontLoad:
47         return NetworkRequest::ReturnCacheDataDontLoad;
48     default:
49         ASSERT_NOT_REACHED();
50         return NetworkRequest::UseProtocolCachePolicy;
51     }
52 }
53
54 static inline NetworkRequest::TargetType platformTargetTypeForRequest(const ResourceRequest& request)
55 {
56     if (request.isXMLHTTPRequest())
57         return NetworkRequest::TargetIsXMLHTTPRequest;
58
59     switch (request.targetType()) {
60     case ResourceRequest::TargetIsMainFrame:
61         return NetworkRequest::TargetIsMainFrame;
62     case ResourceRequest::TargetIsSubframe:
63         return NetworkRequest::TargetIsSubframe;
64     case ResourceRequest::TargetIsSubresource:
65         return NetworkRequest::TargetIsSubresource;
66     case ResourceRequest::TargetIsStyleSheet:
67         return NetworkRequest::TargetIsStyleSheet;
68     case ResourceRequest::TargetIsScript:
69         return NetworkRequest::TargetIsScript;
70     case ResourceRequest::TargetIsFontResource:
71         return NetworkRequest::TargetIsFontResource;
72     case ResourceRequest::TargetIsImage:
73         return NetworkRequest::TargetIsImage;
74     case ResourceRequest::TargetIsObject:
75         return NetworkRequest::TargetIsObject;
76     case ResourceRequest::TargetIsMedia:
77         return NetworkRequest::TargetIsMedia;
78     case ResourceRequest::TargetIsWorker:
79         return NetworkRequest::TargetIsWorker;
80     case ResourceRequest::TargetIsSharedWorker:
81         return NetworkRequest::TargetIsSharedWorker;
82
83         // FIXME: this need to be updated to the right value, but
84         // we need to coordinate with AIR api change.
85     case ResourceRequest::TargetIsFavicon:
86         return NetworkRequest::TargetIsImage;
87     case ResourceRequest::TargetIsPrefetch:
88         return NetworkRequest::TargetIsSubresource;
89     case ResourceRequest::TargetIsPrerender:
90         return NetworkRequest::TargetIsSubresource;
91     case ResourceRequest::TargetIsXHR:
92         return NetworkRequest::TargetIsSubresource;
93     case ResourceRequest::TargetIsTextTrack:
94         return NetworkRequest::TargetIsSubresource;
95     case ResourceRequest::TargetIsUnspecified:
96         return NetworkRequest::TargetIsSubresource;
97
98     default:
99         ASSERT_NOT_REACHED();
100         return NetworkRequest::TargetIsUnknown;
101     }
102 }
103
104 void ResourceRequest::initializePlatformRequest(NetworkRequest& platformRequest, bool cookiesEnabled, bool isInitial, bool isRedirect) const
105 {
106     // If this is the initial load, skip the request body and headers.
107     if (isInitial)
108         platformRequest.setRequestInitial(timeoutInterval());
109     else {
110         platformRequest.setRequestUrl(url().string().utf8().data(),
111                 httpMethod().latin1().data(),
112                 platformCachePolicyForRequest(*this),
113                 platformTargetTypeForRequest(*this),
114                 timeoutInterval());
115
116         platformRequest.setConditional(isConditional());
117
118         if (httpBody() && !httpBody()->isEmpty()) {
119             const Vector<FormDataElement>& elements = httpBody()->elements();
120             // Use setData for simple forms because it is slightly more efficient.
121             if (elements.size() == 1 && elements[0].m_type == FormDataElement::data)
122                 platformRequest.setData(elements[0].m_data.data(), elements[0].m_data.size());
123             else {
124                 for (unsigned i = 0; i < elements.size(); ++i) {
125                     const FormDataElement& element = elements[i];
126                     if (element.m_type == FormDataElement::data)
127                         platformRequest.addMultipartData(element.m_data.data(), element.m_data.size());
128                     else if (element.m_type == FormDataElement::encodedFile)
129                         platformRequest.addMultipartFilename(element.m_filename.characters(), element.m_filename.length());
130 #if ENABLE(BLOB)
131                     else if (element.m_type == FormDataElement::encodedBlob) {
132                         RefPtr<BlobStorageData> blobData = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(KURL(ParsedURLString, element.m_blobURL));
133                         if (blobData) {
134                             for (size_t j = 0; j < blobData->items().size(); ++j) {
135                                 const BlobDataItem& blobItem = blobData->items()[j];
136                                 if (blobItem.type == BlobDataItem::Data)
137                                     platformRequest.addMultipartData(blobItem.data->data() + static_cast<int>(blobItem.offset), static_cast<int>(blobItem.length));
138                                 else {
139                                     ASSERT(blobItem.type == BlobDataItem::File);
140                                     platformRequest.addMultipartFilename(blobItem.path.characters(), blobItem.path.length(), blobItem.offset, blobItem.length, blobItem.expectedModificationTime);
141                                 }
142                             }
143                         }
144                     }
145 #endif
146                     else
147                         ASSERT_NOT_REACHED(); // unknown type
148                 }
149             }
150         }
151
152         for (HTTPHeaderMap::const_iterator it = httpHeaderFields().begin(); it != httpHeaderFields().end(); ++it) {
153             String key = it->first;
154             String value = it->second;
155             if (!key.isEmpty() && !value.isEmpty()) {
156                 // We need to check the encoding and encode the cookie's value using latin1 or utf8 to support unicode characters.
157                 if (equalIgnoringCase(key, "Cookie"))
158                     platformRequest.addHeader(key.latin1().data(), value.containsOnlyLatin1() ? value.latin1().data() : value.utf8().data());
159                 else
160                     platformRequest.addHeader(key.latin1().data(), value.latin1().data());
161             }
162         }
163        
164         // Redirection's response may contain new cookies, so add cookies again.
165         // If there aren't cookies in the header list, we need trying to add cookies.
166         if (cookiesEnabled && (isRedirect || !httpHeaderFields().contains("Cookie")) && !url().isNull()) {
167             // Prepare a cookie header if there are cookies related to this url.
168             String cookiePairs = cookieManager().getCookie(url(), WithHttpOnlyCookies);
169             if (!cookiePairs.isEmpty())
170                 platformRequest.addHeader("Cookie", cookiePairs.containsOnlyLatin1() ? cookiePairs.latin1().data() : cookiePairs.utf8().data());
171         }
172
173         // Locale has the form "en-US". Construct accept language like "en-US, en;q=0.8".
174         std::string locale = BlackBerry::Platform::Client::get()->getLocale();
175         // POSIX locale has '_' instead of '-'.
176         // Replace to conform to HTTP spec.
177         size_t underscore = locale.find('_');
178         if (underscore != std::string::npos)
179             locale.replace(underscore, 1, "-");
180         std::string acceptLanguage = locale + ", " + locale.substr(0, 2) + ";q=0.8";
181         platformRequest.addHeader("Accept-Language", acceptLanguage.c_str());
182     }
183 }
184
185 PassOwnPtr<CrossThreadResourceRequestData> ResourceRequest::doPlatformCopyData(PassOwnPtr<CrossThreadResourceRequestData> data) const
186 {
187     data->m_token = m_token;
188     data->m_anchorText = m_anchorText;
189     data->m_isXMLHTTPRequest = m_isXMLHTTPRequest;
190     data->m_mustHandleInternally = m_mustHandleInternally;
191     return data;
192 }
193
194 void ResourceRequest::doPlatformAdopt(PassOwnPtr<CrossThreadResourceRequestData> data)
195 {
196     m_token = data->m_token;
197     m_anchorText = data->m_anchorText;
198     m_isXMLHTTPRequest = data->m_isXMLHTTPRequest;
199     m_mustHandleInternally = data->m_mustHandleInternally;
200     m_forceDownload = data->m_forceDownload;
201 }
202
203 } // namespace WebCore