ResponseHeader.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-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 "ResponseHeader.h"
00025 #include "Imports.cpp"
00026 
00028 auto_ptr <DataBase> ResponseHeader::copy() const
00029 {
00030     return auto_ptr <DataBase> (new ResponseHeader (*this));
00031 }
00032 
00034 
00040 ResponseHeader::ResponseHeader (int statusCode,
00041                                 const QByteArray &reasonPhrase,
00042                                 int majorVersion,
00043                                 int minorVersion)
00044  :  HeaderBase (new Data)
00045 {
00046     Q_SD (Data);
00047     d->statusCode = statusCode;
00048     d->reasonPhrase = reasonPhrase;
00049     d->majorVersion = majorVersion;
00050     d->minorVersion = minorVersion;
00051 }
00052 
00054 bool ResponseHeader::parseStartLine (const QByteArray &startLine)
00055 {
00056     QRegExp startLineRegExp ("HTTP/(\\d+).(\\d+)\\s*(\\d{3})(.*)",
00057                              Qt::CaseSensitive, QRegExp::RegExp2);
00058     if (!startLineRegExp.exactMatch (startLine)
00059         || startLineRegExp.cap (4).trimmed().isEmpty()) {
00060         return false;
00061     } else {
00062         Q_SD (Data);
00063         d->majorVersion = startLineRegExp.cap (1).toInt();
00064         d->minorVersion = startLineRegExp.cap (2).toInt();
00065         d->statusCode = startLineRegExp.cap (3).toInt();
00066         d->reasonPhrase = startLineRegExp.cap (4).toLatin1().trimmed();
00067         return true;
00068     }
00069 }
00070 
00072 QByteArray ResponseHeader::rawStartLine() const
00073 {
00074     Q_SD (const Data);
00075     QByteArray res = "HTTP/";
00076     res += QByteArray().setNum (d->majorVersion);
00077     res += ".";
00078     res += QByteArray().setNum (d->minorVersion);
00079     res += " ";
00080     res += QByteArray().setNum (d->statusCode);
00081     res += " ";
00082     res += d->reasonPhrase;
00083     res += "\r\n";
00084     return res;
00085 }