| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GUARD_HTTP_RESPONSE_HH |
|---|
| 21 | #define GUARD_HTTP_RESPONSE_HH |
|---|
| 22 | |
|---|
| 23 | #include "session.hh" |
|---|
| 24 | |
|---|
| 25 | #include <boost/asio.hpp> |
|---|
| 26 | #include <vector> |
|---|
| 27 | |
|---|
| 28 | namespace http |
|---|
| 29 | { |
|---|
| 30 | class connection; |
|---|
| 31 | class request; |
|---|
| 32 | class response : private boost::noncopyable |
|---|
| 33 | { |
|---|
| 34 | public: |
|---|
| 35 | class factory |
|---|
| 36 | { |
|---|
| 37 | boost::shared_ptr<connection> conn; |
|---|
| 38 | bool v11; |
|---|
| 39 | bool close; |
|---|
| 40 | bool no_body; |
|---|
| 41 | public: |
|---|
| 42 | factory(boost::shared_ptr<connection> conn) |
|---|
| 43 | : conn(conn), v11(true), close(true), |
|---|
| 44 | no_body(false) |
|---|
| 45 | {} |
|---|
| 46 | void set_v11(bool val) { v11 = val; } |
|---|
| 47 | void set_close(bool val) { close = val; } |
|---|
| 48 | void set_no_body(bool val) { no_body = val; } |
|---|
| 49 | |
|---|
| 50 | boost::shared_ptr<response> operator()(std::string s) |
|---|
| 51 | const { |
|---|
| 52 | boost::shared_ptr<response> rv |
|---|
| 53 | (new response(conn, v11, close, |
|---|
| 54 | no_body, s)); |
|---|
| 55 | return rv; |
|---|
| 56 | } |
|---|
| 57 | }; |
|---|
| 58 | typedef boost::asio::const_buffers_1 |
|---|
| 59 | const_buffer_sequence; |
|---|
| 60 | |
|---|
| 61 | std::ostream &body(std::string content_type); |
|---|
| 62 | std::string logbit() const { return logline; } |
|---|
| 63 | |
|---|
| 64 | void complete(); |
|---|
| 65 | |
|---|
| 66 | const_buffer_sequence get_buffer() const { |
|---|
| 67 | assert(completed); |
|---|
| 68 | return boost::asio::buffer(buffer.data(), |
|---|
| 69 | buffer.size()); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | std::string get_string() const { |
|---|
| 73 | assert(completed); |
|---|
| 74 | return std::string(buffer.begin(), buffer.end()); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | bool get_close() const { return close; } |
|---|
| 78 | void add_header(std::string name, std::string body) { |
|---|
| 79 | hdrs << name << ": " << body << "\r\n"; |
|---|
| 80 | } |
|---|
| 81 | void set_session(boost::shared_ptr<request> req, |
|---|
| 82 | boost::shared_ptr<session>); |
|---|
| 83 | private: |
|---|
| 84 | boost::shared_ptr<connection> conn; |
|---|
| 85 | std::ostringstream hdrs; |
|---|
| 86 | std::ostringstream bdy; |
|---|
| 87 | const bool close; |
|---|
| 88 | const bool no_body; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | std::vector<char> buffer; |
|---|
| 92 | bool completed; |
|---|
| 93 | |
|---|
| 94 | std::string logline; |
|---|
| 95 | |
|---|
| 96 | response(boost::shared_ptr<connection> conn, |
|---|
| 97 | bool v11, bool close, bool no_body, |
|---|
| 98 | std::string respline); |
|---|
| 99 | }; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | #endif |
|---|