fastcgi++
request.hpp
Go to the documentation of this file.
00001 
00002 /***************************************************************************
00003 * Copyright (C) 2007 Eddie Carle [eddie@erctech.org]                       *
00004 *                                                                          *
00005 * This file is part of fastcgi++.                                          *
00006 *                                                                          *
00007 * fastcgi++ is free software: you can redistribute it and/or modify it     *
00008 * under the terms of the GNU Lesser General Public License as  published   *
00009 * by the Free Software Foundation, either version 3 of the License, or (at *
00010 * your option) any later version.                                          *
00011 *                                                                          *
00012 * fastcgi++ is distributed in the hope that it will be useful, but WITHOUT *
00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    *
00014 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public     *
00015 * License for more details.                                                *
00016 *                                                                          *
00017 * You should have received a copy of the GNU Lesser General Public License *
00018 * along with fastcgi++.  If not, see <http://www.gnu.org/licenses/>.       *
00019 ****************************************************************************/
00020 
00021 
00022 #ifndef REQUEST_HPP
00023 #define REQUEST_HPP
00024 
00025 #include <queue>
00026 #include <map>
00027 #include <string>
00028 #include <locale>
00029 
00030 #include <boost/shared_array.hpp>
00031 #include <boost/thread.hpp>
00032 #include <boost/function.hpp>
00033 
00034 #include <fastcgi++/transceiver.hpp>
00035 #include <fastcgi++/protocol.hpp>
00036 #include <fastcgi++/exceptions.hpp>
00037 #include <fastcgi++/fcgistream.hpp>
00038 #include <fastcgi++/http.hpp>
00039 
00041 namespace Fastcgipp
00042 {
00043    template<class T> class Manager;
00044 
00046 
00061    template<class charT> class Request
00062    {
00063    public:
00065 
00070       Request(const size_t maxPostSize=0): m_maxPostSize(maxPostSize), state(Protocol::PARAMS)  { setloc(std::locale::classic()); out.exceptions(std::ios_base::badbit | std::ios_base::failbit | std::ios_base::eofbit); m_environment.clearPostBuffer(); }
00071 
00073       const Http::Environment<charT>& environment() const { return m_environment; }
00074 
00075       // To dump data into the stream without it being code converted and bypassing the stream buffer call Fcgistream::dump(char* data, size_t size)
00076       // or Fcgistream::dump(std::basic_istream<char>& stream)
00077       
00079 
00082       Fcgistream<charT> out;
00083 
00085 
00088       Fcgistream<charT> err;
00089 
00091 
00098       virtual void errorHandler(const std::exception& error);
00099 
00101       virtual void bigPostErrorHandler();
00102 
00104       Protocol::Role role() const { return m_role; }
00105 
00107 
00118       const boost::function<void(Message)>& callback() const { return m_callback; }
00119 
00121 
00130       void setloc(std::locale loc_){ out.imbue(loc_); err.imbue(loc_); }
00131 
00133 
00136       const std::locale& getloc(){ return loc; }
00137 
00138    protected:
00140 
00148       virtual bool response() =0;
00149 
00151 
00161       virtual void inHandler(int bytesReceived) { };
00162 
00164 
00170       const Message& message() const { return m_message; }
00171 
00172    private:
00173       template<class T> friend class Manager;
00174 
00176       std::locale loc;
00177 
00179 
00185       Message m_message;
00186 
00188 
00199       boost::function<void(Message)> m_callback;
00200 
00202       Http::Environment<charT> m_environment;
00203 
00205 
00209       class Messages: public std::queue<Message>, public boost::mutex {};
00211       Messages messages;
00212 
00214       const size_t m_maxPostSize;
00215 
00217 
00224       bool handler();
00226       Transceiver* transceiver;
00228       Protocol::Role m_role;
00230       Protocol::FullId id;
00232       bool killCon;
00234       Protocol::RecordType state;
00236       void complete();
00238 
00247       void set(Protocol::FullId id_, Transceiver& transceiver_, Protocol::Role role_, bool killCon_, boost::function<void(Message)> callback_)
00248       {
00249          killCon=killCon_;
00250          id=id_;
00251          transceiver=&transceiver_;
00252          m_role=role_;
00253          m_callback=callback_;
00254 
00255          err.set(id_, transceiver_, Protocol::ERR);
00256          out.set(id_, transceiver_, Protocol::OUT);
00257       }
00258    };
00259 
00261    namespace Exceptions
00262    {
00266       struct RecordsOutOfOrder: public std::exception
00267       {
00268          const char* what() const throw() { return "FastCGI records received out of order from server."; }
00269       };
00270 
00274       struct UnknownContentType: public std::exception
00275       {
00276          const char* what() const throw() { return "Client sent unknown content type."; }
00277       };
00278    }
00279 }
00280 
00281 #endif