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