Part 2 of removing PlatformString.h, remove PlatformString.h
[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 <wtf/gobject/GOwnPtr.h>
24 #include "GOwnPtrSoup.h"
25 #include "HTTPParsers.h"
26 #include "MIMETypeRegistry.h"
27 #include "SoupURIUtils.h"
28
29 #include <libsoup/soup.h>
30 #include <wtf/text/CString.h>
31 #include <wtf/text/WTFString.h>
32
33 using namespace std;
34
35 namespace WebCore {
36
37 void ResourceRequest::updateSoupMessage(SoupMessage* soupMessage) const
38 {
39     g_object_set(soupMessage, SOUP_MESSAGE_METHOD, httpMethod().utf8().data(), NULL);
40
41     const HTTPHeaderMap& headers = httpHeaderFields();
42     SoupMessageHeaders* soupHeaders = soupMessage->request_headers;
43     if (!headers.isEmpty()) {
44         HTTPHeaderMap::const_iterator end = headers.end();
45         for (HTTPHeaderMap::const_iterator it = headers.begin(); it != end; ++it)
46             soup_message_headers_append(soupHeaders, it->first.string().utf8().data(), it->second.utf8().data());
47     }
48
49     String firstPartyString = firstPartyForCookies().string();
50     if (!firstPartyString.isEmpty()) {
51         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
52         soup_message_set_first_party(soupMessage, firstParty.get());
53     }
54
55     soup_message_set_flags(soupMessage, m_soupFlags);
56 }
57
58 SoupMessage* ResourceRequest::toSoupMessage() const
59 {
60     SoupMessage* soupMessage = soup_message_new(httpMethod().utf8().data(), url().string().utf8().data());
61     if (!soupMessage)
62         return 0;
63
64     const HTTPHeaderMap& headers = httpHeaderFields();
65     SoupMessageHeaders* soupHeaders = soupMessage->request_headers;
66     if (!headers.isEmpty()) {
67         HTTPHeaderMap::const_iterator end = headers.end();
68         for (HTTPHeaderMap::const_iterator it = headers.begin(); it != end; ++it)
69             soup_message_headers_append(soupHeaders, it->first.string().utf8().data(), it->second.utf8().data());
70     }
71
72     String firstPartyString = firstPartyForCookies().string();
73     if (!firstPartyString.isEmpty()) {
74         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
75         soup_message_set_first_party(soupMessage, firstParty.get());
76     }
77
78     soup_message_set_flags(soupMessage, m_soupFlags);
79
80     // Body data is only handled at ResourceHandleSoup::startHttp for
81     // now; this is because this may not be a good place to go
82     // openning and mmapping files. We should maybe revisit this.
83     return soupMessage;
84 }
85
86 void ResourceRequest::updateFromSoupMessage(SoupMessage* soupMessage)
87 {
88     bool shouldPortBeResetToZero = m_url.hasPort() && !m_url.port();
89     m_url = soupURIToKURL(soup_message_get_uri(soupMessage));
90
91     // SoupURI cannot differeniate between an explicitly specified port 0 and
92     // no port specified.
93     if (shouldPortBeResetToZero)
94         m_url.setPort(0);
95
96     m_httpMethod = String::fromUTF8(soupMessage->method);
97
98     m_httpHeaderFields.clear();
99     SoupMessageHeadersIter headersIter;
100     const char* headerName;
101     const char* headerValue;
102     soup_message_headers_iter_init(&headersIter, soupMessage->request_headers);
103     while (soup_message_headers_iter_next(&headersIter, &headerName, &headerValue))
104         m_httpHeaderFields.set(String::fromUTF8(headerName), String::fromUTF8(headerValue));
105
106     if (soupMessage->request_body->data)
107         m_httpBody = FormData::create(soupMessage->request_body->data, soupMessage->request_body->length);
108
109     SoupURI* firstParty = soup_message_get_first_party(soupMessage);
110     if (firstParty)
111         m_firstPartyForCookies = soupURIToKURL(firstParty);
112
113     m_soupFlags = soup_message_get_flags(soupMessage);
114
115     // FIXME: m_allowCookies should probably be handled here and on
116     // doUpdatePlatformRequest somehow.
117 }
118
119 unsigned initializeMaximumHTTPConnectionCountPerHost()
120 {
121     // Soup has its own queue control; it wants to have all requests
122     // given to it, so that it is able to look ahead, and schedule
123     // them in a good way.
124     return 10000;
125 }
126
127 }