-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathulfius.h
More file actions
2212 lines (1994 loc) · 106 KB
/
Copy pathulfius.h
File metadata and controls
2212 lines (1994 loc) · 106 KB
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* @file ulfius.h
* @brief Ulfius framework
*
* REST framework library
*
* ulfius.h: public structures and functions declarations
*
* Copyright 2015-2022 Nicolas Mora <mail@babelouest.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __ULFIUS_H__
#define __ULFIUS_H__
#ifdef __cplusplus
extern "C"
{
#endif
#include "ulfius-cfg.h"
/** External dependencies **/
#ifndef U_DISABLE_GNUTLS
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#endif
#ifndef U_DISABLE_JANSSON
#include <jansson.h>
#endif
#ifndef U_DISABLE_WEBSOCKET
#include <poll.h>
#include <zlib.h>
#include <pthread.h>
#ifndef POLLRDHUP
#define POLLRDHUP 0x2000
#endif
#endif
#include <microhttpd.h>
#if defined(_WIN32) && !defined(U_DISABLE_WEBSOCKET)
#define U_DISABLE_WEBSOCKET
#endif
#if (MHD_VERSION < 0x00095300) && !defined(U_DISABLE_WEBSOCKET)
#define U_DISABLE_WEBSOCKET
#endif
/** Angharad libraries **/
#include <orcania.h>
/** To disable all yder log messages, this flag must be enabled **/
#ifndef U_DISABLE_YDER
#include <yder.h>
#else
#define Y_LOG_MODE_NONE 0
#define Y_LOG_MODE_CONSOLE 0
#define Y_LOG_MODE_SYSLOG 0
#define Y_LOG_MODE_FILE 0
#define Y_LOG_MODE_JOURNALD 0
#define Y_LOG_MODE_CALLBACK 0
#define Y_LOG_MODE_CURRENT 0
#define Y_LOG_LEVEL_NONE 0
#define Y_LOG_LEVEL_DEBUG 0
#define Y_LOG_LEVEL_INFO 0
#define Y_LOG_LEVEL_WARNING 0
#define Y_LOG_LEVEL_ERROR 0
#define Y_LOG_LEVEL_CURRENT 0
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);
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);
void y_log_message(const unsigned long type, const char * message, ...);
int y_close_logs();
#endif
/**
* @defgroup const Constants
* @{
*/
#define ULFIUS_STREAM_BLOCK_SIZE_DEFAULT 1024
#define U_STREAM_END MHD_CONTENT_READER_END_OF_STREAM
#define U_STREAM_ERROR MHD_CONTENT_READER_END_WITH_ERROR
#define U_STREAM_SIZE_UNKNOWN MHD_SIZE_UNKNOWN
#define U_STREAM_SIZE_UNKOWN U_STREAM_SIZE_UNKNOWN // Backward compatibility
#define U_OK 0 ///< No error
#define U_ERROR 1 ///< Error
#define U_ERROR_MEMORY 2 ///< Error in memory allocation
#define U_ERROR_PARAMS 3 ///< Error in input parameters
#define U_ERROR_LIBMHD 4 ///< Error in libmicrohttpd execution
#define U_ERROR_LIBCURL 5 ///< Error in libcurl execution
#define U_ERROR_NOT_FOUND 6 ///< Something was not found
#define U_ERROR_DISCONNECTED 7 ///< Connection closed
#define U_CALLBACK_CONTINUE 0 ///< Callback exited with success, continue to next callback
#define U_CALLBACK_IGNORE 1 ///< Callback decided to be ignored, request.callback_position will not be incremented, continue to next callback
#define U_CALLBACK_COMPLETE 2 ///< Callback exited with success, exit callback list
#define U_CALLBACK_UNAUTHORIZED 3 ///< Request is unauthorized, exit callback list and return status 401
#define U_CALLBACK_ERROR 4 ///< Error during request process, exit callback list and return status 500
#define U_COOKIE_SAME_SITE_EMPTY 0 ///< Set same_site cookie property not set
#define U_COOKIE_SAME_SITE_STRICT 1 ///< Set same_site cookie property to strict
#define U_COOKIE_SAME_SITE_LAX 2 ///< Set same_site cookie property to lax
#define U_COOKIE_SAME_SITE_NONE 3 ///< Set same_site cookie property to none
#define U_USE_IPV4 0x0001 ///< Use instance in IPV4 mode only
#define U_USE_IPV6 0x0010 ///< Use instance in IPV6 mode only
#define U_USE_ALL (U_USE_IPV4|U_USE_IPV6) ///< Use instance in both IPV4 and IPV6 mode
#define U_SSL_VERIFY_PEER 0x0001 ///< Verify TLS session with peers
#define U_SSL_VERIFY_HOSTNAME 0x0010 ///< Verify TLS session with hostname
#define U_POST_PROCESS_NONE 0x0000
#define U_POST_PROCESS_URL_ENCODED 0x0001
#define U_POST_PROCESS_MULTIPART_FORMDATA 0x0010
/**
* Options available to set or get properties using
* ulfius_set_request_properties or ulfius_set_request_properties
*/
typedef enum {
U_OPT_NONE = 0, ///< Empty option to complete a ulfius_set_request_properties or ulfius_set_request_properties
U_OPT_HTTP_VERB = 1, ///< http method (GET, POST, PUT, DELETE, etc.), expected option value type: const char *
U_OPT_HTTP_URL = 2, ///< full url used to call this callback function or full url to call when used in a ulfius_send_http_request, expected option value type: const char *
U_OPT_HTTP_PROXY = 3, ///< proxy address to use for outgoing connections, used by ulfius_send_http_request, expected option value type: const char *
#if MHD_VERSION >= 0x00095208
U_OPT_NETWORK_TYPE = 4, ///< Force connect to ipv4, ipv6 addresses or both, values available are U_USE_ALL, U_USE_IPV4 or U_USE_IPV6, expected option value type: unsigned short
#endif
U_OPT_CHECK_SERVER_CERTIFICATE = 5, ///< check server certificate and hostname, default true, used by ulfius_send_http_request, expected option value type: int
U_OPT_CHECK_SERVER_CERTIFICATE_FLAG = 6, ///< 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, expected option value type: int
U_OPT_CHECK_PROXY_CERTIFICATE = 7, ///< check proxy certificate and hostname, default true, used by ulfius_send_http_request, requires libcurl >= 7.52, expected option value type: int
U_OPT_CHECK_PROXY_CERTIFICATE_FLAG = 8, ///< 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, expected option value type: int
U_OPT_FOLLOW_REDIRECT = 9, ///< follow url redirections, used by ulfius_send_http_request, expected option value type: int
U_OPT_CA_PATH = 10, ///< specify a path to CA certificates instead of system path, used by ulfius_send_http_request, expected option value type: const char *
U_OPT_TIMEOUT = 11, ///< connection timeout used by ulfius_send_http_request, default is 0 _or_ Timeout in seconds to close the connection because of inactivity between the client and the server, expected option value type: unsigned long
U_OPT_AUTH_BASIC_USER = 12, ///< basic authentication username, expected option value type: const char *
U_OPT_AUTH_BASIC_PASSWORD = 13, ///< basic authentication password, expected option value type: const char *
U_OPT_AUTH_BASIC = 14, ///< basic authentication user, then password, expected option value type: const char *, const char *
U_OPT_URL_PARAMETER = 15, ///< Add to the map containing the url variables, both from the route and the ?key=value variables, expected option value type: const char *, const char *
U_OPT_HEADER_PARAMETER = 16, ///< Add to the map containing the header variables, expected option value type: const char *, const char *
U_OPT_COOKIE_PARAMETER = 17, ///< Add to the map containing the cookie variables, expected option value type: const char *, const char *
U_OPT_POST_BODY_PARAMETER = 18, ///< Add to the map containing the post body variables (if available), expected option value type: const char *, const char *
U_OPT_URL_PARAMETER_REMOVE = 19, ///< Remove from the map containing the url variables, both from the route and the ?key=value variables, expected option value type: const char *
U_OPT_HEADER_PARAMETER_REMOVE = 20, ///< Remove from map containing the header variables, expected option value type: const char *
U_OPT_COOKIE_PARAMETER_REMOVE = 21, ///< Remove from map containing the cookie variables, expected option value type: const char *
U_OPT_POST_BODY_PARAMETER_REMOVE = 22, ///< Remove from map containing the post body variables (if available), expected option value type: const char *
U_OPT_BINARY_BODY = 23, ///< Set a raw body to the request or the reponse, expected option value type: const char *, size_t
U_OPT_STRING_BODY = 24, ///< Set a char * body to the request or the reponse, expected option value type: const char *
#ifndef U_DISABLE_JANSSON
U_OPT_JSON_BODY = 25, ///< Set a stringified json_t * body to the request or the reponse, expected option value type: json_t *
#endif
#ifndef U_DISABLE_GNUTLS
U_OPT_CLIENT_CERT_FILE = 26, ///< path to client certificate file for sending http requests with certificate authentication, available only if GnuTLS support is enabled, expected option value type: const char *
U_OPT_CLIENT_KEY_FILE = 27, ///< path to client key file for sending http requests with certificate authentication, available only if GnuTLS support is enabled, expected option value type: const char *
U_OPT_CLIENT_KEY_PASSWORD = 28, ///< password to unlock client key file, available only if GnuTLS support is enabled, expected option value type: const char *
#endif
U_OPT_STATUS = 29, ///< HTTP response status code (200, 404, 500, etc), expected option value type: long
U_OPT_AUTH_REALM = 30, ///< realm to send to the client response on authenticationb failed, expected option value type: const char *
U_OPT_SHARED_DATA = 31, ///< any data shared between callback functions, must be allocated and freed by the callback functions, expected option value type: void *
U_OPT_HTTP_URL_APPEND = 32 ///< append char * value to the current url, expected option value type: const char *
} u_option;
/**
* @}
*/
/*************
* Structures
*************/
/**
* @defgroup struct Structures
* structures definitions
* @{
*/
/**
* struct _u_map
*/
struct _u_map {
int nb_values; /* !< Values count */
char ** keys; /* !< Array of keys */
char ** values; /* !< Array of values */
size_t * lengths; /* !< Lengths of each values */
};
/**
* struct _u_cookie
* the structure containing the response cookie parameters
*/
struct _u_cookie {
char * key; /* !< key if the cookie */
char * value; /* !< value of the cookie */
char * expires; /* !< expiration date of the cookie */
unsigned int max_age; /* !< duration of the cookie in seconds */
char * domain; /* !< domain for the cookie */
char * path; /* !< url path for the cookie */
int secure; /* !< flag to set cookie secure or not */
int http_only; /* !< flag to set cookie for HTTP connections only or not */
int same_site; /* !< flag to set same_site option to the cookie */
};
/**
*
* @struct _u_request
* @brief definition of the parameters available in a struct _u_request
*
*/
struct _u_request {
char * http_protocol; /* !< http protocol used (1.0 or 1.1) */
char * http_verb; /* !< http method (GET, POST, PUT, DELETE, etc.) */
char * http_url; /* !< full url used to call this callback function or full url to call when used in a ulfius_send_http_request */
char * url_path; /* !< url path only used to call this callback function (ex, if http_url is /path/?param=1, url_path is /path/) */
char * proxy; /* !<proxy address to use for outgoing connections, used by ulfius_send_http_request */
#if MHD_VERSION >= 0x00095208
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 */
#endif
int check_server_certificate; /* !< check server certificate and hostname, default true, used by ulfius_send_http_request */
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 */
int check_proxy_certificate; /* !< check proxy certificate and hostname, default true, used by ulfius_send_http_request, requires libcurl >= 7.52 */
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 */
int follow_redirect; /* !< follow url redirections, used by ulfius_send_http_request */
char * ca_path; /* !< specify a path to CA certificates instead of system path, used by ulfius_send_http_request */
char * network_interface; /* !< bind outgoing socket to this interface/IP (CURLOPT_INTERFACE), used by ulfius_send_http_request */
unsigned long timeout; /* !< connection timeout used by ulfius_send_http_request, default is 0 */
struct sockaddr * client_address; /* !< IP address of the client */
char * auth_basic_user; /* !< basic authentication username */
char * auth_basic_password; /* !< basic authentication password */
struct _u_map * map_url; /* !< map containing the url variables, both from the route and the ?key=value variables */
struct _u_map * map_header; /* !< map containing the header variables */
struct _u_map * map_cookie; /* !< map containing the cookie variables */
struct _u_map * map_post_body; /* !< map containing the post body variables (if available) */
unsigned char * binary_body; /* !< raw body */
size_t binary_body_length; /* !< length of raw body */
unsigned int callback_position; /* !< position of the current callback function in the callback list, starts at 0 */
#ifndef U_DISABLE_GNUTLS
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 */
char * client_cert_file; /* !< path to client certificate file for sending http requests with certificate authentication, available only if GnuTLS support is enabled */
char * client_key_file; /* !< path to client key file for sending http requests with certificate authentication, available only if GnuTLS support is enabled */
char * client_key_password; /* !< password to unlock client key file, available only if GnuTLS support is enabled */
#endif
};
/**
*
* @struct _u_response
* @brief definition of the parameters available in a struct _u_response
*
*/
struct _u_response {
long status; /* !< HTTP status code (200, 404, 500, etc) */
char * protocol; /* !< HTTP Protocol sent */
struct _u_map * map_header; /* !< map containing the header variables */
unsigned int nb_cookies; /* !< number of cookies sent */
struct _u_cookie * map_cookie; /* !< array of cookies sent */
char * auth_realm; /* !< realm to send to the client on authenticationb failed */
unsigned char * binary_body; /* !< raw binary content */
size_t binary_body_length; /* !< length of the binary_body */
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 */
void (* stream_callback_free) (void * stream_user_data); /* !< callback function to free data allocated for streaming */
uint64_t stream_size; /* !< size of the streamed data (U_STREAM_SIZE_UNKNOWN if unknown) */
size_t stream_block_size; /* !< size of each block to be streamed, set according to your system */
void * stream_user_data; /* !< user defined data that will be available in your callback stream functions */
void * websocket_handle; /* !< handle for websocket extension */
void * shared_data; /* !< any data shared between callback functions, must be allocated and freed by the callback functions */
void (* free_shared_data)(void * shared_data); /* !< pointer to a function that will free shared_data */
unsigned int timeout; /* !< Timeout in seconds to close the connection because of inactivity between the client and the server */
};
/**
*
* @struct _u_endpoint
* @brief Contains all informations needed for an endpoint
*
*/
struct _u_endpoint {
char * http_method; /* !< http verb (GET, POST, PUT, etc.) in upper case */
char * url_prefix; /* !< prefix for the url (optional) */
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 */
unsigned int priority; /* !< endpoint priority in descending order (0 is the higher priority) */
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. */
struct _u_response * response,
void * user_data);
void * user_data; /* !< pointer to a data or a structure that will be available in callback_function */
};
/**
*
* @struct _u_instance
* @brief Contains the needed data for an ulfius instance to work
*
*/
struct _u_instance {
struct MHD_Daemon * mhd_daemon; /* !< pointer to the libmicrohttpd daemon */
int status; /* !< status of the current instance, status are U_STATUS_STOP, U_STATUS_RUNNING or U_STATUS_ERROR */
unsigned int port; /* !< port number to listen to */
#if MHD_VERSION >= 0x00095208
unsigned short network_type; /* !< Listen to ipv4 and or ipv6 connections, values available are U_USE_ALL, U_USE_IPV4 or U_USE_IPV6 */
#endif
struct sockaddr_in * bind_address; /* !< ipv4 address to listen to (optional) */
struct sockaddr_in6 * bind_address6; /* !< ipv6 address to listen to (optional) */
unsigned int timeout; /* !< Timeout to close the connection because of inactivity between the client and the server */
int nb_endpoints; /* !< Number of available endpoints */
char * default_auth_realm; /* !< Default realm on authentication error */
struct _u_endpoint * endpoint_list; /* !< List of available endpoints */
struct _u_endpoint * default_endpoint; /* !< Default endpoint if no other endpoint match the current url */
struct _u_map * default_headers; /* !< Default headers that will be added to all response->map_header */
size_t max_post_param_size; /* !< maximum size for a post parameter, 0 means no limit, default 0 */
size_t max_post_body_size; /* !< maximum size for the entire post body, 0 means no limit, default 0 */
void * websocket_handler; /* !< handler for the websocket structure */
int (* file_upload_callback) (const struct _u_request * request, /* !< callback function to manage file upload by blocks */
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 * file_upload_cls; /* !< any pointer to pass to the file_upload_callback function */
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 */
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 */
#ifndef U_DISABLE_GNUTLS
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 */
#endif
int allowed_post_processor; /* !< Specifies which content-type are allowed to process in the request->map_post_body parameters list, default value is U_POST_PROCESS_URL_ENCODED|U_POST_PROCESS_MULTIPART_FORMDATA, to disable all, use U_POST_PROCESS_NONE */
};
/**
* @}
*/
/**
* Structures used to facilitate data manipulations (internal)
*/
struct connection_info_struct {
struct _u_instance * u_instance;
struct MHD_PostProcessor * post_processor;
int has_post_processor;
int callback_first_iteration;
struct _u_request * request;
size_t max_post_param_size;
struct _u_map map_url_initial;
};
/**********************************
* Instance functions declarations
**********************************/
/**
* @defgroup mem memory
* memory management function
* @{
*/
/**
* free data allocated by ulfius functions
* @param data data to free
*/
void u_free(void * data);
/**
* Initialize global parameters
* This function isn't thread-safe so it must be called once before any call to
* ulfius_send_http_request, ulfius_send_http_streaming_request, ulfius_send_smtp_email or ulfius_send_smtp_rich_email
* The function ulfius_send_request_close must be called when ulfius send request functions are no longer needed
* @return U_OK on success
*/
int ulfius_global_init(void);
/**
* Close global parameters
*/
void ulfius_global_close(void);
/**
* @}
*/
/**
* @defgroup instance struct _u_instance
* struct _u_instance management functions
* @{
*/
/**
* ulfius_init_instance
*
* Initialize a struct _u_instance * with default values
* Binds to IPV4 addresses only
* @param u_instance the ulfius instance to initialize
* @param port tcp port to bind to, must be between 1 and 65535
* @param bind_address IPv4 address to listen to, optional, the reference is borrowed, the structure isn't copied
* @param default_auth_realm default realm to send to the client on authentication error
* @return U_OK on success
*/
int ulfius_init_instance(struct _u_instance * u_instance, unsigned int port, struct sockaddr_in * bind_address, const char * default_auth_realm);
#if MHD_VERSION >= 0x00095208
/**
* ulfius_init_instance_ipv6
*
* Initialize a struct _u_instance * with default values
* Binds to IPV6 and IPV4 addresses or IPV6 addresses only
* @param port tcp port to bind to, must be between 1 and 65535
* @param bind_address IPv6 address to listen to, optional, the reference is borrowed, the structure isn't copied
* @param network_type Type of network to listen to, values available are U_USE_IPV6 or U_USE_ALL
* @param default_auth_realm default realm to send to the client on authentication error
* @return U_OK on success
*/
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);
#endif
/**
* ulfius_clean_instance
*
* Clean memory allocated by a struct _u_instance *
* @param u_instance an Ulfius instance
*/
void ulfius_clean_instance(struct _u_instance * u_instance);
/**
* ulfius_start_framework
* Initializes the framework and run the webservice based on the parameters given
*
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @return U_OK on success
*/
int ulfius_start_framework(struct _u_instance * u_instance);
/**
* ulfius_start_secure_framework
* Initializes the framework and run the webservice based on the parameters given using an HTTPS connection
*
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @param key_pem private key for the server
* @param cert_pem server certificate
* @return U_OK on success
*/
int ulfius_start_secure_framework(struct _u_instance * u_instance, const char * key_pem, const char * cert_pem);
#ifndef U_DISABLE_GNUTLS
/**
* ulfius_start_secure_ca_trust_framework
* Initializes the framework and run the webservice based on the parameters given using an HTTPS connection
* And using a root server to authenticate client connections
*
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @param key_pem private key for the server
* @param cert_pem server certificate
* @param root_ca_pem client root CA you're willing to trust for this instance
* @return U_OK on success
*/
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);
#endif
/**
* ulfius_start_framework_with_mhd_options
* Initializes the framework and run the webservice based on the specified MHD options table given in parameter
* Read https://www.gnu.org/software/libmicrohttpd/tutorial.html for more information
* This is for user who know what they do, Ulfius' options used in other `ulfius_start_framework_*`
* are good for most use cases where you need a multi-threaded HTTP webservice
* Some struct MHD_OptionItem may cause unexpected problems with Ulfius API
* If you find an unresolved issue with this function you can open an issue in GitHub
* But some issues may not be solvable if fixing them would break Ulfius API or philosophy
* i.e.: you're on your own
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @param mhd_flags OR-ed combination of MHD_FLAG values
* @param mhd_ops struct MHD_OptionItem * options table,
* - MUST contain an option with the fllowing value: {.option = MHD_OPTION_NOTIFY_COMPLETED; .value = (intptr_t)mhd_request_completed; .ptr_value = NULL;}
* - MUST contain an option with the fllowing value: {.option = MHD_OPTION_URI_LOG_CALLBACK; .value = (intptr_t)ulfius_uri_logger; .ptr_value = NULL;}
* - MUST end with a terminal struct MHD_OptionItem: {.option = MHD_OPTION_END; .value = 0; .ptr_value = NULL;}
* @return U_OK on success
*/
int ulfius_start_framework_with_mhd_options(struct _u_instance * u_instance, unsigned int mhd_flags, struct MHD_OptionItem * mhd_ops);
/**
* Internal functions externalized to use ulfius_start_framework_with_mhd_options
*/
void mhd_request_completed (void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe);
void * ulfius_uri_logger (void * cls, const char * uri);
/**
* ulfius_stop_framework
*
* Stop the webservice
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @return U_OK on success
*/
int ulfius_stop_framework(struct _u_instance * u_instance);
/**
* ulfius_set_upload_file_callback_function
*
* Set the callback function to handle file upload
* Used to facilitate large files upload management
* The callback function file_upload_callback will be called
* multiple times, with the uploaded file in striped in parts
*
* Warning: If this function is used, all the uploaded files
* for the instance will be managed via this function, and they
* will no longer be available in the struct _u_request in the
* ulfius callback function afterwards.
*
* Thanks to Thad Phetteplace for the help on this feature
*
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @param file_upload_callback Pointer to a callback function that will handle all file uploads
* @param cls a pointer that will be passed to file_upload_callback each tim it's called
* @return U_OK on success
*/
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);
/**
* @}
*/
/**
* @defgroup endpoints struct _u_endpoint
* struct _u_endpoint management functions
* @{
*/
/***********************************
* Endpoints functions declarations
***********************************/
/**
* Add a struct _u_endpoint * to the specified u_instance
* Can be done during the execution of the webservice for injection
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @param u_endpoint pointer to a struct _u_endpoint that will be copied in the u_instance endpoint_list
* @return U_OK on success
*/
int ulfius_add_endpoint(struct _u_instance * u_instance, const struct _u_endpoint * u_endpoint);
/**
* Add a struct _u_endpoint * to the specified u_instance with its values specified
* Can be done during the execution of the webservice for injection
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @param http_method http verb (GET, POST, PUT, etc.) in upper case
* @param url_prefix prefix for the url (optional)
* @param 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
* @param priority endpoint priority in descending order (0 is the higher priority)
* @param callback_function a pointer to a function that will be executed each time the endpoint is called
* you must declare the function as described.
* @param user_data a pointer to a data or a structure that will be available in callback_function
* @return U_OK on success
*/
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, // Input parameters (set by the framework)
struct _u_response * response, // Output parameters (set by the user)
void * user_data),
void * user_data);
/**
* Add a struct _u_endpoint * list to the specified u_instance
* Can be done during the execution of the webservice for injection
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @param u_endpoint_list pointer to an array of struct _u_endpoint ending with a ulfius_empty_endpoint() that will be copied in the u_instance endpoint_list
* @return U_OK on success
*/
int ulfius_add_endpoint_list(struct _u_instance * u_instance, const struct _u_endpoint ** u_endpoint_list);
/**
* Remove a struct _u_endpoint * from the specified u_instance
* Can be done during the execution of the webservice for injection
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @param u_endpoint pointer to a struct _u_endpoint that will be removed in the u_instance endpoint_list
* The parameters _u_endpoint.http_method, _u_endpoint.url_prefix and _u_endpoint.url_format are strictly compared for the match
* If no endpoint is found, return U_ERROR_NOT_FOUND
* @return U_OK on success
*/
int ulfius_remove_endpoint(struct _u_instance * u_instance, const struct _u_endpoint * u_endpoint);
/**
* ulfius_set_default_endpoint
* Set the default endpoint
* This endpoint will be called if no endpoint match the url called
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @param callback_function a pointer to a function that will be executed each time the endpoint is called
* you must declare the function as described.
* @param user_data a pointer to a data or a structure that will be available in callback_function
* to remove a default endpoint, call ulfius_set_default_endpoint with NULL parameter for callback_function
* @return U_OK on success
*/
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);
/**
* Remove a struct _u_endpoint * from the specified u_instance
* using the specified values used to identify an endpoint
* Can be done during the execution of the webservice for injection
* The parameters _u_endpoint.http_method, _u_endpoint.url_prefix and _u_endpoint.url_format are strictly compared for the match
* If no endpoint is found, return U_ERROR_NOT_FOUND
* @param u_instance pointer to a struct _u_instance that describe its port and bind address
* @param http_method http_method used by the endpoint
* @param url_prefix url_prefix used by the endpoint
* @param url_format url_format used by the endpoint
* @return U_OK on success
*/
int ulfius_remove_endpoint_by_val(struct _u_instance * u_instance, const char * http_method, const char * url_prefix, const char * url_format);
/**
* ulfius_empty_endpoint
* @return empty endpoint that goes at the end of an endpoint list
*/
const struct _u_endpoint * ulfius_empty_endpoint(void);
/**
* ulfius_copy_endpoint
* makes a copy of an endpoint with duplicate values
* @param dest the endpoint destination
* @param source the endpoint source
* @return U_OK on success
*/
int ulfius_copy_endpoint(struct _u_endpoint * dest, const struct _u_endpoint * source);
/**
* u_copy_endpoint_list
* makes a copy of an endpoint list with duplicate values
* @param endpoint_list an array of struct _u_endpoint * finishing with a ulfius_empty_endpoint()
* @return a list with duplicate values
* returned value must be free'd after use
*/
struct _u_endpoint * ulfius_duplicate_endpoint_list(const struct _u_endpoint * endpoint_list);
/**
* ulfius_clean_endpoint
* free allocated memory by an endpoint
* @param endpoint the endpoint to cleanup
*/
void ulfius_clean_endpoint(struct _u_endpoint * endpoint);
/**
* ulfius_clean_endpoint_list
* free allocated memory by an endpoint list
* @param endpoint_list the list of endpoints to cleanup, finishing with a ulfius_empty_endpoint()
*/
void ulfius_clean_endpoint_list(struct _u_endpoint * endpoint_list);
/**
* ulfius_equals_endpoints
* Compare 2 endpoints
* @param endpoint1 the first endpoint to compare
* @param endpoint2 the second endpoint to compare
* @return true if their method, prefix and format are the same or if both are NULL, false otherwise
*/
int ulfius_equals_endpoints(const struct _u_endpoint * endpoint1, const struct _u_endpoint * endpoint2);
/**
* @}
*/
/**
* @defgroup http_smtp_client Client HTTP and SMTP
* client HTTP and SMTP requests management functions
* @{
*/
#ifndef U_DISABLE_CURL
/********************************************
* Requests/Responses functions declarations
********************************************/
/**
* ulfius_send_http_request
* Send a HTTP request and store the result into a _u_response
* @param request the struct _u_request that contains all the input parameters to perform the HTTP request
* @param response the struct _u_response that will be filled with all response parameter values, optional, may be NULL
* @return U_OK on success
*/
int ulfius_send_http_request(const struct _u_request * request, struct _u_response * response);
/**
* ulfius_send_http_request_with_limit
* Send a HTTP request and store the result into a _u_response
* The response body lenght and number of parsed headers can be limited
* @param request the struct _u_request that contains all the input parameters to perform the HTTP request
* @param response the struct _u_response that will be filled with all response parameter values, optional, may be NULL
* @param response_body_limit the maximum size of the response body to retrieve, 0 means no limit
* @param max_header maximum number of headers to parse in the response, 0 means no limit
* @return U_OK on success
*/
int ulfius_send_http_request_with_limit(const struct _u_request * request, struct _u_response * response, size_t response_body_limit, size_t max_header);
/**
* ulfius_send_http_streaming_request
* Send a HTTP request and store the result into a _u_response
* Except for the body which will be available using write_body_function in the write_body_data
* @param request the struct _u_request that contains all the input parameters to perform the HTTP request
* @param response the struct _u_response that will be filled with all response parameter values, optional, may be NULL
* @param write_body_function a pointer to a function that will be used to receive response body in chunks
* @param write_body_data a user-defined poitner that will be passed in parameter to write_body_function
* @return U_OK on success
*/
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);
/**
* ulfius_send_http_streaming_request_max_header
* Send a HTTP request and store the result into a _u_response
* Except for the body which will be available using write_body_function in the write_body_data
* The number of parsed headers in the response can be limited by max_header
* @param request the struct _u_request that contains all the input parameters to perform the HTTP request
* @param response the struct _u_response that will be filled with all response parameter values, optional, may be NULL
* @param write_body_function a pointer to a function that will be used to receive response body in chunks
* @param write_body_data a user-defined poitner that will be passed in parameter to write_body_function
* @param max_header maximum number of headers to parse in the response, 0 means no limit
* @return U_OK on success
*/
int ulfius_send_http_streaming_request_max_header(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, size_t max_header);
/**
* ulfius_send_smtp_email
* Send an email using libcurl
* email is plain/text and UTF8 charset
* @param host smtp server host name
* @param port tcp port number (optional, 0 for default)
* @param use_tls true if the connection is tls secured
* @param verify_certificate true if you want to disable the certificate verification on a tls server
* @param user connection user name (optional, NULL: no user name)
* @param password connection password (optional, NULL: no password)
* @param from from address (mandatory)
* @param to to recipient address (mandatory)
* @param cc cc recipient address (optional, NULL: no cc)
* @param bcc bcc recipient address (optional, NULL: no bcc)
* @param subject email subject (mandatory)
* @param mail_body email body (mandatory)
* @return U_OK on success
*/
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);
/**
* Send an email using libcurl
* email has the content-type specified in parameter
* @param host smtp server host name
* @param port tcp port number (optional, 0 for default)
* @param use_tls true if the connection is tls secured
* @param verify_certificate true if you want to disable the certificate verification on a tls server
* @param user connection user name (optional, NULL: no user name)
* @param password connection password (optional, NULL: no password)
* @param from from address (mandatory)
* @param to to recipient address (mandatory)
* @param cc cc recipient address (optional, NULL: no cc)
* @param bcc bcc recipient address (optional, NULL: no bcc)
* @param content_type: content-type to add to the e-mail body
* @param subject email subject (mandatory)
* @param mail_body email body (mandatory)
* @return U_OK on success
*/
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);
#endif
/**
* @}
*/
/**
* @defgroup cookie Cookies
* Cookies management functions
* @{
*/
/**
* ulfius_add_cookie_to_response
* add a cookie to the cookie map
* @param response the response to add the cookie to
* @param key the cookie key
* @param value the cookie value
* @param expires the expiration date of the ccokie in ISO format (optional)
* @param max_age the maximum age of the cookie in seconds (optional)
* @param domain the domain of the cookie (optional)
* @param path the path of the cookie (optional)
* @param secure wether the cookie must be secure or not (optional)
* @param http_only wether the cookie must be used only for http requests or not (optional)
* @return U_OK on success
*/
int ulfius_add_cookie_to_response(struct _u_response * response, const char * key, const char * value, const char * expires, const unsigned int max_age,
const char * domain, const char * path, const int secure, const int http_only);
/**
* ulfius_add_same_site_cookie_to_response
* add a cookie to the cookie map with a SameSite attribute
* @param response the response to add the cookie to
* @param key the cookie key
* @param value the cookie value
* @param expires the expiration date of the ccokie in ISO format (optional)
* @param max_age the maximum age of the cookie in seconds (optional)
* @param domain the domain of the cookie (optional)
* @param path the path of the cookie (optional)
* @param secure wether the cookie must be secure or not (optional)
* @param http_only wether the cookie must be used only for http requests or not (optional)
* @param same_site parameter must have one of the following values:
* - U_COOKIE_SAME_SITE_NONE - No SameSite attribute
* - U_COOKIE_SAME_SITE_STRICT - SameSite attribute set to 'Strict'
* - U_COOKIE_SAME_SITE_LAX - SameSite attribute set to 'Lax'
* @return U_OK on success
*/
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,
const char * domain, const char * path, const int secure, const int http_only, const int same_site);
/**
* @}
*/
/**
* @defgroup parameters URL, POST and Header parameters
* URL, POST and Header management functions
* @{
*/
/**
* ulfius_add_header_to_response
* add a header to the response
* @param response the response to be updated
* @param key the key of the header
* @param value the value of the header
* @return U_OK on success
*/
int ulfius_add_header_to_response(struct _u_response * response, const char * key, const char * value);
/**
* ulfius_set_string_body_request
* Set a string string_body to a request, replace any existing body in the request
* @param request the request to be updated
* @param string_body string to set to the body response must end with a '\0' character
* @return U_OK on success
*/
int ulfius_set_string_body_request(struct _u_request * request, const char * string_body);
/**
* ulfius_set_binary_body_request
* Set a binary binary_body to a request, replace any existing body in the request
* @param request the request to be updated
* @param binary_body an array of char to set to the body response
* @param length the length of binary_body to set to the request body
* return U_OK on success
*/
int ulfius_set_binary_body_request(struct _u_request * request, const char * binary_body, const size_t length);
/**
* ulfius_set_empty_body_request
* Set an empty request body
* @param request the request to be updated
* @return U_OK on success
*/
int ulfius_set_empty_body_request(struct _u_request * request);
/**
* ulfius_set_string_body_response
* Add a string body to a response, replace any existing body in the response
* @param response the response to be updated
* @param status the http status code to set to the response
* @param body the string body to set, must end with a '\0' character
* @return U_OK on success
*/
int ulfius_set_string_body_response(struct _u_response * response, const unsigned int status, const char * body);
/**
* ulfius_set_binary_body_response
* Add a binary body to a response, replace any existing body in the response
* @param response the response to be updated
* @param status the http status code to set to the response
* @param body the array of char to set
* @param length the length of body to set to the request body
* @return U_OK on success
*/
int ulfius_set_binary_body_response(struct _u_response * response, const unsigned int status, const char * body, const size_t length);
/**
* ulfius_set_empty_body_response
* Set an empty response with only a status
* @param response the response to be updated
* @param status the http status code to set to the response
* @return U_OK on success
*/
int ulfius_set_empty_body_response(struct _u_response * response, const unsigned int status);
/**
* @}
*/
/**
* @defgroup stream Response streaming
* Response streaming function
* @{
*/
/**
* ulfius_set_stream_response
* Set an stream response with a status
* @param response the response to be updated
* @param status the http status code to set to the response
* @param stream_callback a pointer to a function that will handle the response stream
* @param stream_callback_free a pointer to a function that will free its allocated resources during stream_callback
* @param stream_size size of the streamed data (U_STREAM_SIZE_UNKNOWN if unknown)
* @param stream_block_size preferred size of each stream chunk, may be overwritten by the system if necessary
* @param stream_user_data a user-defined pointer that will be available in stream_callback and stream_callback_free
* @return U_OK on success
*/
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);
/**
* @}
*/
/**
* @defgroup request_response_cookie struct _u_request, struct _u_response and struct _u_cookie
* struct _u_request, struct _u_response and struct _u_cookie management functions
* @{
*/
/**
* ulfius_init_request
* Initialize a request structure by allocating inner elements
* @param request the request to initialize
* @return U_OK on success
*/
int ulfius_init_request(struct _u_request * request);
/**
* ulfius_clean_request
* clean the specified request's inner elements
* user must free the parent pointer if needed after clean
* or use ulfius_clean_request_full
* @param request the request to cleanup
* @return U_OK on success