1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
/* Copyright (c) 2013-2017 the Civetweb developers
* Copyright (c) 2013 No Face Press, LLC
*
* License http://opensource.org/licenses/mit-license.php MIT License
*/
#ifndef _CIVETWEB_SERVER_H_
#define _CIVETWEB_SERVER_H_
#ifdef __cplusplus
#include "civetweb.h"
#include <map>
#include <string>
#include <vector>
#include <stdexcept>
// forward declaration
class CivetServer;
/**
* Exception class for thrown exceptions within the CivetHandler object.
*/
class CIVETWEB_API CivetException : public std::runtime_error
{
public:
CivetException(const std::string &msg) : std::runtime_error(msg)
{
}
};
/**
* Basic interface for a URI request handler. Handlers implementations
* must be reentrant.
*/
class CIVETWEB_API CivetHandler
{
public:
/**
* Destructor
*/
virtual ~CivetHandler()
{
}
/**
* Callback method for GET request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handleGet(CivetServer *server, struct mg_connection *conn);
/**
* Callback method for POST request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handlePost(CivetServer *server, struct mg_connection *conn);
/**
* Callback method for HEAD request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handleHead(CivetServer *server, struct mg_connection *conn);
/**
* Callback method for PUT request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handlePut(CivetServer *server, struct mg_connection *conn);
/**
* Callback method for DELETE request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handleDelete(CivetServer *server, struct mg_connection *conn);
/**
* Callback method for OPTIONS request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handleOptions(CivetServer *server, struct mg_connection *conn);
/**
* Callback method for PATCH request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handlePatch(CivetServer *server, struct mg_connection *conn);
};
/**
* Basic interface for a URI authorization handler. Handler implementations
* must be reentrant.
*/
class CIVETWEB_API CivetAuthHandler
{
public:
/**
* Destructor
*/
virtual ~CivetAuthHandler()
{
}
/**
* Callback method for authorization requests. It is up the this handler
* to generate 401 responses if authorization fails.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if authorization succeeded, false otherwise
*/
virtual bool authorize(CivetServer *server, struct mg_connection *conn) = 0;
};
/**
* Basic interface for a websocket handler. Handlers implementations
* must be reentrant.
*/
class CIVETWEB_API CivetWebSocketHandler
{
public:
/**
* Destructor
*/
virtual ~CivetWebSocketHandler()
{
}
/**
* Callback method for when the client intends to establish a websocket
*connection, before websocket handshake.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true to keep socket open, false to close it
*/
virtual bool handleConnection(CivetServer *server,
const struct mg_connection *conn);
/**
* Callback method for when websocket handshake is successfully completed,
*and connection is ready for data exchange.
*
* @param server - the calling server
* @param conn - the connection information
*/
virtual void handleReadyState(CivetServer *server,
struct mg_connection *conn);
/**
* Callback method for when a data frame has been received from the client.
*
* @param server - the calling server
* @param conn - the connection information
* @bits: first byte of the websocket frame, see websocket RFC at
*http://tools.ietf.org/html/rfc6455, section 5.2
* @data, data_len: payload, with mask (if any) already applied.
* @returns true to keep socket open, false to close it
*/
virtual bool handleData(CivetServer *server,
struct mg_connection *conn,
int bits,
char *data,
size_t data_len);
/**
* Callback method for when the connection is closed.
*
* @param server - the calling server
* @param conn - the connection information
*/
virtual void handleClose(CivetServer *server,
const struct mg_connection *conn);
};
/**
* CivetCallbacks
*
* wrapper for mg_callbacks
*/
struct CIVETWEB_API CivetCallbacks : public mg_callbacks {
CivetCallbacks();
};
/**
* CivetServer
*
* Basic class for embedded web server. This has an URL mapping built-in.
*/
class CIVETWEB_API CivetServer
{
public:
/**
* Constructor
*
* This automatically starts the sever.
* It is good practice to call getContext() after this in case there
* were errors starting the server.
*
* Note: CivetServer should not be used as a static instance in a Windows
* DLL, since the constructor creates threads and the destructor joins
* them again (creating/joining threads should not be done in static
* constructors).
*
* @param options - the web server options.
* @param callbacks - optional web server callback methods.
*
* @throws CivetException
*/
CivetServer(const char **options,
const struct CivetCallbacks *callbacks = 0,
const void *UserContext = 0);
CivetServer(std::vector<std::string> options,
const struct CivetCallbacks *callbacks = 0,
const void *UserContext = 0);
/**
* Destructor
*/
virtual ~CivetServer();
/**
* close()
*
* Stops server and frees resources.
*/
void close();
/**
* getContext()
*
* @return the context or 0 if not running.
*/
const struct mg_context *
getContext() const
{
return context;
}
/**
* addHandler(const std::string &, CivetHandler *)
*
* Adds a URI handler. If there is existing URI handler, it will
* be replaced with this one.
*
* URI's are ordered and prefix (REST) URI's are supported.
*
* @param uri - URI to match.
* @param handler - handler instance to use.
*/
void addHandler(const std::string &uri, CivetHandler *handler);
void
addHandler(const std::string &uri, CivetHandler &handler)
{
addHandler(uri, &handler);
}
/**
* addWebSocketHandler
*
* Adds a WebSocket handler for a specific URI. If there is existing URI
*handler, it will
* be replaced with this one.
*
* URI's are ordered and prefix (REST) URI's are supported.
*
* @param uri - URI to match.
* @param handler - handler instance to use.
*/
void addWebSocketHandler(const std::string &uri,
CivetWebSocketHandler *handler);
void
addWebSocketHandler(const std::string &uri, CivetWebSocketHandler &handler)
{
addWebSocketHandler(uri, &handler);
}
/**
* removeHandler(const std::string &)
*
* Removes a handler.
*
* @param uri - the exact URL used in addHandler().
*/
void removeHandler(const std::string &uri);
/**
* removeWebSocketHandler(const std::string &)
*
* Removes a web socket handler.
*
* @param uri - the exact URL used in addWebSocketHandler().
*/
void removeWebSocketHandler(const std::string &uri);
/**
* addAuthHandler(const std::string &, CivetAuthHandler *)
*
* Adds a URI authorization handler. If there is existing URI authorization
* handler, it will be replaced with this one.
*
* URI's are ordered and prefix (REST) URI's are supported.
*
* @param uri - URI to match.
* @param handler - authorization handler instance to use.
*/
void addAuthHandler(const std::string &uri, CivetAuthHandler *handler);
void
addAuthHandler(const std::string &uri, CivetAuthHandler &handler)
{
addAuthHandler(uri, &handler);
}
/**
* removeAuthHandler(const std::string &)
*
* Removes an authorization handler.
*
* @param uri - the exact URL used in addAuthHandler().
*/
void removeAuthHandler(const std::string &uri);
/**
* getListeningPorts()
*
* Returns a list of ports that are listening
*
* @return A vector of ports
*/
std::vector<int> getListeningPorts();
/**
* getCookie(struct mg_connection *conn, const std::string &cookieName,
*std::string &cookieValue)
*
* Puts the cookie value string that matches the cookie name in the
*cookieValue destinaton string.
*
* @param conn - the connection information
* @param cookieName - cookie name to get the value from
* @param cookieValue - cookie value is returned using thiis reference
* @returns the size of the cookie value string read.
*/
static int getCookie(struct mg_connection *conn,
const std::string &cookieName,
std::string &cookieValue);
/**
* getHeader(struct mg_connection *conn, const std::string &headerName)
* @param conn - the connection information
* @param headerName - header name to get the value from
* @returns a char array whcih contains the header value as string
*/
static const char *getHeader(struct mg_connection *conn,
const std::string &headerName);
/**
* getParam(struct mg_connection *conn, const char *, std::string &, size_t)
*
* Returns a query paramter contained in the supplied buffer. The
* occurance value is a zero-based index of a particular key name. This
* should not be confused with the index over all of the keys. Note that
*this
* function assumes that parameters are sent as text in http query string
* format, which is the default for web forms. This function will work for
* html forms with method="GET" and method="POST" attributes. In other
*cases,
* you may use a getParam version that directly takes the data instead of
*the
* connection as a first argument.
*
* @param conn - parameters are read from the data sent through this
*connection
* @param name - the key to search for
* @param dst - the destination string
* @param occurrence - the occurrence of the selected name in the query (0
*based).
* @return true if key was found
*/
static bool getParam(struct mg_connection *conn,
const char *name,
std::string &dst,
size_t occurrence = 0);
/**
* getParam(const std::string &, const char *, std::string &, size_t)
*
* Returns a query paramter contained in the supplied buffer. The
* occurance value is a zero-based index of a particular key name. This
* should not be confused with the index over all of the keys.
*
* @param data - the query string (text)
* @param name - the key to search for
* @param dst - the destination string
* @param occurrence - the occurrence of the selected name in the query (0
*based).
* @return true if key was found
*/
static bool
getParam(const std::string &data,
const char *name,
std::string &dst,
size_t occurrence = 0)
{
return getParam(data.c_str(), data.length(), name, dst, occurrence);
}
/**
* getParam(const char *, size_t, const char *, std::string &, size_t)
*
* Returns a query paramter contained in the supplied buffer. The
* occurance value is a zero-based index of a particular key name. This
* should not be confused with the index over all of the keys.
*
* @param data the - query string (text)
* @param data_len - length of the query string
* @param name - the key to search for
* @param dst - the destination string
* @param occurrence - the occurrence of the selected name in the query (0
*based).
* @return true if key was found
*/
static bool getParam(const char *data,
size_t data_len,
const char *name,
std::string &dst,
size_t occurrence = 0);
/**
* urlDecode(const std::string &, std::string &, bool)
*
* @param src - string to be decoded
* @param dst - destination string
* @param is_form_url_encoded - true if form url encoded
* form-url-encoded data differs from URI encoding in a way that it
* uses '+' as character for space, see RFC 1866 section 8.2.1
* http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
*/
static void
urlDecode(const std::string &src,
std::string &dst,
bool is_form_url_encoded = true)
{
urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
}
/**
* urlDecode(const char *, size_t, std::string &, bool)
*
* @param src - buffer to be decoded
* @param src_len - length of buffer to be decoded
* @param dst - destination string
* @param is_form_url_encoded - true if form url encoded
* form-url-encoded data differs from URI encoding in a way that it
* uses '+' as character for space, see RFC 1866 section 8.2.1
* http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
*/
static void urlDecode(const char *src,
size_t src_len,
std::string &dst,
bool is_form_url_encoded = true);
/**
* urlDecode(const char *, std::string &, bool)
*
* @param src - buffer to be decoded (0 terminated)
* @param dst - destination string
* @param is_form_url_encoded true - if form url encoded
* form-url-encoded data differs from URI encoding in a way that it
* uses '+' as character for space, see RFC 1866 section 8.2.1
* http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
*/
static void urlDecode(const char *src,
std::string &dst,
bool is_form_url_encoded = true);
/**
* urlEncode(const std::string &, std::string &, bool)
*
* @param src - buffer to be encoded
* @param dst - destination string
* @param append - true if string should not be cleared before encoding.
*/
static void
urlEncode(const std::string &src, std::string &dst, bool append = false)
{
urlEncode(src.c_str(), src.length(), dst, append);
}
/**
* urlEncode(const char *, size_t, std::string &, bool)
*
* @param src - buffer to be encoded (0 terminated)
* @param dst - destination string
* @param append - true if string should not be cleared before encoding.
*/
static void
urlEncode(const char *src, std::string &dst, bool append = false);
/**
* urlEncode(const char *, size_t, std::string &, bool)
*
* @param src - buffer to be encoded
* @param src_len - length of buffer to be decoded
* @param dst - destination string
* @param append - true if string should not be cleared before encoding.
*/
static void urlEncode(const char *src,
size_t src_len,
std::string &dst,
bool append = false);
// generic user context which can be set/read,
// the server does nothing with this apart from keep it.
const void *
getUserContext() const
{
return UserContext;
}
protected:
class CivetConnection
{
public:
char *postData;
unsigned long postDataLen;
CivetConnection();
~CivetConnection();
};
struct mg_context *context;
std::map<struct mg_connection *, class CivetConnection> connections;
// generic user context which can be set/read,
// the server does nothing with this apart from keep it.
const void *UserContext;
private:
/**
* requestHandler(struct mg_connection *, void *cbdata)
*
* Handles the incomming request.
*
* @param conn - the connection information
* @param cbdata - pointer to the CivetHandler instance.
* @returns 0 if implemented, false otherwise
*/
static int requestHandler(struct mg_connection *conn, void *cbdata);
static int webSocketConnectionHandler(const struct mg_connection *conn,
void *cbdata);
static void webSocketReadyHandler(struct mg_connection *conn, void *cbdata);
static int webSocketDataHandler(struct mg_connection *conn,
int bits,
char *data,
size_t data_len,
void *cbdata);
static void webSocketCloseHandler(const struct mg_connection *conn,
void *cbdata);
/**
* authHandler(struct mg_connection *, void *cbdata)
*
* Handles the authorization requests.
*
* @param conn - the connection information
* @param cbdata - pointer to the CivetAuthHandler instance.
* @returns 1 if authorized, 0 otherwise
*/
static int authHandler(struct mg_connection *conn, void *cbdata);
/**
* closeHandler(struct mg_connection *)
*
* Handles closing a request (internal handler)
*
* @param conn - the connection information
*/
static void closeHandler(const struct mg_connection *conn);
/**
* Stores the user provided close handler
*/
void (*userCloseHandler)(const struct mg_connection *conn);
};
#endif /* __cplusplus */
#endif /* _CIVETWEB_SERVER_H_ */
|