fastcgi++
fcgistream.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 FCGISTREAM_HPP
00023 #define FCGISTREAM_HPP
00024 
00025 #include <iosfwd>
00026 #include <boost/iostreams/filtering_stream.hpp>
00027 #include <boost/iostreams/categories.hpp>
00028 #include <boost/iostreams/concepts.hpp>
00029 
00030 #include <fastcgi++/protocol.hpp>
00031 #include <fastcgi++/transceiver.hpp>
00032 
00034 namespace Fastcgipp
00035 {
00037 
00169    enum OutputEncoding {NONE, HTML, URL};
00170 
00172    class FcgistreamSink: public boost::iostreams::device<boost::iostreams::output, char>
00173    {
00174    private:
00175       Protocol::FullId m_id;
00176       Protocol::RecordType m_type;
00177       Transceiver* m_transceiver;
00178    public:
00179       std::streamsize write(const char* s, std::streamsize n);
00180 
00181       void set(Protocol::FullId id, Transceiver &transceiver, Protocol::RecordType type) {m_id=id, m_type=type, m_transceiver=&transceiver;}
00182       void dump(const char* data, size_t size) { write(data, size); }
00183       void dump(std::basic_istream<char>& stream);
00184    };
00185 
00187 
00197    template <typename charT> class Fcgistream: public boost::iostreams::filtering_stream<boost::iostreams::output, charT>
00198    {
00199    private:
00200       struct Encoder: public boost::iostreams::multichar_filter<boost::iostreams::output, charT>
00201       {
00202          template<typename Sink> std::streamsize write(Sink& dest, const charT* s, std::streamsize n);
00203 
00204          OutputEncoding m_state;
00205       };
00206       
00207       Encoder& m_encoder;
00208 
00209       FcgistreamSink& m_sink;
00210 
00211    public:
00212       Fcgistream();
00214       void set(Protocol::FullId id, Transceiver& transceiver, Protocol::RecordType type) { m_sink.set(id, transceiver, type); }
00215 
00217       void flush() { boost::iostreams::filtering_stream<boost::iostreams::output, charT>::strict_sync(); }
00218       
00220 
00228       void dump(const char* data, size_t size) { flush(); m_sink.dump(data, size); }
00230 
00237       void dump(std::basic_istream<char>& stream) { flush(); m_sink.dump(stream); }
00238 
00240 
00246       void setEncoding(OutputEncoding x) { m_encoder.m_state=x; }
00247    };
00248 
00250 
00258    struct encoding
00259    {
00260       const OutputEncoding m_type;
00261       encoding(OutputEncoding type): m_type(type) {}
00262    };
00263 
00264    template<class charT, class Traits> std::basic_ostream<charT, Traits>& operator<<(std::basic_ostream<charT, Traits>& os, const encoding& enc);
00265 }
00266 
00267 #endif