Qt 4.8
Classes | Functions
qnetworkrequest.cpp File Reference
#include "qplatformdefs.h"
#include "qnetworkrequest.h"
#include "qnetworkcookie.h"
#include "qnetworkrequest_p.h"
#include "qsslconfiguration.h"
#include "QtCore/qshareddata.h"
#include "QtCore/qlocale.h"
#include "QtCore/qdatetime.h"
#include <ctype.h>
#include <stdio.h>

Go to the source code of this file.

Classes

class  QNetworkRequestPrivate
 

Functions

static QByteArray headerName (QNetworkRequest::KnownHeaders header)
 
static QByteArray headerValue (QNetworkRequest::KnownHeaders header, const QVariant &value)
 
static int name_to_month (const char *month_str)
 
static QVariant parseCookieHeader (const QByteArray &raw)
 
static QNetworkRequest::KnownHeaders parseHeaderName (const QByteArray &headerName)
 
static QVariant parseHeaderValue (QNetworkRequest::KnownHeaders header, const QByteArray &value)
 
static QVariant parseHttpDate (const QByteArray &raw)
 

Function Documentation

◆ headerName()

static QByteArray headerName ( QNetworkRequest::KnownHeaders  header)
static

Definition at line 654 of file qnetworkrequest.cpp.

Referenced by QHttpPart::operator!=(), QNetworkRequest::operator!=(), qmlxmlhttprequest_getResponseHeader(), and QNetworkHeadersPrivate::setCookedHeader().

655 {
656  switch (header) {
658  return "Content-Type";
659 
661  return "Content-Length";
662 
664  return "Location";
665 
667  return "Last-Modified";
668 
670  return "Cookie";
671 
673  return "Set-Cookie";
674 
676  return "Content-Disposition";
677 
678  // no default:
679  // if new values are added, this will generate a compiler warning
680  }
681 
682  return QByteArray();
683 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135

◆ headerValue()

static QByteArray headerValue ( QNetworkRequest::KnownHeaders  header,
const QVariant value 
)
static

Definition at line 685 of file qnetworkrequest.cpp.

Referenced by QHttpPart::operator!=(), and QNetworkHeadersPrivate::setCookedHeader().

686 {
687  switch (header) {
691  return value.toByteArray();
692 
694  switch (value.type()) {
695  case QVariant::Url:
696  return value.toUrl().toEncoded();
697 
698  default:
699  return value.toByteArray();
700  }
701 
703  switch (value.type()) {
704  case QVariant::Date:
705  case QVariant::DateTime:
706  // generate RFC 1123/822 dates:
708 
709  default:
710  return value.toByteArray();
711  }
712 
715  if (cookies.isEmpty() && value.userType() == qMetaTypeId<QNetworkCookie>())
716  cookies << qvariant_cast<QNetworkCookie>(value);
717 
718  QByteArray result;
719  bool first = true;
720  foreach (const QNetworkCookie &cookie, cookies) {
721  if (!first)
722  result += "; ";
723  first = false;
725  }
726  return result;
727  }
728 
731  if (cookies.isEmpty() && value.userType() == qMetaTypeId<QNetworkCookie>())
732  cookies << qvariant_cast<QNetworkCookie>(value);
733 
734  QByteArray result;
735  bool first = true;
736  foreach (const QNetworkCookie &cookie, cookies) {
737  if (!first)
738  result += ", ";
739  first = false;
740  result += cookie.toRawForm(QNetworkCookie::Full);
741  }
742  return result;
743  }
744  }
745 
746  return QByteArray();
747 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QDateTime toDateTime() const
Returns the variant as a QDateTime if the variant has type() DateTime , Date , or String ; otherwise ...
Definition: qvariant.cpp:2349
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QS...
Definition: qvariant.cpp:2383
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
QByteArray toRawForm(RawForm form=Full) const
Returns the raw form of this QNetworkCookie.
QUrl toUrl() const
Returns the variant as a QUrl if the variant has type() Url ; otherwise returns an invalid QUrl...
Definition: qvariant.cpp:2528
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
The QNetworkCookie class holds one network cookie.
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
static QByteArray toHttpDate(const QDateTime &dt)
T qvariant_cast(const QVariant &value)
Returns the given value converted to the template type T.
Definition: qvariant.h:571

◆ name_to_month()

static int name_to_month ( const char *  month_str)
static

Definition at line 967 of file qnetworkrequest.cpp.

Referenced by QNetworkHeadersPrivate::fromHttpDate().

