src/network/access/qhttpnetworkrequest.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    47 QT_BEGIN_NAMESPACE
    47 QT_BEGIN_NAMESPACE
    48 
    48 
    49 QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(QHttpNetworkRequest::Operation op,
    49 QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(QHttpNetworkRequest::Operation op,
    50         QHttpNetworkRequest::Priority pri, const QUrl &newUrl)
    50         QHttpNetworkRequest::Priority pri, const QUrl &newUrl)
    51     : QHttpNetworkHeaderPrivate(newUrl), operation(op), priority(pri), uploadByteDevice(0),
    51     : QHttpNetworkHeaderPrivate(newUrl), operation(op), priority(pri), uploadByteDevice(0),
    52       autoDecompress(false), pipeliningAllowed(false)
    52       autoDecompress(false), pipeliningAllowed(false), withCredentials(true)
    53 {
    53 {
    54 }
    54 }
    55 
    55 
    56 QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(const QHttpNetworkRequestPrivate &other)
    56 QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(const QHttpNetworkRequestPrivate &other)
    57     : QHttpNetworkHeaderPrivate(other)
    57     : QHttpNetworkHeaderPrivate(other)
    59     operation = other.operation;
    59     operation = other.operation;
    60     priority = other.priority;
    60     priority = other.priority;
    61     uploadByteDevice = other.uploadByteDevice;
    61     uploadByteDevice = other.uploadByteDevice;
    62     autoDecompress = other.autoDecompress;
    62     autoDecompress = other.autoDecompress;
    63     pipeliningAllowed = other.pipeliningAllowed;
    63     pipeliningAllowed = other.pipeliningAllowed;
       
    64     customVerb = other.customVerb;
       
    65     withCredentials = other.withCredentials;
    64 }
    66 }
    65 
    67 
    66 QHttpNetworkRequestPrivate::~QHttpNetworkRequestPrivate()
    68 QHttpNetworkRequestPrivate::~QHttpNetworkRequestPrivate()
    67 {
    69 {
    68 }
    70 }
    74         && (uploadByteDevice == other.uploadByteDevice);
    76         && (uploadByteDevice == other.uploadByteDevice);
    75 }
    77 }
    76 
    78 
    77 QByteArray QHttpNetworkRequestPrivate::methodName() const
    79 QByteArray QHttpNetworkRequestPrivate::methodName() const
    78 {
    80 {
    79     QByteArray ba;
       
    80     switch (operation) {
    81     switch (operation) {
       
    82     case QHttpNetworkRequest::Get:
       
    83         return "GET";
       
    84         break;
       
    85     case QHttpNetworkRequest::Head:
       
    86         return "HEAD";
       
    87         break;
       
    88     case QHttpNetworkRequest::Post:
       
    89         return "POST";
       
    90         break;
    81     case QHttpNetworkRequest::Options:
    91     case QHttpNetworkRequest::Options:
    82         ba += "OPTIONS";
    92         return "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         break;
    93     case QHttpNetworkRequest::Put:
    94     case QHttpNetworkRequest::Put:
    94         ba += "PUT";
    95         return "PUT";
    95         break;
    96         break;
    96     case QHttpNetworkRequest::Delete:
    97     case QHttpNetworkRequest::Delete:
    97         ba += "DELETE";
    98         return "DELETE";
    98         break;
    99         break;
    99     case QHttpNetworkRequest::Trace:
   100     case QHttpNetworkRequest::Trace:
   100         ba += "TRACE";
   101         return "TRACE";
   101         break;
   102         break;
   102     case QHttpNetworkRequest::Connect:
   103     case QHttpNetworkRequest::Connect:
   103         ba += "CONNECT";
   104         return "CONNECT";
       
   105         break;
       
   106     case QHttpNetworkRequest::Custom:
       
   107         return customVerb;
   104         break;
   108         break;
   105     default:
   109     default:
   106         break;
   110         break;
   107     }
   111     }
   108     return ba;
   112     return QByteArray();
   109 }
   113 }
   110 
   114 
   111 QByteArray QHttpNetworkRequestPrivate::uri(bool throughProxy) const
   115 QByteArray QHttpNetworkRequestPrivate::uri(bool throughProxy) const
   112 {
   116 {
   113     QUrl::FormattingOptions format(QUrl::RemoveFragment);
   117     QUrl::FormattingOptions format(QUrl::RemoveFragment);
   126     return uri;
   130     return uri;
   127 }
   131 }
   128 
   132 
   129 QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request, bool throughProxy)
   133 QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request, bool throughProxy)
   130 {
   134 {
   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();
   135     QList<QPair<QByteArray, QByteArray> > fields = request.header();
       
   136     QByteArray ba;
       
   137     ba.reserve(40 + fields.length()*25); // very rough lower bound estimation
       
   138 
       
   139     ba += request.d->methodName();
       
   140     ba += ' ';
       
   141     ba += request.d->uri(throughProxy);
       
   142 
       
   143     ba += " HTTP/";
       
   144     ba += QByteArray::number(request.majorVersion());
       
   145     ba += '.';
       
   146     ba += QByteArray::number(request.minorVersion());
       
   147     ba += "\r\n";
       
   148 
   140     QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin();
   149     QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin();
   141     for (; it != fields.constEnd(); ++it)
   150     QList<QPair<QByteArray, QByteArray> >::const_iterator endIt = fields.constEnd();
   142         ba += it->first + ": " + it->second + "\r\n";
   151     for (; it != endIt; ++it) {
       
   152         ba += it->first;
       
   153         ba += ": ";
       
   154         ba += it->second;
       
   155         ba += "\r\n";
       
   156     }
   143     if (request.d->operation == QHttpNetworkRequest::Post) {
   157     if (request.d->operation == QHttpNetworkRequest::Post) {
   144         // add content type, if not set in the request
   158         // add content type, if not set in the request
   145         if (request.headerField("content-type").isEmpty())
   159         if (request.headerField("content-type").isEmpty())
   146             ba += "Content-Type: application/x-www-form-urlencoded\r\n";
   160             ba += "Content-Type: application/x-www-form-urlencoded\r\n";
   147         if (!request.d->uploadByteDevice && request.d->url.hasQuery()) {
   161         if (!request.d->uploadByteDevice && request.d->url.hasQuery()) {
   148             QByteArray query = request.d->url.encodedQuery();
   162             QByteArray query = request.d->url.encodedQuery();
   149             ba += "Content-Length: "+ QByteArray::number(query.size()) + "\r\n";
   163             ba += "Content-Length: ";
   150             ba += "\r\n";
   164             ba += QByteArray::number(query.size());
       
   165             ba += "\r\n\r\n";
   151             ba += query;
   166             ba += query;
   152         } else {
   167         } else {
   153             ba += "\r\n";
   168             ba += "\r\n";
   154         }
   169         }
   155     } else {
   170     } else {
   228 void QHttpNetworkRequest::setOperation(Operation operation)
   243 void QHttpNetworkRequest::setOperation(Operation operation)
   229 {
   244 {
   230     d->operation = operation;
   245     d->operation = operation;
   231 }
   246 }
   232 
   247 
       
   248 QByteArray QHttpNetworkRequest::customVerb() const
       
   249 {
       
   250     return d->customVerb;
       
   251 }
       
   252 
       
   253 void QHttpNetworkRequest::setCustomVerb(const QByteArray &customVerb)
       
   254 {
       
   255     d->customVerb = customVerb;
       
   256 }
       
   257 
   233 QHttpNetworkRequest::Priority QHttpNetworkRequest::priority() const
   258 QHttpNetworkRequest::Priority QHttpNetworkRequest::priority() const
   234 {
   259 {
   235     return d->priority;
   260     return d->priority;
   236 }
   261 }
   237 
   262 
   248 void QHttpNetworkRequest::setPipeliningAllowed(bool b)
   273 void QHttpNetworkRequest::setPipeliningAllowed(bool b)
   249 {
   274 {
   250     d->pipeliningAllowed = b;
   275     d->pipeliningAllowed = b;
   251 }
   276 }
   252 
   277 
       
   278 bool QHttpNetworkRequest::withCredentials() const
       
   279 {
       
   280     return d->withCredentials;
       
   281 }
       
   282 
       
   283 void QHttpNetworkRequest::setWithCredentials(bool b)
       
   284 {
       
   285     d->withCredentials = b;
       
   286 }
       
   287 
   253 void QHttpNetworkRequest::setUploadByteDevice(QNonContiguousByteDevice *bd)
   288 void QHttpNetworkRequest::setUploadByteDevice(QNonContiguousByteDevice *bd)
   254 {
   289 {
   255     d->uploadByteDevice = bd;
   290     d->uploadByteDevice = bd;
   256 }
   291 }
   257 
   292