Line data Source code
1 : #ifndef THINGER_HTTP_HEADERS_HPP
2 : #define THINGER_HTTP_HEADERS_HPP
3 :
4 : #include <algorithm>
5 : #include <boost/logic/tribool.hpp>
6 : #include <boost/algorithm/string.hpp>
7 : #include <boost/lexical_cast.hpp>
8 : #include "http_frame.hpp"
9 :
10 : namespace thinger::http{
11 :
12 : namespace misc_strings {
13 : static const std::string name_value_separator{": "};
14 : static const std::string crlf = "\r\n";
15 : static const std::string lf = "\n";
16 : static const std::string space = " ";
17 : static const std::string http_1_1 = "HTTP/1.1";
18 : static const std::string close = "close";
19 : } // namespace misc_strings
20 :
21 : namespace header{
22 : // TODO replace any header constant in code by those strings
23 : const std::string content_type = "Content-Type";
24 : const std::string cache_control = "Cache-Control";
25 : const std::string connection = "Connection";
26 : const std::string upgrade = "Upgrade";
27 : const std::string accept = "Accept";
28 : const std::string content_length = "Content-Length";
29 : const std::string transfer_encoding = "Transfer-Encoding";
30 : const std::string user_agent = "User-Agent";
31 : const std::string authorization = "Authorization";
32 : const std::string set_cookie = "Set-Cookie";
33 : const std::string cookie = "Cookie";
34 : const std::string location = "Location";
35 : const std::string host = "Host";
36 : const std::string referer = "Referer";
37 : const std::string x_frame_options = "X-Frame-Options";
38 : }
39 :
40 : namespace connection{
41 : const std::string keep_alive = "keep-alive";
42 : const std::string close = "close";
43 : const std::string upgrade = "upgrade";
44 : }
45 :
46 : namespace accept{
47 : const std::string event_stream = "text/event-stream";
48 : }
49 :
50 : namespace content_type{
51 : const std::string application_form_urlencoded = "application/x-www-form-urlencoded";
52 : const std::string text_html = "text/html";
53 : }
54 :
55 : class headers : public http_frame{
56 :
57 : public:
58 : using http_header = std::pair<std::string, std::string>;
59 :
60 : // constructors
61 3939 : headers() = default;
62 3939 : ~headers() override = default;
63 :
64 : // setters
65 : void add_header(std::string key, std::string value);
66 : void set_header(std::string key, std::string value);
67 6 : void set(std::string key, std::string value) { set_header(std::move(key), std::move(value)); }
68 : void set_proxy(std::string key, std::string value);
69 : void add_proxy(std::string key, std::string value);
70 : bool upgrade() const;
71 : bool stream() const;
72 : bool has_header(std::string_view key) const;
73 : bool remove_header(std::string_view key);
74 : void set_http_version_major(uint8_t http_version_major);
75 : void set_http_version_minor(uint8_t http_version_minor);
76 : void set_keep_alive(bool keep_alive);
77 :
78 : // getters
79 : std::vector<http_header>& get_headers();
80 : const std::vector<http_header>& get_headers() const;
81 : static std::string get_parameter(const std::string& key, std::string_view name) ;
82 : std::vector<std::string> get_headers_with_key(std::string_view key) const;
83 : const std::string& get_header(std::string_view key) const;
84 :
85 : const std::string& get_authorization() const;
86 : const std::string& get_cookie() const;
87 : const std::string& get_user_agent() const;
88 : const std::string& get_content_type() const;
89 : bool is_content_type(const std::string& value) const;
90 : bool empty_headers() const;
91 : size_t get_content_length() const;
92 : int get_http_version_major() const;
93 : int get_http_version_minor() const;
94 : bool keep_alive() const;
95 : bool inline is_header(std::string_view key, std::string_view header) const;
96 :
97 : // debug
98 : void debug_headers(std::ostream& os) const;
99 : void log(const char* scope, int level) const override;
100 :
101 : // process header intended to be override to process headers
102 : virtual void process_header(std::string key, std::string value);
103 :
104 : protected:
105 : std::vector<http_header> headers_;
106 : std::vector<http_header> proxy_headers_;
107 : boost::tribool keep_alive_ = boost::indeterminate;
108 : bool upgrade_ = false;
109 : bool stream_ = false;
110 : size_t content_length_ = 0;
111 : uint8_t http_version_major_ = 1;
112 : uint8_t http_version_minor_ = 1;
113 : };
114 :
115 : }
116 :
117 : #endif
|