fastcgi++
|
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 00178 bool virtual inProcessor() { return false; } 00179 00181 00187 const Message& message() const { return m_message; } 00188 00189 private: 00190 template<class T> friend class Manager; 00191 00193 std::locale loc; 00194 00196 00202 Message m_message; 00203 00205 00216 boost::function<void(Message)> m_callback; 00217 00219 Http::Environment<charT> m_environment; 00220 00222 00226 class Messages: public std::queue<Message>, public boost::mutex {}; 00228 Messages messages; 00229 00231 const size_t m_maxPostSize; 00232 00234 00241 bool handler(); 00243 Transceiver* transceiver; 00245 Protocol::Role m_role; 00247 Protocol::FullId id; 00249 bool killCon; 00251 Protocol::RecordType state; 00253 void complete(); 00255 00264 void set(Protocol::FullId id_, Transceiver& transceiver_, Protocol::Role role_, bool killCon_, boost::function<void(Message)> callback_) 00265 { 00266 killCon=killCon_; 00267 id=id_; 00268 transceiver=&transceiver_; 00269 m_role=role_; 00270 m_callback=callback_; 00271 00272 err.set(id_, transceiver_, Protocol::ERR); 00273 out.set(id_, transceiver_, Protocol::OUT); 00274 } 00275 }; 00276 00278 namespace Exceptions 00279 { 00283 struct RecordsOutOfOrder: public std::exception 00284 { 00285 const char* what() const throw() { return "FastCGI records received out of order from server."; } 00286 }; 00287 00291 struct UnknownContentType: public std::exception 00292 { 00293 const char* what() const throw() { return "Client sent unknown content type."; } 00294 }; 00295 } 00296 } 00297 00298 #endif