src/network/access/qhttpnetworkrequest.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtNetwork module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qhttpnetworkrequest_p.h"
       
    43 #include "private/qnoncontiguousbytedevice_p.h"
       
    44 
       
    45 #ifndef QT_NO_HTTP
       
    46 
       
    47 QT_BEGIN_NAMESPACE
       
    48 
       
    49 QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(QHttpNetworkRequest::Operation op,
       
    50         QHttpNetworkRequest::Priority pri, const QUrl &newUrl)
       
    51     : QHttpNetworkHeaderPrivate(newUrl), operation(op), priority(pri), uploadByteDevice(0),
       
    52       autoDecompress(false), pipeliningAllowed(false)
       
    53 {
       
    54 }
       
    55 
       
    56 QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(const QHttpNetworkRequestPrivate &other)
       
    57     : QHttpNetworkHeaderPrivate(other)
       
    58 {
       
    59     operation = other.operation;
       
    60     priority = other.priority;
       
    61     uploadByteDevice = other.uploadByteDevice;
       
    62     autoDecompress = other.autoDecompress;
       
    63     pipeliningAllowed = other.pipeliningAllowed;
       
    64 }
       
    65 
       
    66 QHttpNetworkRequestPrivate::~QHttpNetworkRequestPrivate()
       
    67 {
       
    68 }
       
    69 
       
    70 bool QHttpNetworkRequestPrivate::operator==(const QHttpNetworkRequestPrivate &other) const
       
    71 {
       
    72     return QHttpNetworkHeaderPrivate::operator==(other)
       
    73         && (operation == other.operation)
       
    74         && (uploadByteDevice == other.uploadByteDevice);
       
    75 }
       
    76 
       
    77 QByteArray QHttpNetworkRequestPrivate::methodName() const
       
    78 {
       
    79     QByteArray ba;
       
    80     switch (operation) {
       
    81     case QHttpNetworkRequest::Options:
       
    82         ba += "OPTIONS";
       
    83         break;
       
    84     case QHttpNetworkRequest::Get:
       
    85         ba += "GET";
       
    86         break;
       
    87     case QHttpNetworkRequest::Head:
       
    88         ba += "HEAD";
       
    89         break;
       
    90     case QHttpNetworkRequest::Post:
       
    91         ba += "POST";
       
    92         break;
       
    93     case QHttpNetworkRequest::Put:
       
    94         ba += "PUT";
       
    95         break;
       
    96     case QHttpNetworkRequest::Delete:
       
    97         ba += "DELETE";
       
    98         break;
       
    99     case QHttpNetworkRequest::Trace:
       
   100         ba += "TRACE";
       
   101         break;
       
   102     case QHttpNetworkRequest::Connect:
       
   103         ba += "CONNECT";
       
   104         break;
       
   105     default:
       
   106         break;
       
   107     }
       
   108     return ba;
       
   109 }
       
   110 
       
   111 QByteArray QHttpNetworkRequestPrivate::uri(bool throughProxy) const
       
   112 {
       
   113     QUrl::FormattingOptions format(QUrl::RemoveFragment);
       
   114 
       
   115     // for POST, query data is send as content
       
   116     if (operation == QHttpNetworkRequest::Post && !uploadByteDevice)
       
   117         format |= QUrl::RemoveQuery;
       
   118     // for requests through proxy, the Request-URI contains full url
       
   119     if (throughProxy)
       
   120         format |= QUrl::RemoveUserInfo;
       
   121     else
       
   122         format |= QUrl::RemoveScheme | QUrl::RemoveAuthority;
       
   123     QByteArray uri = url.toEncoded(format);
       
   124     if (uri.isEmpty() || (throughProxy && url.path().isEmpty()))
       
   125         uri += '/';
       
   126     return uri;
       
   127 }
       
   128 
       
   129 QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request, bool throughProxy)
       
   130 {
       
   131     QByteArray ba = request.d->methodName();
       
   132     QByteArray uri = request.d->uri(throughProxy);
       
   133     ba += ' ' + uri;
       
   134 
       
   135     QString majorVersion = QString::number(request.majorVersion());
       
   136     QString minorVersion = QString::number(request.minorVersion());
       
   137     ba += " HTTP/" + majorVersion.toLatin1() + '.' + minorVersion.toLatin1() + "\r\n";
       
   138 
       
   139     QList<QPair<QByteArray, QByteArray> > fields = request.header();
       
   140     QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin();
       
   141     for (; it != fields.constEnd(); ++it)
       
   142         ba += it->first + ": " + it->second + "\r\n";
       
   143     if (request.d->operation == QHttpNetworkRequest::Post) {
       
   144         // add content type, if not set in the request
       
   145         if (request.headerField("content-type").isEmpty())
       
   146             ba += "Content-Type: application/x-www-form-urlencoded\r\n";
       
   147         if (!request.d->uploadByteDevice && request.d->url.hasQuery()) {
       
   148             QByteArray query = request.d->url.encodedQuery();
       
   149             ba += "Content-Length: "+ QByteArray::number(query.size()) + "\r\n";
       
   150             ba += "\r\n";
       
   151             ba += query;
       
   152         } else {
       
   153             ba += "\r\n";
       
   154         }
       
   155     } else {
       
   156         ba += "\r\n";
       
   157     }
       
   158      return ba;
       
   159 }
       
   160 
       
   161 
       
   162 // QHttpNetworkRequest
       
   163 
       
   164 QHttpNetworkRequest::QHttpNetworkRequest(const QUrl &url, Operation operation, Priority priority)
       
   165     : d(new QHttpNetworkRequestPrivate(operation, priority, url))
       
   166 {
       
   167 }
       
   168 
       
   169 QHttpNetworkRequest::QHttpNetworkRequest(const QHttpNetworkRequest &other)
       
   170     : QHttpNetworkHeader(other), d(other.d)
       
   171 {
       
   172 }
       
   173 
       
   174 QHttpNetworkRequest::~QHttpNetworkRequest()
       
   175 {
       
   176 }
       
   177 
       
   178 QUrl QHttpNetworkRequest::url() const
       
   179 {
       
   180     return d->url;
       
   181 }
       
   182 void QHttpNetworkRequest::setUrl(const QUrl &url)
       
   183 {
       
   184     d->url = url;
       
   185 }
       
   186 
       
   187 qint64 QHttpNetworkRequest::contentLength() const
       
   188 {
       
   189     return d->contentLength();
       
   190 }
       
   191 
       
   192 void QHttpNetworkRequest::setContentLength(qint64 length)
       
   193 {
       
   194     d->setContentLength(length);
       
   195 }
       
   196 
       
   197 QList<QPair<QByteArray, QByteArray> > QHttpNetworkRequest::header() const
       
   198 {
       
   199     return d->fields;
       
   200 }
       
   201 
       
   202 QByteArray QHttpNetworkRequest::headerField(const QByteArray &name, const QByteArray &defaultValue) const
       
   203 {
       
   204     return d->headerField(name, defaultValue);
       
   205 }
       
   206 
       
   207 void QHttpNetworkRequest::setHeaderField(const QByteArray &name, const QByteArray &data)
       
   208 {
       
   209     d->setHeaderField(name, data);
       
   210 }
       
   211 
       
   212 QHttpNetworkRequest &QHttpNetworkRequest::operator=(const QHttpNetworkRequest &other)
       
   213 {
       
   214     d = other.d;
       
   215     return *this;
       
   216 }
       
   217 
       
   218 bool QHttpNetworkRequest::operator==(const QHttpNetworkRequest &other) const
       
   219 {
       
   220     return d->operator==(*other.d);
       
   221 }
       
   222 
       
   223 QHttpNetworkRequest::Operation QHttpNetworkRequest::operation() const
       
   224 {
       
   225     return d->operation;
       
   226 }
       
   227 
       
   228 void QHttpNetworkRequest::setOperation(Operation operation)
       
   229 {
       
   230     d->operation = operation;
       
   231 }
       
   232 
       
   233 QHttpNetworkRequest::Priority QHttpNetworkRequest::priority() const
       
   234 {
       
   235     return d->priority;
       
   236 }
       
   237 
       
   238 void QHttpNetworkRequest::setPriority(Priority priority)
       
   239 {
       
   240     d->priority = priority;
       
   241 }
       
   242 
       
   243 bool QHttpNetworkRequest::isPipeliningAllowed() const
       
   244 {
       
   245     return d->pipeliningAllowed;
       
   246 }
       
   247 
       
   248 void QHttpNetworkRequest::setPipeliningAllowed(bool b)
       
   249 {
       
   250     d->pipeliningAllowed = b;
       
   251 }
       
   252 
       
   253 void QHttpNetworkRequest::setUploadByteDevice(QNonContiguousByteDevice *bd)
       
   254 {
       
   255     d->uploadByteDevice = bd;
       
   256 }
       
   257 
       
   258 QNonContiguousByteDevice* QHttpNetworkRequest::uploadByteDevice() const
       
   259 {
       
   260     return d->uploadByteDevice;
       
   261 }
       
   262 
       
   263 int QHttpNetworkRequest::majorVersion() const
       
   264 {
       
   265     return 1;
       
   266 }
       
   267 
       
   268 int QHttpNetworkRequest::minorVersion() const
       
   269 {
       
   270     return 1;
       
   271 }
       
   272 
       
   273 
       
   274 QT_END_NAMESPACE
       
   275 
       
   276 #endif
       
   277