968 {
969  switch (month_str[0]) {
970  case 'J':
971  switch (month_str[1]) {
972  case 'a':
973  return 1;
974  break;
975  case 'u':
976  switch (month_str[2] ) {
977  case 'n':
978  return 6;
979  break;
980  case 'l':
981  return 7;
982  break;
983  }
984  }
985  break;
986  case 'F':
987  return 2;
988  break;
989  case 'M':
990  switch (month_str[2] ) {
991  case 'r':
992  return 3;
993  break;
994  case 'y':
995  return 5;
996  break;
997  }
998  break;
999  case 'A':
1000  switch (month_str[1]) {
1001  case 'p':
1002  return 4;
1003  break;
1004  case 'u':
1005  return 8;
1006  break;
1007  }
1008  break;
1009  case 'O':
1010  return 10;
1011  break;
1012  case 'S':
1013  return 9;
1014  break;
1015  case 'N':
1016  return 11;
1017  break;
1018  case 'D':
1019  return 12;
1020  break;
1021  }
1022 
1023  return 0;
1024 }

◆ parseCookieHeader()

static QVariant parseCookieHeader ( const QByteArray raw)
static

Definition at line 788 of file qnetworkrequest.cpp.

Referenced by parseHeaderValue().

789 {
790  QList<QNetworkCookie> result;
791  QList<QByteArray> cookieList = raw.split(';');
792  foreach (const QByteArray &cookie, cookieList) {
794  if (parsed.count() != 1)
795  return QVariant(); // invalid Cookie: header
796 
797  result += parsed;
798  }
799 
800  return QVariant::fromValue(result);
801 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
static QList< QNetworkCookie > parseCookies(const QByteArray &cookieString)
Parses the cookie string cookieString as received from a server response in the "Set-Cookie:" header...
QByteArray trimmed() const
Returns a byte array that has whitespace removed from the start and the end.
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
QList< QByteArray > split(char sep) const
Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays...

◆ parseHeaderName()

static QNetworkRequest::KnownHeaders parseHeaderName ( const QByteArray headerName)
static

Definition at line 749 of file qnetworkrequest.cpp.

Referenced by QNetworkHeadersPrivate::parseAndSetHeader().

750 {
751  if (headerName.isEmpty())
753 
754  switch (tolower(headerName.at(0))) {
755  case 'c':
756  if (qstricmp(headerName.constData(), "content-type") == 0)
758  else if (qstricmp(headerName.constData(), "content-length") == 0)
760  else if (qstricmp(headerName.constData(), "cookie") == 0)
762  break;
763 
764  case 'l':
765  if (qstricmp(headerName.constData(), "location") == 0)
767  else if (qstricmp(headerName.constData(), "last-modified") == 0)
769  break;
770 
771  case 's':
772  if (qstricmp(headerName.constData(), "set-cookie") == 0)
774  break;
775  }
776 
777  return QNetworkRequest::KnownHeaders(-1); // nothing found
778 }
KnownHeaders
List of known header types that QNetworkRequest parses.
int qstricmp(const char *str1, const char *str2)
A safe stricmp() function.
Definition: qbytearray.cpp:279
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
char at(int i) const
Returns the character at index position i in the byte array.
Definition: qbytearray.h:413

◆ parseHeaderValue()

static QVariant parseHeaderValue ( QNetworkRequest::KnownHeaders  header,
const QByteArray value 
)
static

Definition at line 803 of file qnetworkrequest.cpp.

Referenced by QNetworkHeadersPrivate::parseAndSetHeader().

804 {
805  // header is always a valid value
806  switch (header) {
808  // copy exactly, convert to QString
809  return QString::fromLatin1(value);
810 
812  bool ok;
813  qint64 result = value.trimmed().toLongLong(&ok);
814  if (ok)
815  return result;
816  return QVariant();
817  }
818 
820  QUrl result = QUrl::fromEncoded(value, QUrl::StrictMode);
821  if (result.isValid() && !result.scheme().isEmpty())
822  return result;
823  return QVariant();
824  }
825 
827  return parseHttpDate(value);
828 
830  return parseCookieHeader(value);
831 
834 
835  default:
836  Q_ASSERT(0);
837  }
838  return QVariant();
839 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
Definition: qurl.cpp:4303
qlonglong toLongLong(bool *ok=0, int base=10) const
Returns the byte array converted to a {long long} using base base, which is 10 by default and must be...
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static QVariant parseCookieHeader(const QByteArray &raw)
static QList< QNetworkCookie > parseCookies(const QByteArray &cookieString)
Parses the cookie string cookieString as received from a server response in the "Set-Cookie:" header...
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QByteArray trimmed() const
Returns a byte array that has whitespace removed from the start and the end.
__int64 qint64
Definition: qglobal.h:942
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
static QVariant parseHttpDate(const QByteArray &raw)
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static QUrl fromEncoded(const QByteArray &url)
Parses input and returns the corresponding QUrl.
Definition: qurl.cpp:5964

◆ parseHttpDate()

static QVariant parseHttpDate ( const QByteArray raw)
static

Definition at line 780 of file qnetworkrequest.cpp.

Referenced by parseHeaderValue().

781 {
783  if (dt.isValid())
784  return dt;
785  return QVariant(); // transform an invalid QDateTime into a null QVariant
786 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool isValid() const
Returns true if both the date and the time are valid; otherwise returns false.
Definition: qdatetime.cpp:2346
static QDateTime fromHttpDate(const QByteArray &value)
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216