ConcreteProtocol.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 "ConcreteProtocol.h"
00025 #include "IdentityContentLengthBodyReader.h"
00026 #include "IdentityContentLengthBodyWriter.h"
00027 #include "IdentityReadAllBodyReader.h"
00028 #include "RequestHeader.h"
00029 #include "ResponseHeader.h"
00030 #include "Imports.cpp"
00031 
00033 ConcreteProtocol::ConcreteProtocol()
00034 {
00035 }
00036 
00038 ConcreteProtocol::~ConcreteProtocol()
00039 {
00040 }
00041 
00043 
00052 Header ConcreteProtocol::createHeader (const QByteArray &rawBytes)
00053 {
00054     RequestHeader requestHeader;
00055     if (requestHeader.parse (rawBytes))
00056         return requestHeader;
00057     ResponseHeader response;
00058     if (response.parse (rawBytes))
00059         return response;
00060     return Header();
00061 }
00062 
00064 
00075 BodyReader * ConcreteProtocol::createBodyReader (const Header &header)
00076 {
00077     auto_ptr <BodyReader> bodyReader = doCreateBodyReader (*header);
00078     return addBodyReader (bodyReader);
00079 }
00080 
00082 ConcreteProtocol::ReaderPointer
00083 ConcreteProtocol::doCreateBodyReader (const HeaderBase &header)
00084 {
00085     const QByteArray encoding = header.fieldValue ("Transfer-Encoding");
00086     const QByteArray length = header.fieldValue ("Content-Length");
00087 
00088     if (encoding.isEmpty() || encoding == "identity") {
00089         int intLength = length.toInt();
00090         if (length.isEmpty() || intLength < 0)
00091             return ReaderPointer (new IdentityReadAllBodyReader);
00092         else
00093             return ReaderPointer
00094                 (new IdentityContentLengthBodyReader (intLength));
00095     }
00096     Q_ASSERT (false);
00097     return ReaderPointer();
00098 }
00099 
00101 
00112 BodyWriter * ConcreteProtocol::createBodyWriter (const Header &header)
00113 {
00114     auto_ptr <BodyWriter> bodyWriter = doCreateBodyWriter (*header);
00115     return addBodyWriter (bodyWriter);
00116 }
00117 
00119 ConcreteProtocol::WriterPointer
00120 ConcreteProtocol::doCreateBodyWriter (const HeaderBase &header)
00121 {
00122     const QByteArray encoding = header.fieldValue ("Transfer-Encoding");
00123     const QByteArray length = header.fieldValue ("Content-Length");
00124 
00125     if (encoding.isEmpty() || encoding == "identity") {
00126         int intLength = length.toInt();
00127         if (length.isEmpty() || intLength < 0) {
00128             Q_ASSERT (false); // we need a IdentityWriteAllWriter
00129             return WriterPointer();
00130         } else
00131             return WriterPointer
00132                 (new IdentityContentLengthBodyWriter (intLength));
00133     }
00134     Q_ASSERT (false);
00135     return WriterPointer();
00136 }
00137 
00139 
00143 void ConcreteProtocol::destroyBodyReader (BodyReader *reader)
00144 {
00145     Q_ASSERT (bodyReaders.contains (reader));
00146     bodyReaders.remove (reader);
00147     delete reader;
00148 }
00149 
00151 
00155 void ConcreteProtocol::destroyBodyWriter (BodyWriter *writer)
00156 {
00157     Q_ASSERT (bodyWriters.contains (writer));
00158     bodyWriters.remove (writer);
00159     delete writer;
00160 }
00161 
00163 BodyReader * ConcreteProtocol::addBodyReader (ReaderPointer reader)
00164 {
00165     Q_ASSERT (reader.get() != 0);
00166     Q_ASSERT (!bodyReaders.contains (reader.get()));
00167     bodyReaders.insert (reader.get());
00168     return reader.release();
00169 }
00170 
00172 BodyWriter * ConcreteProtocol::addBodyWriter (WriterPointer writer)
00173 {
00174     Q_ASSERT (writer.get() != 0);
00175     Q_ASSERT (!bodyWriters.contains (writer.get()));
00176     bodyWriters.insert (writer.get());
00177     return writer.release();
00178 }