Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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#ifndef SEASTAR_MODULE
25#include <limits>
26#include <cctype>
27#include <vector>
28#include <boost/intrusive/list.hpp>
29#endif
30#include <seastar/http/request_parser.hh>
31#include <seastar/http/request.hh>
32#include <seastar/core/seastar.hh>
33#include <seastar/core/sstring.hh>
34#include <seastar/core/distributed.hh>
35#include <seastar/core/queue.hh>
36#include <seastar/core/gate.hh>
38#include <seastar/util/std-compat.hh>
39#include <seastar/util/modules.hh>
40#include <seastar/http/routes.hh>
41#include <seastar/net/tls.hh>
42#include <seastar/core/shared_ptr.hh>
43
44namespace seastar {
45
46namespace http {
47SEASTAR_MODULE_EXPORT
48struct reply;
49}
50
51namespace httpd {
52
53SEASTAR_MODULE_EXPORT
54class http_server;
55SEASTAR_MODULE_EXPORT
56class http_stats;
57
58using namespace std::chrono_literals;
59
60SEASTAR_MODULE_EXPORT_BEGIN
62 metrics::metric_groups _metric_groups;
63public:
64 http_stats(http_server& server, const sstring& name);
65};
66
67class connection : public boost::intrusive::list_base_hook<> {
68 http_server& _server;
70 input_stream<char> _read_buf;
71 output_stream<char> _write_buf;
72 socket_address _client_addr;
73 socket_address _server_addr;
74 static constexpr size_t limit = 4096;
76 http_request_parser _parser;
77 std::unique_ptr<http::request> _req;
78 std::unique_ptr<http::reply> _resp;
79 // null element marks eof
81 bool _done = false;
82 const bool _tls;
83public:
84 [[deprecated("use connection(http_server&, connected_socket&&, bool tls)")]]
86 : connection(server, std::move(fd), tls) {}
87 connection(http_server& server, connected_socket&& fd, bool tls)
88 : _server(server)
89 , _fd(std::move(fd))
90 , _read_buf(_fd.input())
91 , _write_buf(_fd.output())
92 , _client_addr(_fd.remote_address())
93 , _server_addr(_fd.local_address())
94 , _tls(tls) {
95 on_new_connection();
96 }
98 socket_address client_addr, socket_address server_addr, bool tls)
99 : _server(server)
100 , _fd(std::move(fd))
101 , _read_buf(_fd.input())
102 , _write_buf(_fd.output())
103 , _client_addr(std::move(client_addr))
104 , _server_addr(std::move(server_addr))
105 , _tls(tls) {
106 on_new_connection();
107 }
108 ~connection();
109 void on_new_connection();
110
111 future<> process();
112 void shutdown();
113 future<> read();
114 future<> read_one();
115 future<> respond();
116 future<> do_response_loop();
117
118 void set_headers(http::reply& resp);
119
120 future<> start_response();
121
122 future<bool> generate_reply(std::unique_ptr<http::request> req);
123 void generate_error_reply_and_close(std::unique_ptr<http::request> req, http::reply::status_type status, const sstring& msg);
124
125 future<> write_body();
126
127 output_stream<char>& out();
128};
129
131
133 std::vector<server_socket> _listeners;
134 http_stats _stats;
135 uint64_t _total_connections = 0;
136 uint64_t _current_connections = 0;
137 uint64_t _requests_served = 0;
138 uint64_t _read_errors = 0;
139 uint64_t _respond_errors = 0;
141 sstring _date = http_date();
142 timer<> _date_format_timer { [this] {_date = http_date();} };
143 size_t _content_length_limit = std::numeric_limits<size_t>::max();
144 bool _content_streaming = false;
145 gate _task_gate;
146public:
147 routes _routes;
150 explicit http_server(const sstring& name) : _stats(*this, name) {
151 _date_format_timer.arm_periodic(1s);
152 }
179 [[deprecated("use listen(socket_address addr, server_credentials_ptr credentials)")]]
181
182 size_t get_content_length_limit() const;
183
184 void set_content_length_limit(size_t limit);
185
186 bool get_content_streaming() const;
187
188 void set_content_streaming(bool b);
189
190 future<> listen(socket_address addr, server_credentials_ptr credentials);
191 future<> listen(socket_address addr, listen_options lo, server_credentials_ptr credentials);
192 future<> listen(socket_address addr, listen_options lo);
193 future<> listen(socket_address addr);
194 future<> stop();
195
196 future<> do_accepts(int which);
197 future<> do_accepts(int which, bool with_tls);
198
199 uint64_t total_connections() const;
200 uint64_t current_connections() const;
201 uint64_t requests_served() const;
202 uint64_t read_errors() const;
203 uint64_t reply_errors() const;
204 // Write the current date in the specific "preferred format" defined in
205 // RFC 7231, Section 7.1.1.1.
206 static sstring http_date();
207private:
208 future<> do_accept_one(int which, bool with_tls);
209 boost::intrusive::list<connection> _connections;
210 friend class seastar::httpd::connection;
211 friend class http_server_tester;
212};
213
215public:
216 static std::vector<server_socket>& listeners(http_server& server) {
217 return server._listeners;
218 }
219};
220
221/*
222 * A helper class to start, set and listen an http server
223 * typical use would be:
224 *
225 * auto server = new http_server_control();
226 * server->start().then([server] {
227 * server->set_routes(set_routes);
228 * }).then([server, port] {
229 * server->listen(port);
230 * }).then([port] {
231 * std::cout << "Seastar HTTP server listening on port " << port << " ...\n";
232 * });
233 */
235 std::unique_ptr<distributed<http_server>> _server_dist;
236private:
237 static sstring generate_server_name();
238public:
239 http_server_control() : _server_dist(new distributed<http_server>) {
240 }
241
242 future<> start(const sstring& name = generate_server_name());
243 future<> stop() noexcept;
244 future<> set_routes(std::function<void(routes& r)> fun);
245 future<> listen(socket_address addr);
249 distributed<http_server>& server();
250};
251SEASTAR_MODULE_EXPORT_END
252}
253
254}
Definition: api.hh:183
output_stream< char > output(size_t buffer_size=8192)
input_stream< char > input(connected_socket_input_stream_config csisc={})
socket_address local_address() const noexcept
Local address of the socket.
socket_address remote_address() const noexcept
Remote address of the socket.
A representation of a possibly not-yet-computed value.
Definition: future.hh:1197
Definition: gate.hh:62
Definition: httpd.hh:67
Definition: httpd.hh:234
Definition: httpd.hh:214
Definition: httpd.hh:132
void set_tls_credentials(server_credentials_ptr credentials)
set tls credentials for the server Setting the tls credentials will set the http-server to work in ht...
Definition: httpd.hh:61
Definition: routes.hh:81
holds the metric definition.
Definition: metrics_registration.hh:94
Definition: queue.hh:45
Definition: sharded.hh:183
Definition: socket_defs.hh:47
Definition: timer.hh:84
void arm_periodic(duration delta) noexcept
Definition: timer.hh:189
server_socket listen(socket_address sa)
holds the metric_groups definition needed by class that reports metrics
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: reply.hh:61
status_type
Definition: reply.hh:65
Definition: api.hh:397