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 #include "Imports.cpp"
00026 
00027 using Gnutella::Handshaking::RequestHeader;
00028 
00030 
00037 RequestHeader::RequestHeader (const QString &str)
00038  :  majorVersion_ (0), minorVersion_ (0)
00039 {
00040     parse (str);
00041 }
00042 
00044 
00052 RequestHeader::RequestHeader (int majorVersion, int minorVersion)
00053  :  majorVersion_ (majorVersion), minorVersion_ (minorVersion)
00054 {
00055     setValid (true);
00056 }
00057 
00059 RequestHeader::RequestHeader (const RequestHeader &header)
00060  :  Header (header),
00061     majorVersion_ (header.majorVersion_),
00062     minorVersion_ (header.minorVersion_)
00063 {
00064 }
00065 
00067 RequestHeader& RequestHeader::operator= (const RequestHeader &header)
00068 {
00069     Header::operator =(header);
00070     majorVersion_ = header.majorVersion_;
00071     minorVersion_ = header.minorVersion_;
00072     return *this;
00073 }
00074 
00076 
00084 bool RequestHeader::parseLine (const QString &line, int number)
00085 {
00086     if (number != 0)
00087         return Header::parseLine(line, number);
00088 
00089     QStringList lst = line.simplified().split(" ");
00090     if (lst.count() == 2 && lst[0] == "GNUTELLA") {
00091         QString v = lst[1];
00092         if (v.length() >= 11 && v.left(8) == "CONNECT/" &&
00093             v[8].isDigit() && v[9] == '.' && v[10].isDigit()) {
00094                 majorVersion_ = v[8].toLatin1() - '0';
00095                 minorVersion_ = v[10].toLatin1() - '0';
00096                 return true;
00097         }
00098     }
00099 
00100     return false;
00101 }
00102 
00104 
00107 QString RequestHeader::toString() const
00108 {
00109     QString res("GNUTELLA CONNECT/%1.%2\r\n%3\r\n");
00110     return res.arg(majorVersion_).arg(minorVersion_).arg(Header::toString());
00111 }