| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GUARD_HTTP_REQUEST_HH |
|---|
| 21 | #define GUARD_HTTP_REQUEST_HH |
|---|
| 22 | |
|---|
| 23 | #include "../server.hh" |
|---|
| 24 | #include "../uri.hh" |
|---|
| 25 | |
|---|
| 26 | #include <boost/noncopyable.hpp> |
|---|
| 27 | #include <map> |
|---|
| 28 | |
|---|
| 29 | namespace db { class user; } |
|---|
| 30 | |
|---|
| 31 | namespace http |
|---|
| 32 | { |
|---|
| 33 | class request : private boost::noncopyable |
|---|
| 34 | { |
|---|
| 35 | public: |
|---|
| 36 | typedef std::multimap<std::string,std::string>::const_iterator |
|---|
| 37 | form_data_iterator; |
|---|
| 38 | |
|---|
| 39 | request(std::string, server::conn_cb cb, |
|---|
| 40 | boost::asio::ip::address); |
|---|
| 41 | bool get_v11() const { return v11; } |
|---|
| 42 | bool get_close() const { return close; } |
|---|
| 43 | |
|---|
| 44 | void body_append(std::string b) { |
|---|
| 45 | form_data_ready = false; |
|---|
| 46 | body += b; |
|---|
| 47 | } |
|---|
| 48 | std::string get_body() const { return body; } |
|---|
| 49 | |
|---|
| 50 | bool has_body() const { |
|---|
| 51 | return header.find("transfer-encoding") != header.end() |
|---|
| 52 | || |
|---|
| 53 | header.find("content-length") != header.end(); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | ::uri get_uri() const { return uri; } |
|---|
| 57 | |
|---|
| 58 | std::string get_method() const { return method; } |
|---|
| 59 | std::string get_host() const { return host; } |
|---|
| 60 | int get_port() const { return port; } |
|---|
| 61 | std::string get_path() const { return uri.get_path(); } |
|---|
| 62 | |
|---|
| 63 | std::string get_form_field(std::string name) const; |
|---|
| 64 | bool has_form_field(std::string name) const; |
|---|
| 65 | std::pair<form_data_iterator,form_data_iterator> |
|---|
| 66 | get_form_fields(std::string name) const { |
|---|
| 67 | if (!form_data_ready) parse_form(); |
|---|
| 68 | return form_data.equal_range(name); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | std::string get_form_as_query() const; |
|---|
| 72 | |
|---|
| 73 | std::string get_header(std::string s) const; |
|---|
| 74 | |
|---|
| 75 | std::string logbit() const { |
|---|
| 76 | return get_method() + " " + get_path() + |
|---|
| 77 | " (" + get_header("referer") + ")" + |
|---|
| 78 | " [" + get_header("user-agent") + "]"; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | std::string get_sessid() const; |
|---|
| 82 | boost::shared_ptr<session> &get_session() const; |
|---|
| 83 | |
|---|
| 84 | bool is_authenticated() const; |
|---|
| 85 | boost::shared_ptr<db::user> get_user() const; |
|---|
| 86 | |
|---|
| 87 | boost::asio::ip::address get_peer() const { return peer; } |
|---|
| 88 | |
|---|
| 89 | private: |
|---|
| 90 | server::conn_cb cb; |
|---|
| 91 | boost::asio::ip::address peer; |
|---|
| 92 | std::string method; |
|---|
| 93 | std::map<std::string, std::string> header; |
|---|
| 94 | bool v11; |
|---|
| 95 | bool close; |
|---|
| 96 | ::uri uri; |
|---|
| 97 | std::string host; |
|---|
| 98 | int port; |
|---|
| 99 | std::string body; |
|---|
| 100 | mutable bool form_data_ready; |
|---|
| 101 | mutable std::multimap<std::string,std::string> form_data; |
|---|
| 102 | |
|---|
| 103 | void parse_header(std::string); |
|---|
| 104 | void parse_form() const; |
|---|
| 105 | }; |
|---|
| 106 | |
|---|
| 107 | inline |
|---|
| 108 | std::string request::get_form_field(std::string k) const |
|---|
| 109 | { |
|---|
| 110 | if (!form_data_ready) parse_form(); |
|---|
| 111 | std::multimap<std::string, std::string>::const_iterator it |
|---|
| 112 | = form_data.find(k); |
|---|
| 113 | if (it == form_data.end()) |
|---|
| 114 | { |
|---|
| 115 | return ""; |
|---|
| 116 | } |
|---|
| 117 | return it->second; |
|---|
| 118 | } |
|---|
| 119 | inline |
|---|
| 120 | bool request::has_form_field(std::string k) const |
|---|
| 121 | { |
|---|
| 122 | if (!form_data_ready) parse_form(); |
|---|
| 123 | std::multimap<std::string, std::string>::const_iterator it |
|---|
| 124 | = form_data.find(k); |
|---|
| 125 | return it != form_data.end(); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | #endif |
|---|