SingleHostClientHttpSession.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 "SingleHostClientHttpSession.h"
00025 #include "ClientHttpSessionStatus.h"
00026 #include "BodyEnd.h"
00027 #include "RequestHeader.h"
00028 #include "ResponseHeader.h"
00029 #include "Imports.cpp"
00030 
00031 SingleHostClientHttpSession::SingleHostClientHttpSession (
00032                                     ClientHttpSessionStatus *status,
00033                                     Session *session)
00034  :  status_ (status), session_ (session),
00035     receivedDataDispatcher_(), outFile_ (0)
00036 {
00037     receivedDataDispatcher_.registerTypeHandler (
00038         Callable <void (const ResponseHeader &)>
00039             (this, &SingleHostClientHttpSession::received));
00040 
00041     receivedDataDispatcher_.registerTypeHandler (
00042         Callable <void (const RawData &)>
00043             (this, &SingleHostClientHttpSession::received));
00044 
00045     receivedDataDispatcher_.registerTypeHandler (
00046         Callable <void (const BodyEnd &)>
00047             (this, &SingleHostClientHttpSession::received));
00048 
00049     receivedDataDispatcher_.registerNotHandledHandler(
00050         Callable <void (const DataBase &)>
00051             (this, &SingleHostClientHttpSession::received));
00052 }
00053 
00054 SingleHostClientHttpSession::~SingleHostClientHttpSession()
00055 {
00056 }
00057 
00059 
00062 void SingleHostClientHttpSession::open()
00063 {
00064     session_->open();
00065 }
00066 
00067 void SingleHostClientHttpSession::close()
00068 {
00069     session_->close();
00070 }
00071 
00072 void SingleHostClientHttpSession::abort()
00073 {
00074     session_->abort();
00075 }
00076 
00077 void SingleHostClientHttpSession::get (const Uri &uri, File *outFile)
00078 {
00079     Q_ASSERT (outFile != 0);
00080     outFile_ = outFile;
00081     RequestHeader requestHeader = createRequestHeader ("GET", uri);
00082     requestHeader.setFieldValue ("Content-Length", "0");
00083     session_->send (requestHeader);
00084 }
00085 
00086 RequestHeader
00087 SingleHostClientHttpSession::createRequestHeader (const QByteArray &method,
00088                                                   const Uri &uri)
00089 {
00090     RequestHeader request (method, requestUri (uri), 1, 1);
00091     request.setFieldValue ("Host", host (uri));
00092     return request;
00093 }
00094 
00095 QByteArray SingleHostClientHttpSession::requestUri (const Uri &uri)
00096 {
00097     if (uri.path().isEmpty())
00098         return "/" + uri.encoded (Uri::Query);
00099     else
00100         return uri.encoded (Uri::Path | Uri::Query);
00101 }
00102 
00103 QByteArray SingleHostClientHttpSession::host (const Uri &uri)
00104 {
00105     QByteArray res = uri.encoded (Uri::Host | Uri::Port);
00106     return res.mid (2); // Drop the leading "//" in "//host.tld[:port]"
00107 }
00108 
00109 void SingleHostClientHttpSession::sessionReceivedData (Session *,
00110                                                        const Data &data)
00111 {
00112     receivedDataDispatcher_.dispatch (*data);
00113 }
00114 
00115 void SingleHostClientHttpSession::received (const ResponseHeader &header)
00116 {
00117     status_->clientHttpSessionResponseStarted (header);
00118 }
00119 
00120 void SingleHostClientHttpSession::received (const RawData &chunk)
00121 {
00122     Q_ASSERT (outFile_ != 0);
00123     outFile_->write (chunk.toRawBytes());
00124 }
00125 
00126 void SingleHostClientHttpSession::received (const BodyEnd &bodyEnd)
00127 {
00128     status_->clientHttpSessionResponseFinished (bodyEnd);
00129 }
00130 
00131 void SingleHostClientHttpSession::received (const DataBase &)
00132 {
00133     Q_ASSERT (false); // Unknown/unhandled Data type received!
00134 }
00135 
00136 void SingleHostClientHttpSession::sessionSendingData (Session *, const Data &)
00137 {
00138     // Nothing to do here.
00139 }
00140 
00141 void SingleHostClientHttpSession::sessionClosing (Session *)
00142 {
00143     // \todo states??
00144     status_->clientHttpSessionClosing();
00145 }
00146 
00147 void SingleHostClientHttpSession::sessionClosed (Session *)
00148 {
00149     // \todo states??
00150     status_->clientHttpSessionClosed();
00151 }