MLIB
Loading...
Searching...
No Matches
httpd.h
Go to the documentation of this file.
1#pragma once
7
8#include "tcpserver.h"
9#include <string>
10#include <map>
11#include <deque>
12
14#define HTTPD_MAX_HEADER 8192
15
18#define HTTPD_OK 0
19#define HTTPD_ERR_WRITE -1
20#define HTTPD_ERR_FOPEN -2
21#define HTTPD_ERR_FREAD -3
23
24namespace mlib {
25
26class http_connection;
27
29struct ci_less : public std::function<bool (std::string, std::string)>
30{
31 bool operator() (const std::string& lhs, const std::string& rhs) const
32 {
33 return _stricmp (lhs.c_str (), rhs.c_str ()) < 0;
34 }
35};
36
39typedef std::map<std::string, std::string, ci_less> str_pairs;
40
42typedef int (*uri_handler) (const char* uri, http_connection& client, void* info);
43
45class http_connection : public thread
46{
47public:
48 friend class httpd;
49
50 const char* get_uri () const;
51 const char* get_method () const;
52 const char* get_all_ihdr () const;
53 void add_ohdr (const char* hdr, const char* value);
54 const char* get_ihdr (const char* hdr) const;
55 const char* get_ohdr (const char* hdr) const;
56 const char* get_query () const;
57 const char* get_body () const;
58 int get_content_length () const;
59 const std::string& get_qparam (const char* key);
60 bool has_qparam (const char* key);
61 const std::string& get_bparam (const char* key);
62 bool has_bparam (const char* key);
63
64 sockstream& out ();
65 void respond (unsigned int code, const char* reason = 0);
66 void redirect (const char* uri, unsigned int code = 303);
67 void serve404 (const char* text = 0);
68 int serve_file (const std::string& full_path);
69 int serve_shtml (const std::string& full_path);
70 int serve_buffer (const BYTE* buffer, size_t sz);
71 int serve_buffer (const std::string& str);
72
73 void respond_part (const char* part_type, const char* bound);
74 void respond_next (bool last);
75
76protected:
77 http_connection (sock& socket, httpd& server);
78 void run () override;
79 void term () override;
80
81 httpd& parent;
82 sockstream ws;
83
84private:
85 bool parse_url ();
86 bool parse_headers ();
87 bool parse_body ();
88 void parse_query ();
89 void process_valid_request ();
90 void process_ssi (const char* request);
91 int do_auth ();
92 void serve401 (const char* realm);
93
94 char request[HTTPD_MAX_HEADER];
95 char* uri;
96 char* query;
97 char* http_version;
98 char* headers;
99 char* body;
100 int content_len;
101 std::string part_boundary;
102 bool response_sent;
103 size_t req_len;
104 str_pairs oheaders;
105 str_pairs iheaders;
106 str_pairs qparams;
107 str_pairs bparams;
108 bool query_parsed, body_parsed;
109
110 struct user
111 {
112 std::string name;
113 std::string pwd;
114 };
115 std::multimap<std::string, user> auth; // authenticated users
116};
117
119class httpd : public tcpserver
120{
121public:
122 explicit httpd (unsigned short port = 0, unsigned int maxconn = 0);
123 ~httpd ();
124
125 void add_ohdr (const char* hdr, const char* value);
126 void remove_ohdr (const char* hdr);
127 void add_handler (const char* uri, uri_handler func, void* info = 0);
128 bool add_user (const char* realm, const char* username, const char* pwd);
129 bool remove_user (const char* realm, const char* username);
130 void add_realm (const char* realm, const char* uri);
131 void add_var (const char* name, const char* fmt, void* addr, double multiplier = 1.);
132 std::string get_var (const char* name);
133 void aquire_varlock ();
134 void release_varlock ();
135 bool try_varlock ();
136
137 void name (const char* nam);
138 void docroot (const char* path);
139 const char* docroot () const;
140 void add_alias (const char* uri, const char* path);
141
142 void default_uri (const char* name)
143 {
144 defuri = name;
145 };
146 const char* default_uri ()
147 {
148 return defuri.c_str ();
149 };
150
151 void add_mime_type (const char* ext, const char* type, bool shtml = false);
152 void delete_mime_type (const char* ext);
153
154 bool is_protected (const char* uri, std::string& realm);
155 bool authenticate (const char* realm, const char* user, const char* pwd);
156
157 friend class http_connection;
158
159protected:
160 int invoke_handler (const char* uri, http_connection& client);
161 bool find_alias (const char* uri, char* path);
162 const char* guess_mimetype (const char* file, bool& shtml);
163 http_connection* make_thread (sock& connection);
164
165private:
166 str_pairs out_headers;
167 mlib::criticalsection hdr_lock;
168 str_pairs realms;
169
170 struct handle_info
171 {
172 handle_info (uri_handler h_, void* info_);
173
174 uri_handler h;
175 void* nfo;
176 std::shared_ptr<mlib::criticalsection> in_use;
177 };
178 std::map<std::string, handle_info> handlers;
179
180 std::map<std::string, std::string> aliases;
181
182 struct var_info
183 {
184 std::string fmt;
185 void* addr;
186 double multiplier;
187 };
188 std::map<std::string, var_info> variables;
189 mlib::criticalsection varlock;
190
191 struct user
192 {
193 std::string name;
194 std::string pwd;
195 };
196 std::multimap<std::string, user> credentials;
197
198 struct mimetype
199 {
200 std::string suffix;
201 std::string type;
202 bool shtml;
203 };
204 std::deque<mimetype> types;
205
206 std::string root;
207 std::string defuri;
208};
209
210/*==================== INLINE FUNCTIONS ===========================*/
211
213inline const char* http_connection::get_uri () const
214{
215 return uri;
216};
217
219inline const char* http_connection::get_method () const
220{
221 return request;
222};
223
225inline const char* http_connection::get_all_ihdr () const
226{
227 return headers;
228};
229
231inline const char* http_connection::get_query () const
232{
233 return query ? query : "";
234};
235
237inline const char* http_connection::get_body () const
238{
239 return body;
240};
241
244{
245 return content_len;
246};
247
250{
251 return ws;
252};
253
255inline const char* httpd::docroot () const
256{
257 return root.c_str ();
258}
259
262{
263 varlock.enter ();
264}
265
268{
269 varlock.leave ();
270}
271
276inline bool httpd::try_varlock ()
277{
278 return varlock.try_enter ();
279}
280
281inline httpd::handle_info::handle_info (uri_handler h_, void* info_)
282 : h (h_)
283 , nfo (info_)
284 , in_use{std::make_shared<criticalsection> ()}
285{}
286
287void parse_urlparams (const char* par_str, str_pairs& params);
288
289} // namespace mlib
Lightweight inter-thread synchronization.
Definition critsect.h:25
Representation of a HTTP client connection request.
Definition httpd.h:46
const char * get_method() const
Return HTTP method (GET, POST, etc.)
Definition httpd.h:219
const char * get_ohdr(const char *hdr) const
Returns the value of a response header.
Definition httpd.cpp:1009
const char * get_query() const
Return URI query string (everything after '?' and before '#')
Definition httpd.h:231
const std::string & get_bparam(const char *key)
Return the value of a form parameter or the empty string if the body doesn't have the parameter.
Definition httpd.cpp:500
http_connection(sock &socket, httpd &server)
Protected constructor used by httpd class to create a new connection thread.
Definition httpd.cpp:104
void respond_next(bool last)
Send subsequent parts of a multi-part response.
Definition httpd.cpp:1046
sockstream & out()
Return socket object associated with this connection.
Definition httpd.h:249
int serve_buffer(const BYTE *buffer, size_t sz)
Send the content of a buffer.
Definition httpd.cpp:679
bool has_bparam(const char *key)
Return true if the body contains the given parameter.
Definition httpd.cpp:513
void term() override
Finalization function called after run.
Definition httpd.cpp:250
const char * get_ihdr(const char *hdr) const
Returns the value of a request header.
Definition httpd.cpp:990
const char * get_uri() const
Return URI of this connection.
Definition httpd.h:213
int serve_shtml(const std::string &full_path)
Send the content of a file, processing any SSI directives.
Definition httpd.cpp:533
const char * get_all_ihdr() const
Return all incoming headers.
Definition httpd.h:225
int get_content_length() const
Return request body.
Definition httpd.h:243
void respond(unsigned int code, const char *reason=0)
Send the beginning of HTTP response.
Definition httpd.cpp:902
void run() override
Thread run loop.
Definition httpd.cpp:125
void serve404(const char *text=0)
Sends a 404 (page not found) response.
Definition httpd.cpp:387
bool has_qparam(const char *key)
Return true if the query contains the given parameter.
Definition httpd.cpp:488
int serve_file(const std::string &full_path)
Send the content of a file.
Definition httpd.cpp:737
void respond_part(const char *part_type, const char *bound)
Send first part of a multi-part response.
Definition httpd.cpp:1028
const std::string & get_qparam(const char *key)
Return the value of a query parameter or the empty string if the query doesn't have the parameter.
Definition httpd.cpp:475
void add_ohdr(const char *hdr, const char *value)
Add or modify a response header.
Definition httpd.cpp:854
const char * get_body() const
Return request body.
Definition httpd.h:237
void redirect(const char *uri, unsigned int code=303)
Generate a HTTP redirect response to a new uri.
Definition httpd.cpp:865
void aquire_varlock()
Acquire lock on server's variables.
Definition httpd.h:261
bool is_protected(const char *uri, std::string &realm)
Find the longest match realm that covers an uri.
Definition httpd.cpp:1303
bool add_user(const char *realm, const char *username, const char *pwd)
Add a new user to a relm or modifies password for an existing user.
Definition httpd.cpp:1244
void add_mime_type(const char *ext, const char *type, bool shtml=false)
Add/change content of table matching MIME types to file extensions.
Definition httpd.cpp:1183
const char * guess_mimetype(const char *file, bool &shtml)
Guess MIME type of a file and if SSI replacement should be enabled based on file extension.
Definition httpd.cpp:1487
http_connection * make_thread(sock &connection)
Create an new http_connection object for a new incoming connection.
Definition httpd.cpp:1130
bool authenticate(const char *realm, const char *user, const char *pwd)
Verify user credentials for a realm.
Definition httpd.cpp:1326
int invoke_handler(const char *uri, http_connection &client)
Invoke a user defined URI handler.
Definition httpd.cpp:1349
bool try_varlock()
Try to acquire lock on server's variables.
Definition httpd.h:276
void add_realm(const char *realm, const char *uri)
Add a new access realm.
Definition httpd.cpp:1230
void release_varlock()
Release lock on server's variables.
Definition httpd.h:267
void name(const char *nam)
Change server name.
Definition httpd.cpp:1115
void add_ohdr(const char *hdr, const char *value)
Add or modify a server response header.
Definition httpd.cpp:1143
void add_var(const char *name, const char *fmt, void *addr, double multiplier=1.)
Add or modify a user variable.
Definition httpd.cpp:1420
void remove_ohdr(const char *hdr)
Remove a server response header.
Definition httpd.cpp:1153
void add_handler(const char *uri, uri_handler func, void *info=0)
Add or modify an URI handler function.
Definition httpd.cpp:1171
const char * docroot() const
Return current file origin.
Definition httpd.h:255
httpd(unsigned short port=0, unsigned int maxconn=0)
Constructor.
Definition httpd.cpp:1090
void add_alias(const char *uri, const char *path)
Maps a local file path to a path in the URI space.
Definition httpd.cpp:1369
void delete_mime_type(const char *ext)
Remove a file type from the MIME type table.
Definition httpd.cpp:1209
std::string get_var(const char *name)
Return the current string representation of a variable.
Definition httpd.cpp:1431
~httpd()
Destructor.
Definition httpd.cpp:1106
bool find_alias(const char *uri, char *path)
Retrieve the local file path mapped to an URI.
Definition httpd.cpp:1387
Encapsulation of a Windows socket.
Definition wsockstream.h:27
tcpserver(unsigned short port, const std::string &name=std::string(), unsigned int max_conn=0)
Opens the socket and initializes the connections table.
Definition tcpserver.cpp:70
void maxconn(unsigned int new_max)
Set maximum number of connections accepted.
Definition tcpserver.cpp:386
thread(std::function< unsigned int()> func)
Make a thread with the given function body.
Definition thread.cpp:71
virtual void name(const std::string &nam)
Set thread's name.
Definition thread.cpp:160
virtual const std::string & name() const
Return object's name.
Definition syncbase.h:60
#define HTTPD_MAX_HEADER
Maximum size of HTTP header.
Definition httpd.h:14
int(* uri_handler)(const char *uri, http_connection &client, void *info)
User defined URL handler function.
Definition httpd.h:42
std::map< std::string, std::string, ci_less > str_pairs
Definition httpd.h:39
Case insensitive comparison function.
Definition httpd.h:30
Definition of tcpserver class.
generic_sockstream< std::iostream > sockstream
Bidirectional socket stream.
Definition wsockstream.h:261