⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qhttp.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
{    http->d_func()->closeConn();}class QHttpHeaderPrivate{    Q_DECLARE_PUBLIC(QHttpHeader)public:    inline virtual ~QHttpHeaderPrivate() {}    QList<QPair<QString, QString> > values;    bool valid;    QHttpHeader *q_ptr;};/**************************************************** * * QHttpHeader * ****************************************************//*!    \class QHttpHeader    \brief The QHttpHeader class contains header information for HTTP.    \ingroup io    \module network    In most cases you should use the more specialized derivatives of    this class, QHttpResponseHeader and QHttpRequestHeader, rather    than directly using QHttpHeader.    QHttpHeader provides the HTTP header fields. A HTTP header field    consists of a name followed by a colon, a single space, and the    field value. (See RFC 1945.) Field names are case-insensitive. A    typical header field looks like this:    \code    content-type: text/html    \endcode    In the API the header field name is called the "key" and the    content is called the "value". You can get and set a header    field's value by using its key with value() and setValue(), e.g.    \code    header.setValue("content-type", "text/html");    QString contentType = header.value("content-type");    \endcode    Some fields are so common that getters and setters are provided    for them as a convenient alternative to using \l value() and    \l setValue(), e.g. contentLength() and contentType(),    setContentLength() and setContentType().    Each header key has a \e single value associated with it. If you    set the value for a key which already exists the previous value    will be discarded.    \sa QHttpRequestHeader QHttpResponseHeader*//*!    \fn int QHttpHeader::majorVersion() const    Returns the major protocol-version of the HTTP header.*//*!    \fn int QHttpHeader::minorVersion() const    Returns the minor protocol-version of the HTTP header.*//*!        Constructs an empty HTTP header.*/QHttpHeader::QHttpHeader()    : d_ptr(new QHttpHeaderPrivate){    Q_D(QHttpHeader);    d->q_ptr = this;    d->valid = true;}/*!        Constructs a copy of \a header.*/QHttpHeader::QHttpHeader(const QHttpHeader &header)    : d_ptr(new QHttpHeaderPrivate){    Q_D(QHttpHeader);    d->q_ptr = this;    d->valid = header.d_func()->valid;    d->values = header.d_func()->values;}/*!    Constructs a HTTP header for \a str.    This constructor parses the string \a str for header fields and    adds this information. The \a str should consist of one or more    "\r\n" delimited lines; each of these lines should have the format    key, colon, space, value.*/QHttpHeader::QHttpHeader(const QString &str)    : d_ptr(new QHttpHeaderPrivate){    Q_D(QHttpHeader);    d->q_ptr = this;    d->valid = true;    parse(str);}/*! \internal */QHttpHeader::QHttpHeader(QHttpHeaderPrivate &dd, const QString &str)    : d_ptr(&dd){    Q_D(QHttpHeader);    d->q_ptr = this;    d->valid = true;    if (!str.isEmpty())        parse(str);}/*! \internal */QHttpHeader::QHttpHeader(QHttpHeaderPrivate &dd, const QHttpHeader &header)    : d_ptr(&dd){    Q_D(QHttpHeader);    d->q_ptr = this;    d->valid = header.d_func()->valid;    d->values = header.d_func()->values;}/*!    Destructor.*/QHttpHeader::~QHttpHeader(){    delete d_ptr;}/*!    Assigns \a h and returns a reference to this http header.*/QHttpHeader &QHttpHeader::operator=(const QHttpHeader &h){    Q_D(QHttpHeader);    d->values = h.d_func()->values;    d->valid = h.d_func()->valid;    return *this;}/*!    Returns true if the HTTP header is valid; otherwise returns false.    A QHttpHeader is invalid if it was created by parsing a malformed string.*/bool QHttpHeader::isValid() const{    Q_D(const QHttpHeader);    return d->valid;}/*! \internal    Parses the HTTP header string \a str for header fields and adds    the keys/values it finds. If the string is not parsed successfully    the QHttpHeader becomes \link isValid() invalid\endlink.    Returns true if \a str was successfully parsed; otherwise returns false.    \sa toString()*/bool QHttpHeader::parse(const QString &str){    Q_D(QHttpHeader);    QStringList lst;    int pos = str.indexOf(QLatin1Char('\n'));    if (pos > 0 && str.at(pos - 1) == QLatin1Char('\r'))        lst = str.trimmed().split(QLatin1String("\r\n"));    else        lst = str.trimmed().split(QLatin1String("\n"));    lst.removeAll(QString()); // No empties    if (lst.isEmpty())        return true;    QStringList lines;    QStringList::Iterator it = lst.begin();    for(; it != lst.end(); ++it) {        if (!(*it).isEmpty()) {            if ((*it)[0].isSpace()) {                if (!lines.isEmpty()) {                    lines.last() += QLatin1Char(' ');                    lines.last() += (*it).trimmed();                }            } else {                lines.append((*it));            }        }    }    int number = 0;    it = lines.begin();    for(; it != lines.end(); ++it) {        if (!parseLine(*it, number++)) {            d->valid = false;            return false;        }    }    return true;}/*! \internal*/void QHttpHeader::setValid(bool v){    Q_D(QHttpHeader);    d->valid = v;}/*!    Returns the first value for the entry with the given \a key. If no entry    has this \a key, an empty string is returned.    \sa setValue() removeValue() hasKey() keys()*/QString QHttpHeader::value(const QString &key) const{    Q_D(const QHttpHeader);    QString lowercaseKey = key.toLower();    QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin();    while (it != d->values.constEnd()) {        if ((*it).first == lowercaseKey)            return (*it).second;        ++it;    }    return QString();}/*!    Returns all the entries with the given \a key. If no entry    has this \a key, an empty string list is returned.*/QStringList QHttpHeader::allValues(const QString &key) const{    Q_D(const QHttpHeader);    QString lowercaseKey = key.toLower();    QStringList valueList;    QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin();    while (it != d->values.constEnd()) {        if ((*it).first == lowercaseKey)            valueList.append((*it).second);        ++it;    }    return valueList;}/*!    Returns a list of the keys in the HTTP header.    \sa hasKey()*/QStringList QHttpHeader::keys() const{    Q_D(const QHttpHeader);    QStringList keyList;    QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin();    while (it != d->values.constEnd()) {        if (!keyList.contains((*it).first))            keyList.append((*it).first);        ++it;    }    return keyList;}/*!    Returns true if the HTTP header has an entry with the given \a    key; otherwise returns false.    \sa value() setValue() keys()*/bool QHttpHeader::hasKey(const QString &key) const{    Q_D(const QHttpHeader);    QString lowercaseKey = key.toLower();    QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin();    while (it != d->values.constEnd()) {        if ((*it).first == lowercaseKey)            return true;        ++it;    }    return false;}/*!    Sets the value of the entry with the \a key to \a value.    If no entry with \a key exists, a new entry with the given \a key    and \a value is created. If an entry with the \a key already    exists, the first value is discarded and replaced with the given    \a value.    \sa value() hasKey() removeValue()*/void QHttpHeader::setValue(const QString &key, const QString &value){    Q_D(QHttpHeader);    QList<QPair<QString, QString> >::Iterator it = d->values.begin();    while (it != d->values.end()) {        if ((*it).first == key) {            (*it).second = value;            return;        }        ++it;    }    // not found so add    addValue(key, value);}/*!    Sets the header entries to be the list of key value pairs in \a values.*/void QHttpHeader::setValues(const QList<QPair<QString, QString> > &values){    Q_D(QHttpHeader);    d->values = values;}/*!    Adds a new entry with the \a key and \a value.*/void QHttpHeader::addValue(const QString &key, const QString &value){    Q_D(QHttpHeader);    d->values.append(qMakePair(key, value));}/*!    Returns all the entries in the header.*/QList<QPair<QString, QString> > QHttpHeader::values() const{    Q_D(const QHttpHeader);    return d->values;}/*!    Removes the entry with the key \a key from the HTTP header.    \sa value() setValue()*/void QHttpHeader::removeValue(const QString &key){    Q_D(QHttpHeader);    QList<QPair<QString, QString> >::Iterator it = d->values.begin();    while (it != d->values.end()) {        if ((*it).first == key) {            d->values.erase(it);            return;        }        ++it;    }}/*!    Removes all the entries with the key \a key from the HTTP header.*/void QHttpHeader::removeAllValues(const QString &key){    Q_D(QHttpHeader);    QList<QPair<QString, QString> >::Iterator it = d->values.begin();    while (it != d->values.end()) {        if ((*it).first == key) {            it = d->values.erase(it);            continue;        }        ++it;    }}/*! \internal    Parses the single HTTP header line \a line which has the format    key, colon, space, value, and adds key/value to the headers. The    linenumber is \a number. Returns true if the line was successfully    parsed and the key/value added; otherwise returns false.    \sa parse()*/bool QHttpHeader::parseLine(const QString &line, int){    int i = line.indexOf(QLatin1Char(':'));    if (i == -1)        return false;    addValue(line.left(i).trimmed().toLower(), line.mid(i + 1).trimmed());    return true;}/*!    Returns a string representation of the HTTP header.    The string is suitable for use by the constructor that takes a    QString. It consists of lines with the format: key, colon, space,    value, "\r\n".*/QString QHttpHeader::toString() const{    Q_D(const QHttpHeader);    if (!isValid())        return QLatin1String("");    QString ret = QLatin1String("");    QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin();    while (it != d->values.constEnd()) {        ret += (*it).first + QLatin1String(": ") + (*it).second + QLatin1String("\r\n");        ++it;    }    return ret;}/*!    Returns true if the header has an entry for the special HTTP    header field \c content-length; otherwise returns false.    \sa contentLength() setContentLength()*/bool QHttpHeader::hasContentLength() const{    return hasKey(QLatin1String("content-length"));}/*!    Returns the value of the special HTTP header field \c    content-length.    \sa setContentLength() hasContentLength()*/uint QHttpHeader::contentLength() const{    return value(QLatin1String("content-length")).toUInt();}/*!    Sets the value of the special HTTP header field \c content-length    to \a len.    \sa contentLength() hasContentLength()*/void QHttpHeader::setContentLength(int len){    setValue(QLatin1String("content-length"), QString::number(len));}/*!    Returns true if the header has an entry for the the special HTTP    header field \c content-type; otherwise returns false.    \sa contentType() setContentType()*/bool QHttpHeader::hasContentType() const{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -