Ulfius
HTTP Framework for REST Applications in C
|
Include file ulfius.h
in your source file:
You can use pkg-config
to provide the compile and link options for Ulfius:
If you don't or can't have pkg-config for the build, you can set the linker options -lulfius -lorcania -lyder
.
The options -lorcania
and -lyder
are not necessary if you don't directly use Orcania or Yder functions. But in doubt, add them anyway.
On your linker command, add Ulfius as a dependency library, e.g. -lulfius
for gcc.
When specified, some functions return U_OK
on success, and other values otherwise. U_OK
is 0, other values are non-0 values. The defined return value list is the following:
Ulfius uses the memory allocation functions malloc/realloc/calloc/free
by default, but you can overwrite this and use any other memory allocation functions of your choice. Use Orcania's functions o_set_alloc_funcs
and o_get_alloc_funcs
to set and get memory allocation functions.
Accessing those functions requires you to directly link your application also against the Orcania library. To do so, add -lorcania
to your linking command.
If you use a version of libmicrohttpd
older than 0.9.61
, you need to set the mhd_response_copy_data = 1
in your _u_instance
if you use a memory allocator whose allocated return values may not directly be passed to free() or if you want to make sure all free() will always go via your user-provided free callback.
Data structures allocated have their specific cleanup functions. To free pointer allocated, you should use the function u_free
that is intended to use your memory management functions.
It's recommended to use ulfius_global_init
and ulfius_global_close
at the beginning and at the end of your program to initialize and cleanup internal values and settings. This will make outgoing requests faster, especially if you use lots of them, and dispatch your memory allocation functions in curl and Jansson if you changed them. These functions are NOT thread-safe, so you must use them in a single thread context.
Ulfius framework runs as an async task in the background. When initialized, a thread is executed in the background. This thread will listen to the specified port and dispatch the calls to the specified callback functions. Ulfius allows adding and removing new endpoints during the instance execution.
To run a webservice, you must initialize a struct _u_instance
and add your endpoints.
The struct _u_instance
is defined as:
In the struct _u_instance
structure, the element port
must be set to the port number you want to listen to, the element bind_address
is used if you want to listen only to a specific IP address. The element mhd_daemon
is used by the framework, don't modify it.
You can use the functions ulfius_init_instance
, ulfius_init_instance_ipv6
and ulfius_clean_instance
to facilitate the manipulation of the structure:
Since Ulfius 2.6, you can bind to IPv4 connections, IPv6 or both. By default, ulfius_init_instance
binds to IPv4 addresses only. If you want to bind to both IPv4 and IPv6 addresses, use ulfius_init_instance_ipv6
with the value parameter network_type
set to U_USE_ALL
. If you want to bind to IPv6 addresses only, use ulfius_init_instance_ipv6
with the value parameter network_type
set to U_USE_IPV6
.
If you bind your instance to an address, you MUST set the port number to struct sockaddr_in.sport
because the struct _u_instance.port
will be ignored.
The struct _u_endpoint
is defined as:
Some functions help you facilitate endpoints manipulation:
HTTP Method can be an existing or not existing method, or *
for any method. You must specify a url_prefix, a url_format or both, callback_function is mandatory, user_data is optional.
If you fill your array of endpoints manually, your struct _u_endpoint
array MUST end with an empty struct _u_endpoint
.
You can manually declare an endpoint or use the dedicated functions as int ulfius_add_endpoint
or int ulfius_add_endpoint_by_val
. It's recommended to use the dedicated functions to fill this array though.
If you manipulate the attribute u_instance.endpoint_list
, you must end the list with an empty endpoint (see const struct _u_endpoint * ulfius_empty_endpoint()
), and you must set the attribute u_instance.nb_endpoints
accordingly. Also, you must use dynamically allocated values (malloc
) for attributes http_method
, url_prefix
and url_format
.
Ulfius allows multiple callbacks for the same endpoint. This is helpful when you need to execute several actions in sequence, for example check authentication, get resource, set cookie, then gzip response body. That's also why a priority must be set for each callback.
The priority is in descending order, which means that it starts with 0 (highest priority) and priority decreases when priority number increases. There is no more signification to the priority number, which means you can use any increments of your choice.
Warning
: Having 2 callback functions with the same priority number will result in an undefined execution order result.
To help passing parameters between callback functions of the same request, the value struct _u_response.shared_data
can be used. It's recommended to use the function ulfius_set_response_shared_data
with a pointer to a free function for shared_data
, therefore the framework will automatically clean struct _u_response.shared_data
at the end of the callback list.
If you need to differentiate multiple URLs with similar pattern, you can use priorities among multiple callback function.
For example, if you have 2 endpoints with the following patterns:
1- /example/:id
2- /example/findByStatus
You'll probably need the callback referred in 2- to be called and the callback referred in 1- not when the URL called is the exact pattern as in 2-. Nevertheless, you'll need callback referred in 1- in all the other cases.
In that case, you'll have to set a higher priority to the endpoint with the URL 2- and return its callback function with the value U_CALLBACK_COMPLETE
. Remember, if the first callback returns U_CALLBACK_CONTINUE
, the second callback will be called afterwards.
The starting point function are ulfius_start_framework
, ulfius_start_secure_framework
, ulfius_start_secure_ca_trust_framework
or ulfius_start_framework_with_mhd_options
:
In your program, where you want to start the web server, execute the function ulfius_start_framework(struct _u_instance * u_instance)
for a non-secure http connection.
Use the function ulfius_start_secure_framework(struct _u_instance * u_instance, const char * key_pem, const char * cert_pem)
for a secure https connection, using a valid private key and a valid corresponding server certificate, see GnuTLS documentation for certificate generation.
Finally, use the function 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)
to start a secure https connection and be able to authenticate clients with a certificate.
Those function accept the previously declared instance
as first parameter. You can reuse the same callback function as much as you want for different endpoints. On success, these functions returns U_OK
, otherwise an error code.
Note: for security concerns, after running ulfius_start_secure_framework
or ulfius_start_secure_ca_trust_framework
, you can free the parameters key_pem
, cert_pem
and root_ca_pem
if you want to.
To stop the webservice, call the following function:
The callback function is the function executed when a user calls an endpoint managed by your webservice (as defined in your struct _u_endpoint
list).
The callback function has the following signature:
In the callback function definition, the variables request
and response
will be initialized by the framework, and the user_data
variable will be assigned to the user_data defined in your endpoint list definition.
The request variable is defined as:
Functions dedicated to handle the request:
The function ulfius_set_request_properties
allows to put a variable set of request properties in a single-line. The parameter list MUST end with the option U_OPT_NONE
Options available:
Option | Description |
---|---|
U_OPT_NONE | Empty option to complete a ulfius_set_request_properties or ulfius_set_request_properties |
U_OPT_HTTP_VERB | http method (GET, POST, PUT, DELETE, etc.), expected option value type: const char * |
U_OPT_HTTP_URL | 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_URL_APPEND | append char * value to the current url, expected option value type: const char * |
U_OPT_HTTP_PROXY | proxy address to use for outgoing connections, used by ulfius_send_http_request, expected option value type: const char * |
U_OPT_NETWORK_TYPE | 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 |
U_OPT_CHECK_SERVER_CERTIFICATE | check server certificate and hostname, default true, used by ulfius_send_http_request, expected option value type: int |
U_OPT_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, expected option value type: int |
U_OPT_CHECK_PROXY_CERTIFICATE | 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 | 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 | follow URL redirections, used by ulfius_send_http_request, expected option value type: int |
U_OPT_CA_PATH | 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 | 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 | basic authentication username, expected option value type: const char * |
U_OPT_AUTH_BASIC_PASSWORD | basic authentication password, expected option value type: const char * |
U_OPT_URL_PARAMETER | 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 | Add to the map containing the header variables, expected option value type: const char *, const char * |
U_OPT_COOKIE_PARAMETER | Add to the map containing the cookie variables, expected option value type: const char *, const char * |
U_OPT_POST_BODY_PARAMETER | Add to the map containing the post body variables (if available), expected option value type: const char *, const char * |
U_OPT_URL_PARAMETER_REMOVE | 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 | Remove from map containing the header variables, expected option value type: const char * |
U_OPT_COOKIE_PARAMETER_REMOVE | Remove from map containing the cookie variables, expected option value type: const char * |
U_OPT_POST_BODY_PARAMETER_REMOVE | Remove from map containing the post body variables (if available), expected option value type: const char * |
U_OPT_BINARY_BODY | Set a raw body to the request or the response, expected option value type: const char *, size_t |
U_OPT_STRING_BODY | Set a char * body to the request or the response, expected option value type: const char * |
U_OPT_JSON_BODY | Set a stringified json_t * body to the request or the response, expected option value type: json_t * |
U_OPT_CLIENT_CERT_FILE | 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 | 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 | password to unlock client key file, available only if GnuTLS support is enabled, expected option value type: const char * |
Example:
The response variable is defined as:
In the response variable set by the framework to the callback function, the structure is initialized with no data.
The user can set the binary_body
before the return statement, or no response body at all if no need. If a binary_body
is set, its size must be set to binary_body_length
. binary_body
is freed by the framework when the response has been sent to the client, so you must use dynamically allocated values. If no status is set, status 200 will be sent to the client.
Some functions are dedicated to handle the response:
The function ulfius_set_response_properties
allows to put a variable set of response properties in a single-line. The parameter list MUST end with the option U_OPT_NONE
Options available:
Option | Description |
---|---|
U_OPT_NONE | Empty option to complete a ulfius_set_request_properties or ulfius_set_request_properties |
U_OPT_STATUS | HTTP response status code (200, 404, 500, etc), expected option value type: long |
U_OPT_AUTH_REALM | realm to send to the client response on authentication failed, expected option value type: const char * |
U_OPT_SHARED_DATA | any data shared between callback functions, must be allocated and freed by the callback functions, expected option value type: void * |
U_OPT_TIMEOUT | Timeout in seconds to close the connection because of inactivity between the client and the server, expected option value type: long |
U_OPT_HEADER_PARAMETER | Add to the map containing the header variables, expected option value type: const char *, const char * |
U_OPT_HEADER_PARAMETER_REMOVE | Remove from map containing the header variables, expected option value type: const char * |
U_OPT_BINARY_BODY | Set a raw body to the request or the response, expected option value type: const char *, size_t |
U_OPT_STRING_BODY | Set a char * body to the request or the response, expected option value type: const char * |
U_OPT_JSON_BODY | Set a stringified json_t * body to the request or the response, expected option value type: json_t * |
Example:
The callback returned value can have the following values:
U_CALLBACK_CONTINUE
: The framework can transfer the request and the response to the next callback function in priority order if there is one, or complete the transaction and send back the response to the client.U_CALLBACK_IGNORE
: The framework can transfer the request and the response to the next callback function in priority order if there is one, or complete the transaction and send back the response to the client, the counter request->callback_position
will not be incremented. If at the end of the callback list request->callback_position
is 0, the default callback (if set) will be called.U_CALLBACK_COMPLETE
: The framework must complete the transaction and send the response to the client without calling any further callback function.U_CALLBACK_UNAUTHORIZED
: The framework must complete the transaction without calling any further callback function and send an unauthorized response to the client with the status 401, the body specified and the auth_realm
value if specified.U_CALLBACK_ERROR
: An error occurred during execution, the framework will complete the transaction without calling any further callback function and send an error 500 to the client.Except for the return values U_CALLBACK_UNAUTHORIZED
and U_CALLBACK_ERROR
, the callback return value isn't useful to specify the response sent back to the client. Use the struct _u_response
variable in your callback function to set all values in the HTTP response.
In Ulfius 2.0, hard dependency with libjansson
has been removed, the Jansson library is now optional but enabled by default.
If you want to remove JSON dependency, build Ulfius library using Makefile with the flag JANSSONFLAG=1
or CMake with the option -DWITH_WEBSOCKET=off
.
if JSON is enabled, the following functions are available in Ulfius:
The jansson
API documentation is available at the following address: Jansson documentation.
Note: According to the JSON RFC section 6, the MIME media type for JSON text is application/json
. Thus, if there is no HTTP header specifying JSON content-type, the functions ulfius_get_json_body_request
and ulfius_get_json_body_response
will return NULL.
In addition with manipulating the raw parameters of the structures, you can use the _u_request
and _u_response
structures by using specific functions designed to facilitate their use and memory management:
The Ulfius framework will automatically free the variables referenced by the request and responses structures, so you must use dynamically allocated values for the response pointers.
You may be careful with characters encoding if you use non UTF8 characters in your application or webservice source code, and especially if you use different encoding in the same application. Ulfius may not work properly.
In the callback function, you can access the POST parameters in the struct _u_request.map_post_body
. The parameters keys are case-sensitive.
This variable is a struct _u_map
, therefore you can access it using the struct _u_map documentation.
In the callback function, you can access the URL and query parameters in the struct _u_request.map_url
. This variable contains both URL parameters and query string parameters, the parameters keys are case-sensitive. If a parameter appears multiple times in the URL and the query string, the values will be chained in the struct _u_request.map_url
, separated by a comma ,
.
This variable is a struct _u_map
, therefore you can access it using the struct _u_map documentation.
In the callback function, you can access the header parameters in the struct _u_request.map_header
.
In the callback function, you can access the header parameters in the struct _u_request.map_header
. The parameters keys are case-sensitive. If a parameter appears multiple times in the header, the values will be chained in the struct _u_request.map_header
, separated by a comma ,
.
This variable is a struct _u_map
, therefore you can access it using the struct _u_map documentation.
The map_cookie structure will contain a set of key/values for the cookies. The cookie structure is defined as
You can use the functions ulfius_add_cookie_to_response
or ulfius_add_same_site_cookie_to_response
in your callback function to facilitate cookies management. These functions are defined as:
If you need to remove a cookie on the client, you can send a cookie with the same key but an empty value and a expiration in the past.
Please note that the client (browser, app, etc.) doesn't have to remove the cookies if it doesn't want to.
Ulfius allows file upload to the server. Beware that an uploaded file will be stored in the request object in memory, so uploading large files may dramatically slow the application or even crash it, depending on your system. An uploaded file is stored in the request->map_body
structure. You can use u_map_get_length
to get the exact length of the file as it may not be a string format.
If you want to limit the size of a post parameter, if you want to limit the file size for example, set the value struct _u_instance.max_post_param_size
. Files or post data exceeding this size will be truncated to the size struct _u_instance.max_post_param_size
. If this parameter is 0, then no limit is set. Default value is 0.
If you want to handle file upload yourself, you can intercept the file upload process with your own callback function. Before running the webservice with ulfius_start_framework
, you must call the function ulfius_set_upload_file_callback_function
with a pointer to your file upload callback function. By using this method, the specified callback function will be executed as much as needed with a chunk of the file upload each time.
This function ulfius_set_upload_file_callback_function
has the following prototype:
This callback function will be called before all the other callback functions, and be aware that not all parameters, especially URL parameters, will be present during the file upload callback function executions.
See examples/sheep_counter
for a file upload example.
By default, Ulfius will check input body data to be valid utf8 characters. This will most likely break binary files uploaded via multipart/form-data
transfert-encoding.
If you don't or can't use ulfius_set_upload_file_callback_function
, you must disable utf8 check before starting ulfius instance, then in the callback function, make sure to use u_map_get_length
for every request->map_post_body
element.
If you need to stream data, i.e. send a variable and potentially large amount of data, or if you need to send a chunked response, you can define and use stream_callback_function
in the struct _u_response
.
Not that if you stream data to the client, any data that was in the response->binary_body
will be ignored. You must at least set the function pointer struct _u_response.stream_callback
to stream data. Set stream_size
to U_STREAM_SIZE_UNKNOWN if you don't know the size of the data you need to send, like in audio stream for example. Set stream_block_size
according to you system resources to avoid out of memory errors, also, set stream_callback_free
with a pointer to a function that will free values allocated by your stream callback function, as a close()
file for example, and finally, you can set stream_user_data
to a pointer.
You can use the function ulfius_set_stream_response
to set those parameters.
The prototype of the stream_callback
function is the following:
The return value must be the size of the data put in out_buf
.
This function will be called over and over in loop as long as the client has the connection opened.
If you want to close the stream from the server side, return U_STREAM_END
in the stream_callback
function. If a problem occurred, you can close the connection with a U_STREAM_ERROR
return value.
While the stream_callback_free
function is as simple as:
Check the application stream_example
in the example folder.
The websocket protocol is defined in the RFC6455. A websocket is a full-duplex communication layer between a server and a client initiated by a HTTP request. Once the websocket handshake is complete between the client and the server, the TCP socket between them is kept open and messages in a specific format can be exchanged. Any side of the socket can send a message to the other side, which allows the server to push messages to the client.
Ulfius implements websocket communication, both server-side and client-side. The following chapter will describe how to create a websocket service or a websocket client by using callback functions. The framework will handle sending and receiving messages with the clients, and your application will deal with high level functions to facilitate the communication process.
During the websocket connection, you can either send messages, read the incoming messages, close the connection, or wait until the connection is closed by the client or by a network problem.
A websocket message has the following structure:
The different opcode values available are the following:
To send a message to the client, use the dedicated functions ulfius_websocket_send_message
, ulfius_websocket_send_fragmented_message
or ulfius_websocket_send_json_message
:
To get the first message of the incoming or outcoming if you need to with ulfius_websocket_pop_first_message
, this will remove the first message of the list, and return it as a pointer. You must free the message using the function ulfius_clear_websocket_message
after use:
All the sent or received messages are stored by default in the struct _websocket_manager
attributes message_list_incoming
and message_list_outcoming
. To skip storing incoming and/or outcoming messages, you can set the flag struct _websocket_manager.keep_messages
with the values U_WEBSOCKET_KEEP_INCOMING
, U_WEBSOCKET_KEEP_OUTCOMING
or U_WEBSOCKET_KEEP_NONE
. The flag is set to default with U_WEBSOCKET_KEEP_INCOMING|U_WEBSOCKET_KEEP_OUTCOMING
.
if you exchange messages in JSON format, you can use ulfius_websocket_parse_json_message
to parse a struct _websocket_message *
payload into a json_t *
object.
It seems that some browsers like Firefox or Chromium don't like to receive fragmented messages, they will close the connection with a fragmented message is received. Use ulfius_websocket_send_fragmented_message
with caution then.
To start a websocket communication between the client and your application, you must use the dedicated function ulfius_start_websocket_cb
with proper values:
According to the Websockets RFC, parameters websocket_protocol
and websocket_extensions
are specific for your application.
In Ulfius Implementation, if you specify a list of protocols as a string of protocol names, separated by a comma (,
), Ulfius framework will check each one and see if they match the list of protocols specified by the client. The resulting protocol list will be sent back to the client. Likewise, the websocket extension is specific to your application, you can specify a list of websocket extension separated by a comma (,
).
If no protocol match your list, the connection will be closed by the framework and will return an error 400 to the client. If you set a NULL
value for the protocol and/or the extension, Ulfius will not accept any protocols and/or extension sent by the client, but the websocket connexion will be opened.
3 callback functions are available for the websocket implementation:
websocket_manager_callback
: This function will be called in a separate thread, and the websocket will remain open as long as this callback function is not completed. In this function, your program will have access to the websocket status (connected or not), and the list of messages sent and received. When this function ends, the websocket will close itself automatically.websocket_incoming_message_callback
: This function will be called every time a new message is sent by the client. Although, it is in synchronous mode, which means that you won't have 2 different websocket_incoming_message_callback
of the same websocket executed at the same time.websocket_onclose_callback
: This optional function will be called right after the websocket connection is closed, but before the websocket structure is cleaned.You must specify at least one of the callback functions between websocket_manager_callback
or websocket_incoming_message_callback
.
When the function ulfius_stop_framework
is called, it will wait for all running websockets to end by themselves, there is no force close. So if you have a websocket_manager_callback
function running, you MUST end this function in order to make a clean stop of the http daemon.
For each of these callback function, you can specify a *_user_data
pointer containing any data you need.
Since Ulfius 2.7.0, you have advanced functions to handle websocket extensions based on the functions ulfius_add_websocket_extension_message_perform
for the server websockets and ulfius_add_websocket_client_extension_message_perform
for the clients websockets.
These functions add the possibility to run a callback function before a message is sent and/or after a message is received.
The callback functions websocket_extension_server_match
and websocket_extension_client_match
can be use if you expect to match an extension with parameters. If NULL
, then instead an exact match between const char * extension
and the extension received will be checked to enable or not this extension callback functions.
The callback function websocket_extension_message_out_perform
can modify the message data and data lenght and the RSV flags. The callback function websocket_extension_message_in_perform
can modify the message data only. Inside these functions, data_in
and data_len_in
are the current data, your extension callback function must update data_out
with a o_malloc
'ed data and set the new data length using data_len_out
and return U_OK
on success. If your function doesn't return U_OK
, the message data won't be updated and data_out
won't be free'd if set.
You can call ulfius_add_websocket_extension_message_perform
or ulfius_add_websocket_client_extension_message_perform
multiple times for a websocket definition. In that case the extension callbacks function will be called in the same order for the websocket_extension_message_out_perform
callbacks, and in reverse order for the websocket_extension_message_in_perform
callbacks.
The Websocket extension permessage-deflate used to compress the message data is available in Ulfius. To use this extension in your websocket server, you must call ulfius_add_websocket_deflate_extension
after calling ulfius_set_websocket_response
and before finishing the callback endpoint.
See the sample code in websocket_example/websocket_server.c
To close a websocket communication from the server, you can do one of the following:
websocket_manager_callback
, it will result in closing the websocket connectionU_WEBSOCKET_OPCODE_CLOSE
ulfius_websocket_wait_close
or ulfius_websocket_send_close_signal
described belowIf no websocket_manager_callback
is specified, you can send a U_WEBSOCKET_OPCODE_CLOSE
in the websocket_incoming_message_callback
function when you need, or call the function ulfius_websocket_send_close_signal
.
If a callback function websocket_onclose_callback
has been specified, this function will be executed on every case at the end of the websocket connection.
If the websocket handshake hasn't been correctly completed or if an error appears during the handshake connection, the callback websocket_onclose_callback
will be called anyway, even if the callback functions websocket_manager_callback
or websocket_incoming_message_callback
are skipped due to no websocket connection. This is to allow the calling program to close opened resources or clean allocated memory. Beware that in this specific case, the parameter struct _websocket_manager * websocket_manager
may be NULL
.
The following functions allow the application to know if the the websocket is still open, to enforce closing the websocket or to wait until the websocket is closed by the client:
Ulfius allows to create a websocket connection as a client. The behavior is quite similar to the server-side websocket. The application will open a websocket connection specified by a struct _u_request
, and a set of callback functions to manage the websocket once connected.
You can manually fill the struct _u_request
with your parameters or use the dedicated function ulfius_set_websocket_request
:
The url
specified must have one of the following form:
The websocket_protocol
and websocket_extensions
values are optional. To specify multiple protocol, you must separate them with the comma ,
character. To specify multiple extensions, you must separate them with the semicolon ;
character.
You can also specify additional headers or cookies to the request.
Any body parameter or body raw value will be ignored, the header Content-Length
will be set to 0.
The header User-Agent
value will be Ulfius Websocket Client Framework
, feel free to modify it afterwards if you need.
The Websocket extension permessage-deflate used to compress the message data is available in Ulfius. To use this extension in your websocket client, you must call ulfius_add_websocket_client_deflate_extension
after calling ulfius_set_websocket_request
and before calling ulfius_open_websocket_client_connection
. Also, the parameter struct _websocket_client_handler
must be initialized to {NULL, NULL}
before calling ulfius_set_websocket_request
.
See the sample code in websocket_example/websocket_client.c
Once the request is completed, you can open the websocket connection with ulfius_open_websocket_client_connection
:
If the websocket connection is established, U_OK
will be returned and the websocket connection will be executed in a separate thread.
To close a websocket communication, you can do one of the following:
websocket_manager_callback
, it will result in closing the websocket connectionU_WEBSOCKET_OPCODE_CLOSE
ulfius_websocket_wait_close
described below, this function will return U_OK when the websocket is closedulfius_websocket_client_connection_send_close_signal
described below, this function is non-blocking, it will send a closing signal to the websocket and will return even if the websocket is still open. You can use ulfius_websocket_wait_close
or ulfius_websocket_client_connection_status
to check if the websocket is closed.Note - broken pipe
In some cases, when the client websocket connection is secured via TLS. If the connection is already closed, or broken, a SIGPIPE
signal can occur, leading to a program crash. To avoid this issue, you can handle SIGPIPE
signals using sigaction
.
The following code is a simple example where all SIGPIPE signals are simply ignored:
Check the sigaction
documentation for more details.
The following functions allow the application to know if the the websocket is still open, to enforce closing the websocket or to wait until the websocket is closed by the server:
Ulfius allows output functions to send HTTP or SMTP requests. These functions use libcurl
. You can disable these functions by appending the argument CURLFLAG=1
when you build the library with Makefile or by disabling the flag in CMake build:
The functions int ulfius_send_http_request(const struct _u_request * request, struct _u_response * response)
and 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)
are based on libcurl
API.
They allow to send an HTTP request with the parameters specified by the _u_request
structure. Use the parameter _u_request.http_url
to specify the distant URL to call.
You can fill the maps in the _u_request
structure with parameters, they will be used to build the request. Note that if you fill _u_request.map_post_body
with parameters, the content-type application/x-www-form-urlencoded
will be used to encode the data.
The response parameters is stored into the _u_response
structure. If you specify NULL for the response structure, the http call will still be made but no response details will be returned. If you use ulfius_send_http_request
, the response body will be stored in the parameter response->*body*
.
If you use ulfius_send_http_streaming_request
, the response body will be available in the write_body_function
specified in the call. The ulfius_send_http_streaming_request
can be used for streaming data or large response, or if you need to receive a checked response from the server.
Return value is U_OK
on success.
This functions are defined as:
The function ulfius_send_smtp_email
is used to send emails using a smtp server. It is based on libcurl
API. It's used to send plain/text emails via a smtp server. The function ulfius_send_smtp_rich_email
is used to send an e-mail with a specified content-type.
The functions are defined as:
Ulfius includes the functions ulfius_url_decode
and ulfius_url_encode
to transform a string into url-safe string and vice-versa. Both functions take a string in input and return a head-allocated string as output that must be u_free'd after use. Note: In the callback functions, the request->map_url
values are already url-decoded by the framework.
Those functions can be useful to debug requests or responses, or can be used to demonstrate how their respective parameters are translated in HTTP language.
The struct _u_map
is a simple key/value mapping API used in the requests and the response for setting parameters. The available functions to use this structure are:
Allow Content-Enconding
header with ulfius_send_http_request
to compress the response body Add http_compression callback example Add static_compressed_inmemory_website callback example Add callback return value U_CALLBACK_IGNORE
to igore incrementation of request->callback_position
Add ulfius_add_websocket_extension_message_perform
and ulfius_add_websocket_client_extension_message_perform
for advanced websocket extensions management Add Compression Extensions for WebSocket
When declaring a struct _websocket_client_handler
for websocket client API, you must initialize its members content to NULL
before using it:
When using ulfius_set_websocket_response
with parameters websocket_protocol
or websocket_extensions
set to NULL
, Ulfius will no longer accept any protocol or extension sent by the client, but will return no extension nor protocol to the client, and the websocket connexion will be "raw".
Add IPv6 support.
Add struct _u_request->callback_position
to know the position of the current callback in the callback list.
Add option to ignore non UTF8 strings in incoming requests.
Allow client certificate authentication
Improve websocket service features with lots of bugfixes and add the possibility to send a fragmented message.
Add websocket client functionality. Allow to create a websocket client connection and exchange messages with the websocket service. In http://
/ws://
non-secure mode or https://
/wss://
secure mode.
Add a command-line websocket client: uwsc
.
Not much on the API, a lot on the build process. Install via CMake script.
Allow to use your own callback function when uploading files with ulfius_set_upload_file_callback_function
, so a large file can be uploaded, even with the option struct _u_instance.max_post_param_size
set.
I know it wasn't long since Ulfius 2.0 was released. But after some review and tests, I realized some adjustments had to be made to avoid bugs and to clean the framework a little bit more.
Some of the adjustments made in the new release:
ulfius_send_http_request
that didn't send back all headers value with the same name (#19)ulfius.h
, because it could lead to horrifying bugs when you compile Ulfius with websocket but add #define U_DISABLE_WEBSOCKET
in your application.ulfius_set_[string|json|binary]_body
. You may have to update your legacy code.The minor version number has been incremented, from 2.0 to 2.1 because some of the changes may require changes in your own code.
Ulfius 2.0 brings several changes that make the library incompatible with Ulfius 1.0.x branch. The goal of making Ulfius 2.0 is to make a spring cleaning of some functions, remove what is apparently useless, and should bring bugs and memory loss. The main new features are multiple callback functions and websockets implementation.
Instead of having an authentication callback function, then a main callback function, you can now have as much callback functions as you want for the same endpoint. A priority
number has been added in the struct _u_endpoint
and the auth_callback function and its dependencies have been removed.
For example, let's say you have the following endpoints defined:
GET
/api/tomato/:tomato
=> tomato_get_callback
function, priority 10GET
/api/potato/:potato
=> potato_get_callback
function, priority 10GET
/api/*
=> api_validate_callback
function, priority 5*
*
=> authentication_callback
function, priority 1GET
*
=> gzip_body_callback
function, priority 99Then if the client calls the URL GET
/api/potato/myPotato
, the following callback functions will be called in that order:
authentication_callback
api_validate_callback
potato_get_callback
gzip_body_callback
Warning: In this example, the URL parameter myPotato
will be available only in the potato_get_callback
function, because the other endpoints did not defined a URL parameter after /potato
.
If you need to communicate between callback functions for any purpose, you can use the new parameter struct _u_response.shared_data
. This is a void *
pointer initialized to NULL
.
The dedicated function ulfius_set_response_shared_data
can be used to set struct _u_response.shared_data
and struct _u_response.free_shared_data
. If struct _u_response.free_shared_data
is set, the function will be used to free struct _u_response.shared_data
at the end of the callback list.
Note: If you call ulfius_set_response_shared_data
multiple times in the same request, before replacing _u_response.shared_data
, the function ulfius_set_response_shared_data
will free the previous pointer with the previous struct _u_response.free_shared_data
if set.
the values string_body
and json_body
have been removed from the structures struct _u_request
and struct _u_response
. This may be painless in the response if you used only the functions ulfius_set_xxx_body_response
. Otherwise, you should make small arrangements to your code.
Ulfius now allows websockets communication between the client and the server. Check the API.md file for implementation details.
Using websocket requires libgnutls. It also requires a recent version of Libmicrohttpd, at least 0.9.53.
If you don't need or can't use this feature, you can disable it by adding the option WEBSOCKETFLAG=1
to the make command when you build Ulfius:
In Ulfius 1.0, libjansson and libcurl were mandatory to build the library, but their usage was not in the core of the framework. Although they can be very useful, so the dependency is now optional.
They are enabled by default, but if you don't need them, you can disable them when you build Ulfius library.
This dependency allows to use the following functions:
If you want to disable these functions, append JANSSONFLAG=1
when you build Ulfius library.
This dependency allows to use the following functions:
If you want to disable these functions, append CURLFLAG=1
when you build Ulfius library.
If you wan to disable libjansson and libcurl, you can append both parameters.
You can find some ready-to-use callback functions in the folder example_callbacks.
struct _u_response
or struct _u_instance
. But you shouldn't use them like this anyway so it won't be a problem.ulfius_set_*_body_response
. You may have to update your legacy code. The new functions names are: If you already have programs that use Ulfius 1.x and want to update them to the brand new fresh Ulfius 2.0, it may require the following minor changes.
Endpoints structure have changed, ulfius_add_endpoint_by_val
now requires only one callback function, but requires a priority number.
If you don't use authentication callback functions, you can simply remove the NULL, NULL, NULL
parameters corresponding to the former authentication callback function pointer, the authentication callback user data, and the realm value. Then add any number as a priority, 0 for example.
If you use authentication callback functions, split your ulfius_add_endpoint_by_val
call in 2 separate calls, one for the authentication function, one for the main callback function. For example:
The return value for the callback functions must be adapted, instead of U_OK, U_ERROR or U_ERROR_UNAUTHORIZED, you must use one of the following:
If you want more details on the multiple callback functions, check the documentation.
Other functions may have change their name or signature, check the documentation for more information.