1 /* Copyright (c) 2013-2014 the Civetweb developers
2 * Copyright (c) 2013 No Face Press, LLC
4 * License http://opensource.org/licenses/mit-license.php MIT License
7 #ifndef _CIVETWEB_SERVER_H_
8 #define _CIVETWEB_SERVER_H_
17 // forward declaration
21 * Exception class for thrown exceptions within the CivetHandler object.
23 class CIVETWEB_API CivetException
: public std::runtime_error
26 CivetException(const std::string
&msg
) : std::runtime_error(msg
)
32 * Basic interface for a URI request handler. Handlers implementations
35 class CIVETWEB_API CivetHandler
41 virtual ~CivetHandler()
46 * Callback method for GET request.
48 * @param server - the calling server
49 * @param conn - the connection information
50 * @returns true if implemented, false otherwise
52 virtual bool handleGet(CivetServer
*server
, struct mg_connection
*conn
);
55 * Callback method for POST request.
57 * @param server - the calling server
58 * @param conn - the connection information
59 * @returns true if implemented, false otherwise
61 virtual bool handlePost(CivetServer
*server
, struct mg_connection
*conn
);
64 * Callback method for HEAD request.
66 * @param server - the calling server
67 * @param conn - the connection information
68 * @returns true if implemented, false otherwise
70 virtual bool handleHead(CivetServer
*server
, struct mg_connection
*conn
);
73 * Callback method for PUT request.
75 * @param server - the calling server
76 * @param conn - the connection information
77 * @returns true if implemented, false otherwise
79 virtual bool handlePut(CivetServer
*server
, struct mg_connection
*conn
);
82 * Callback method for DELETE request.
84 * @param server - the calling server
85 * @param conn - the connection information
86 * @returns true if implemented, false otherwise
88 virtual bool handleDelete(CivetServer
*server
, struct mg_connection
*conn
);
91 * Callback method for OPTIONS request.
93 * @param server - the calling server
94 * @param conn - the connection information
95 * @returns true if implemented, false otherwise
97 virtual bool handleOptions(CivetServer
*server
, struct mg_connection
*conn
);
100 * Callback method for PATCH request.
102 * @param server - the calling server
103 * @param conn - the connection information
104 * @returns true if implemented, false otherwise
106 virtual bool handlePatch(CivetServer
*server
, struct mg_connection
*conn
);
110 * Basic interface for a URI authorization handler. Handler implementations
113 class CIVETWEB_API CivetAuthHandler
119 virtual ~CivetAuthHandler()
124 * Callback method for authorization requests. It is up the this handler
125 * to generate 401 responses if authorization fails.
127 * @param server - the calling server
128 * @param conn - the connection information
129 * @returns true if authorization succeeded, false otherwise
131 virtual bool authorize(CivetServer
*server
, struct mg_connection
*conn
) = 0;
135 * Basic interface for a websocket handler. Handlers implementations
138 class CIVETWEB_API CivetWebSocketHandler
144 virtual ~CivetWebSocketHandler()
149 * Callback method for when the client intends to establish a websocket
150 *connection, before websocket handshake.
152 * @param server - the calling server
153 * @param conn - the connection information
154 * @returns true to keep socket open, false to close it
156 virtual bool handleConnection(CivetServer
*server
,
157 const struct mg_connection
*conn
);
160 * Callback method for when websocket handshake is successfully completed,
161 *and connection is ready for data exchange.
163 * @param server - the calling server
164 * @param conn - the connection information
166 virtual void handleReadyState(CivetServer
*server
,
167 struct mg_connection
*conn
);
170 * Callback method for when a data frame has been received from the client.
172 * @param server - the calling server
173 * @param conn - the connection information
174 * @bits: first byte of the websocket frame, see websocket RFC at
175 *http://tools.ietf.org/html/rfc6455, section 5.2
176 * @data, data_len: payload, with mask (if any) already applied.
177 * @returns true to keep socket open, false to close it
179 virtual bool handleData(CivetServer
*server
,
180 struct mg_connection
*conn
,
186 * Callback method for when the connection is closed.
188 * @param server - the calling server
189 * @param conn - the connection information
191 virtual void handleClose(CivetServer
*server
,
192 const struct mg_connection
*conn
);
198 * wrapper for mg_callbacks
200 struct CIVETWEB_API CivetCallbacks
: public mg_callbacks
{
207 * Basic class for embedded web server. This has an URL mapping built-in.
209 class CIVETWEB_API CivetServer
215 * This automatically starts the sever.
216 * It is good practice to call getContext() after this in case there
217 * were errors starting the server.
219 * Note: CivetServer should not be used as a static instance in a Windows
220 * DLL, since the constructor creates threads and the destructor joins
221 * them again (creating/joining threads should not be done in static
224 * @param options - the web server options.
225 * @param callbacks - optional web server callback methods.
227 * @throws CivetException
229 CivetServer(const char **options
,
230 const struct CivetCallbacks
*callbacks
= 0);
231 CivetServer(std::vector
<std::string
> options
,
232 const struct CivetCallbacks
*callbacks
= 0);
237 virtual ~CivetServer();
242 * Stops server and frees resources.
249 * @return the context or 0 if not running.
251 const struct mg_context
*
258 * addHandler(const std::string &, CivetHandler *)
260 * Adds a URI handler. If there is existing URI handler, it will
261 * be replaced with this one.
263 * URI's are ordered and prefix (REST) URI's are supported.
265 * @param uri - URI to match.
266 * @param handler - handler instance to use.
268 void addHandler(const std::string
&uri
, CivetHandler
*handler
);
271 addHandler(const std::string
&uri
, CivetHandler
&handler
)
273 addHandler(uri
, &handler
);
277 * addWebSocketHandler
279 * Adds a WebSocket handler for a specific URI. If there is existing URI
281 * be replaced with this one.
283 * URI's are ordered and prefix (REST) URI's are supported.
285 * @param uri - URI to match.
286 * @param handler - handler instance to use.
288 void addWebSocketHandler(const std::string
&uri
,
289 CivetWebSocketHandler
*handler
);
292 addWebSocketHandler(const std::string
&uri
, CivetWebSocketHandler
&handler
)
294 addWebSocketHandler(uri
, &handler
);
298 * removeHandler(const std::string &)
302 * @param uri - the exact URL used in addHandler().
304 void removeHandler(const std::string
&uri
);
307 * removeWebSocketHandler(const std::string &)
309 * Removes a web socket handler.
311 * @param uri - the exact URL used in addWebSocketHandler().
313 void removeWebSocketHandler(const std::string
&uri
);
316 * addAuthHandler(const std::string &, CivetAuthHandler *)
318 * Adds a URI authorization handler. If there is existing URI authorization
319 * handler, it will be replaced with this one.
321 * URI's are ordered and prefix (REST) URI's are supported.
323 * @param uri - URI to match.
324 * @param handler - authorization handler instance to use.
326 void addAuthHandler(const std::string
&uri
, CivetAuthHandler
*handler
);
329 addAuthHandler(const std::string
&uri
, CivetAuthHandler
&handler
)
331 addAuthHandler(uri
, &handler
);
335 * removeAuthHandler(const std::string &)
337 * Removes an authorization handler.
339 * @param uri - the exact URL used in addAuthHandler().
341 void removeAuthHandler(const std::string
&uri
);
344 * getListeningPorts()
346 * Returns a list of ports that are listening
348 * @return A vector of ports
351 std::vector
<int> getListeningPorts();
354 * getCookie(struct mg_connection *conn, const std::string &cookieName,
355 *std::string &cookieValue)
357 * Puts the cookie value string that matches the cookie name in the
358 *cookieValue destinaton string.
360 * @param conn - the connection information
361 * @param cookieName - cookie name to get the value from
362 * @param cookieValue - cookie value is returned using thiis reference
363 * @returns the size of the cookie value string read.
365 static int getCookie(struct mg_connection
*conn
,
366 const std::string
&cookieName
,
367 std::string
&cookieValue
);
370 * getHeader(struct mg_connection *conn, const std::string &headerName)
371 * @param conn - the connection information
372 * @param headerName - header name to get the value from
373 * @returns a char array whcih contains the header value as string
375 static const char *getHeader(struct mg_connection
*conn
,
376 const std::string
&headerName
);
379 * getParam(struct mg_connection *conn, const char *, std::string &, size_t)
381 * Returns a query paramter contained in the supplied buffer. The
382 * occurance value is a zero-based index of a particular key name. This
383 * should not be confused with the index over all of the keys. Note that
385 * function assumes that parameters are sent as text in http query string
386 * format, which is the default for web forms. This function will work for
387 * html forms with method="GET" and method="POST" attributes. In other
389 * you may use a getParam version that directly takes the data instead of
391 * connection as a first argument.
393 * @param conn - parameters are read from the data sent through this
395 * @param name - the key to search for
396 * @param dst - the destination string
397 * @param occurrence - the occurrence of the selected name in the query (0
399 * @return true if key was found
401 static bool getParam(struct mg_connection
*conn
,
404 size_t occurrence
= 0);
407 * getParam(const std::string &, const char *, std::string &, size_t)
409 * Returns a query paramter contained in the supplied buffer. The
410 * occurance value is a zero-based index of a particular key name. This
411 * should not be confused with the index over all of the keys.
413 * @param data - the query string (text)
414 * @param name - the key to search for
415 * @param dst - the destination string
416 * @param occurrence - the occurrence of the selected name in the query (0
418 * @return true if key was found
421 getParam(const std::string
&data
,
424 size_t occurrence
= 0)
426 return getParam(data
.c_str(), data
.length(), name
, dst
, occurrence
);
430 * getParam(const char *, size_t, const char *, std::string &, size_t)
432 * Returns a query paramter contained in the supplied buffer. The
433 * occurance value is a zero-based index of a particular key name. This
434 * should not be confused with the index over all of the keys.
436 * @param data the - query string (text)
437 * @param data_len - length of the query string
438 * @param name - the key to search for
439 * @param dst - the destination string
440 * @param occurrence - the occurrence of the selected name in the query (0
442 * @return true if key was found
444 static bool getParam(const char *data
,
448 size_t occurrence
= 0);
451 * urlDecode(const std::string &, std::string &, bool)
453 * @param src - string to be decoded
454 * @param dst - destination string
455 * @param is_form_url_encoded - true if form url encoded
456 * form-url-encoded data differs from URI encoding in a way that it
457 * uses '+' as character for space, see RFC 1866 section 8.2.1
458 * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
461 urlDecode(const std::string
&src
,
463 bool is_form_url_encoded
= true)
465 urlDecode(src
.c_str(), src
.length(), dst
, is_form_url_encoded
);
469 * urlDecode(const char *, size_t, std::string &, bool)
471 * @param src - buffer to be decoded
472 * @param src_len - length of buffer to be decoded
473 * @param dst - destination string
474 * @param is_form_url_encoded - true if form url encoded
475 * form-url-encoded data differs from URI encoding in a way that it
476 * uses '+' as character for space, see RFC 1866 section 8.2.1
477 * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
479 static void urlDecode(const char *src
,
482 bool is_form_url_encoded
= true);
485 * urlDecode(const char *, std::string &, bool)
487 * @param src - buffer to be decoded (0 terminated)
488 * @param dst - destination string
489 * @param is_form_url_encoded true - if form url encoded
490 * form-url-encoded data differs from URI encoding in a way that it
491 * uses '+' as character for space, see RFC 1866 section 8.2.1
492 * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
494 static void urlDecode(const char *src
,
496 bool is_form_url_encoded
= true);
499 * urlEncode(const std::string &, std::string &, bool)
501 * @param src - buffer to be encoded
502 * @param dst - destination string
503 * @param append - true if string should not be cleared before encoding.
506 urlEncode(const std::string
&src
, std::string
&dst
, bool append
= false)
508 urlEncode(src
.c_str(), src
.length(), dst
, append
);
512 * urlEncode(const char *, size_t, std::string &, bool)
514 * @param src - buffer to be encoded (0 terminated)
515 * @param dst - destination string
516 * @param append - true if string should not be cleared before encoding.
519 urlEncode(const char *src
, std::string
&dst
, bool append
= false);
522 * urlEncode(const char *, size_t, std::string &, bool)
524 * @param src - buffer to be encoded
525 * @param src_len - length of buffer to be decoded
526 * @param dst - destination string
527 * @param append - true if string should not be cleared before encoding.
529 static void urlEncode(const char *src
,
532 bool append
= false);
535 class CivetConnection
539 unsigned long postDataLen
;
545 struct mg_context
*context
;
546 std::map
<struct mg_connection
*, class CivetConnection
> connections
;
550 * requestHandler(struct mg_connection *, void *cbdata)
552 * Handles the incomming request.
554 * @param conn - the connection information
555 * @param cbdata - pointer to the CivetHandler instance.
556 * @returns 0 if implemented, false otherwise
558 static int requestHandler(struct mg_connection
*conn
, void *cbdata
);
560 static int webSocketConnectionHandler(const struct mg_connection
*conn
,
562 static void webSocketReadyHandler(struct mg_connection
*conn
, void *cbdata
);
563 static int webSocketDataHandler(struct mg_connection
*conn
,
568 static void webSocketCloseHandler(const struct mg_connection
*conn
,
571 * authHandler(struct mg_connection *, void *cbdata)
573 * Handles the authorization requests.
575 * @param conn - the connection information
576 * @param cbdata - pointer to the CivetAuthHandler instance.
577 * @returns 1 if authorized, 0 otherwise
579 static int authHandler(struct mg_connection
*conn
, void *cbdata
);
582 * closeHandler(struct mg_connection *)
584 * Handles closing a request (internal handler)
586 * @param conn - the connection information
588 static void closeHandler(const struct mg_connection
*conn
);
591 * Stores the user provided close handler
593 void (*userCloseHandler
)(const struct mg_connection
*conn
);
596 #endif /* __cplusplus */
597 #endif /* _CIVETWEB_SERVER_H_ */