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 "HeaderReader.h" 00025 #include "Header.h" 00026 #include "Imports.cpp" 00027 00028 using namespace Http; 00029 00030 namespace Http { 00031 00032 enum Constants 00033 { 00034 MaximalHeaderLength = 4096 00035 }; 00036 00037 } // namespace Http; 00038 00039 HeaderReader::HeaderReader (Connection *connection) 00040 : p() 00041 { 00042 Q_ASSERT (connection != 0); 00043 00044 p.connection = connection; 00045 p.header = 0; 00046 p.bytesRead = 0; 00047 p.done = true; 00048 00049 connect (p.connection, SIGNAL (readyRead()), this, SLOT (readyRead())); 00050 } 00051 00052 HeaderReader::~HeaderReader() 00053 { 00054 if (p.header != 0) 00055 delete p.header; 00056 } 00057 00058 void HeaderReader::startReading (Header *header) 00059 { 00060 Q_ASSERT (header != 0); // We need an object of the rigth type! 00061 Q_ASSERT (p.done == true); 00062 00063 p.handshakeBytes.resize (0); 00064 p.handshakeBytes.reserve (MaximalHeaderLength); 00065 p.handshakeBytes.append (char (0)); 00066 p.bytesRead = 0; 00067 p.done = false; 00068 if (p.header) 00069 delete p.header; 00070 p.header = header; 00071 QMetaObject::invokeMethod (this, "readyRead", Qt::QueuedConnection); 00072 } 00073 00074 void HeaderReader::readyRead () 00075 { 00076 if (p.done) 00077 return; 00078 00079 bool ready = false; 00080 do { 00081 if (!p.connection->getChar (p.handshakeBytes.data() + p.bytesRead)) 00082 return; 00083 00084 p.handshakeBytes.append (char (0)); 00085 ++p.bytesRead; 00086 if (p.bytesRead == MaximalHeaderLength) { 00087 p.done = true; 00088 emit readError(); // would be Error::InvalidHeader 00089 return; 00090 } 00091 if (p.handshakeBytes.lastIndexOf ("\r\n\r\n") != -1) 00092 ready = true; 00093 if (p.handshakeBytes.lastIndexOf ("\n\n") != -1) 00094 ready = true; 00095 } while (!ready); 00096 00097 QString handshakeString = QString::fromLatin1 (p.handshakeBytes.data()); 00098 00099 if (p.header->parse (handshakeString)) { 00100 p.done = true; 00101 emit headerRead(); 00102 } else { 00103 delete p.header; 00104 p.header = 0; 00105 p.done = true; 00106 emit readError(); // would be Error::InvalidHeader 00107 } 00108 return; 00109 } 00110 00111 const Header * HeaderReader::header() const 00112 { 00113 Q_ASSERT (p.done == true); 00114 00115 return p.header; 00116 }