ResponseHeader.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 "ResponseHeader.h"
00025 #include "Imports.cpp"
00026 
00027 using Gnutella::Handshaking::ResponseHeader;
00028 
00030 
00037 ResponseHeader::ResponseHeader (const QString &str)
00038  : majorVersion_(), minorVersion_(), statusCode_ (0), reasonPhrase_()
00039 {
00040     parse (str);
00041 }
00042 
00044 
00052 ResponseHeader::ResponseHeader (int code, const QString &phrase, int majorVersion, int minorVersion)
00053  :  majorVersion_ (majorVersion), minorVersion_ (minorVersion),
00054     statusCode_ (code), reasonPhrase_ (phrase)
00055 {
00056     setValid (true);
00057 }
00058 
00060 ResponseHeader::ResponseHeader (const ResponseHeader &header)
00061  :  Header (header),
00062     majorVersion_ (header.majorVersion_), minorVersion_ (header.minorVersion_),
00063     statusCode_ (header.statusCode_), reasonPhrase_ (header.reasonPhrase_)
00064 {
00065 }
00066 
00068 ResponseHeader& ResponseHeader::operator= (const ResponseHeader &header)
00069 {
00070     Header::operator =(header);
00071     statusCode_   = header.statusCode_;
00072     reasonPhrase_ = header.reasonPhrase_;
00073     majorVersion_ = header.majorVersion_;
00074     minorVersion_ = header.minorVersion_;
00075     return *this;
00076 }
00077 
00079 
00087 bool ResponseHeader::parseLine (const QString &line, int number)
00088 {
00089     if (number != 0)
00090         return Header::parseLine(line, number);
00091 
00092     QStringList lst = line.simplified().split(" ");
00093     if (lst.count() >= 1) {
00094         QString v = lst[0];
00095         if (v.length() >= 12 && v.left(9) == "GNUTELLA/" &&
00096             v[9].isDigit() && v[10] == '.' && v[11].isDigit()) {
00097                 majorVersion_ = v[9].toLatin1() - '0';
00098                 minorVersion_ = v[11].toLatin1() - '0';
00099                 if (lst.count() >= 2)
00100                     statusCode_ = lst[1].toInt();
00101                 if (lst.count() >= 3) {
00102                     lst.pop_front();
00103                     lst.pop_front();
00104                     reasonPhrase_ = lst.join (" ");
00105                 }
00106                 return true;
00107         }
00108     }
00109 
00110     return false;
00111 }
00112 
00114 
00117 QString ResponseHeader::toString() const
00118 {
00119     QString format("GNUTELLA/%1.%2 %3 %4\r\n%5\r\n");
00120     return format.arg(majorVersion_)
00121                  .arg(minorVersion_)
00122                  .arg(statusCode_)
00123                  .arg(reasonPhrase_)
00124                  .arg(Header::toString());
00125 }