Line data Source code
1 : // Copyright (c) 2011-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/edhttp
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 : #pragma once
19 :
20 : // eventdispatcher
21 : //
22 : #include <eventdispatcher/tcp_bio_client.h>
23 :
24 :
25 : // libaddr
26 : //
27 : #include <libaddr/addr_range.h>
28 :
29 :
30 : // C++
31 : //
32 : #include <map>
33 : #include <vector>
34 :
35 :
36 :
37 : namespace edhttp
38 : {
39 :
40 :
41 :
42 :
43 :
44 :
45 : // name / value pairs
46 : typedef std::map<std::string, std::string> header_t;
47 :
48 : // attachment buffer
49 : typedef std::vector<char> attachment_t;
50 :
51 :
52 : class http_request
53 : {
54 : public:
55 : typedef std::shared_ptr<http_request> pointer_t;
56 :
57 : addr::addr_range::vector_t
58 : get_address_ranges() const;
59 : bool unique_host() const;
60 : std::string get_host() const;
61 : bool unique_port() const;
62 : int get_port() const;
63 : std::string get_agent_name() const;
64 : std::string get_method() const;
65 : std::string get_path() const;
66 : std::string get_header(std::string const & name) const;
67 : std::string get_post(std::string const & name) const;
68 : std::string get_body() const; // also returns data
69 : std::string get_request(bool keep_alive) const;
70 :
71 : void set_uri(std::string const & uri);
72 : void set_address_ranges(addr::addr_range::vector_t const & address_ranges);
73 : void set_host(std::string const & host);
74 : void set_port(int port);
75 : void set_agent_name(std::string const & agent_name);
76 : void set_method(std::string const & method);
77 : void set_path(std::string const & path);
78 : void set_header(std::string const & name, std::string const & value);
79 : void set_post(std::string const & name, std::string const & value);
80 : void set_basic_auth(std::string const & username, std::string const & secret);
81 : void set_data(std::string const & data);
82 : void set_body(std::string const & body);
83 :
84 : private:
85 : //std::string f_host = std::string();
86 : //int32_t f_port = -1;
87 : addr::addr_range::vector_t f_address_ranges = addr::addr_range::vector_t();
88 : std::string f_agent_name = std::string("edhttp");
89 : std::string f_method = std::string();
90 : std::string f_path = std::string();
91 : header_t f_headers = header_t();
92 : header_t f_post = header_t();
93 : std::string f_body = std::string();
94 : std::vector<attachment_t> f_attachments = std::vector<attachment_t>(); // not used yet (Also look in a way that allows us to avoid an extra copy)
95 : bool f_has_body = false;
96 : bool f_has_data = false;
97 : bool f_has_post = false;
98 : bool f_has_attachment = false; // not used yet
99 : };
100 :
101 :
102 : class http_client;
103 :
104 :
105 0 : class http_response
106 : {
107 : public:
108 : typedef std::shared_ptr<http_response> pointer_t;
109 :
110 : enum class protocol_t
111 : {
112 : UNKNOWN,
113 : HTTP_1_0,
114 : HTTP_1_1
115 : };
116 :
117 : std::string get_original_header() const;
118 : protocol_t get_protocol() const;
119 : int get_response_code() const;
120 : std::string get_http_message() const;
121 : bool has_header(std::string const & name) const;
122 : std::string get_header(std::string const & name) const;
123 : std::string get_response() const;
124 :
125 : void append_original_header(std::string const & header);
126 : void set_protocol(protocol_t protocol);
127 : void set_response_code(int code);
128 : void set_http_message(std::string const& message);
129 : void set_header(std::string const & name, std::string const & value);
130 : void set_response(std::string const & response);
131 :
132 : private:
133 : friend http_client;
134 :
135 : void read_response(ed::tcp_bio_client::pointer_t connection);
136 :
137 : std::string f_original_header = std::string();
138 : protocol_t f_protocol = protocol_t::UNKNOWN;
139 : int32_t f_response_code = 0;
140 : std::string f_http_message = std::string();
141 : header_t f_header = header_t();
142 : std::string f_response = std::string();
143 : };
144 :
145 :
146 : class http_client
147 : {
148 : public:
149 : http_client() {}
150 :
151 : http_client(http_client const &) = delete;
152 : http_client & operator = (http_client const &) = delete;
153 :
154 : bool get_keep_alive() const;
155 :
156 : void set_keep_alive(bool keep_alive);
157 :
158 : http_response::pointer_t send_request(http_request const & request);
159 :
160 : private:
161 : bool f_keep_alive = true;
162 : ed::tcp_bio_client::pointer_t f_connection = ed::tcp_bio_client::pointer_t();
163 : std::string f_host = std::string();
164 : int32_t f_port = -1;
165 : };
166 :
167 :
168 : } // namespace edhttp
169 : // vim: ts=4 sw=4 et
|