Ulfius
HTTP Framework for REST Applications in C
ulfius.h
Go to the documentation of this file.
1 
27 #ifndef __ULFIUS_H__
28 #define __ULFIUS_H__
29 
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #endif
34 
35 #include "ulfius-cfg.h"
36 
39 #ifndef U_DISABLE_GNUTLS
40  #ifndef _GNU_SOURCE
41  #define _GNU_SOURCE
42  #endif
43  #include <gnutls/gnutls.h>
44  #include <gnutls/x509.h>
45 #endif
46 
47 #ifndef U_DISABLE_WEBSOCKET
48  #include <poll.h>
49  #include <zlib.h>
50  #ifndef POLLRDHUP
51  #define POLLRDHUP 0x2000
52  #endif
53 #endif
54 
55 #include <pthread.h>
56 #include <microhttpd.h>
57 
58 #if defined(_WIN32) && !defined(U_DISABLE_WEBSOCKET)
59  #define U_DISABLE_WEBSOCKET
60 #endif
61 
62 #if (MHD_VERSION < 0x00095300) && !defined(U_DISABLE_WEBSOCKET)
63  #define U_DISABLE_WEBSOCKET
64 #endif
65 
67 #include <orcania.h>
68 
70 #ifndef U_DISABLE_YDER
71  #include <yder.h>
72 #else
73 
74 #define Y_LOG_MODE_NONE 0
75 #define Y_LOG_MODE_CONSOLE 0
76 #define Y_LOG_MODE_SYSLOG 0
77 #define Y_LOG_MODE_FILE 0
78 #define Y_LOG_MODE_JOURNALD 0
79 #define Y_LOG_MODE_CALLBACK 0
80 #define Y_LOG_MODE_CURRENT 0
81 
82 #define Y_LOG_LEVEL_NONE 0
83 #define Y_LOG_LEVEL_DEBUG 0
84 #define Y_LOG_LEVEL_INFO 0
85 #define Y_LOG_LEVEL_WARNING 0
86 #define Y_LOG_LEVEL_ERROR 0
87 #define Y_LOG_LEVEL_CURRENT 0
88 
89 int y_init_logs(const char * app, const unsigned long init_mode, const unsigned long init_level, const char * init_log_file, const char * message);
90 int y_set_logs_callback(void (* y_callback_log_message) (void * cls, const char * app_name, const time_t date, const unsigned long level, const char * message), void * cls, const char * message);
91 void y_log_message(const unsigned long type, const char * message, ...);
92 int y_close_logs();
93 #endif
94 
95 #ifndef U_DISABLE_JANSSON
96 #include <jansson.h>
97 #endif
98 
104 #define ULFIUS_STREAM_BLOCK_SIZE_DEFAULT 1024
105 #define U_STREAM_END MHD_CONTENT_READER_END_OF_STREAM
106 #define U_STREAM_ERROR MHD_CONTENT_READER_END_WITH_ERROR
107 #define U_STREAM_SIZE_UNKNOWN MHD_SIZE_UNKNOWN
108 #define U_STREAM_SIZE_UNKOWN U_STREAM_SIZE_UNKNOWN // Backward compatibility
109 
110 #define U_OK 0
111 #define U_ERROR 1
112 #define U_ERROR_MEMORY 2
113 #define U_ERROR_PARAMS 3
114 #define U_ERROR_LIBMHD 4
115 #define U_ERROR_LIBCURL 5
116 #define U_ERROR_NOT_FOUND 6
117 #define U_ERROR_DISCONNECTED 7
118 
119 #define U_CALLBACK_CONTINUE 0
120 #define U_CALLBACK_IGNORE 1
121 #define U_CALLBACK_COMPLETE 2
122 #define U_CALLBACK_UNAUTHORIZED 3
123 #define U_CALLBACK_ERROR 4
124 
125 #define U_COOKIE_SAME_SITE_NONE 0
126 #define U_COOKIE_SAME_SITE_STRICT 1
127 #define U_COOKIE_SAME_SITE_LAX 2
128 
129 #define U_USE_IPV4 0x0001
130 #define U_USE_IPV6 0x0010
131 #define U_USE_ALL (U_USE_IPV4|U_USE_IPV6)
132 
133 #define U_SSL_VERIFY_PEER 0x0001
134 #define U_SSL_VERIFY_HOSTNAME 0x0010
135 
140 typedef enum {
145 #if MHD_VERSION >= 0x00095208
146  U_OPT_NETWORK_TYPE = 4,
147 #endif
168 #ifndef U_DISABLE_JANSSON
170 #endif
171 #ifndef U_DISABLE_GNUTLS
175 #endif
181 
186 /*************
187  * Structures
188  *************/
189 
199 struct _u_map {
200  int nb_values; /* !< Values count */
201  char ** keys; /* !< Array of keys */
202  char ** values; /* !< Array of values */
203  size_t * lengths; /* !< Lengths of each values */
204 };
205 
210 struct _u_cookie {
211  char * key; /* !< key if the cookie */
212  char * value; /* !< value of the cookie */
213  char * expires; /* !< expiration date of the cookie */
214  unsigned int max_age; /* !< duration of the cookie in seconds */
215  char * domain; /* !< domain for the cookie */
216  char * path; /* !< url path for the cookie */
217  int secure; /* !< flag to set cookie secure or not */
218  int http_only; /* !< flag to set cookie for HTTP connections only or not */
219  int same_site; /* !< flag to set same_site option to the cookie */
220 };
221 
228 struct _u_request {
229  char * http_protocol; /* !< http protocol used (1.0 or 1.1) */
230  char * http_verb; /* !< http method (GET, POST, PUT, DELETE, etc.) */
231  char * http_url; /* !< full url used to call this callback function or full url to call when used in a ulfius_send_http_request */
232  char * url_path; /* !< url path only used to call this callback function (ex, if http_url is /path/?param=1, url_path is /path/) */
233  char * proxy; /* !<proxy address to use for outgoing connections, used by ulfius_send_http_request */
234 #if MHD_VERSION >= 0x00095208
235  unsigned short network_type; /* !< Force connect to ipv4, ipv6 addresses or both, values available are U_USE_ALL, U_USE_IPV4 or U_USE_IPV6 */
236 #endif
237  int check_server_certificate; /* !< check server certificate and hostname, default true, used by ulfius_send_http_request */
238  int check_server_certificate_flag; /* !< check certificate peer and or server hostname if check_server_certificate is enabled, values available are U_SSL_VERIFY_PEER, U_SSL_VERIFY_HOSTNAME or both, default value is both (U_SSL_VERIFY_PEER|U_SSL_VERIFY_HOSTNAME), used by ulfius_send_http_request */
239  int check_proxy_certificate; /* !< check proxy certificate and hostname, default true, used by ulfius_send_http_request, requires libcurl >= 7.52 */
240  int check_proxy_certificate_flag; /* !< check certificate peer and or proxy hostname if check_proxy_certificate is enabled, values available are U_SSL_VERIFY_PEER, U_SSL_VERIFY_HOSTNAME or both, default value is both (U_SSL_VERIFY_PEER|U_SSL_VERIFY_HOSTNAME), used by ulfius_send_http_request, requires libcurl >= 7.52 */
241  int follow_redirect; /* !< follow url redirections, used by ulfius_send_http_request */
242  char * ca_path; /* !< specify a path to CA certificates instead of system path, used by ulfius_send_http_request */
243  unsigned long timeout; /* !< connection timeout used by ulfius_send_http_request, default is 0 */
244  struct sockaddr * client_address; /* !< IP address of the client */
245  char * auth_basic_user; /* !< basic authentication username */
246  char * auth_basic_password; /* !< basic authentication password */
247  struct _u_map * map_url; /* !< map containing the url variables, both from the route and the ?key=value variables */
248  struct _u_map * map_header; /* !< map containing the header variables */
249  struct _u_map * map_cookie; /* !< map containing the cookie variables */
250  struct _u_map * map_post_body; /* !< map containing the post body variables (if available) */
251  void * binary_body; /* !< raw body */
252  size_t binary_body_length; /* !< length of raw body */
253  unsigned int callback_position; /* !< position of the current callback function in the callback list, starts at 0 */
254 #ifndef U_DISABLE_GNUTLS
255  gnutls_x509_crt_t client_cert; /* !< x509 certificate of the client if the instance uses client certificate authentication and the client is authenticated, available only if GnuTLS support is enabled */
256  char * client_cert_file; /* !< path to client certificate file for sending http requests with certificate authentication, available only if GnuTLS support is enabled */
257  char * client_key_file; /* !< path to client key file for sending http requests with certificate authentication, available only if GnuTLS support is enabled */
258  char * client_key_password; /* !< password to unlock client key file, available only if GnuTLS support is enabled */
259 #endif
260 };
261 
268 struct _u_response {
269  long status; /* !< HTTP status code (200, 404, 500, etc) */
270  char * protocol; /* !< HTTP Protocol sent */
271  struct _u_map * map_header; /* !< map containing the header variables */
272  unsigned int nb_cookies; /* !< number of cookies sent */
273  struct _u_cookie * map_cookie; /* !< array of cookies sent */
274  char * auth_realm; /* !< realm to send to the client on authenticationb failed */
275  void * binary_body; /* !< raw binary content */
276  size_t binary_body_length; /* !< length of the binary_body */
277  ssize_t (* stream_callback) (void * stream_user_data, uint64_t offset, char * out_buf, size_t max); /* !< callback function to stream data in response body */
278  void (* stream_callback_free) (void * stream_user_data); /* !< callback function to free data allocated for streaming */
279  uint64_t stream_size; /* !< size of the streamed data (U_STREAM_SIZE_UNKNOWN if unknown) */
280  size_t stream_block_size; /* !< size of each block to be streamed, set according to your system */
281  void * stream_user_data; /* !< user defined data that will be available in your callback stream functions */
282  void * websocket_handle; /* !< handle for websocket extension */
283  void * shared_data; /* !< any data shared between callback functions, must be allocated and freed by the callback functions */
284  void (* free_shared_data)(void * shared_data); /* !< pointer to a function that will free shared_data */
285  unsigned int timeout; /* !< Timeout in seconds to close the connection because of inactivity between the client and the server */
286 };
287 
294 struct _u_endpoint {
295  char * http_method; /* !< http verb (GET, POST, PUT, etc.) in upper case */
296  char * url_prefix; /* !< prefix for the url (optional) */
297  char * url_format; /* !< string used to define the endpoint format, separate words with / to define a variable in the url, prefix it with @ or :, example: /test/resource/:name/elements, on an url_format that ends with '*', the rest of the url will not be tested */
298  unsigned int priority; /* !< endpoint priority in descending order (0 is the higher priority) */
299  int (* callback_function)(const struct _u_request * request, /* !< pointer to a function that will be executed each time the endpoint is called, you must declare the function as described. */
300  struct _u_response * response,
301  void * user_data);
302  void * user_data; /* !< pointer to a data or a structure that will be available in callback_function */
303 };
304 
311 struct _u_instance {
312  struct MHD_Daemon * mhd_daemon; /* !< pointer to the libmicrohttpd daemon */
313  int status; /* !< status of the current instance, status are U_STATUS_STOP, U_STATUS_RUNNING or U_STATUS_ERROR */
314  unsigned int port; /* !< port number to listen to */
315 #if MHD_VERSION >= 0x00095208
316  unsigned short network_type; /* !< Listen to ipv4 and or ipv6 connections, values available are U_USE_ALL, U_USE_IPV4 or U_USE_IPV6 */
317 #endif
318  struct sockaddr_in * bind_address; /* !< ipv4 address to listen to (optional) */
319  struct sockaddr_in6 * bind_address6; /* !< ipv6 address to listen to (optional) */
320  unsigned int timeout; /* !< Timeout to close the connection because of inactivity between the client and the server */
321  int nb_endpoints; /* !< Number of available endpoints */
322  char * default_auth_realm; /* !< Default realm on authentication error */
323  struct _u_endpoint * endpoint_list; /* !< List of available endpoints */
324  struct _u_endpoint * default_endpoint; /* !< Default endpoint if no other endpoint match the current url */
325  struct _u_map * default_headers; /* !< Default headers that will be added to all response->map_header */
326  size_t max_post_param_size; /* !< maximum size for a post parameter, 0 means no limit, default 0 */
327  size_t max_post_body_size; /* !< maximum size for the entire post body, 0 means no limit, default 0 */
328  void * websocket_handler; /* !< handler for the websocket structure */
329  int (* file_upload_callback) (const struct _u_request * request, /* !< callback function to manage file upload by blocks */
330  const char * key,
331  const char * filename,
332  const char * content_type,
333  const char * transfer_encoding,
334  const char * data,
335  uint64_t off,
336  size_t size,
337  void * cls);
338  void * file_upload_cls; /* !< any pointer to pass to the file_upload_callback function */
339  int mhd_response_copy_data; /* !< to choose between MHD_RESPMEM_MUST_COPY and MHD_RESPMEM_MUST_FREE, only if you use MHD < 0.9.61, otherwise this option is skipped because it's useless */
340  int check_utf8; /* !< check that all parameters values in the request (url, header and post_body), are valid utf8 strings, if a parameter value has non utf8 character, the value, will be ignored, default 1 */
341 #ifndef U_DISABLE_GNUTLS
342  int use_client_cert_auth; /* !< Internal variable use to indicate if the instance uses client certificate authentication, Do not change this value, available only if websocket support is enabled */
343 #endif
344 };
345 
355  struct MHD_PostProcessor * post_processor;
358  struct _u_request * request;
360  struct _u_map map_url_initial;
361 };
362 
363 /**********************************
364  * Instance functions declarations
365  **********************************/
366 
377 void u_free(void * data);
378 
386 int ulfius_global_init(void);
387 
391 void ulfius_global_close(void);
392 
414 int ulfius_init_instance(struct _u_instance * u_instance, unsigned int port, struct sockaddr_in * bind_address, const char * default_auth_realm);
415 
416 #if MHD_VERSION >= 0x00095208
428 int ulfius_init_instance_ipv6(struct _u_instance * u_instance, unsigned int port, struct sockaddr_in6 * bind_address, unsigned short network_type, const char * default_auth_realm);
429 #endif
430 
437 void ulfius_clean_instance(struct _u_instance * u_instance);
438 
446 int ulfius_start_framework(struct _u_instance * u_instance);
447 
457 int ulfius_start_secure_framework(struct _u_instance * u_instance, const char * key_pem, const char * cert_pem);
458 
459 #ifndef U_DISABLE_GNUTLS
471 int ulfius_start_secure_ca_trust_framework(struct _u_instance * u_instance, const char * key_pem, const char * cert_pem, const char * root_ca_pem);
472 #endif
473 
492 int ulfius_start_framework_with_mhd_options(struct _u_instance * u_instance, unsigned int mhd_flags, struct MHD_OptionItem * options);
493 
497 void mhd_request_completed (void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe);
498 void * ulfius_uri_logger (void * cls, const char * uri);
499 
507 int ulfius_stop_framework(struct _u_instance * u_instance);
508 
530  int (* file_upload_callback) (const struct _u_request * request,
531  const char * key,
532  const char * filename,
533  const char * content_type,
534  const char * transfer_encoding,
535  const char * data,
536  uint64_t off,
537  size_t size,
538  void * cls),
539  void * cls);
540 
551 /***********************************
552  * Endpoints functions declarations
553  ***********************************/
554 
562 int ulfius_add_endpoint(struct _u_instance * u_instance, const struct _u_endpoint * u_endpoint);
563 
581 int ulfius_add_endpoint_by_val(struct _u_instance * u_instance,
582  const char * http_method,
583  const char * url_prefix,
584  const char * url_format,
585  unsigned int priority,
586  int (* callback_function)(const struct _u_request * request, // Input parameters (set by the framework)
587  struct _u_response * response, // Output parameters (set by the user)
588  void * user_data),
589  void * user_data);
590 
598 int ulfius_add_endpoint_list(struct _u_instance * u_instance, const struct _u_endpoint ** u_endpoint_list);
599 
609 int ulfius_remove_endpoint(struct _u_instance * u_instance, const struct _u_endpoint * u_endpoint);
610 
622 int ulfius_set_default_endpoint(struct _u_instance * u_instance,
623  int (* callback_function)(const struct _u_request * request, struct _u_response * response, void * user_data),
624  void * user_data);
625 
638 int ulfius_remove_endpoint_by_val(struct _u_instance * u_instance, const char * http_method, const char * url_prefix, const char * url_format);
639 
644 const struct _u_endpoint * ulfius_empty_endpoint(void);
645 
653 int ulfius_copy_endpoint(struct _u_endpoint * dest, const struct _u_endpoint * source);
654 
662 struct _u_endpoint * ulfius_duplicate_endpoint_list(const struct _u_endpoint * endpoint_list);
663 
669 void ulfius_clean_endpoint(struct _u_endpoint * endpoint);
670 
676 void ulfius_clean_endpoint_list(struct _u_endpoint * endpoint_list);
677 
685 int ulfius_equals_endpoints(const struct _u_endpoint * endpoint1, const struct _u_endpoint * endpoint2);
686 
697 #ifndef U_DISABLE_CURL
698 /********************************************
699  * Requests/Responses functions declarations
700  ********************************************/
701 
709 int ulfius_send_http_request(const struct _u_request * request, struct _u_response * response);
710 
721 int ulfius_send_http_streaming_request(const struct _u_request * request, struct _u_response * response, size_t (* write_body_function)(void * contents, size_t size, size_t nmemb, void * user_data), void * write_body_data);
722 
741 int ulfius_send_smtp_email(const char * host,
742  const int port,
743  const int use_tls,
744  const int verify_certificate,
745  const char * user,
746  const char * password,
747  const char * from,
748  const char * to,
749  const char * cc,
750  const char * bcc,
751  const char * subject,
752  const char * mail_body);
753 
773 int ulfius_send_smtp_rich_email(const char * host,
774  const int port,
775  const int use_tls,
776  const int verify_certificate,
777  const char * user,
778  const char * password,
779  const char * from,
780  const char * to,
781  const char * cc,
782  const char * bcc,
783  const char * content_type,
784  const char * subject,
785  const char * mail_body);
786 #endif
787 
812 int ulfius_add_cookie_to_response(struct _u_response * response, const char * key, const char * value, const char * expires, const unsigned int max_age,
813  const char * domain, const char * path, const int secure, const int http_only);
814 
833 int ulfius_add_same_site_cookie_to_response(struct _u_response * response, const char * key, const char * value, const char * expires, const unsigned int max_age,
834  const char * domain, const char * path, const int secure, const int http_only, const int same_site);
835 
854 int ulfius_add_header_to_response(struct _u_response * response, const char * key, const char * value);
855 
863 int ulfius_set_string_body_request(struct _u_request * request, const char * string_body);
864 
873 int ulfius_set_binary_body_request(struct _u_request * request, const char * binary_body, const size_t length);
874 
881 int ulfius_set_empty_body_request(struct _u_request * request);
882 
891 int ulfius_set_string_body_response(struct _u_response * response, const unsigned int status, const char * body);
892 
902 int ulfius_set_binary_body_response(struct _u_response * response, const unsigned int status, const char * body, const size_t length);
903 
911 int ulfius_set_empty_body_response(struct _u_response * response, const unsigned int status);
912 
935 int ulfius_set_stream_response(struct _u_response * response,
936  const unsigned int status,
937  ssize_t (* stream_callback) (void * stream_user_data, uint64_t offset, char * out_buf, size_t max),
938  void (* stream_callback_free) (void * stream_user_data),
939  uint64_t stream_size,
940  size_t stream_block_size,
941  void * stream_user_data);
942 
959 int ulfius_init_request(struct _u_request * request);
960 
969 int ulfius_clean_request(struct _u_request * request);
970 
977 int ulfius_clean_request_full(struct _u_request * request);
978 
986 int ulfius_copy_request(struct _u_request * dest, const struct _u_request * source);
987 
993 int ulfius_set_request_properties(struct _u_request * request, ...);
994 
1000 int ulfius_init_response(struct _u_response * response);
1001 
1010 int ulfius_clean_response(struct _u_response * response);
1011 
1017 int ulfius_clean_response_full(struct _u_response * response);
1018 
1025 int ulfius_copy_response(struct _u_response * dest, const struct _u_response * source);
1026 
1033 int ulfius_clean_cookie(struct _u_cookie * cookie);
1034 
1041 int ulfius_copy_cookie(struct _u_cookie * dest, const struct _u_cookie * source);
1042 
1049 struct _u_request * ulfius_duplicate_request(const struct _u_request * request);
1050 
1057 struct _u_response * ulfius_duplicate_response(const struct _u_response * response);
1058 
1064 int ulfius_set_response_properties(struct _u_response * response, ...);
1065 
1074 int ulfius_set_response_shared_data(struct _u_response * response, void * shared_data, void (* free_shared_data) (void * shared_data));
1075 
1094 char * ulfius_url_decode(const char * str);
1095 
1104 char * ulfius_url_encode(const char * str);
1105 
1116 #ifndef U_DISABLE_JANSSON
1127 json_t * ulfius_get_json_body_request(const struct _u_request * request, json_error_t * json_error);
1128 
1136 int ulfius_set_json_body_request(struct _u_request * request, json_t * j_body);
1137 
1148 json_t * ulfius_get_json_body_response(struct _u_response * response, json_error_t * json_error);
1149 
1158 int ulfius_set_json_body_response(struct _u_response * response, const unsigned int status, const json_t * j_body);
1159 #endif
1160 
1171 /************************************************************************
1172  * _u_map declarations *
1173  * _u_map is a simple map structure that handles sets of key/value maps *
1174  ************************************************************************/
1175 
1182 int u_map_init(struct _u_map * u_map);
1183 
1189 int u_map_clean(struct _u_map * u_map);
1190 
1196 int u_map_clean_full(struct _u_map * u_map);
1197 
1203 int u_map_clean_enum(char ** array);
1204 
1210 const char ** u_map_enum_keys(const struct _u_map * u_map);
1211 
1217 const char ** u_map_enum_values(const struct _u_map * u_map);
1218 
1227 int u_map_has_key(const struct _u_map * u_map, const char * key);
1228 
1237 int u_map_has_value(const struct _u_map * u_map, const char * value);
1238 
1248 int u_map_has_value_binary(const struct _u_map * u_map, const char * value, size_t length);
1249 
1258 int u_map_has_key_case(const struct _u_map * u_map, const char * key);
1259 
1268 int u_map_has_value_case(const struct _u_map * u_map, const char * value);
1269 
1278 int u_map_put(struct _u_map * u_map, const char * key, const char * value);
1279 
1291 int u_map_put_binary(struct _u_map * u_map, const char * key, const char * value, uint64_t offset, size_t length);
1292 
1300 ssize_t u_map_get_length(const struct _u_map * u_map, const char * key);
1301 
1309 ssize_t u_map_get_case_length(const struct _u_map * u_map, const char * key);
1310 
1318 const char * u_map_get(const struct _u_map * u_map, const char * key);
1319 
1327 const char * u_map_get_case(const struct _u_map * u_map, const char * key);
1328 
1336 int u_map_remove_from_key(struct _u_map * u_map, const char * key);
1337 
1345 int u_map_remove_from_key_case(struct _u_map * u_map, const char * key);
1346 
1354 int u_map_remove_from_value(struct _u_map * u_map, const char * value);
1355 
1363 int u_map_remove_from_value_case(struct _u_map * u_map, const char * value);
1364 
1372 int u_map_remove_from_value_binary(struct _u_map * u_map, const char * key, size_t length);
1373 
1380 int u_map_remove_at(struct _u_map * u_map, const int index);
1381 
1388 struct _u_map * u_map_copy(const struct _u_map * source);
1389 
1397 int u_map_copy_into(struct _u_map * dest, const struct _u_map * source);
1398 
1405 int u_map_count(const struct _u_map * source);
1406 
1412 int u_map_empty(struct _u_map * u_map);
1413 
1424 #ifndef U_DISABLE_WEBSOCKET
1425 
1426 /**********************************
1427  * Websocket functions declarations
1428  **********************************/
1429 
1430 #define U_WEBSOCKET_USER_AGENT "Ulfius Websocket Client Framework"
1431 
1432 #define U_WEBSOCKET_MAGIC_STRING "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
1433 #define U_WEBSOCKET_UPGRADE_VALUE "websocket"
1434 #define U_WEBSOCKET_BAD_REQUEST_BODY "Error in websocket handshake, wrong parameters"
1435 #define U_WEBSOCKET_USEC_WAIT 50
1436 #define WEBSOCKET_MAX_CLOSE_TRY 10
1437 
1438 #define U_WEBSOCKET_BIT_FIN 0x80
1439 #define U_WEBSOCKET_MASK 0x80
1440 #define U_WEBSOCKET_LEN_MASK 0x7F
1441 #define U_WEBSOCKET_OPCODE_CONTINUE 0x00
1442 #define U_WEBSOCKET_OPCODE_TEXT 0x01
1443 #define U_WEBSOCKET_OPCODE_BINARY 0x02
1444 #define U_WEBSOCKET_OPCODE_CLOSE 0x08
1445 #define U_WEBSOCKET_OPCODE_PING 0x09
1446 #define U_WEBSOCKET_OPCODE_PONG 0x0A
1447 #define U_WEBSOCKET_OPCODE_CLOSED 0xFD
1448 #define U_WEBSOCKET_OPCODE_ERROR 0xFE
1449 #define U_WEBSOCKET_OPCODE_NONE 0xFF
1450 
1451 #define U_WEBSOCKET_NONE 0
1452 #define U_WEBSOCKET_SERVER 1
1453 #define U_WEBSOCKET_CLIENT 2
1454 
1455 #define U_WEBSOCKET_STATUS_OPEN 0
1456 #define U_WEBSOCKET_STATUS_CLOSE 1
1457 #define U_WEBSOCKET_STATUS_ERROR 2
1458 
1459 #define U_WEBSOCKET_RSV1 0x40
1460 #define U_WEBSOCKET_RSV2 0x20
1461 #define U_WEBSOCKET_RSV3 0x10
1462 
1463 #define WEBSOCKET_RESPONSE_HTTP 0x0001
1464 #define WEBSOCKET_RESPONSE_UPGRADE 0x0002
1465 #define WEBSOCKET_RESPONSE_CONNECTION 0x0004
1466 #define WEBSOCKET_RESPONSE_ACCEPT 0x0008
1467 #define WEBSOCKET_RESPONSE_PROTCOL 0x0010
1468 #define WEBSOCKET_RESPONSE_EXTENSION 0x0020
1469 
1470 #define WEBSOCKET_DEFLATE_CHUNK_SIZE 32768
1471 #define WEBSOCKET_DEFLATE_WINDOWS_BITS 15
1472 
1473 #define U_WEBSOCKET_KEEP_NONE 0x00
1474 #define U_WEBSOCKET_KEEP_INCOMING 0x01
1475 #define U_WEBSOCKET_KEEP_OUTCOMING 0x10
1476 
1480 struct _websocket_deflate_context {
1481  z_stream infstream;
1482  z_stream defstream;
1483  int deflate_mask;
1484  int inflate_mask;
1485  uint server_no_context_takeover;
1486  uint client_no_context_takeover;
1487  uint server_max_window_bits;
1488  uint client_max_window_bits;
1489 };
1490 
1495 struct _websocket_extension {
1496  char * extension_server;
1497  char * extension_client;
1498  uint8_t rsv;
1499  int (* websocket_extension_message_out_perform)(const uint8_t opcode,
1500  const uint64_t data_len_in,
1501  const char * data_in,
1502  uint64_t * data_len_out,
1503  char ** data_out,
1504  const uint64_t fragment_len,
1505  void * user_data,
1506  void * context);
1507  void * websocket_extension_message_out_perform_user_data;
1508  int (* websocket_extension_message_in_perform)(const uint8_t opcode,
1509  const uint64_t data_len_in,
1510  const char * data_in,
1511  uint64_t * data_len_out,
1512  char ** data_out,
1513  const uint64_t fragment_len,
1514  void * user_data,
1515  void * context);
1516  void * websocket_extension_message_in_perform_user_data;
1517  int (* websocket_extension_server_match)(const char * extension_client,
1518  const char ** extension_client_list,
1519  char ** extension_server,
1520  void * user_data,
1521  void ** context);
1522  void * websocket_extension_server_match_user_data;
1523  int (* websocket_extension_client_match)(const char * extension_server,
1524  void * user_data,
1525  void ** context);
1526  void * websocket_extension_client_match_user_data;
1527  void (* websocket_extension_free_context)(void * user_data,
1528  void * context);
1529  void * websocket_extension_free_context_user_data;
1530  int enabled;
1531  void * context;
1532 };
1533 
1540 struct _websocket_manager {
1541  struct _websocket_message_list * message_list_incoming; /* !< list of incoming messages */
1542  struct _websocket_message_list * message_list_outcoming; /* !< list of outcoming messages */
1543  int keep_messages; /* !< keep incoming and/or outcoming messages, flags available are U_WEBSOCKET_KEEP_INCOMING, U_WEBSOCKET_KEEP_OUTCOMING, U_WEBSOCKET_KEEP_NONE, default is U_WEBSOCKET_KEEP_INCOMING|U_WEBSOCKET_KEEP_OUTCOMING */
1544  int connected; /* !< flag to know if the websocket is connected or not */
1545  int ping_sent; /* !< flag to know if the websocket has sent a ping frame or not, before receiving a pong */
1546  int close_flag; /* !< flag to set before closing a websocket */
1547  MHD_socket mhd_sock; /* !< reference to libmicrohttpd's socket for websocket server */
1548  int tcp_sock; /* !< tcp socket for websocket client */
1549  int tls; /* !< set to 1 if the websocket is in a TLS socket */
1550  gnutls_session_t gnutls_session; /* !< GnuTLS session for websocket client */
1551  gnutls_certificate_credentials_t xcred; /* !< certificate credential used by GnuTLS */
1552  char * protocol; /* !< websocket protocol */
1553  char * extensions; /* !< websocket extension */
1554  pthread_mutex_t read_lock; /* !< mutex to read data in the socket */
1555  pthread_mutex_t write_lock; /* !< mutex to write data in the socket */
1556  pthread_mutex_t status_lock; /* !< mutex to broadcast new status */
1557  pthread_cond_t status_cond; /* !< condition to broadcast new status */
1558  struct pollfd fds_in;
1559  struct pollfd fds_out;
1560  int type;
1561  int rsv_expected;
1562  struct _pointer_list * websocket_extension_list;
1563 };
1564 
1570 struct _websocket_message {
1571  time_t datestamp; /* !< date stamp of the message */
1572  uint8_t rsv; /* !< flags RSV1-3 of the message */
1573  uint8_t opcode; /* !< opcode for the message (string or binary) */
1574  uint8_t has_mask; /* !< does the message contain a mask? */
1575  uint8_t mask[4]; /* !< mask used if any */
1576  size_t data_len; /* !< length of the data */
1577  char * data; /* !< message data */
1578  size_t fragment_len; /* !< length of the fragment, 0 if not fragmented */
1579  uint8_t fin; /* !< flag fin (end of fragmented message) */
1580 };
1581 
1585 struct _websocket_message_list {
1586  struct _websocket_message ** list; /* !< messages list */
1587  size_t len; /* !< message list length */
1588 };
1589 
1594 struct _websocket {
1595  struct _u_instance * instance; /* !< reference to the ulfius instance if any */
1596  struct _u_request * request; /* !< refrence to the ulfius request of any */
1597  void (* websocket_manager_callback) (const struct _u_request * request, /* !< reference to a function called after the websocket handshake */
1598  struct _websocket_manager * websocket_manager,
1599  void * websocket_manager_user_data);
1600  void * websocket_manager_user_data; /* !< a user-defined reference that will be available in websocket_manager_callback */
1601  void (* websocket_incoming_message_callback) (const struct _u_request * request, /* !< reference to a function called each time a message arrives */
1602  struct _websocket_manager * websocket_manager,
1603  const struct _websocket_message * message,
1604  void * websocket_incoming_user_data);
1605  void * websocket_incoming_user_data; /* !< a user-defined reference that will be available in websocket_incoming_message_callback */
1606  void (* websocket_onclose_callback) (const struct _u_request * request, /* !< reference to a function called after the websocket connection ends */
1607  struct _websocket_manager * websocket_manager,
1608  void * websocket_onclose_user_data);
1609  void * websocket_onclose_user_data; /* !< a user-defined reference that will be available in websocket_onclose_callback */
1610  struct _websocket_manager * websocket_manager; /* !< refrence to the websocket manager if any */
1611  struct MHD_UpgradeResponseHandle * urh; /* !< reference used by libmicrohttpd to upgrade the connection */
1612 };
1613 
1617 struct _websocket_client_handler {
1618  struct _websocket * websocket; /* !< the websocket to use */
1619  struct _u_response * response; /* !< the response attached to the websocket */
1620 };
1621 
1622 /********************************/
1624 /********************************/
1625 
1635 int ulfius_websocket_send_message(struct _websocket_manager * websocket_manager,
1636  const uint8_t opcode,
1637  const uint64_t data_len,
1638  const char * data);
1639 
1651 int ulfius_websocket_send_fragmented_message(struct _websocket_manager * websocket_manager,
1652  const uint8_t opcode,
1653  const uint64_t data_len,
1654  const char * data,
1655  const uint64_t fragment_len);
1656 
1663 #ifndef U_DISABLE_JANSSON
1664 int ulfius_websocket_send_json_message(struct _websocket_manager * websocket_manager,
1665  json_t * message);
1666 #endif
1667 
1677 struct _websocket_message * ulfius_websocket_pop_first_message(struct _websocket_message_list * message_list);
1678 
1683 void ulfius_clear_websocket_message(struct _websocket_message * message);
1684 
1685 /********************************/
1687 /********************************/
1688 
1703 int ulfius_set_websocket_response(struct _u_response * response,
1704  const char * websocket_protocol,
1705  const char * websocket_extensions,
1706  void (* websocket_manager_callback) (const struct _u_request * request,
1707  struct _websocket_manager * websocket_manager,
1708  void * websocket_manager_user_data),
1709  void * websocket_manager_user_data,
1710  void (* websocket_incoming_message_callback) (const struct _u_request * request,
1711  struct _websocket_manager * websocket_manager,
1712  const struct _websocket_message * message,
1713  void * websocket_incoming_user_data),
1714  void * websocket_incoming_user_data,
1715  void (* websocket_onclose_callback) (const struct _u_request * request,
1716  struct _websocket_manager * websocket_manager,
1717  void * websocket_onclose_user_data),
1718  void * websocket_onclose_user_data);
1719 
1737  const char * extension_server,
1738  uint8_t rsv,
1739  int (* websocket_extension_message_out_perform)(const uint8_t opcode,
1740  const uint64_t data_len_in,
1741  const char * data_in,
1742  uint64_t * data_len_out,
1743  char ** data_out,
1744  const uint64_t fragment_len,
1745  void * user_data,
1746  void * context),
1747  void * websocket_extension_message_out_perform_user_data,
1748  int (* websocket_extension_message_in_perform)(const uint8_t opcode,
1749  const uint64_t data_len_in,
1750  const char * data_in,
1751  uint64_t * data_len_out,
1752  char ** data_out,
1753  const uint64_t fragment_len,
1754  void * user_data,
1755  void * context),
1756  void * websocket_extension_message_in_perform_user_data,
1757  int (* websocket_extension_server_match)(const char * extension_client,
1758  const char ** extension_client_list,
1759  char ** extension_server,
1760  void * user_data,
1761  void ** context),
1762  void * websocket_extension_server_match_user_data,
1763  void (* websocket_extension_free_context)(void * user_data,
1764  void * context),
1765  void * websocket_extension_free_context_user_data);
1766 
1781 int websocket_extension_message_out_deflate(const uint8_t opcode,
1782  const uint64_t data_len_in,
1783  const char * data_in,
1784  uint64_t * data_len_out,
1785  char ** data_out,
1786  const uint64_t fragment_len,
1787  void * user_data,
1788  void * context);
1789 
1803 int websocket_extension_message_in_inflate(const uint8_t opcode,
1804  const uint64_t data_len_in,
1805  const char * data_in,
1806  uint64_t * data_len_out,
1807  char ** data_out,
1808  const uint64_t fragment_len,
1809  void * user_data,
1810  void * context);
1811 
1818 void websocket_extension_deflate_free_context(void * user_data, void * context);
1819 
1829 int websocket_extension_server_match_deflate(const char * extension_client, const char ** extension_client_list, char ** extension_server, void * user_data, void ** context);
1830 
1840 
1850 int ulfius_websocket_send_close_signal(struct _websocket_manager * websocket_manager);
1851 
1859 int ulfius_websocket_status(struct _websocket_manager * websocket_manager);
1860 
1869 int ulfius_websocket_wait_close(struct _websocket_manager * websocket_manager, unsigned int timeout);
1870 
1871 /********************************/
1873 /********************************/
1874 
1889  void (* websocket_manager_callback) (const struct _u_request * request,
1890  struct _websocket_manager * websocket_manager,
1891  void * websocket_manager_user_data),
1892  void * websocket_manager_user_data,
1893  void (* websocket_incoming_message_callback) (const struct _u_request * request,
1894  struct _websocket_manager * websocket_manager,
1895  const struct _websocket_message * message,
1896  void * websocket_incoming_user_data),
1897  void * websocket_incoming_user_data,
1898  void (* websocket_onclose_callback) (const struct _u_request * request,
1899  struct _websocket_manager * websocket_manager,
1900  void * websocket_onclose_user_data),
1901  void * websocket_onclose_user_data,
1902  struct _websocket_client_handler * websocket_client_handler,
1903  struct _u_response * response);
1904 
1920 int ulfius_add_websocket_client_extension_message_perform(struct _websocket_client_handler * websocket_client_handler,
1921  const char * extension,
1922  uint8_t rsv,
1923  int (* websocket_extension_message_out_perform)(const uint8_t opcode,
1924  const uint64_t data_len_in,
1925  const char * data_in,
1926  uint64_t * data_len_out,
1927  char ** data_out,
1928  const uint64_t fragment_len,
1929  void * user_data,
1930  void * context),
1931  void * websocket_extension_message_out_perform_user_data,
1932  int (* websocket_extension_message_in_perform)(const uint8_t opcode,
1933  const uint64_t data_len_in,
1934  const char * data_in,
1935  uint64_t * data_len_out,
1936  char ** data_out,
1937  const uint64_t fragment_len,
1938  void * user_data,
1939  void * context),
1940  void * websocket_extension_message_in_perform_user_data,
1941  int (* websocket_extension_client_match)(const char * extension_server,
1942  void * user_data,
1943  void ** context),
1944  void * websocket_extension_client_match_user_data,
1945  void (* websocket_extension_free_context)(void * user_data,
1946  void * context),
1947  void * websocket_extension_free_context_user_data);
1948 
1956 int websocket_extension_client_match_deflate(const char * extension_server, void * user_data, void ** context);
1957 
1965 int ulfius_add_websocket_client_deflate_extension(struct _websocket_client_handler * websocket_client_handler);
1966 
1972 int ulfius_websocket_client_connection_send_close_signal(struct _websocket_client_handler * websocket_client_handler);
1973 
1979 int ulfius_websocket_client_connection_close(struct _websocket_client_handler * websocket_client_handler);
1980 
1987 int ulfius_websocket_client_connection_status(struct _websocket_client_handler * websocket_client_handler);
1988 
1997 int ulfius_websocket_client_connection_wait_close(struct _websocket_client_handler * websocket_client_handler, unsigned int timeout);
1998 
2008 int ulfius_set_websocket_request(struct _u_request * request,
2009  const char * url,
2010  const char * websocket_protocol,
2011  const char * websocket_extensions);
2012 
2013 #endif
2014 
2016 #define ULFIUS_URL_SEPARATOR "/"
2017 #define ULFIUS_HTTP_ENCODING_JSON "application/json"
2018 #define ULFIUS_HTTP_HEADER_CONTENT "Content-Type"
2019 #define ULFIUS_HTTP_NOT_FOUND_BODY "Resource not found"
2020 #define ULFIUS_HTTP_ERROR_BODY "Server Error"
2021 
2022 #define ULFIUS_COOKIE_ATTRIBUTE_EXPIRES "Expires"
2023 #define ULFIUS_COOKIE_ATTRIBUTE_MAX_AGE "Max-Age"
2024 #define ULFIUS_COOKIE_ATTRIBUTE_DOMAIN "Domain"
2025 #define ULFIUS_COOKIE_ATTRIBUTE_PATH "Path"
2026 #define ULFIUS_COOKIE_ATTRIBUTE_SECURE "Secure"
2027 #define ULFIUS_COOKIE_ATTRIBUTE_HTTPONLY "HttpOnly"
2028 
2029 #define ULFIUS_POSTBUFFERSIZE 65536
2030 
2031 #define U_STATUS_STOP 0
2032 #define U_STATUS_RUNNING 1
2033 #define U_STATUS_ERROR 2
2034 
2035 #ifndef U_DISABLE_WEBSOCKET
2036 
2040 struct _websocket_handle {
2041  char * websocket_protocol; /* !< protocol for the websocket */
2042  char * websocket_extensions; /* !< extensions for the websocket */
2043  void (* websocket_manager_callback) (const struct _u_request * request, /* !< callback function for working with the websocket */
2044  struct _websocket_manager * websocket_manager,
2045  void * websocket_manager_user_data);
2046  void * websocket_manager_user_data; /* !< user-defined data that will be handled to websocket_manager_callback */
2047  void (* websocket_incoming_message_callback) (const struct _u_request * request, /* !< callback function that will be called every time a message arrives from the client in the websocket */
2048  struct _websocket_manager * websocket_manager,
2049  const struct _websocket_message * message,
2050  void * websocket_incoming_user_data);
2051  void * websocket_incoming_user_data; /* !< user-defined data that will be handled to websocket_incoming_message_callback */
2052  void (* websocket_onclose_callback) (const struct _u_request * request, /* !< callback function that will be called if the websocket is open while the program calls ulfius_stop_framework */
2053  struct _websocket_manager * websocket_manager,
2054  void * websocket_onclose_user_data);
2055  void * websocket_onclose_user_data; /* !< user-defined data that will be handled to websocket_onclose_callback */
2056  int rsv_expected;
2057  struct _pointer_list * websocket_extension_list;
2058 };
2059 
2063 struct _websocket_handler {
2064  pthread_mutex_t websocket_active_lock; /* !< mutex to change nb_websocket_active value */
2065  size_t nb_websocket_active; /* !< number of active websocket */
2066  struct _websocket ** websocket_active; /* !< array of active websocket */
2067  pthread_mutex_t websocket_close_lock; /* !< mutex to broadcast close signal */
2068  pthread_cond_t websocket_close_cond; /* !< condition to broadcast close signal */
2069  int pthread_init;
2070 };
2071 
2072 #endif // U_DISABLE_WEBSOCKET
2073 
2084 #ifndef U_DISABLE_GNUTLS
2092 char * ulfius_export_client_certificate_pem(const struct _u_request * request);
2093 
2101 int ulfius_import_client_certificate_pem(struct _u_request * request, const char * str_cert);
2102 
2103 #endif // U_DISABLE_GNUTLS
2104 
2109 #ifdef __cplusplus
2110 }
2111 #endif
2112 
2113 #endif // __ULFIUS_H__
char * ulfius_export_client_certificate_pem(const struct _u_request *request)
Definition: u_request.c:888
int ulfius_import_client_certificate_pem(struct _u_request *request, const char *str_cert)
Definition: u_request.c:912
u_option
Definition: ulfius.h:140
@ U_OPT_POST_BODY_PARAMETER_REMOVE
Remove from map containing the post body variables (if available), expected option value type: const ...
Definition: ulfius.h:165
@ U_OPT_CHECK_PROXY_CERTIFICATE
check proxy certificate and hostname, default true, used by ulfius_send_http_request,...
Definition: ulfius.h:150
@ U_OPT_CLIENT_KEY_PASSWORD
password to unlock client key file, available only if GnuTLS support is enabled, expected option valu...
Definition: ulfius.h:174
@ U_OPT_STATUS
HTTP response status code (200, 404, 500, etc), expected option value type: long.
Definition: ulfius.h:176
@ U_OPT_FOLLOW_REDIRECT
follow url redirections, used by ulfius_send_http_request, expected option value type: int
Definition: ulfius.h:152
@ U_OPT_COOKIE_PARAMETER
Add to the map containing the cookie variables, expected option value type: const char *,...
Definition: ulfius.h:160
@ U_OPT_TIMEOUT
connection timeout used by ulfius_send_http_request, default is 0 or Timeout in seconds to close the ...
Definition: ulfius.h:154
@ U_OPT_AUTH_BASIC_USER
basic authentication username, expected option value type: const char *
Definition: ulfius.h:155
@ U_OPT_CA_PATH
specify a path to CA certificates instead of system path, used by ulfius_send_http_request,...
Definition: ulfius.h:153
@ U_OPT_AUTH_REALM
realm to send to the client response on authenticationb failed, expected option value type: const cha...
Definition: ulfius.h:177
@ U_OPT_HTTP_URL
full url used to call this callback function or full url to call when used in a ulfius_send_http_requ...
Definition: ulfius.h:143
@ U_OPT_HEADER_PARAMETER_REMOVE
Remove from map containing the header variables, expected option value type: const char *.
Definition: ulfius.h:163
@ U_OPT_CLIENT_CERT_FILE
path to client certificate file for sending http requests with certificate authentication,...
Definition: ulfius.h:172
@ U_OPT_CLIENT_KEY_FILE
path to client key file for sending http requests with certificate authentication,...
Definition: ulfius.h:173
@ U_OPT_JSON_BODY
Set a stringified json_t * body to the request or the reponse, expected option value type: json_t *.
Definition: ulfius.h:169
@ U_OPT_HTTP_PROXY
proxy address to use for outgoing connections, used by ulfius_send_http_request, expected option valu...
Definition: ulfius.h:144
@ U_OPT_COOKIE_PARAMETER_REMOVE
Remove from map containing the cookie variables, expected option value type: const char *.
Definition: ulfius.h:164
@ U_OPT_HTTP_VERB
http method (GET, POST, PUT, DELETE, etc.), expected option value type: const char *
Definition: ulfius.h:142
@ U_OPT_NONE
Empty option to complete a ulfius_set_request_properties or ulfius_set_request_properties.
Definition: ulfius.h:141
@ U_OPT_BINARY_BODY
Set a raw body to the request or the reponse, expected option value type: const char *,...
Definition: ulfius.h:166
@ U_OPT_AUTH_BASIC_PASSWORD
basic authentication password, expected option value type: const char *
Definition: ulfius.h:156
@ U_OPT_SHARED_DATA
any data shared between callback functions, must be allocated and freed by the callback functions,...
Definition: ulfius.h:178
@ U_OPT_URL_PARAMETER_REMOVE
Remove from the map containing the url variables, both from the route and the ?key=value variables,...
Definition: ulfius.h:162
@ U_OPT_CHECK_SERVER_CERTIFICATE
check server certificate and hostname, default true, used by ulfius_send_http_request,...
Definition: ulfius.h:148
@ U_OPT_POST_BODY_PARAMETER
Add to the map containing the post body variables (if available), expected option value type: const c...
Definition: ulfius.h:161
@ U_OPT_CHECK_PROXY_CERTIFICATE_FLAG
check certificate peer and or proxy hostname if check_proxy_certificate is enabled,...
Definition: ulfius.h:151
@ U_OPT_STRING_BODY
Set a char * body to the request or the reponse, expected option value type: const char *.
Definition: ulfius.h:167
@ U_OPT_URL_PARAMETER
Add to the map containing the url variables, both from the route and the ?key=value variables,...
Definition: ulfius.h:158
@ U_OPT_HEADER_PARAMETER
Add to the map containing the header variables, expected option value type: const char *,...
Definition: ulfius.h:159
@ U_OPT_AUTH_BASIC
basic authentication user, then password, expected option value type: const char *,...
Definition: ulfius.h:157
@ U_OPT_HTTP_URL_APPEND
append char * value to the current url, expected option value type: const char *
Definition: ulfius.h:179
@ U_OPT_CHECK_SERVER_CERTIFICATE_FLAG
check certificate peer and or server hostname if check_server_certificate is enabled,...
Definition: ulfius.h:149
void ulfius_clean_endpoint_list(struct _u_endpoint *endpoint_list)
Definition: ulfius.c:1396
int ulfius_add_endpoint_list(struct _u_instance *u_instance, const struct _u_endpoint **u_endpoint_list)
Definition: ulfius.c:1447
int ulfius_add_endpoint(struct _u_instance *u_instance, const struct _u_endpoint *u_endpoint)
Definition: ulfius.c:1407
int ulfius_remove_endpoint(struct _u_instance *u_instance, const struct _u_endpoint *u_endpoint)
Definition: ulfius.c:1464
int ulfius_copy_endpoint(struct _u_endpoint *dest, const struct _u_endpoint *source)
Definition: ulfius.c:1338
int ulfius_remove_endpoint_by_val(struct _u_instance *u_instance, const char *http_method, const char *url_prefix, const char *url_format)
Definition: ulfius.c:1570
int ulfius_add_endpoint_by_val(struct _u_instance *u_instance, const char *http_method, const char *url_prefix, const char *url_format, unsigned int priority, int(*callback_function)(const struct _u_request *request, struct _u_response *response, void *user_data), void *user_data)
Definition: ulfius.c:1547
const struct _u_endpoint * ulfius_empty_endpoint(void)
Definition: ulfius.c:1518
int ulfius_equals_endpoints(const struct _u_endpoint *endpoint1, const struct _u_endpoint *endpoint2)
Definition: ulfius.c:1529
int ulfius_set_default_endpoint(struct _u_instance *u_instance, int(*callback_function)(const struct _u_request *request, struct _u_response *response, void *user_data), void *user_data)
Definition: ulfius.c:1583
struct _u_endpoint * ulfius_duplicate_endpoint_list(const struct _u_endpoint *endpoint_list)
Definition: ulfius.c:1360
void ulfius_clean_endpoint(struct _u_endpoint *endpoint)
Definition: ulfius.c:1381
int ulfius_send_smtp_email(const char *host, const int port, const int use_tls, const int verify_certificate, const char *user, const char *password, const char *from, const char *to, const char *cc, const char *bcc, const char *subject, const char *mail_body)
Definition: u_send_request.c:909
int ulfius_send_http_streaming_request(const struct _u_request *request, struct _u_response *response, size_t(*write_body_function)(void *contents, size_t size, size_t nmemb, void *user_data), void *write_body_data)
Definition: u_send_request.c:191
int ulfius_send_smtp_rich_email(const char *host, const int port, const int use_tls, const int verify_certificate, const char *user, const char *password, const char *from, const char *to, const char *cc, const char *bcc, const char *content_type, const char *subject, const char *mail_body)
Definition: u_send_request.c:706
int ulfius_send_http_request(const struct _u_request *request, struct _u_response *response)
Definition: u_send_request.c:159
void mhd_request_completed(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
Definition: ulfius.c:284
int ulfius_init_instance(struct _u_instance *u_instance, unsigned int port, struct sockaddr_in *bind_address, const char *default_auth_realm)
Definition: ulfius.c:1731
void * ulfius_uri_logger(void *cls, const char *uri)
Definition: ulfius.c:205
void ulfius_clean_instance(struct _u_instance *u_instance)
Definition: ulfius.c:1626
int ulfius_stop_framework(struct _u_instance *u_instance)
Definition: ulfius.c:1303
int ulfius_start_secure_ca_trust_framework(struct _u_instance *u_instance, const char *key_pem, const char *cert_pem, const char *root_ca_pem)
Definition: ulfius.c:1216
int ulfius_start_framework(struct _u_instance *u_instance)
Definition: ulfius.c:1150
int ulfius_start_framework_with_mhd_options(struct _u_instance *u_instance, unsigned int mhd_flags, struct MHD_OptionItem *options)
Definition: ulfius.c:1276
int ulfius_start_secure_framework(struct _u_instance *u_instance, const char *key_pem, const char *cert_pem)
Definition: ulfius.c:1167
int ulfius_set_upload_file_callback_function(struct _u_instance *u_instance, int(*file_upload_callback)(const struct _u_request *request, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size, void *cls), void *cls)
Definition: ulfius.c:1606
void ulfius_global_close(void)
Definition: ulfius.c:1929
int ulfius_global_init(void)
Definition: ulfius.c:1905
void u_free(void *data)
Definition: ulfius.c:1745
int ulfius_add_header_to_response(struct _u_response *response, const char *key, const char *value)
Definition: u_response.c:711
int ulfius_set_empty_body_request(struct _u_request *request)
Definition: u_request.c:786
int ulfius_set_string_body_response(struct _u_response *response, const unsigned int status, const char *body)
Definition: u_response.c:510
int ulfius_set_string_body_request(struct _u_request *request, const char *string_body)
Definition: u_request.c:739
int ulfius_set_binary_body_response(struct _u_response *response, const unsigned int status, const char *body, const size_t length)
Definition: u_response.c:528
int ulfius_set_binary_body_request(struct _u_request *request, const char *binary_body, const size_t length)
Definition: u_request.c:761
int ulfius_set_empty_body_response(struct _u_response *response, const unsigned int status)
Definition: u_response.c:549
int ulfius_set_stream_response(struct _u_response *response, const unsigned int status, ssize_t(*stream_callback)(void *stream_user_data, uint64_t offset, char *out_buf, size_t max), void(*stream_callback_free)(void *stream_user_data), uint64_t stream_size, size_t stream_block_size, void *stream_user_data)
Definition: u_response.c:563
int u_map_init(struct _u_map *u_map)
Definition: u_map.c:33
int u_map_remove_from_value_binary(struct _u_map *u_map, const char *key, size_t length)
Definition: u_map.c:287
int u_map_remove_from_key(struct _u_map *u_map, const char *key)
Definition: u_map.c:237
int u_map_clean(struct _u_map *u_map)
Definition: u_map.c:66
int u_map_remove_at(struct _u_map *u_map, const int index)
Definition: u_map.c:333
int u_map_remove_from_value_case(struct _u_map *u_map, const char *value)
Definition: u_map.c:310
struct _u_map * u_map_copy(const struct _u_map *source)
Definition: u_map.c:452
ssize_t u_map_get_case_length(const struct _u_map *u_map, const char *key)
Definition: u_map.c:438
const char * u_map_get(const struct _u_map *u_map, const char *key)
Definition: u_map.c:368
int u_map_remove_from_key_case(struct _u_map *u_map, const char *key)
Definition: u_map.c:260
int u_map_has_value(const struct _u_map *u_map, const char *value)
Definition: u_map.c:125
int u_map_has_value_case(const struct _u_map *u_map, const char *value)
Definition: u_map.c:398
int u_map_clean_enum(char **array)
Definition: u_map.c:91
int u_map_clean_full(struct _u_map *u_map)
Definition: u_map.c:82
const char ** u_map_enum_keys(const struct _u_map *u_map)
Definition: u_map.c:105
int u_map_has_key_case(const struct _u_map *u_map, const char *key)
Definition: u_map.c:386
ssize_t u_map_get_length(const struct _u_map *u_map, const char *key)
Definition: u_map.c:424
int u_map_empty(struct _u_map *u_map)
Definition: u_map.c:504
int u_map_copy_into(struct _u_map *dest, const struct _u_map *source)
Definition: u_map.c:477
int u_map_put(struct _u_map *u_map, const char *key, const char *value)
Definition: u_map.c:141
int u_map_has_value_binary(const struct _u_map *u_map, const char *value, size_t length)
Definition: u_map.c:129
const char ** u_map_enum_values(const struct _u_map *u_map)
Definition: u_map.c:109
int u_map_has_key(const struct _u_map *u_map, const char *key)
Definition: u_map.c:113
int u_map_remove_from_value(struct _u_map *u_map, const char *value)
Definition: u_map.c:283
int u_map_put_binary(struct _u_map *u_map, const char *key, const char *value, uint64_t offset, size_t length)
Definition: u_map.c:149
int u_map_count(const struct _u_map *source)
Definition: u_map.c:495
const char * u_map_get_case(const struct _u_map *u_map, const char *key)
Definition: u_map.c:410
char * ulfius_url_encode(const char *str)
Definition: ulfius.c:1842
char * ulfius_url_decode(const char *str)
Definition: ulfius.c:1876
Contains all informations needed for an endpoint.
Definition: ulfius.h:294
int(* callback_function)(const struct _u_request *request, struct _u_response *response, void *user_data)
Definition: ulfius.h:299
void * user_data
Definition: ulfius.h:302
unsigned int priority
Definition: ulfius.h:298
char * http_method
Definition: ulfius.h:295
char * url_format
Definition: ulfius.h:297
char * url_prefix
Definition: ulfius.h:296
Contains the needed data for an ulfius instance to work.
Definition: ulfius.h:311
int status
Definition: ulfius.h:313
size_t max_post_param_size
Definition: ulfius.h:326
struct MHD_Daemon * mhd_daemon
Definition: ulfius.h:312
unsigned int port
Definition: ulfius.h:314
unsigned int timeout
Definition: ulfius.h:320
size_t max_post_body_size
Definition: ulfius.h:327
struct sockaddr_in * bind_address
Definition: ulfius.h:318
void * file_upload_cls
Definition: ulfius.h:338
struct _u_map * default_headers
Definition: ulfius.h:325
int check_utf8
Definition: ulfius.h:340
struct sockaddr_in6 * bind_address6
Definition: ulfius.h:319
struct _u_endpoint * endpoint_list
Definition: ulfius.h:323
int nb_endpoints
Definition: ulfius.h:321
struct _u_endpoint * default_endpoint
Definition: ulfius.h:324
void * websocket_handler
Definition: ulfius.h:328
int(* file_upload_callback)(const struct _u_request *request, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size, void *cls)
Definition: ulfius.h:329
char * default_auth_realm
Definition: ulfius.h:322
int mhd_response_copy_data
Definition: ulfius.h:339
int use_client_cert_auth
Definition: ulfius.h:342
Definition: ulfius.h:199
int nb_values
Definition: ulfius.h:200
size_t * lengths
Definition: ulfius.h:203
char ** keys
Definition: ulfius.h:201
char ** values
Definition: ulfius.h:202
definition of the parameters available in a struct _u_request
Definition: ulfius.h:228
unsigned long timeout
Definition: ulfius.h:243
int check_server_certificate_flag
Definition: ulfius.h:238
int follow_redirect
Definition: ulfius.h:241
char * auth_basic_password
Definition: ulfius.h:246
char * http_protocol
Definition: ulfius.h:229
int check_proxy_certificate_flag
Definition: ulfius.h:240
char * client_key_password
Definition: ulfius.h:258
void * binary_body
Definition: ulfius.h:251
gnutls_x509_crt_t client_cert
Definition: ulfius.h:255
struct _u_map * map_cookie
Definition: ulfius.h:249
size_t binary_body_length
Definition: ulfius.h:252
struct _u_map * map_header
Definition: ulfius.h:248
int check_server_certificate
Definition: ulfius.h:237
struct sockaddr * client_address
Definition: ulfius.h:244
char * ca_path
Definition: ulfius.h:242
struct _u_map * map_post_body
Definition: ulfius.h:250
int check_proxy_certificate
Definition: ulfius.h:239
char * client_key_file
Definition: ulfius.h:257
char * url_path
Definition: ulfius.h:232
char * auth_basic_user
Definition: ulfius.h:245
char * http_verb
Definition: ulfius.h:230
unsigned int callback_position
Definition: ulfius.h:253
char * proxy
Definition: ulfius.h:233
char * http_url
Definition: ulfius.h:231
struct _u_map * map_url
Definition: ulfius.h:247
char * client_cert_file
Definition: ulfius.h:256
definition of the parameters available in a struct _u_response
Definition: ulfius.h:268
char * protocol
Definition: ulfius.h:270
size_t stream_block_size
Definition: ulfius.h:280
void * stream_user_data
Definition: ulfius.h:281
ssize_t(* stream_callback)(void *stream_user_data, uint64_t offset, char *out_buf, size_t max)
Definition: ulfius.h:277
void * websocket_handle
Definition: ulfius.h:282
uint64_t stream_size
Definition: ulfius.h:279
size_t binary_body_length
Definition: ulfius.h:276
struct _u_map * map_header
Definition: ulfius.h:271
long status
Definition: ulfius.h:269
unsigned int nb_cookies
Definition: ulfius.h:272
unsigned int timeout
Definition: ulfius.h:285
struct _u_cookie * map_cookie
Definition: ulfius.h:273
char * auth_realm
Definition: ulfius.h:274
void(* stream_callback_free)(void *stream_user_data)
Definition: ulfius.h:278
void(* free_shared_data)(void *shared_data)
Definition: ulfius.h:284
void * binary_body
Definition: ulfius.h:275
void * shared_data
Definition: ulfius.h:283
Definition: ulfius.h:353
struct _u_request * request
Definition: ulfius.h:358
struct _u_map map_url_initial
Definition: ulfius.h:360
struct _u_instance * u_instance
Definition: ulfius.h:354
size_t max_post_param_size
Definition: ulfius.h:359
struct MHD_PostProcessor * post_processor
Definition: ulfius.h:355
int callback_first_iteration
Definition: ulfius.h:357
int has_post_processor
Definition: ulfius.h:356
int websocket_extension_message_in_inflate(const uint8_t opcode, const uint64_t data_len_in, const char *data_in, uint64_t *data_len_out, char **data_out, const uint64_t fragment_len, void *user_data, void *context)
Definition: u_websocket.c:1962
int ulfius_add_websocket_extension_message_perform(struct _u_response *response, const char *extension_server, uint8_t rsv, int(*websocket_extension_message_out_perform)(const uint8_t opcode, const uint64_t data_len_in, const char *data_in, uint64_t *data_len_out, char **data_out, const uint64_t fragment_len, void *user_data, void *context), void *websocket_extension_message_out_perform_user_data, int(*websocket_extension_message_in_perform)(const uint8_t opcode, const uint64_t data_len_in, const char *data_in, uint64_t *data_len_out, char **data_out, const uint64_t fragment_len, void *user_data, void *context), void *websocket_extension_message_in_perform_user_data, int(*websocket_extension_server_match)(const char *extension_client, const char **extension_client_list, char **extension_server, void *user_data, void **context), void *websocket_extension_server_match_user_data, void(*websocket_extension_free_context)(void *user_data, void *context), void *websocket_extension_free_context_user_data)
Definition: u_websocket.c:1803
int ulfius_websocket_client_connection_send_close_signal(struct _websocket_client_handler *websocket_client_handler)
Definition: u_websocket.c:2675
int ulfius_websocket_send_close_signal(struct _websocket_manager *websocket_manager)
Definition: u_websocket.c:2200
int ulfius_websocket_send_json_message(struct _websocket_manager *websocket_manager, json_t *message)
Definition: u_websocket.c:1549
int websocket_extension_server_match_deflate(const char *extension_client, const char **extension_client_list, char **extension_server, void *user_data, void **context)
Definition: u_websocket.c:2040
int ulfius_add_websocket_client_deflate_extension(struct _websocket_client_handler *websocket_client_handler)
Definition: u_websocket.c:2666
int ulfius_websocket_client_connection_wait_close(struct _websocket_client_handler *websocket_client_handler, unsigned int timeout)
Definition: u_websocket.c:2729
int ulfius_add_websocket_client_extension_message_perform(struct _websocket_client_handler *websocket_client_handler, const char *extension, uint8_t rsv, int(*websocket_extension_message_out_perform)(const uint8_t opcode, const uint64_t data_len_in, const char *data_in, uint64_t *data_len_out, char **data_out, const uint64_t fragment_len, void *user_data, void *context), void *websocket_extension_message_out_perform_user_data, int(*websocket_extension_message_in_perform)(const uint8_t opcode, const uint64_t data_len_in, const char *data_in, uint64_t *data_len_out, char **data_out, const uint64_t fragment_len, void *user_data, void *context), void *websocket_extension_message_in_perform_user_data, int(*websocket_extension_client_match)(const char *extension_server, void *user_data, void **context), void *websocket_extension_client_match_user_data, void(*websocket_extension_free_context)(void *user_data, void *context), void *websocket_extension_free_context_user_data)
Definition: u_websocket.c:2348
int ulfius_websocket_client_connection_status(struct _websocket_client_handler *websocket_client_handler)
Definition: u_websocket.c:2715
int ulfius_websocket_wait_close(struct _websocket_manager *websocket_manager, unsigned int timeout)
Definition: u_websocket.c:2229
int ulfius_websocket_status(struct _websocket_manager *websocket_manager)
Definition: u_websocket.c:2215
int ulfius_open_websocket_client_connection(struct _u_request *request, void(*websocket_manager_callback)(const struct _u_request *request, struct _websocket_manager *websocket_manager, void *websocket_manager_user_data), void *websocket_manager_user_data, void(*websocket_incoming_message_callback)(const struct _u_request *request, struct _websocket_manager *websocket_manager, const struct _websocket_message *message, void *websocket_incoming_user_data), void *websocket_incoming_user_data, void(*websocket_onclose_callback)(const struct _u_request *request, struct _websocket_manager *websocket_manager, void *websocket_onclose_user_data), void *websocket_onclose_user_data, struct _websocket_client_handler *websocket_client_handler, struct _u_response *response)
Definition: u_websocket.c:2434
int websocket_extension_message_out_deflate(const uint8_t opcode, const uint64_t data_len_in, const char *data_in, uint64_t *data_len_out, char **data_out, const uint64_t fragment_len, void *user_data, void *context)
Definition: u_websocket.c:1872
int ulfius_set_websocket_response(struct _u_response *response, const char *websocket_protocol, const char *websocket_extensions, void(*websocket_manager_callback)(const struct _u_request *request, struct _websocket_manager *websocket_manager, void *websocket_manager_user_data), void *websocket_manager_user_data, void(*websocket_incoming_message_callback)(const struct _u_request *request, struct _websocket_manager *websocket_manager, const struct _websocket_message *message, void *websocket_incoming_user_data), void *websocket_incoming_user_data, void(*websocket_onclose_callback)(const struct _u_request *request, struct _websocket_manager *websocket_manager, void *websocket_onclose_user_data), void *websocket_onclose_user_data)
Definition: u_websocket.c:1756
int ulfius_add_websocket_deflate_extension(struct _u_response *response)
Definition: u_websocket.c:2188
void websocket_extension_deflate_free_context(void *user_data, void *context)
Definition: u_websocket.c:2181
int ulfius_websocket_client_connection_close(struct _websocket_client_handler *websocket_client_handler)
Definition: u_websocket.c:2688
int ulfius_set_websocket_request(struct _u_request *request, const char *url, const char *websocket_protocol, const char *websocket_extensions)
Definition: u_websocket.c:2307
int ulfius_websocket_send_message(struct _websocket_manager *websocket_manager, const uint8_t opcode, const uint64_t data_len, const char *data)
Definition: u_websocket.c:1537
int ulfius_websocket_send_fragmented_message(struct _websocket_manager *websocket_manager, const uint8_t opcode, const uint64_t data_len, const char *data, const uint64_t fragment_len)
Definition: u_websocket.c:1443
struct _websocket_message * ulfius_websocket_pop_first_message(struct _websocket_message_list *message_list)
Definition: u_websocket.c:1563
int websocket_extension_client_match_deflate(const char *extension_server, void *user_data, void **context)
Definition: u_websocket.c:2555
void ulfius_clear_websocket_message(struct _websocket_message *message)
Definition: u_websocket.c:1585