Bytemaster's Boost Libraries
/Users/dlarimer/dev/libs/rpc/include/boost/rpc/http/http.hpp
00001 #ifndef _BOOST_RPC_HTTP_HTTP_HPP
00002 #define _BOOST_RPC_HTTP_HTTP_HPP
00003 
00004 // download a file... 
00005 http::request req;
00006 req << http::url( "path" ) << http::header( "Content-Type", "json" )
00007     << "Hello World";
00008     
00009 // execute the request and return once headers have been parsed.
00010 // the body may be quite large, so we will simply return the socket and
00011 // allow the requester to read the body 'at will'
00012 http::response r = boost::rpc::http::exec( http::request( "url" ) ); 
00013 r.code(); r.reason(); 
00014 std::ofstream of.open( r.path(), "wb" );
00015 while( size_t s = r.read_some( buf, size ) ) {
00016     of.write( buf, s );
00017 }
00018 r.body(); // reads the whole body and returns it as a string 
00019 
00020 
00021 namespace boost { namespace rpc { namespace http {
00022 
00023  struct get {
00024     boost::filesystem::path path;
00025  };
00026  struct version {
00027     version( const std::string& v = "HTTP/1.1" ):ver(v){}
00028     std::string ver;
00029  };
00030  struct body {
00031     body( const std::string& str = "" );
00032     body( const boost::shared_ptr<std::iostream>& s );
00033     :m_body(s){};
00034     boost::shared_ptr<std::iostream> m_body;
00035  };
00036 
00037  struct header {
00038    header( const std::string& _name="", const std::string& _value="")
00039    :name(_name),value(_value){}
00040 
00041    std::string name;
00042    std::string value;
00043  };
00044 
00045  header get( const boost::filesystem::path& p ) 
00046 
00047 
00048  class request {
00049    public:
00050      request();
00051      const std::vector<header>& headers()const;
00052      friend request& operator<<( request& req, const header& h ) {
00053          req.m_headers.push_back(h);
00054          return req;
00055      }
00056 
00057      template<typename T>
00058      friend request& operator<<( request& req, const T& content ) {
00059          if( !m_body ) 
00060             m_body = boost::shared_ptr<std::iostream>(new std::stringstream());
00061          *m_body << content;
00062          return req;
00063      }
00064    private:
00065      std::string                      m_version;
00066      std::vector<header>              m_headers; 
00067      boost::shared_ptr<std::iostream> m_body;
00068  };
00069 
00070  class response {
00071     public:
00072         std::string         reason()const;
00073         int                 code()const;
00074         std::vector<header> headers()const;
00075 
00076         size_t      read_some( char* buf, size_t buf_size );   
00077         size_t      read( char* buf, size_t buf_size );   
00078         std::string body();
00079     private:
00080         response( boost::cmt::asio::socket::ptr& s );
00081         friend response exec( const request& );
00082 
00083         std::string                    m_http_version;
00084         int                            m_status_code;
00085         std::string                    m_reason_phrase;
00086         std::vector<header>            m_headers; 
00087         boost::cmt::asio::socket::ptr  m_sock;
00088  };
00089     
00090 
00091  class connection {
00092    public:
00093      connection();
00094      ~connection();
00095 
00096     
00097    private:
00098     class connection_private* my;
00099  };
00100 } } }
00101 #endif // _BOOST_RPC_HTTP_HTTP_HPP
 All Classes Namespaces Files Functions Variables Typedefs Defines