| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GUARD_HTTP_CONNECTION_HH |
|---|
| 21 | #define GUARD_HTTP_CONNECTION_HH |
|---|
| 22 | |
|---|
| 23 | #include "../tls/session.hh" |
|---|
| 24 | #include "../server.hh" |
|---|
| 25 | #include "response.hh" |
|---|
| 26 | |
|---|
| 27 | #include <boost/asio.hpp> |
|---|
| 28 | #include <boost/shared_ptr.hpp> |
|---|
| 29 | |
|---|
| 30 | namespace http |
|---|
| 31 | { |
|---|
| 32 | class connection : public server::connection |
|---|
| 33 | , public boost::enable_shared_from_this<connection> |
|---|
| 34 | { |
|---|
| 35 | public: |
|---|
| 36 | typedef boost::shared_ptr<connection> ptr; |
|---|
| 37 | class factory : public server::connection_factory |
|---|
| 38 | { |
|---|
| 39 | bool https; |
|---|
| 40 | public: |
|---|
| 41 | factory(bool https) : https(https) {} |
|---|
| 42 | boost::shared_ptr<server::connection> |
|---|
| 43 | operator()(std::string port_name, |
|---|
| 44 | boost::asio::ip::tcp::acceptor &acc, |
|---|
| 45 | tls::init init, |
|---|
| 46 | server::conn_cb srv_cb) { |
|---|
| 47 | boost::shared_ptr<server::connection> rv |
|---|
| 48 | (new connection(port_name, https, |
|---|
| 49 | acc, init, srv_cb)); |
|---|
| 50 | return rv; |
|---|
| 51 | } |
|---|
| 52 | }; |
|---|
| 53 | std::string get_port_name() const { return port_name; } |
|---|
| 54 | boost::shared_ptr<server::connection_factory> |
|---|
| 55 | get_factory() const { |
|---|
| 56 | boost::shared_ptr<server::connection_factory> rv |
|---|
| 57 | (new factory(https)); |
|---|
| 58 | return rv; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | connection(std::string port_name, |
|---|
| 62 | bool https, |
|---|
| 63 | boost::asio::ip::tcp::acceptor &acc, |
|---|
| 64 | tls::init, |
|---|
| 65 | server::conn_cb srv_cb); |
|---|
| 66 | |
|---|
| 67 | ~connection(); |
|---|
| 68 | |
|---|
| 69 | server::conn_cb get_srv_cb() { return srv_cb; } |
|---|
| 70 | protected: |
|---|
| 71 | server::conn_cb get_conn_cb() { return srv_cb; } |
|---|
| 72 | private: |
|---|
| 73 | const std::string port_name; |
|---|
| 74 | const bool https; |
|---|
| 75 | |
|---|
| 76 | server::conn_cb srv_cb; |
|---|
| 77 | boost::asio::io_service &ios; |
|---|
| 78 | boost::asio::ip::tcp::endpoint ep; |
|---|
| 79 | boost::asio::ip::tcp::socket peer; |
|---|
| 80 | tls::session<boost::asio::ip::tcp::socket&> speer; |
|---|
| 81 | |
|---|
| 82 | std::string loghead; |
|---|
| 83 | |
|---|
| 84 | boost::posix_time::ptime startup_timestamp; |
|---|
| 85 | boost::posix_time::ptime request_timestamp; |
|---|
| 86 | size_t num_requests; |
|---|
| 87 | bool idle; |
|---|
| 88 | |
|---|
| 89 | boost::asio::streambuf readbuf; |
|---|
| 90 | |
|---|
| 91 | void tick(bool); |
|---|
| 92 | |
|---|
| 93 | void accept(boost::system::error_code ec); |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | static void kill(ptr); |
|---|
| 99 | static void abort(ptr, boost::system::error_code ec); |
|---|
| 100 | static void get_request(ptr, boost::system::error_code ec); |
|---|
| 101 | static void got_request(ptr, |
|---|
| 102 | boost::system::error_code ec, size_t); |
|---|
| 103 | static void reqcont(ptr self, |
|---|
| 104 | boost::shared_ptr<request> req, |
|---|
| 105 | boost::shared_ptr<response> resp, |
|---|
| 106 | response::factory respfac, |
|---|
| 107 | size_t bodylen, |
|---|
| 108 | boost::system::error_code ec, |
|---|
| 109 | std::size_t); |
|---|
| 110 | static void sent_response(ptr, |
|---|
| 111 | boost::shared_ptr<response>, |
|---|
| 112 | boost::system::error_code ec, size_t); |
|---|
| 113 | }; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | #endif |
|---|