d86b88cf1dbc1deea4e8596a3954335e22aef266
[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 <BlackBerryPlatformClient.h>
24 #include <network/NetworkRequest.h>
25 #include <wtf/text/CString.h>
26
27 using BlackBerry::Platform::NetworkRequest;
28
29 namespace WebCore {
30
31 unsigned initializeMaximumHTTPConnectionCountPerHost()
32 {
33     return 6;
34 }
35
36 static inline NetworkRequest::CachePolicy platformCachePolicyForRequest(const ResourceRequest& request)
37 {
38     switch (request.cachePolicy()) {
39     case WebCore::UseProtocolCachePolicy:
40         return NetworkRequest::UseProtocolCachePolicy;
41     case WebCore::ReloadIgnoringCacheData:
42         return NetworkRequest::ReloadIgnoringCacheData;
43     case WebCore::ReturnCacheDataElseLoad:
44         return NetworkRequest::ReturnCacheDataElseLoad;
45     case WebCore::ReturnCacheDataDontLoad:
46         return NetworkRequest::ReturnCacheDataDontLoad;
47     default:
48         ASSERT_NOT_REACHED();
49         return NetworkRequest::UseProtocolCachePolicy;
50     }
51 }
52
53 static inline NetworkRequest::TargetType platformTargetTypeForRequest(const ResourceRequest& request)
54 {
55     if (request.isXMLHTTPRequest())
56         return NetworkRequest::TargetIsXMLHTTPRequest;
57
58     switch (request.targetType()) {
59     case ResourceRequest::TargetIsMainFrame:
60         return NetworkRequest::TargetIsMainFrame;
61     case ResourceRequest::TargetIsSubframe:
62         return NetworkRequest::TargetIsSubframe;
63     case ResourceRequest::TargetIsSubresource:
64         return NetworkRequest::TargetIsSubresource;
65     case ResourceRequest::TargetIsStyleSheet:
66         return NetworkRequest::TargetIsStyleSheet;
67     case ResourceRequest::TargetIsScript:
68         return NetworkRequest::TargetIsScript;
69     case ResourceRequest::TargetIsFontResource:
70         return NetworkRequest::TargetIsFontResource;
71     case ResourceRequest::TargetIsImage:
72         return NetworkRequest::TargetIsImage;
73     case ResourceRequest::TargetIsObject:
74         return NetworkRequest::TargetIsObject;
75     case ResourceRequest::TargetIsMedia:
76         return NetworkRequest::TargetIsMedia;
77     case ResourceRequest::TargetIsWorker:
78         return NetworkRequest::TargetIsWorker;
79     case ResourceRequest::TargetIsSharedWorker:
80         return NetworkRequest::TargetIsSharedWorker;
81     default:
82         ASSERT_NOT_REACHED();
83         return NetworkRequest::TargetIsUnknown;
84     }
85 }
86
87 void ResourceRequest::initializePlatformRequest(NetworkRequest& platformRequest, bool isInitial) const
88 {
89     // If this is the initial load, skip the request body and headers.
90     if (isInitial)
91         platformRequest.setRequestInitial(timeoutInterval());
92     else {
93         platformRequest.setRequestUrl(url().string().utf8().data(),
94                 httpMethod().latin1().data(),
95                 platformCachePolicyForRequest(*this),
96                 platformTargetTypeForRequest(*this),
97                 timeoutInterval());
98
99         platformRequest.setConditional(isConditional());
100
101         if (httpBody() && !httpBody()->isEmpty()) {
102             const Vector<FormDataElement>& elements = httpBody()->elements();
103             // Use setData for simple forms because it is slightly more efficient.
104             if (elements.size() == 1 && elements[0].m_type == FormDataElement::data)
105                 platformRequest.setData(elements[0].m_data.data(), elements[0].m_data.size());
106             else {
107                 for (unsigned i = 0; i < elements.size(); ++i) {
108                     const FormDataElement& element = elements[i];
109                     if (element.m_type == FormDataElement::data)
110                         platformRequest.addMultipartData(element.m_data.data(), element.m_data.size());
111                     else if (element.m_type == FormDataElement::encodedFile)
112                         platformRequest.addMultipartFilename(element.m_filename.characters(), element.m_filename.length());
113 #if ENABLE(BLOB)
114                     else if (element.m_type == FormDataElement::encodedBlob) {
115                         RefPtr<BlobStorageData> blobData = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(KURL(ParsedURLString, element.m_blobURL));
116                         if (blobData) {
117                             for (size_t j = 0; j < blobData->items().size(); ++j) {
118                                 const BlobDataItem& blobItem = blobData->items()[j];
119                                 if (blobItem.type == BlobDataItem::Data)
120                                     platformRequest.addMultipartData(blobItem.data->data() + static_cast<int>(blobItem.offset), static_cast<int>(blobItem.length));
121                                 else {
122                                     ASSERT(blobItem.type == BlobDataItem::File);
123                                     platformRequest.addMultipartFilename(blobItem.path.characters(), blobItem.path.length(), blobItem.offset, blobItem.length, blobItem.expectedModificationTime);
124                                 }
125                             }
126                         }
127                     }
128 #endif
129                     else
130                         ASSERT_NOT_REACHED(); // unknown type
131                 }
132             }
133         }
134
135         for (HTTPHeaderMap::const_iterator it = httpHeaderFields().begin(); it != httpHeaderFields().end(); ++it) {
136             String key = it->first;
137             String value = it->second;
138             if (!key.isEmpty() && !value.isEmpty())
139                 platformRequest.addHeader(key.latin1().data(), value.latin1().data());
140         }
141
142         // Locale has the form "en-US". Construct accept language like "en-US, en;q=0.8".
143         std::string locale = BlackBerry::Platform::Client::get()->getLocale();
144         // POSIX locale has '_' instead of '-'.
145         // Replace to conform to HTTP spec.
146         size_t underscore = locale.find('_');
147         if (underscore != std::string::npos)
148             locale.replace(underscore, 1, "-");
149         std::string acceptLanguage = locale + ", " + locale.substr(0, 2) + ";q=0.8";
150         platformRequest.addHeader("Accept-Language", acceptLanguage.c_str());
151     }
152 }
153
154 PassOwnPtr<CrossThreadResourceRequestData> ResourceRequest::doPlatformCopyData(PassOwnPtr<CrossThreadResourceRequestData> data) const
155 {
156     data->m_token = m_token;
157     data->m_anchorText = m_anchorText;
158     data->m_isXMLHTTPRequest = m_isXMLHTTPRequest;
159     data->m_mustHandleInternally = m_mustHandleInternally;
160     return data;
161 }
162
163 void ResourceRequest::doPlatformAdopt(PassOwnPtr<CrossThreadResourceRequestData> data)
164 {
165     m_token = data->m_token;
166     m_anchorText = data->m_anchorText;
167     m_isXMLHTTPRequest = data->m_isXMLHTTPRequest;
168     m_mustHandleInternally = data->m_mustHandleInternally;
169     m_forceDownload = data->m_forceDownload;
170 }
171
172 } // namespace WebCore