2010-10-13 Sergio Villar Senin <svillar@igalia.com>
[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 void ResourceRequest::updateSoupMessage(SoupMessage* soupMessage) const
37 {
38     g_object_set(soupMessage, SOUP_MESSAGE_METHOD, httpMethod().utf8().data(), NULL);
39
40     const HTTPHeaderMap& headers = httpHeaderFields();
41     SoupMessageHeaders* soupHeaders = soupMessage->request_headers;
42     if (!headers.isEmpty()) {
43         HTTPHeaderMap::const_iterator end = headers.end();
44         for (HTTPHeaderMap::const_iterator it = headers.begin(); it != end; ++it)
45             soup_message_headers_append(soupHeaders, it->first.string().utf8().data(), it->second.utf8().data());
46     }
47
48 #ifdef HAVE_LIBSOUP_2_29_90
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 #endif
55
56     soup_message_set_flags(soupMessage, m_soupFlags);
57 }
58
59 SoupMessage* ResourceRequest::toSoupMessage() const
60 {
61     SoupMessage* soupMessage = soup_message_new(httpMethod().utf8().data(), url().string().utf8().data());
62     if (!soupMessage)
63         return 0;
64
65     const HTTPHeaderMap& headers = httpHeaderFields();
66     SoupMessageHeaders* soupHeaders = soupMessage->request_headers;
67     if (!headers.isEmpty()) {
68         HTTPHeaderMap::const_iterator end = headers.end();
69         for (HTTPHeaderMap::const_iterator it = headers.begin(); it != end; ++it)
70             soup_message_headers_append(soupHeaders, it->first.string().utf8().data(), it->second.utf8().data());
71     }
72
73 #ifdef HAVE_LIBSOUP_2_29_90
74     String firstPartyString = firstPartyForCookies().string();
75     if (!firstPartyString.isEmpty()) {
76         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
77         soup_message_set_first_party(soupMessage, firstParty.get());
78     }
79 #endif
80
81     soup_message_set_flags(soupMessage, m_soupFlags);
82
83     // Body data is only handled at ResourceHandleSoup::startHttp for
84     // now; this is because this may not be a good place to go
85     // openning and mmapping files. We should maybe revisit this.
86     return soupMessage;
87 }
88
89 void ResourceRequest::updateFromSoupMessage(SoupMessage* soupMessage)
90 {
91     SoupURI* soupURI = soup_message_get_uri(soupMessage);
92     GOwnPtr<gchar> uri(soup_uri_to_string(soupURI, FALSE));
93     m_url = KURL(KURL(), String::fromUTF8(uri.get()));
94
95     m_httpMethod = String::fromUTF8(soupMessage->method);
96
97     SoupMessageHeadersIter headersIter;
98     const char* headerName;
99     const char* headerValue;
100
101     soup_message_headers_iter_init(&headersIter, soupMessage->request_headers);
102     while (soup_message_headers_iter_next(&headersIter, &headerName, &headerValue))
103         m_httpHeaderFields.set(String::fromUTF8(headerName), String::fromUTF8(headerValue));
104
105     if (soupMessage->request_body->data)
106         m_httpBody = FormData::create(soupMessage->request_body->data, soupMessage->request_body->length);
107
108 #ifdef HAVE_LIBSOUP_2_29_90
109     SoupURI* firstParty = soup_message_get_first_party(soupMessage);
110     if (firstParty) {
111         GOwnPtr<gchar> firstPartyURI(soup_uri_to_string(firstParty, FALSE));
112         m_firstPartyForCookies = KURL(KURL(), String::fromUTF8(firstPartyURI.get()));
113     }
114 #endif
115
116     m_soupFlags = soup_message_get_flags(soupMessage);
117
118     // FIXME: m_allowCookies should probably be handled here and on
119     // doUpdatePlatformRequest somehow.
120 }
121
122 unsigned initializeMaximumHTTPConnectionCountPerHost()
123 {
124     // Soup has its own queue control; it wants to have all requests
125     // given to it, so that it is able to look ahead, and schedule
126     // them in a good way.
127     return 10000;
128 }
129
130 }