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 "CString.h"
24#include "GOwnPtr.h"
25#include "GOwnPtrGtk.h"
26#include "PlatformString.h"
27
28#include <libsoup/soup.h>
29
30using namespace std;
31
32namespace WebCore {
33
34SoupMessage* ResourceRequest::toSoupMessage() const
35{
36    SoupMessage* soupMessage = soup_message_new(httpMethod().utf8().data(), url().string().utf8().data());
37    if (!soupMessage)
38        return 0;
39
40    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    // Body data is only handled at ResourceHandleSoup::startHttp for
57    // now; this is because this may not be a good place to go
58    // openning and mmapping files. We should maybe revisit this.
59    return soupMessage;
60}
61
62void ResourceRequest::updateFromSoupMessage(SoupMessage* soupMessage)
63{
64    SoupURI* soupURI = soup_message_get_uri(soupMessage);
65    GOwnPtr<gchar> uri(soup_uri_to_string(soupURI, FALSE));
66    m_url = KURL(KURL(), String::fromUTF8(uri.get()));
67
68    m_httpMethod = String::fromUTF8(soupMessage->method);
69
70    SoupMessageHeadersIter headersIter;
71    const char* headerName;
72    const char* headerValue;
73
74    soup_message_headers_iter_init(&headersIter, soupMessage->request_headers);
75    while (soup_message_headers_iter_next(&headersIter, &headerName, &headerValue))
76        m_httpHeaderFields.set(String::fromUTF8(headerName), String::fromUTF8(headerValue));
77
78    if (soupMessage->request_body->data)
79        m_httpBody = FormData::create(soupMessage->request_body->data, soupMessage->request_body->length);
80
81#ifdef HAVE_LIBSOUP_2_29_90
82    SoupURI* firstParty = soup_message_get_first_party(soupMessage);
83    if (firstParty) {
84        GOwnPtr<gchar> firstPartyURI(soup_uri_to_string(firstParty, FALSE));
85        m_firstPartyForCookies = KURL(KURL(), String::fromUTF8(firstPartyURI.get()));
86    }
87#endif
88
89    // FIXME: m_allowCookies should probably be handled here and on
90    // doUpdatePlatformRequest somehow.
91}
92
93unsigned initializeMaximumHTTPConnectionCountPerHost()
94{
95    // Soup has its own queue control; it wants to have all requests
96    // given to it, so that it is able to look ahead, and schedule
97    // them in a good way.
98    return 10000;
99}
100
101}
102