RequestHeader.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "RequestHeader.h"
00025 
00026 using namespace Http;
00027 
00028 namespace Http {
00029 
00030 class RequestHeaderPrivate
00031 {
00032 public:
00033     QString     method;
00034     QString     path;
00035     int         majorVersion;
00036     int         minorVersion;
00037 
00038     RequestHeaderPrivate() : method(), path(), majorVersion(), minorVersion() {}
00039 };
00040 
00041 } // namespace Http;
00042 
00046 RequestHeader::RequestHeader()
00047     : d (new RequestHeaderPrivate)
00048 {
00049     setValid(false);
00050 }
00051 
00056 RequestHeader::RequestHeader (const QString &method,
00057                                       const QString &path,
00058                                       int majorVersion,
00059                                       int minorVersion)
00060     : d (new RequestHeaderPrivate)
00061 {
00062     setRequestLine (method, path, minorVersion, majorVersion);
00063 }
00064 
00068 RequestHeader::RequestHeader (const RequestHeader &header)
00069     : Header (header), d (new RequestHeaderPrivate)
00070 {
00071     d->method       = header.d->method;
00072     d->path         = header.d->path;
00073     d->majorVersion = header.d->majorVersion;
00074     d->minorVersion = header.d->minorVersion;
00075 }
00076 
00078 RequestHeader::~RequestHeader()
00079 {
00080 }
00081 
00085 RequestHeader &
00086 RequestHeader::operator= (const RequestHeader &header)
00087 {
00088     Header::operator= (header);
00089     d->method       = header.d->method;
00090     d->path         = header.d->path;
00091     d->majorVersion = header.d->majorVersion;
00092     d->minorVersion = header.d->minorVersion;
00093     return *this;
00094 }
00095 
00103 RequestHeader::RequestHeader(const QString &str)
00104     : d (new RequestHeaderPrivate)
00105 {
00106     parse(str);
00107 }
00108 
00116 void
00117 RequestHeader::setRequestLine (const QString &method,
00118                                    const QString &path,
00119                                    int majorVersion,
00120                                    int minorVersion)
00121 {
00122     setValid(true);
00123     d->method       = method;
00124     d->path         = path;
00125     d->majorVersion = majorVersion;
00126     d->minorVersion = minorVersion;
00127 }
00128 
00134 QString
00135 RequestHeader::method() const
00136 {
00137     return d->method;
00138 }
00139 
00145 QString
00146 RequestHeader::path() const
00147 {
00148     return d->path;
00149 }
00150 
00156 int
00157 RequestHeader::majorVersion() const
00158 {
00159     return d->majorVersion;
00160 }
00161 
00167 int
00168 RequestHeader::minorVersion() const
00169 {
00170     return d->minorVersion;
00171 }
00172 
00175 bool
00176 RequestHeader::parseLine (const QString &line,
00177                               int number)
00178 {
00179     if (number != 0)
00180         return Header::parseLine(line, number);
00181 
00182     QStringList lst = line.simplified().split(" ");
00183     if (lst.count() > 0) {
00184         d->method = lst[0];
00185         if (lst.count() > 1) {
00186             d->path = lst[1];
00187             if (lst.count() > 2) {
00188                 QString v = lst[2];
00189                 if (v.length() >= 8 && v.left(5) == "HTTP/" &&
00190                         v[5].isDigit() && v[6] == '.' && v[7].isDigit()) {
00191                     d->majorVersion = v[5].toLatin1() - '0';
00192                     d->minorVersion = v[7].toLatin1() - '0';
00193                     return true;
00194                 }
00195             }
00196         }
00197     }
00198 
00199     return false;
00200 }
00201 
00204 QString
00205 RequestHeader::toString() const
00206 {
00207     QString format("%1 %2 HTTP/%3.%4\r\n%5\r\n");
00208     return format.arg (d->method)
00209                  .arg (d->path)
00210                  .arg (d->majorVersion)
00211                  .arg (d->minorVersion)
00212                  .arg(Header::toString());
00213 }