752388b7eee59a57e5c4528a20b7218b85188697
[WebKit-https.git] / Source / WebCore / platform / network / soup / ResourceRequestSoup.cpp
1 /*
2  * Copyright (C) 2009 Gustavo Noronha Silva
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 License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "ResourceRequest.h"
22
23 #include "GOwnPtrSoup.h"
24 #include "HTTPParsers.h"
25 #include "MIMETypeRegistry.h"
26 #include "SoupURIUtils.h"
27 #include <wtf/gobject/GOwnPtr.h>
28 #include <wtf/text/CString.h>
29 #include <wtf/text/WTFString.h>
30
31 using namespace std;
32
33 namespace WebCore {
34
35 void ResourceRequest::updateSoupMessageHeaders(SoupMessageHeaders* soupHeaders) const
36 {
37     const HTTPHeaderMap& headers = httpHeaderFields();
38     if (!headers.isEmpty()) {
39         HTTPHeaderMap::const_iterator end = headers.end();
40         for (HTTPHeaderMap::const_iterator it = headers.begin(); it != end; ++it)
41             soup_message_headers_append(soupHeaders, it->key.string().utf8().data(), it->value.utf8().data());
42     }
43 }
44
45 void ResourceRequest::updateFromSoupMessageHeaders(SoupMessageHeaders* soupHeaders)
46 {
47     m_httpHeaderFields.clear();
48     SoupMessageHeadersIter headersIter;
49     soup_message_headers_iter_init(&headersIter, soupHeaders);
50     const char* headerName;
51     const char* headerValue;
52     while (soup_message_headers_iter_next(&headersIter, &headerName, &headerValue))
53         m_httpHeaderFields.set(String::fromUTF8(headerName), String::fromUTF8(headerValue));
54 }
55
56 void ResourceRequest::updateSoupMessage(SoupMessage* soupMessage) const
57 {
58     g_object_set(soupMessage, SOUP_MESSAGE_METHOD, httpMethod().utf8().data(), NULL);
59
60     GOwnPtr<SoupURI> uri(soupURI());
61     soup_message_set_uri(soupMessage, uri.get());
62
63     updateSoupMessageHeaders(soupMessage->request_headers);
64
65     String firstPartyString = firstPartyForCookies().string();
66     if (!firstPartyString.isEmpty()) {
67         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
68         soup_message_set_first_party(soupMessage, firstParty.get());
69     }
70
71     soup_message_set_flags(soupMessage, m_soupFlags);
72 }
73
74 SoupMessage* ResourceRequest::toSoupMessage() const
75 {
76     SoupMessage* soupMessage = soup_message_new(httpMethod().utf8().data(), url().string().utf8().data());
77     if (!soupMessage)
78         return 0;
79
80     updateSoupMessageHeaders(soupMessage->request_headers);
81
82     String firstPartyString = firstPartyForCookies().string();
83     if (!firstPartyString.isEmpty()) {
84         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
85         soup_message_set_first_party(soupMessage, firstParty.get());
86     }
87
88     soup_message_set_flags(soupMessage, m_soupFlags);
89
90     // Body data is only handled at ResourceHandleSoup::startHttp for
91     // now; this is because this may not be a good place to go
92     // openning and mmapping files. We should maybe revisit this.
93     return soupMessage;
94 }
95
96 void ResourceRequest::updateFromSoupMessage(SoupMessage* soupMessage)
97 {
98     bool shouldPortBeResetToZero = m_url.hasPort() && !m_url.port();
99     m_url = soupURIToKURL(soup_message_get_uri(soupMessage));
100
101     // SoupURI cannot differeniate between an explicitly specified port 0 and
102     // no port specified.
103     if (shouldPortBeResetToZero)
104         m_url.setPort(0);
105
106     m_httpMethod = String::fromUTF8(soupMessage->method);
107
108     updateFromSoupMessageHeaders(soupMessage->request_headers);
109
110     if (soupMessage->request_body->data)
111         m_httpBody = FormData::create(soupMessage->request_body->data, soupMessage->request_body->length);
112
113     SoupURI* firstParty = soup_message_get_first_party(soupMessage);
114     if (firstParty)
115         m_firstPartyForCookies = soupURIToKURL(firstParty);
116
117     m_soupFlags = soup_message_get_flags(soupMessage);
118
119     // FIXME: m_allowCookies should probably be handled here and on
120     // doUpdatePlatformRequest somehow.
121 }
122
123 unsigned initializeMaximumHTTPConnectionCountPerHost()
124 {
125     // Soup has its own queue control; it wants to have all requests
126     // given to it, so that it is able to look ahead, and schedule
127     // them in a good way.
128     return 10000;
129 }
130
131 SoupURI* ResourceRequest::soupURI() const
132 {
133     // WebKit does not support fragment identifiers in data URLs, but soup does.
134     // Before passing the URL to soup, we should make sure to urlencode any '#'
135     // characters, so that soup does not interpret them as fragment identifiers.
136     // See http://wkbug.com/68089
137     if (m_url.protocolIsData()) {
138         String urlString = m_url.string();
139         urlString.replace("#", "%23");
140         return soup_uri_new(urlString.utf8().data());
141     }
142
143     KURL url = m_url;
144     url.removeFragmentIdentifier();
145     SoupURI* uri = soup_uri_new(url.string().utf8().data());
146
147     // Versions of libsoup prior to 2.42 have a soup_uri_new that will convert empty passwords that are not
148     // prefixed by a colon into null. Some parts of soup like the SoupAuthenticationManager will only be active
149     // when both the username and password are non-null. When we have credentials, empty usernames and passwords
150     // should be empty strings instead of null.
151     if (!url.user().isEmpty() || !url.pass().isEmpty()) {
152         soup_uri_set_user(uri, url.user().utf8().data());
153         soup_uri_set_password(uri, url.pass().utf8().data());
154     }
155     return uri;
156 }
157
158 }