]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/http/httpd.hh
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / seastar / include / seastar / http / httpd.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright 2015 Cloudius Systems
20 */
21
22 #pragma once
23
24 #include <seastar/http/request_parser.hh>
25 #include <seastar/http/request.hh>
26 #include <seastar/core/seastar.hh>
27 #include <seastar/core/sstring.hh>
28 #include <seastar/core/app-template.hh>
29 #include <seastar/core/circular_buffer.hh>
30 #include <seastar/core/distributed.hh>
31 #include <seastar/core/queue.hh>
32 #include <seastar/core/gate.hh>
33 #include <seastar/core/metrics_registration.hh>
34 #include <seastar/util/std-compat.hh>
35 #include <iostream>
36 #include <algorithm>
37 #include <unordered_map>
38 #include <queue>
39 #include <bitset>
40 #include <limits>
41 #include <cctype>
42 #include <vector>
43 #include <boost/intrusive/list.hpp>
44 #include <seastar/http/routes.hh>
45 #include <seastar/net/tls.hh>
46 #include <seastar/core/shared_ptr.hh>
47
48 namespace seastar {
49
50 namespace httpd {
51
52 class http_server;
53 class http_stats;
54 struct reply;
55
56 using namespace std::chrono_literals;
57
58 class http_stats {
59 metrics::metric_groups _metric_groups;
60 public:
61 http_stats(http_server& server, const sstring& name);
62 };
63
64 class connection : public boost::intrusive::list_base_hook<> {
65 http_server& _server;
66 connected_socket _fd;
67 input_stream<char> _read_buf;
68 output_stream<char> _write_buf;
69 static constexpr size_t limit = 4096;
70 using tmp_buf = temporary_buffer<char>;
71 http_request_parser _parser;
72 std::unique_ptr<request> _req;
73 std::unique_ptr<reply> _resp;
74 // null element marks eof
75 queue<std::unique_ptr<reply>> _replies { 10 };
76 bool _done = false;
77 public:
78 connection(http_server& server, connected_socket&& fd,
79 socket_address addr)
80 : _server(server), _fd(std::move(fd)), _read_buf(_fd.input()), _write_buf(
81 _fd.output()) {
82 on_new_connection();
83 }
84 ~connection();
85 void on_new_connection();
86
87 future<> process();
88 void shutdown();
89 future<> read();
90 future<> read_one();
91 future<> respond();
92 future<> do_response_loop();
93
94 void set_headers(reply& resp);
95
96 future<> start_response();
97 future<> write_reply_headers(std::unordered_map<sstring, sstring>::iterator hi);
98
99 static short hex_to_byte(char c);
100
101 /**
102 * Convert a hex encoded 2 bytes substring to char
103 */
104 static char hexstr_to_char(const std::string_view& in, size_t from);
105
106 /**
107 * URL_decode a substring and place it in the given out sstring
108 */
109 static bool url_decode(const std::string_view& in, sstring& out);
110
111 /**
112 * Add a single query parameter to the parameter list
113 */
114 static void add_param(request& req, const std::string_view& param);
115
116 /**
117 * Set the query parameters in the request objects.
118 * query param appear after the question mark and are separated
119 * by the ampersand sign
120 */
121 static sstring set_query_param(request& req);
122
123 future<bool> generate_reply(std::unique_ptr<request> req);
124 void generate_error_reply_and_close(std::unique_ptr<request> req, reply::status_type status, const sstring& msg);
125
126 future<> write_body();
127
128 output_stream<char>& out();
129 };
130
131 class http_server_tester;
132
133 class http_server {
134 std::vector<server_socket> _listeners;
135 http_stats _stats;
136 uint64_t _total_connections = 0;
137 uint64_t _current_connections = 0;
138 uint64_t _requests_served = 0;
139 uint64_t _read_errors = 0;
140 uint64_t _respond_errors = 0;
141 shared_ptr<seastar::tls::server_credentials> _credentials;
142 sstring _date = http_date();
143 timer<> _date_format_timer { [this] {_date = http_date();} };
144 size_t _content_length_limit = std::numeric_limits<size_t>::max();
145 gate _task_gate;
146 public:
147 routes _routes;
148 using connection = seastar::httpd::connection;
149 explicit http_server(const sstring& name) : _stats(*this, name) {
150 _date_format_timer.arm_periodic(1s);
151 }
152 /*!
153 * \brief set tls credentials for the server
154 * Setting the tls credentials will set the http-server to work in https mode.
155 *
156 * To use the https, create server credentials and pass it to the server before it starts.
157 *
158 * Use case example using seastar threads for clarity:
159
160 distributed<http_server> server; // typical server
161
162 seastar::shared_ptr<seastar::tls::credentials_builder> creds = seastar::make_shared<seastar::tls::credentials_builder>();
163 sstring ms_cert = "MyCertificate.crt";
164 sstring ms_key = "MyKey.key";
165
166 creds->set_dh_level(seastar::tls::dh_params::level::MEDIUM);
167
168 creds->set_x509_key_file(ms_cert, ms_key, seastar::tls::x509_crt_format::PEM).get();
169 creds->set_system_trust().get();
170
171
172 server.invoke_on_all([creds](http_server& server) {
173 server.set_tls_credentials(creds->build_server_credentials());
174 return make_ready_future<>();
175 }).get();
176 *
177 */
178 void set_tls_credentials(shared_ptr<seastar::tls::server_credentials> credentials);
179
180 size_t get_content_length_limit() const;
181
182 void set_content_length_limit(size_t limit);
183
184 future<> listen(socket_address addr, listen_options lo);
185 future<> listen(socket_address addr);
186 future<> stop();
187
188 future<> do_accepts(int which);
189
190 uint64_t total_connections() const;
191 uint64_t current_connections() const;
192 uint64_t requests_served() const;
193 uint64_t read_errors() const;
194 uint64_t reply_errors() const;
195 // Write the current date in the specific "preferred format" defined in
196 // RFC 7231, Section 7.1.1.1.
197 static sstring http_date();
198 private:
199 future<> do_accept_one(int which);
200 boost::intrusive::list<connection> _connections;
201 friend class seastar::httpd::connection;
202 friend class http_server_tester;
203 };
204
205 class http_server_tester {
206 public:
207 static std::vector<server_socket>& listeners(http_server& server) {
208 return server._listeners;
209 }
210 };
211
212 /*
213 * A helper class to start, set and listen an http server
214 * typical use would be:
215 *
216 * auto server = new http_server_control();
217 * server->start().then([server] {
218 * server->set_routes(set_routes);
219 * }).then([server, port] {
220 * server->listen(port);
221 * }).then([port] {
222 * std::cout << "Seastar HTTP server listening on port " << port << " ...\n";
223 * });
224 */
225 class http_server_control {
226 std::unique_ptr<distributed<http_server>> _server_dist;
227 private:
228 static sstring generate_server_name();
229 public:
230 http_server_control() : _server_dist(new distributed<http_server>) {
231 }
232
233 future<> start(const sstring& name = generate_server_name());
234 future<> stop();
235 future<> set_routes(std::function<void(routes& r)> fun);
236 future<> listen(socket_address addr);
237 future<> listen(socket_address addr, listen_options lo);
238 distributed<http_server>& server();
239 };
240
241 }
242
243 }