2 * Copyright (C) 2009 Gustavo Noronha Silva
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.
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.
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.
21 #include "ResourceRequest.h"
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>
35 void ResourceRequest::updateSoupMessageMembers(SoupMessage* soupMessage) const
37 updateSoupMessageHeaders(soupMessage->request_headers);
39 String firstPartyString = firstPartyForCookies().string();
40 if (!firstPartyString.isEmpty()) {
41 GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
42 soup_message_set_first_party(soupMessage, firstParty.get());
45 soup_message_set_flags(soupMessage, m_soupFlags);
47 if (!acceptEncoding())
48 soup_message_disable_feature(soupMessage, SOUP_TYPE_CONTENT_DECODER);
51 void ResourceRequest::updateSoupMessageHeaders(SoupMessageHeaders* soupHeaders) const
53 const HTTPHeaderMap& headers = httpHeaderFields();
54 if (!headers.isEmpty()) {
55 HTTPHeaderMap::const_iterator end = headers.end();
56 for (HTTPHeaderMap::const_iterator it = headers.begin(); it != end; ++it)
57 soup_message_headers_append(soupHeaders, it->key.string().utf8().data(), it->value.utf8().data());
61 void ResourceRequest::updateFromSoupMessageHeaders(SoupMessageHeaders* soupHeaders)
63 m_httpHeaderFields.clear();
64 SoupMessageHeadersIter headersIter;
65 soup_message_headers_iter_init(&headersIter, soupHeaders);
66 const char* headerName;
67 const char* headerValue;
68 while (soup_message_headers_iter_next(&headersIter, &headerName, &headerValue))
69 m_httpHeaderFields.set(String::fromUTF8(headerName), String::fromUTF8(headerValue));
72 void ResourceRequest::updateSoupMessage(SoupMessage* soupMessage) const
74 g_object_set(soupMessage, SOUP_MESSAGE_METHOD, httpMethod().utf8().data(), NULL);
76 GOwnPtr<SoupURI> uri(soupURI());
77 soup_message_set_uri(soupMessage, uri.get());
79 updateSoupMessageMembers(soupMessage);
82 SoupMessage* ResourceRequest::toSoupMessage() const
84 SoupMessage* soupMessage = soup_message_new(httpMethod().utf8().data(), url().string().utf8().data());
88 updateSoupMessageMembers(soupMessage);
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.
96 void ResourceRequest::updateFromSoupMessage(SoupMessage* soupMessage)
98 bool shouldPortBeResetToZero = m_url.hasPort() && !m_url.port();
99 m_url = soupURIToKURL(soup_message_get_uri(soupMessage));
101 // SoupURI cannot differeniate between an explicitly specified port 0 and
102 // no port specified.
103 if (shouldPortBeResetToZero)
106 m_httpMethod = String::fromUTF8(soupMessage->method);
108 updateFromSoupMessageHeaders(soupMessage->request_headers);
110 if (soupMessage->request_body->data)
111 m_httpBody = FormData::create(soupMessage->request_body->data, soupMessage->request_body->length);
113 SoupURI* firstParty = soup_message_get_first_party(soupMessage);
115 m_firstPartyForCookies = soupURIToKURL(firstParty);
117 m_soupFlags = soup_message_get_flags(soupMessage);
119 // FIXME: m_allowCookies should probably be handled here and on
120 // doUpdatePlatformRequest somehow.
123 unsigned initializeMaximumHTTPConnectionCountPerHost()
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.
131 SoupURI* ResourceRequest::soupURI() const
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());
144 url.removeFragmentIdentifier();
145 SoupURI* uri = soup_uri_new(url.string().utf8().data());
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());