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

📄 q3http.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <qplatformdefs.h>#include "q3http.h"#ifndef QT_NO_NETWORKPROTOCOL_HTTP#include "q3socket.h"#include "qtextstream.h"#include "qmap.h"#include "qstring.h"#include "qstringlist.h"#include "q3cstring.h"#include "qbuffer.h"#include "q3urloperator.h"#include "qtimer.h"#include "private/q3membuf_p.h"#include "qevent.h"#include "q3url.h"#include "qhttp.h"//#define Q3HTTP_DEBUGclass Q3HttpPrivate{public:    Q3HttpPrivate() :	state( Q3Http::Unconnected ),	error( Q3Http::NoError ),	hostname( QString() ),	port( 0 ),	toDevice( 0 ),	postDevice( 0 ),	bytesDone( 0 ),	chunkedSize( -1 ),	idleTimer( 0 )    {	pending.setAutoDelete( true );    }    Q3Socket socket;    Q3PtrList<Q3HttpRequest> pending;    Q3Http::State state;    Q3Http::Error error;    QString errorString;    QString hostname;    Q_UINT16 port;    QByteArray buffer;    QIODevice* toDevice;    QIODevice* postDevice;    uint bytesDone;    uint bytesTotal;    Q_LONG chunkedSize;    Q3HttpRequestHeader header;    bool readHeader;    QString headerStr;    Q3HttpResponseHeader response;    int idleTimer;    Q3Membuf rba;};class Q3HttpRequest{public:    Q3HttpRequest()    {	id = ++idCounter;    }    virtual ~Q3HttpRequest()    { }    virtual void start( Q3Http * ) = 0;    virtual bool hasRequestHeader();    virtual Q3HttpRequestHeader requestHeader();    virtual QIODevice* sourceDevice() = 0;    virtual QIODevice* destinationDevice() = 0;    int id;private:    static int idCounter;};int Q3HttpRequest::idCounter = 0;bool Q3HttpRequest::hasRequestHeader(){    return false;}Q3HttpRequestHeader Q3HttpRequest::requestHeader(){    return Q3HttpRequestHeader();}/**************************************************** * * Q3HttpNormalRequest * ****************************************************/class Q3HttpNormalRequest : public Q3HttpRequest{public:    Q3HttpNormalRequest( const Q3HttpRequestHeader &h, QIODevice *d, QIODevice *t ) :	header(h), to(t)    {	is_ba = false;	data.dev = d;    }    Q3HttpNormalRequest( const Q3HttpRequestHeader &h, QByteArray *d, QIODevice *t ) :	header(h), to(t)    {	is_ba = true;	data.ba = d;    }    ~Q3HttpNormalRequest()    {	if ( is_ba )	    delete data.ba;    }    void start( Q3Http * );    bool hasRequestHeader();    Q3HttpRequestHeader requestHeader();    QIODevice* sourceDevice();    QIODevice* destinationDevice();protected:    Q3HttpRequestHeader header;private:    union {	QByteArray *ba;	QIODevice *dev;    } data;    bool is_ba;    QIODevice *to;};void Q3HttpNormalRequest::start( Q3Http *http ){    http->d->header = header;    if ( is_ba ) {	http->d->buffer = *data.ba;	if ( http->d->buffer.size() > 0 )	    http->d->header.setContentLength( http->d->buffer.size() );	http->d->postDevice = 0;    } else {	http->d->buffer = QByteArray();	if ( data.dev && ( data.dev->isOpen() || data.dev->open(IO_ReadOnly) ) ) {	    http->d->postDevice = data.dev;	    if ( http->d->postDevice->size() > 0 )		http->d->header.setContentLength( http->d->postDevice->size() );	} else {	    http->d->postDevice = 0;	}    }    if ( to && ( to->isOpen() || to->open(IO_WriteOnly) ) )	http->d->toDevice = to;    else	http->d->toDevice = 0;    http->sendRequest();}bool Q3HttpNormalRequest::hasRequestHeader(){    return true;}Q3HttpRequestHeader Q3HttpNormalRequest::requestHeader(){    return header;}QIODevice* Q3HttpNormalRequest::sourceDevice(){    if ( is_ba )	return 0;    return data.dev;}QIODevice* Q3HttpNormalRequest::destinationDevice(){    return to;}/**************************************************** * * Q3HttpPGHRequest * (like a Q3HttpNormalRequest, but for the convenience * functions put(), get() and head() -- i.e. set the * host header field correctly before sending the * request) * ****************************************************/class Q3HttpPGHRequest : public Q3HttpNormalRequest{public:    Q3HttpPGHRequest( const Q3HttpRequestHeader &h, QIODevice *d, QIODevice *t ) :	Q3HttpNormalRequest( h, d, t )    { }    Q3HttpPGHRequest( const Q3HttpRequestHeader &h, QByteArray *d, QIODevice *t ) :	Q3HttpNormalRequest( h, d, t )    { }    ~Q3HttpPGHRequest()    { }    void start( Q3Http * );};void Q3HttpPGHRequest::start( Q3Http *http ){    header.setValue( QLatin1String("Host"), http->d->hostname );    Q3HttpNormalRequest::start( http );}/**************************************************** * * Q3HttpSetHostRequest * ****************************************************/class Q3HttpSetHostRequest : public Q3HttpRequest{public:    Q3HttpSetHostRequest( const QString &h, Q_UINT16 p ) :	hostname(h), port(p)    { }    void start( Q3Http * );    QIODevice* sourceDevice()    { return 0; }    QIODevice* destinationDevice()    { return 0; }private:    QString hostname;    Q_UINT16 port;};void Q3HttpSetHostRequest::start( Q3Http *http ){    http->d->hostname = hostname;    http->d->port = port;    http->finishedWithSuccess();}/**************************************************** * * Q3HttpCloseRequest * ****************************************************/class Q3HttpCloseRequest : public Q3HttpRequest{public:    Q3HttpCloseRequest()    { }    void start( Q3Http * );    QIODevice* sourceDevice()    { return 0; }    QIODevice* destinationDevice()    { return 0; }};void Q3HttpCloseRequest::start( Q3Http *http ){    http->close();}/**************************************************** * * Q3HttpHeader * ****************************************************//*!    \class Q3HttpHeader q3http.h    \brief The Q3HttpHeader class contains header information for HTTP.    \compat    In most cases you should use the more specialized derivatives of    this class, Q3HttpResponseHeader and Q3HttpRequestHeader, rather    than directly using Q3HttpHeader.    Q3HttpHeader 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 Q3HttpRequestHeader Q3HttpResponseHeader*//*!    \fn int Q3HttpHeader::majorVersion() const    Returns the major protocol-version of the HTTP header.*//*!    \fn int Q3HttpHeader::minorVersion() const    Returns the minor protocol-version of the HTTP header.*//*!	Constructs an empty HTTP header.*/Q3HttpHeader::Q3HttpHeader()    : valid( true ){}/*!	Constructs a copy of \a header.*/Q3HttpHeader::Q3HttpHeader( const Q3HttpHeader& header )    : valid( header.valid ){    values = header.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.*/Q3HttpHeader::Q3HttpHeader( const QString& str )    : valid( true ){    parse( str );}/*!    Destructor.*/Q3HttpHeader::~Q3HttpHeader(){}/*!    Assigns \a h and returns a reference to this http header.*/Q3HttpHeader& Q3HttpHeader::operator=( const Q3HttpHeader& h ){    values = h.values;    valid = h.valid;    return *this;}/*!    Returns true if the HTTP header is valid; otherwise returns false.    A Q3HttpHeader is invalid if it was created by parsing a malformed string.*/bool Q3HttpHeader::isValid() const{    return 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 Q3HttpHeader becomes \link isValid() invalid\endlink.    Returns true if \a str was successfully parsed; otherwise returns false.    \sa toString()*/bool Q3HttpHeader::parse( const QString& str ){    QStringList lst;    int pos = str.find( QLatin1Char('\n') );    if ( pos > 0 && str.at( pos - 1 ) == QLatin1Char('\r') )	lst = QStringList::split( QLatin1String("\r\n"), str.stripWhiteSpace(), false );    else	lst = QStringList::split( QLatin1String("\n"), str.stripWhiteSpace(), false );    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() += QLatin1String(" ");		    lines.last() += (*it).stripWhiteSpace();		}

⌨️ 快捷键说明

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