Embedded HTTP Server
http_server.h
1 //
2 // Copyright (C) 2020 Maciej Sobczak
3 //
4 // This file declares the interface of the minimal, embedded HTTP server.
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file Boost_Software_License_1_0.txt
8 // or copy at http://www.opensource.org/licenses/bsl1.0.html)
9 //
10 
11 #ifndef HTTP_SERVER_H_INCLUDED
12 #define HTTP_SERVER_H_INCLUDED
13 
14 #include <ostream>
15 #include <string>
16 #include <functional>
17 #include <unordered_map>
18 #include <vector>
19 
21 namespace http
22 {
23 
25 const unsigned int log_connections = 0x01;
26 const unsigned int log_static_requests = 0x02;
27 const unsigned int log_static_responses = 0x04;
28 const unsigned int log_dynamic_requests = 0x08;
29 const unsigned int log_dynamic_responses = 0x10;
30 const unsigned int log_everything = 0x1f;
31 
43 void server_start(int port_number, const char * base_directory);
44 void server_start(int port_number, const char * base_directory,
45  std::ostream & error_log, unsigned int log_events_mask = log_everything);
46 
49 {
52 };
53 
57 typedef std::function<void(std::ostream &, connection_event)>
59 
68 
73 typedef std::function<void(std::ostream &, const std::string &, const std::string &)>
75 
88 void register_generic_get_action(const char * name, get_action_type f);
89 
103 void register_html_get_action(const char * name, get_action_type f);
104 
118 void register_text_get_action(const char * name, get_action_type f);
119 
127 typedef std::function<void(std::ostream &, const std::string &, const std::string &,
128  std::istream &, std::size_t, const std::string &)>
130 
143 void register_generic_post_action(const char * name, post_action_type f);
144 
158 void register_html_post_action(const char * name, post_action_type f);
159 
173 void register_text_post_action(const char * name, post_action_type f);
174 
181 std::string html_encode(const std::string & s);
182 
190 std::string url_encode(const std::string & s);
191 
197 std::string url_decode(const std::string & s);
198 
200 typedef std::unordered_map<std::string, std::string> params_map_type;
201 
208 params_map_type decode_params(const std::string & params, bool decode);
209 params_map_type decode_params(const std::vector<char> & params, bool decode);
210 
224 std::string header(const std::string & mime_type,
225  std::size_t content_length = 0, bool cache = false);
226 
227 } // namespace http
228 
229 #endif // HTTP_SERVER_H_INCLUDED
std::string url_encode(const std::string &s)
std::string header(const std::string &mime_type, std::size_t content_length=0, bool cache=false)
std::function< void(std::ostream &, const std::string &, const std::string &)> get_action_type
Definition: http_server.h:74
Namespace with scope for all Embedded HTTP Server definitions.
Definition: http_server.h:21
const unsigned int log_connections
Bit masks for various categories of diagnostic log messages.
Definition: http_server.h:25
connection_event
Type defining possible connection events, used to notify the connection callback.
Definition: http_server.h:48
void register_connection_callback(connection_callback_type callback)
std::unordered_map< std::string, std::string > params_map_type
Type of map {key->value,...} for decoding URL and form parameters.
Definition: http_server.h:200
std::function< void(std::ostream &, const std::string &, const std::string &, std::istream &, std::size_t, const std::string &)> post_action_type
Definition: http_server.h:129
void register_text_get_action(const char *name, get_action_type f)
std::string html_encode(const std::string &s)
params_map_type decode_params(const std::string &params, bool decode)
void register_html_get_action(const char *name, get_action_type f)
The given stream is to be closed and destroyed.
Definition: http_server.h:51
void server_start(int port_number, const char *base_directory)
Start the embedded HTTP server.
void register_generic_get_action(const char *name, get_action_type f)
void register_generic_post_action(const char *name, post_action_type f)
void register_html_post_action(const char *name, post_action_type f)
void register_text_post_action(const char *name, post_action_type f)
The given stream (client connection) was just created.
Definition: http_server.h:50
std::function< void(std::ostream &, connection_event)> connection_callback_type
Definition: http_server.h:58
std::string url_decode(const std::string &s)