1/*
2    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
3
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library 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    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library 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 "NetworkingContext.h"
22#include "ResourceRequest.h"
23#include "ThirdPartyCookiesQt.h"
24
25#include <qglobal.h>
26
27#include <QNetworkRequest>
28#include <QUrl>
29
30namespace WebCore {
31
32// The limit can be found in qhttpnetworkconnection.cpp.
33// To achieve the best result we want WebKit to schedule the jobs so we
34// are using the limit as found in Qt. To allow Qt to fill its queue
35// and prepare jobs we will schedule two more downloads.
36// Per TCP connection there is 1 current processed, 3 possibly pipelined
37// and 2 ready to re-fill the pipeline.
38unsigned initializeMaximumHTTPConnectionCountPerHost()
39{
40    return 6 * (1 + 3 + 2);
41}
42
43QNetworkRequest ResourceRequest::toNetworkRequest(NetworkingContext *context) const
44{
45    QNetworkRequest request;
46    request.setUrl(url());
47    request.setOriginatingObject(context ? context->originatingObject() : 0);
48
49    const HTTPHeaderMap &headers = httpHeaderFields();
50    for (HTTPHeaderMap::const_iterator it = headers.begin(), end = headers.end();
51         it != end; ++it) {
52        QByteArray name = QString(it->key).toLatin1();
53        QByteArray value = QString(it->value).toLatin1();
54        // QNetworkRequest::setRawHeader() would remove the header if the value is null
55        // Make sure to set an empty header instead of null header.
56        if (!value.isNull())
57            request.setRawHeader(name, value);
58        else
59            request.setRawHeader(name, "");
60    }
61
62    // Make sure we always have an Accept header; some sites require this to
63    // serve subresources
64    if (!request.hasRawHeader("Accept"))
65        request.setRawHeader("Accept", "*/*");
66
67    switch (cachePolicy()) {
68    case ReloadIgnoringCacheData:
69        request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork);
70        break;
71    case ReturnCacheDataElseLoad:
72        request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
73        break;
74    case ReturnCacheDataDontLoad:
75        request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysCache);
76        break;
77    case UseProtocolCachePolicy:
78        // QNetworkRequest::PreferNetwork
79    default:
80        break;
81    }
82
83    if (!allowCookies() || !thirdPartyCookiePolicyPermits(context, url(), firstPartyForCookies())) {
84        request.setAttribute(QNetworkRequest::CookieSaveControlAttribute, QNetworkRequest::Manual);
85        request.setAttribute(QNetworkRequest::CookieLoadControlAttribute, QNetworkRequest::Manual);
86    }
87
88    if (!allowCookies())
89        request.setAttribute(QNetworkRequest::AuthenticationReuseAttribute, QNetworkRequest::Manual);
90
91    return request;
92}
93
94}
95
96