Disable HTTP request "Accept-Encoding:" header field on gstreamer source element...
[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     if (!acceptEncoding())
74         soup_message_disable_feature(soupMessage, SOUP_TYPE_CONTENT_DECODER);
75 }
76
77 SoupMessage* ResourceRequest::toSoupMessage() const
78 {
79     SoupMessage* soupMessage = soup_message_new(httpMethod().utf8().data(), url().string().utf8().data());
80     if (!soupMessage)
81         return 0;
82
83     updateSoupMessageHeaders(soupMessage->request_headers);
84
85     String firstPartyString = firstPartyForCookies().string();
86     if (!firstPartyString.isEmpty()) {
87         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
88         soup_message_set_first_party(soupMessage, firstParty.get());
89     }
90
91     soup_message_set_flags(soupMessage, m_soupFlags);
92
93     // Body data is only handled at ResourceHandleSoup::startHttp for
94     // now; this is because this may not be a good place to go
95     // openning and mmapping files. We should maybe revisit this.
96     return soupMessage;
97 }
98
99 void ResourceRequest::updateFromSoupMessage(SoupMessage* soupMessage)
100 {
101     bool shouldPortBeResetToZero = m_url.hasPort() && !m_url.port();
102     m_url = soupURIToKURL(soup_message_get_uri(soupMessage));
103
104     // SoupURI cannot differeniate between an explicitly specified port 0 and
105     // no port specified.
106     if (shouldPortBeResetToZero)
107         m_url.setPort(0);
108
109     m_httpMethod = String::fromUTF8(soupMessage->method);
110
111     updateFromSoupMessageHeaders(soupMessage->request_headers);
112
113     if (soupMessage->request_body->data)
114         m_httpBody = FormData::create(soupMessage->request_body->data, soupMessage->request_body->length);
115
116     SoupURI* firstParty = soup_message_get_first_party(soupMessage);
117     if (firstParty)
118         m_firstPartyForCookies = soupURIToKURL(firstParty);
119
120     m_soupFlags = soup_message_get_flags(soupMessage);
121
122     // FIXME: m_allowCookies should probably be handled here and on
123     // doUpdatePlatformRequest somehow.
124 }
125
126 unsigned initializeMaximumHTTPConnectionCountPerHost()
127 {
128     // Soup has its own queue control; it wants to have all requests
129     // given to it, so that it is able to look ahead, and schedule
130     // them in a good way.
131     return 10000;
132 }
133
134 SoupURI* ResourceRequest::soupURI() const
135 {
136     // WebKit does not support fragment identifiers in data URLs, but soup does.
137     // Before passing the URL to soup, we should make sure to urlencode any '#'
138     // characters, so that soup does not interpret them as fragment identifiers.
139     // See http://wkbug.com/68089
140     if (m_url.protocolIsData()) {
141         String urlString = m_url.string();
142         urlString.replace("#", "%23");
143         return soup_uri_new(urlString.utf8().data());
144     }
145
146     KURL url = m_url;
147     url.removeFragmentIdentifier();
148     SoupURI* uri = soup_uri_new(url.string().utf8().data());
149
150     // Versions of libsoup prior to 2.42 have a soup_uri_new that will convert empty passwords that are not
151     // prefixed by a colon into null. Some parts of soup like the SoupAuthenticationManager will only be active
152     // when both the username and password are non-null. When we have credentials, empty usernames and passwords
153     // should be empty strings instead of null.
154     if (!url.user().isEmpty() || !url.pass().isEmpty()) {
155         soup_uri_set_user(uri, url.user().utf8().data());
156         soup_uri_set_password(uri, url.pass().utf8().data());
157     }
158     return uri;
159 }
160
161 }