HTTP Server#

This module contains the HTTP Server API functions.

Functions#

sl_status_t
sl_http_server_init(sl_http_server_t *handle, const sl_http_server_config_t *config)

HTTP server initialization function.

sl_status_t
sl_http_server_deinit(sl_http_server_t *handle)

Deinitializes the HTTP server and frees all associated resources.

sl_status_t
sl_http_server_start(sl_http_server_t *handle)

Starts the HTTP server to accept incoming requests.

sl_status_t
sl_http_server_stop(sl_http_server_t *handle)

Stops the HTTP server and waits for all ongoing requests to complete.

sl_status_t
sl_http_server_get_request_headers(sl_http_server_t *handle, sl_http_server_request_t *request, sl_http_header_t *headers, uint16_t header_count)

Retrieves the headers from an HTTP request.

sl_status_t
sl_http_server_read_request_data(sl_http_server_t *handle, sl_http_recv_req_data_t *data)

Reads the HTTP request data being received after the headers.

sl_status_t
sl_http_server_send_response(sl_http_server_t *handle, sl_http_server_response_t *response)

Sends the HTTP response for the current request.

sl_status_t
sl_http_server_write_data(sl_http_server_t *handle, uint8_t *data, uint32_t data_length)

Sends a chunk of data as part of the HTTP response.

Function Documentation#

sl_http_server_init#

sl_status_t sl_http_server_init (sl_http_server_t * handle, const sl_http_server_config_t * config)

HTTP server initialization function.

Parameters
[out]handle

Pointer to an sl_http_server_t object that will be initialized with the server handle. Must not be NULL.

[in]config

Pointer to the HTTP server configuration structure of type sl_http_server_config_t. Must not be NULL.

This function initializes an HTTP server using the provided configuration structure and returns a handle to the server. It sets up the server's configuration, request buffer, and event flags. All subsequent HTTP server APIs must be used with the server handle returned in the handle parameter.

Returns


Definition at line 54 of file components/service/sl_http_server/inc/sl_http_server.h

sl_http_server_deinit#

sl_status_t sl_http_server_deinit (sl_http_server_t * handle)

Deinitializes the HTTP server and frees all associated resources.

Parameters
[in]handle

Pointer to an sl_http_server_t object representing the HTTP server handle. Must not be NULL.

This function deinitializes the HTTP server, releasing all resources associated with the provided HTTP server handle. It closes the server socket, deletes event flags, and resets the request handlers. After calling this function, the handle parameter should not be used in any other API without reinitializing it using sl_http_server_init.

  • The HTTP server handle should be initialized using sl_http_server_init before calling this function.

Returns

Note

  • After calling this function, the handle parameter should not be used in any other API without calling sl_http_server_init again.


Definition at line 79 of file components/service/sl_http_server/inc/sl_http_server.h

sl_http_server_start#

sl_status_t sl_http_server_start (sl_http_server_t * handle)

Starts the HTTP server to accept incoming requests.

Parameters
[in]handle

Pointer to an sl_http_server_t object representing the HTTP server handle. Must not be NULL.

This function spawns a new thread dedicated to accept the incoming HTTP requests on the configured HTTP port. The HTTP request handlers are invoked from this thread to process the requests. It ensures that the server is ready to handle client connections.

  • The HTTP server handle should be initialized using sl_http_server_init before calling this function.

Returns


Definition at line 101 of file components/service/sl_http_server/inc/sl_http_server.h

sl_http_server_stop#

sl_status_t sl_http_server_stop (sl_http_server_t * handle)

Stops the HTTP server and waits for all ongoing requests to complete.

Parameters
[in]handle

Pointer to an sl_http_server_t object representing the HTTP server handle. Must not be NULL.

This function sends a stop command to the HTTP server thread, and waits for all ongoing requests to complete. It ensures that the server thread is properly terminated, and all resources associated with the server thread are released.

  • The HTTP server handle should be initialized using sl_http_server_init before calling this function.

Returns


Definition at line 122 of file components/service/sl_http_server/inc/sl_http_server.h

sl_http_server_get_request_headers#

sl_status_t sl_http_server_get_request_headers (sl_http_server_t * handle, sl_http_server_request_t * request, sl_http_header_t * headers, uint16_t header_count)

Retrieves the headers from an HTTP request.

Parameters
[in]handle

Pointer to an sl_http_server_t object representing the HTTP server handle. Must not be NULL.

[in]request

Pointer to an sl_http_server_request_t object representing the HTTP request from which headers are to be retrieved. Must not be NULL.

[out]headers

Pointer to an array of sl_http_header_t where the retrieved headers would be stored. Must not be NULL.

[in]header_count

The number of headers that the headers array can hold.

This function extracts all headers from a given HTTP request, and stores them in the provided headers array. It parses the HTTP request to identify and extract the headers, which are then stored in the array pointed to by the headers parameter.

Returns


Definition at line 150 of file components/service/sl_http_server/inc/sl_http_server.h

sl_http_server_read_request_data#

sl_status_t sl_http_server_read_request_data (sl_http_server_t * handle, sl_http_recv_req_data_t * data)

Reads the HTTP request data being received after the headers.

Parameters
[in]handle

Pointer to an sl_http_server_t object representing the HTTP server handle. Must not be NULL.

[in]data

Pointer to an sl_http_recv_req_data_t structure where the received request data would be stored. Must not be NULL.

This function reads the data portion of an HTTP request that follows the headers. It is used to process the body of the request.

    • The req_data_length parameter in sl_http_server_request_t must be greater than 0.

    • This function must be called from within the request handler of the corresponding request.

Returns


Definition at line 178 of file components/service/sl_http_server/inc/sl_http_server.h

sl_http_server_send_response#

sl_status_t sl_http_server_send_response (sl_http_server_t * handle, sl_http_server_response_t * response)

Sends the HTTP response for the current request.

Parameters
[in]handle

Pointer to an sl_http_server_t object representing the HTTP server handle. Must not be NULL.

[in]response

Pointer to an sl_http_server_response_t object representing the response to be sent. Must not be NULL.

This function makes the HTTP server send the response to the current request. It can only be called once per request.

  • The HTTP server handle should be initialized using sl_http_server_init before calling this function.

Returns


Definition at line 202 of file components/service/sl_http_server/inc/sl_http_server.h

sl_http_server_write_data#

sl_status_t sl_http_server_write_data (sl_http_server_t * handle, uint8_t * data, uint32_t data_length)

Sends a chunk of data as part of the HTTP response.

Parameters
[in]handle

Pointer to an sl_http_server_t object representing the HTTP server handle. Must not be NULL.

[in]data

Pointer to the data to be sent. Must not be NULL.

[in]data_length

Length of the data to be sent.

This function sends a chunk of data as part of the HTTP response. It should be used after a response has been started with sl_http_server_send_response, and can be called multiple times to send the response data in chunks.

Returns


Definition at line 230 of file components/service/sl_http_server/inc/sl_http_server.h