📄 q3http.cpp
字号:
} else { lines.append( (*it) ); } } } int number = 0; it = lines.begin(); for( ; it != lines.end(); ++it ) { if ( !parseLine( *it, number++ ) ) { valid = false; return false; } } return true;}/*! \internal*/void Q3HttpHeader::setValid( bool v ){ valid = v;}/*! Returns the 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 Q3HttpHeader::value( const QString& key ) const{ return values[ key.lower() ];}/*! Returns a list of the keys in the HTTP header. \sa hasKey()*/QStringList Q3HttpHeader::keys() const{ return values.keys();}/*! Returns true if the HTTP header has an entry with the given \a key; otherwise returns false. \sa value() setValue() keys()*/bool Q3HttpHeader::hasKey( const QString& key ) const{ return values.contains( key.lower() );}/*! 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, its value is discarded and replaced with the given \a value. \sa value() hasKey() removeValue()*/void Q3HttpHeader::setValue( const QString& key, const QString& value ){ values[ key.lower() ] = value;}/*! Removes the entry with the key \a key from the HTTP header. \sa value() setValue()*/void Q3HttpHeader::removeValue( const QString& key ){ values.remove( key.lower() );}/*! \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 Q3HttpHeader::parseLine( const QString& line, int ){ int i = line.find( QLatin1String(":") ); if ( i == -1 ) return false; values.insert( line.left( i ).stripWhiteSpace().lower(), line.mid( i + 1 ).stripWhiteSpace() ); 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 Q3HttpHeader::toString() const{ if ( !isValid() ) return QLatin1String(""); QString ret = QLatin1String(""); QMap<QString,QString>::ConstIterator it = values.begin(); for( ; it != values.end(); ++it ) ret += it.key() + QLatin1String(": ") + it.data() + QLatin1String("\r\n"); 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 Q3HttpHeader::hasContentLength() const{ return hasKey( QLatin1String("content-length") );}/*! Returns the value of the special HTTP header field \c content-length. \sa setContentLength() hasContentLength()*/uint Q3HttpHeader::contentLength() const{ return values[ QLatin1String("content-length") ].toUInt();}/*! Sets the value of the special HTTP header field \c content-length to \a len. \sa contentLength() hasContentLength()*/void Q3HttpHeader::setContentLength( int len ){ values[ 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 Q3HttpHeader::hasContentType() const{ return hasKey( QLatin1String("content-type") );}/*! Returns the value of the special HTTP header field \c content-type. \sa setContentType() hasContentType()*/QString Q3HttpHeader::contentType() const{ QString type = values[ QLatin1String("content-type") ]; if ( type.isEmpty() ) return QString(); int pos = type.find( QLatin1String(";") ); if ( pos == -1 ) return type; return type.left( pos ).stripWhiteSpace();}/*! Sets the value of the special HTTP header field \c content-type to \a type. \sa contentType() hasContentType()*/void Q3HttpHeader::setContentType( const QString& type ){ values[ QLatin1String("content-type") ] = type;}/**************************************************** * * Q3HttpResponseHeader * ****************************************************//*! \class Q3HttpResponseHeader q3http.h \brief The Q3HttpResponseHeader class contains response header information for HTTP. \compat This class is used by the Q3Http class to report the header information that the client received from the server. HTTP responses have a status code that indicates the status of the response. This code is a 3-digit integer result code (for details see to RFC 1945). In addition to the status code, you can also specify a human-readable text that describes the reason for the code ("reason phrase"). This class allows you to get the status code and the reason phrase. \sa Q3HttpRequestHeader Q3Http*//*! Constructs an empty HTTP response header.*/Q3HttpResponseHeader::Q3HttpResponseHeader(){ setValid( false );}/*! Constructs a HTTP response header with the status code \a code, the reason phrase \a text and the protocol-version \a majorVer and \a minorVer.*/Q3HttpResponseHeader::Q3HttpResponseHeader( int code, const QString& text, int majorVer, int minorVer ) : Q3HttpHeader(), statCode( code ), reasonPhr( text ), majVer( majorVer ), minVer( minorVer ){}/*! Constructs a copy of \a header.*/Q3HttpResponseHeader::Q3HttpResponseHeader( const Q3HttpResponseHeader& header ) : Q3HttpHeader( header ), statCode( header.statCode ), reasonPhr( header.reasonPhr ), majVer( header.majVer ), minVer( header.minVer ){}/*! Constructs a HTTP response header from the string \a str. The string is parsed and the information is set. The \a str should consist of one or more "\r\n" delimited lines; the first line should be the status-line (format: HTTP-version, space, status-code, space, reason-phrase); each of remaining lines should have the format key, colon, space, value.*/Q3HttpResponseHeader::Q3HttpResponseHeader( const QString& str ) : Q3HttpHeader(){ parse( str );}/*! Sets the status code to \a code, the reason phrase to \a text and the protocol-version to \a majorVer and \a minorVer. \sa statusCode() reasonPhrase() majorVersion() minorVersion()*/void Q3HttpResponseHeader::setStatusLine( int code, const QString& text, int majorVer, int minorVer ){ setValid( true ); statCode = code; reasonPhr = text; majVer = majorVer; minVer = minorVer;}/*! Returns the status code of the HTTP response header. \sa reasonPhrase() majorVersion() minorVersion()*/int Q3HttpResponseHeader::statusCode() const{ return statCode;}/*! Returns the reason phrase of the HTTP response header. \sa statusCode() majorVersion() minorVersion()*/QString Q3HttpResponseHeader::reasonPhrase() const{ return reasonPhr;}/*! Returns the major protocol-version of the HTTP response header. \sa minorVersion() statusCode() reasonPhrase()*/int Q3HttpResponseHeader::majorVersion() const{ return majVer;}/*! Returns the minor protocol-version of the HTTP response header. \sa majorVersion() statusCode() reasonPhrase()*/int Q3HttpResponseHeader::minorVersion() const{ return minVer;}/*! \reimp*/bool Q3HttpResponseHeader::parseLine( const QString& line, int number ){ if ( number != 0 ) return Q3HttpHeader::parseLine( line, number ); QString l = line.simplifyWhiteSpace(); if ( l.length() < 10 ) return false; if ( l.left( 5 ) == QLatin1String("HTTP/") && l[5].isDigit() && l[6] == QLatin1Char('.') && l[7].isDigit() && l[8] == QLatin1Char(' ') && l[9].isDigit() ) { majVer = l[5].latin1() - '0'; minVer = l[7].latin1() - '0'; int pos = l.find( QLatin1Char(' '), 9 ); if ( pos != -1 ) { reasonPhr = l.mid( pos + 1 ); statCode = l.mid( 9, pos - 9 ).toInt(); } else { statCode = l.mid( 9 ).toInt(); reasonPhr.clear(); } } else { return false; } return true;}/*! \reimp*/QString Q3HttpResponseHeader::toString() const{ QString ret( QLatin1String("HTTP/%1.%2 %3 %4\r\n%5\r\n") ); return ret.arg( majVer ).arg ( minVer ).arg( statCode ).arg( reasonPhr ).arg( Q3HttpHeader::toString() );}/**************************************************** * * Q3HttpRequestHeader * ****************************************************//*! \class Q3HttpRequestHeader q3http.h \brief The Q3HttpRequestHeader class contains request header information for HTTP. \compat This class is used in the Q3Http class to report the header information if the client requests something from the server. HTTP requests have a method which describes the request's action. The most common requests are "GET" and "POST". In addition to the request method the header also includes a request-URI to specify the location for the method to use. The method, request-URI and protocol-version can be set using a constructor or later using setRequest(). The values can be obtained using method(), path(), majorVersion() and minorVersion(). This class is a Q3HttpHeader subclass so that class's functions, e.g. \link Q3HttpHeader::setValue() setValue()\endlink, \link Q3HttpHeader::value() value()\endlink, etc. are also available. \sa Q3HttpResponseHeader Q3Http*//*! Constructs an empty HTTP request header.*/Q3HttpRequestHeader::Q3HttpRequestHeader() : Q3HttpHeader(){ setValid( false );}/*! Constructs a HTTP request header for the method \a method, the request-URI \a path and the protocol-version \a majorVer and \a minorVer.*/Q3HttpRequestHeader::Q3HttpRequestHeader( const QString& method, const QString& path, int majorVer, int minorVer ) : Q3HttpHeader(), m( method ), p( path ), majVer( majorVer ), minVer( minorVer ){}/*! Constructs a copy of \a header.*/Q3HttpRequestHeader::Q3HttpRequestHeader( const Q3HttpRequestHeader& header ) : Q3HttpHeader( header ), m( header.m ), p( header.p ), majVer( header.majVer ), minVer( header.minVer ){}/*! Constructs a HTTP request header from the string \a str. The \a str should consist of one or more "\r\n" delimited lines; the first line should be the request-line (format: method, space, request-URI, space HTTP-version); each of the remaining lines should have the format key, colon, space, value.*/Q3HttpRequestHeader::Q3HttpRequestHeader( const QString& str ) : Q3HttpHeader(){ parse( str );}/*! This function sets the request method to \a method, the request-URI to \a path and the protocol-version to \a majorVer and \a minorVer. \sa method() path() majorVersion() minorVersion()*/void Q3HttpRequestHeader::setRequest( const QString& method, const QString& path, int majorVer, int minorVer ){ setValid( true ); m = method; p = path; majVer = majorVer; minVer = minorVer;}/*! Returns the method of the HTTP request header. \sa path() majorVersion() minorVersion() setRequest()*/QString Q3HttpRequestHeader::method() const{ return m;}/*! Returns the request-URI of the HTTP request header. \sa method() majorVersion() minorVersion() setRequest()*/QString Q3HttpRequestHeader::path() const{ return p;}/*! Returns the major protocol-version of the HTTP request header. \sa minorVersion() method() path() setRequest()*/int Q3HttpRequestHeader::majorVersion() const{ return majVer;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -