HTTP Server

Overview

The HTTP Server component provides an ability for running a lightweight web server on ESP32. Following are detailed steps to use the API exposed by HTTP Server:

  • httpd_start(): Creates an instance of HTTP server, allocate memory/resources for it depending upon the specified configuration and outputs a handle to the server instance. The server has both, a listening socket (TCP) for HTTP traffic, and a control socket (UDP) for control signals, which are selected in a round robin fashion in the server task loop. The task priority and stack size are configurable during server instance creation by passing httpd_config_t structure to httpd_start(). TCP traffic is parsed as HTTP requests and, depending on the requested URI, user registered handlers are invoked which are supposed to send back HTTP response packets.
  • httpd_stop(): This stops the server with the provided handle and frees up any associated memory/resources. This is a blocking function that first signals a halt to the server task and then waits for the task to terminate. While stopping, the task will close all open connections, remove registered URI handlers and reset all session context data to empty.
  • httpd_register_uri_handler(): A URI handler is registered by passing object of type httpd_uri_t structure which has members including uri name, method type (eg. HTTPD_GET/HTTPD_POST/HTTPD_PUT etc.), function pointer of type esp_err_t *handler (httpd_req_t *req) and user_ctx pointer to user context data.

Application Example

/* Our URI handler function to be called during GET /uri request */
esp_err_t get_handler(httpd_req_t *req)
{
    /* Send a simple response */
    const char[] resp = "URI GET Response";
    httpd_resp_send(req, resp, strlen(resp));
    return ESP_OK;
}

/* Our URI handler function to be called during POST /uri request */
esp_err_t post_handler(httpd_req_t *req)
{
    /* Destination buffer for content of HTTP POST request.
     * httpd_req_recv() accepts char* only, but content could
     * as well be any binary data (needs type casting).
     * In case of string data, null termination will be absent, and
     * content length would give length of string */
    char[100] content;

    /* Truncate if content length larger than the buffer */
    size_t recv_size = MIN(req->content_len, sizeof(content));

    int ret = httpd_req_recv(req, content, recv_size);
    if (ret <= 0) {  /* 0 return value indicates connection closed */
        /* Check if timeout occurred */
        if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
            /* In case of timeout one can choose to retry calling
             * httpd_req_recv(), but to keep it simple, here we
             * respond with an HTTP 408 (Request Timeout) error */
            httpd_resp_send_408(req);
        }
        /* In case of error, returning ESP_FAIL will
         * ensure that the underlying socket is closed */
        return ESP_FAIL;
    }

    /* Send a simple response */
    const char[] resp = "URI POST Response";
    httpd_resp_send(req, resp, strlen(resp));
    return ESP_OK;
}

/* URI handler structure for GET /uri */
httpd_uri_t uri_get = {
    .uri      = "/uri",
    .method   = HTTP_GET,
    .handler  = get_handler,
    .user_ctx = NULL
};

/* URI handler structure for POST /uri */
httpd_uri_t uri_post = {
    .uri      = "/uri",
    .method   = HTTP_POST,
    .handler  = post_handler,
    .user_ctx = NULL
};

/* Function for starting the webserver */
httpd_handle_t start_webserver(void)
{
    /* Generate default configuration */
    httpd_config_t config = HTTPD_DEFAULT_CONFIG();

    /* Empty handle to esp_http_server */
    httpd_handle_t server = NULL;

    /* Start the httpd server */
    if (httpd_start(&server, &config) == ESP_OK) {
        /* Register URI handlers */
        httpd_register_uri_handler(server, &uri_get);
        httpd_register_uri_handler(server, &uri_post);
    }
    /* If server failed to start, handle will be NULL */
    return server;
}

/* Function for stopping the webserver */
void stop_webserver(httpd_handle_t server)
{
    if (server) {
        /* Stop the httpd server */
        httpd_stop(server);
    }
}

Simple HTTP server example

Check HTTP server example under protocols/http_server/simple where handling of arbitrary content lengths, reading request headers and URL query parameters, and setting response headers is demonstrated.

Persistent Connections

HTTP server features persistent connections, allowing for the re-use of the same connection (session) for several transfers, all the while maintaining context specific data for the session. Context data may be allocated dynamically by the handler in which case a custom function may need to be specified for freeing this data when the connection/session is closed.

Persistent Connections Example

/* Custom function to free context */
void free_ctx_func(void *ctx)
{
    /* Could be something other than free */
    free(ctx);
}

esp_err_t adder_post_handler(httpd_req_t *req)
{
    /* Create session's context if not already available */
    if (! req->sess_ctx) {
        req->sess_ctx = malloc(sizeof(ANY_DATA_TYPE));  /*!< Pointer to context data */
        req->free_ctx = free_ctx_func;                  /*!< Function to free context data */
    }

    /* Access context data */
    ANY_DATA_TYPE *ctx_data = (ANY_DATA_TYPE *)req->sess_ctx;

    /* Respond */
    ...............
    ...............
    ...............

    return ESP_OK;
}

Check the example under protocols/http_server/persistent_sockets.

API Reference

Functions

esp_err_t httpd_register_uri_handler(httpd_handle_t handle, const httpd_uri_t *uri_handler)

Registers a URI handler.

Example usage:

esp_err_t my_uri_handler(httpd_req_t* req)
{
    // Recv , Process and Send
    ....
    ....
    ....

    // Fail condition
    if (....) {
        // Return fail to close session //
        return ESP_FAIL;
    }

    // On success
    return ESP_OK;
}

// URI handler structure
httpd_uri_t my_uri {
    .uri      = "/my_uri/path/xyz",
    .method   = HTTPD_GET,
    .handler  = my_uri_handler,
    .user_ctx = NULL
};

// Register handler
if (httpd_register_uri_handler(server_handle, &my_uri) != ESP_OK) {
   // If failed to register handler
   ....
}
Note
URI handlers can be registered in real time as long as the server handle is valid.

Return
  • ESP_OK : On successfully registering the handler
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_HANDLERS_FULL : If no slots left for new handler
  • ESP_ERR_HTTPD_HANDLER_EXISTS : If handler with same URI and method is already registered
Parameters
  • handle: handle to HTTPD server instance
  • uri_handler: pointer to handler that needs to be registered

esp_err_t httpd_unregister_uri_handler(httpd_handle_t handle, const char *uri, httpd_method_t method)

Unregister a URI handler.

Return
  • ESP_OK : On successfully deregistering the handler
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_NOT_FOUND : Handler with specified URI and method not found
Parameters
  • handle: handle to HTTPD server instance
  • uri: URI string
  • method: HTTP method

esp_err_t httpd_unregister_uri(httpd_handle_t handle, const char *uri)

Unregister all URI handlers with the specified uri string.

Return
  • ESP_OK : On successfully deregistering all such handlers
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_NOT_FOUND : No handler registered with specified uri string
Parameters
  • handle: handle to HTTPD server instance
  • uri: uri string specifying all handlers that need to be deregisterd

esp_err_t httpd_set_recv_override(httpd_req_t *r, httpd_recv_func_t recv_func)

Override web server’s receive function.

This function overrides the web server’s receive function. This same function is used to read and parse HTTP headers as well as body.

Note
This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
Return
  • ESP_OK : On successfully registering override
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
Parameters
  • r: The request being responded to
  • recv_func: The receive function to be set for this request

esp_err_t httpd_set_send_override(httpd_req_t *r, httpd_send_func_t send_func)

Override web server’s send function.

This function overrides the web server’s send function. This same function is used to send out any response to any HTTP request.

Note
This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
Return
  • ESP_OK : On successfully registering override
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
Parameters
  • r: The request being responded to
  • send_func: The send function to be set for this request

int httpd_req_to_sockfd(httpd_req_t *r)

Get the Socket Descriptor from the HTTP request.

This API will return the socket descriptor of the session for which URI handler was executed on reception of HTTP request. This is useful when user wants to call functions that require session socket fd, from within a URI handler, ie. : httpd_sess_get_ctx(), httpd_trigger_sess_close(), httpd_sess_update_timestamp().

Note
This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
Return
  • Socket descriptor : The socket descriptor for this request
  • -1 : Invalid/NULL request pointer
Parameters
  • r: The request whose socket descriptor should be found

int httpd_req_recv(httpd_req_t *r, char *buf, size_t buf_len)

API to read content data from the HTTP request.

This API will read HTTP content data from the HTTP request into provided buffer. Use content_len provided in httpd_req_t structure to know the length of data to be fetched. If content_len is too large for the buffer then user may have to make multiple calls to this function, each time fetching ‘buf_len’ number of bytes, while the pointer to content data is incremented internally by the same number.

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • If an error is returned, the URI handler must further return an error. This will ensure that the erroneous socket is closed and cleaned up by the web server.
  • Presently Chunked Encoding is not supported
Return
  • Bytes : Number of bytes read into the buffer successfully
  • 0 : Buffer length parameter is zero / connection closed by peer
  • HTTPD_SOCK_ERR_INVALID : Invalid arguments
  • HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket recv()
  • HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket recv()
Parameters
  • r: The request being responded to
  • buf: Pointer to a buffer that the data will be read into
  • buf_len: Length of the buffer

size_t httpd_req_get_hdr_value_len(httpd_req_t *r, const char *field)

Search for a field in request headers and return the string length of it’s value.

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • Once httpd_resp_send() API is called all request headers are purged, so request headers need be copied into separate buffers if they are required later.
Return
  • Length : If field is found in the request URL
  • Zero : Field not found / Invalid request / Null arguments
Parameters
  • r: The request being responded to
  • field: The header field to be searched in the request

esp_err_t httpd_req_get_hdr_value_str(httpd_req_t *r, const char *field, char *val, size_t val_size)

Get the value string of a field from the request headers.

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • Once httpd_resp_send() API is called all request headers are purged, so request headers need be copied into separate buffers if they are required later.
  • If output size is greater than input, then the value is truncated, accompanied by truncation error as return value.
  • Use httpd_req_get_hdr_value_len() to know the right buffer length
Return
  • ESP_OK : Field found in the request header and value string copied
  • ESP_ERR_NOT_FOUND : Key not found
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid HTTP request pointer
  • ESP_ERR_HTTPD_RESULT_TRUNC : Value string truncated
Parameters
  • r: The request being responded to
  • field: The field to be searched in the header
  • val: Pointer to the buffer into which the value will be copied if the field is found
  • val_size: Size of the user buffer “val”

size_t httpd_req_get_url_query_len(httpd_req_t *r)

Get Query string length from the request URL.

Note
This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid
Return
  • Length : Query is found in the request URL
  • Zero : Query not found / Null arguments / Invalid request
Parameters
  • r: The request being responded to

esp_err_t httpd_req_get_url_query_str(httpd_req_t *r, char *buf, size_t buf_len)

Get Query string from the request URL.

Note
  • Presently, the user can fetch the full URL query string, but decoding will have to be performed by the user. Request headers can be read using httpd_req_get_hdr_value_str() to know the ‘Content-Type’ (eg. Content-Type: application/x-www-form-urlencoded) and then the appropriate decoding algorithm needs to be applied.
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid
  • If output size is greater than input, then the value is truncated, accompanied by truncation error as return value
  • Use httpd_req_get_url_query_len() to know the right buffer length
Return
  • ESP_OK : Query is found in the request URL and copied to buffer
  • ESP_ERR_NOT_FOUND : Query not found
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid HTTP request pointer
  • ESP_ERR_HTTPD_RESULT_TRUNC : Query string truncated
Parameters
  • r: The request being responded to
  • buf: Pointer to the buffer into which the query string will be copied (if found)
  • buf_len: Length of output buffer

esp_err_t httpd_query_key_value(const char *qry, const char *key, char *val, size_t val_size)

Helper function to get a URL query tag from a query string of the type param1=val1&param2=val2.

Note
  • The components of URL query string (keys and values) are not URLdecoded. The user must check for ‘Content-Type’ field in the request headers and then depending upon the specified encoding (URLencoded or otherwise) apply the appropriate decoding algorithm.
  • If actual value size is greater than val_size, then the value is truncated, accompanied by truncation error as return value.
Return
  • ESP_OK : Key is found in the URL query string and copied to buffer
  • ESP_ERR_NOT_FOUND : Key not found
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_RESULT_TRUNC : Value string truncated
Parameters
  • qry: Pointer to query string
  • key: The key to be searched in the query string
  • val: Pointer to the buffer into which the value will be copied if the key is found
  • val_size: Size of the user buffer “val”

esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, size_t buf_len)

API to send a complete HTTP response.

This API will send the data as an HTTP response to the request. This assumes that you have the entire response ready in a single buffer. If you wish to send response in incremental chunks use httpd_resp_send_chunk() instead.

If no status code and content-type were set, by default this will send 200 OK status code and content type as text/html. You may call the following functions before this API to configure the response headers : httpd_resp_set_status() - for setting the HTTP status string, httpd_resp_set_type() - for setting the Content Type, httpd_resp_set_hdr() - for appending any additional field value entries in the response header

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • Once this API is called, the request has been responded to.
  • No additional data can then be sent for the request.
  • Once this API is called, all request headers are purged, so request headers need be copied into separate buffers if they are required later.
Return
  • ESP_OK : On successfully sending the response packet
  • ESP_ERR_INVALID_ARG : Null request pointer
  • ESP_ERR_HTTPD_RESP_HDR : Essential headers are too large for internal buffer
  • ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid request
Parameters
  • r: The request being responded to
  • buf: Buffer from where the content is to be fetched
  • buf_len: Length of the buffer

esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, size_t buf_len)

API to send one HTTP chunk.

This API will send the data as an HTTP response to the request. This API will use chunked-encoding and send the response in the form of chunks. If you have the entire response contained in a single buffer, please use httpd_resp_send() instead.

If no status code and content-type were set, by default this will send 200 OK status code and content type as text/html. You may call the following functions before this API to configure the response headers httpd_resp_set_status() - for setting the HTTP status string, httpd_resp_set_type() - for setting the Content Type, httpd_resp_set_hdr() - for appending any additional field value entries in the response header

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • When you are finished sending all your chunks, you must call this function with buf_len as 0.
  • Once this API is called, all request headers are purged, so request headers need be copied into separate buffers if they are required later.
Return
  • ESP_OK : On successfully sending the response packet chunk
  • ESP_ERR_INVALID_ARG : Null request pointer
  • ESP_ERR_HTTPD_RESP_HDR : Essential headers are too large for internal buffer
  • ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
Parameters
  • r: The request being responded to
  • buf: Pointer to a buffer that stores the data
  • buf_len: Length of the data from the buffer that should be sent out

esp_err_t httpd_resp_set_status(httpd_req_t *r, const char *status)

API to set the HTTP status code.

This API sets the status of the HTTP response to the value specified. By default, the ‘200 OK’ response is sent as the response.

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • This API only sets the status to this value. The status isn’t sent out until any of the send APIs is executed.
  • Make sure that the lifetime of the status string is valid till send function is called.
Return
  • ESP_OK : On success
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
Parameters
  • r: The request being responded to
  • status: The HTTP status code of this response

esp_err_t httpd_resp_set_type(httpd_req_t *r, const char *type)

API to set the HTTP content type.

This API sets the ‘Content Type’ field of the response. The default content type is ‘text/html’.

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • This API only sets the content type to this value. The type isn’t sent out until any of the send APIs is executed.
  • Make sure that the lifetime of the type string is valid till send function is called.
Return
  • ESP_OK : On success
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
Parameters
  • r: The request being responded to
  • type: The Content Type of the response

esp_err_t httpd_resp_set_hdr(httpd_req_t *r, const char *field, const char *value)

API to append any additional headers.

This API sets any additional header fields that need to be sent in the response.

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • The header isn’t sent out until any of the send APIs is executed.
  • The maximum allowed number of additional headers is limited to value of max_resp_headers in config structure.
  • Make sure that the lifetime of the field value strings are valid till send function is called.
Return
  • ESP_OK : On successfully appending new header
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_RESP_HDR : Total additional headers exceed max allowed
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
Parameters
  • r: The request being responded to
  • field: The field name of the HTTP header
  • value: The value of this HTTP header

esp_err_t httpd_resp_send_404(httpd_req_t *r)

Helper function for HTTP 404.

Send HTTP 404 message. If you wish to send additional data in the body of the response, please use the lower-level functions directly.

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • Once this API is called, all request headers are purged, so request headers need be copied into separate buffers if they are required later.
Return
  • ESP_OK : On successfully sending the response packet
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
Parameters
  • r: The request being responded to

esp_err_t httpd_resp_send_408(httpd_req_t *r)

Helper function for HTTP 408.

Send HTTP 408 message. If you wish to send additional data in the body of the response, please use the lower-level functions directly.

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • Once this API is called, all request headers are purged, so request headers need be copied into separate buffers if they are required later.
Return
  • ESP_OK : On successfully sending the response packet
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
Parameters
  • r: The request being responded to

esp_err_t httpd_resp_send_500(httpd_req_t *r)

Helper function for HTTP 500.

Send HTTP 500 message. If you wish to send additional data in the body of the response, please use the lower-level functions directly.

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • Once this API is called, all request headers are purged, so request headers need be copied into separate buffers if they are required later.
Return
  • ESP_OK : On successfully sending the response packet
  • ESP_ERR_INVALID_ARG : Null arguments
  • ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  • ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
Parameters
  • r: The request being responded to

int httpd_send(httpd_req_t *r, const char *buf, size_t buf_len)

Raw HTTP send.

Call this API if you wish to construct your custom response packet. When using this, all essential header, eg. HTTP version, Status Code, Content Type and Length, Encoding, etc. will have to be constructed manually, and HTTP delimeters (CRLF) will need to be placed correctly for separating sub-sections of the HTTP response packet.

If the send override function is set, this API will end up calling that function eventually to send data out.

Note
  • This API is supposed to be called only from the context of a URI handler where httpd_req_t* request pointer is valid.
  • Unless the response has the correct HTTP structure (which the user must now ensure) it is not guaranteed that it will be recognized by the client. For most cases, you wouldn’t have to call this API, but you would rather use either of : httpd_resp_send(), httpd_resp_send_chunk()
Return
  • Bytes : Number of bytes that were sent successfully
  • HTTPD_SOCK_ERR_INVALID : Invalid arguments
  • HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket send()
  • HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket send()
Parameters
  • r: The request being responded to
  • buf: Buffer from where the fully constructed packet is to be read
  • buf_len: Length of the buffer

esp_err_t httpd_start(httpd_handle_t *handle, const httpd_config_t *config)

Starts the web server.

Create an instance of HTTP server and allocate memory/resources for it depending upon the specified configuration.

Example usage:

//Function for starting the webserver
httpd_handle_t start_webserver(void)
{
     // Generate default configuration
     httpd_config_t config = HTTPD_DEFAULT_CONFIG();

     // Empty handle to http_server
     httpd_handle_t server = NULL;

     // Start the httpd server
     if (httpd_start(&server, &config) == ESP_OK) {
         // Register URI handlers
         httpd_register_uri_handler(server, &uri_get);
         httpd_register_uri_handler(server, &uri_post);
     }
     // If server failed to start, handle will be NULL
     return server;
}

Return
  • ESP_OK : Instance created successfully
  • ESP_ERR_INVALID_ARG : Null argument(s)
  • ESP_ERR_HTTPD_ALLOC_MEM : Failed to allocate memory for instance
  • ESP_ERR_HTTPD_TASK : Failed to launch server task
Parameters
  • config: : Configuration for new instance of the server
  • handle: : Handle to newly created instance of the server. NULL on error

esp_err_t httpd_stop(httpd_handle_t handle)

Stops the web server.

Deallocates memory/resources used by an HTTP server instance and deletes it. Once deleted the handle can no longer be used for accessing the instance.

Example usage:

// Function for stopping the webserver
void stop_webserver(httpd_handle_t server)
{
     // Ensure handle is non NULL
     if (server != NULL) {
         // Stop the httpd server
         httpd_stop(server);
     }
}

Return
  • ESP_OK : Server stopped successfully
  • ESP_ERR_INVALID_ARG : Handle argument is Null
Parameters
  • handle: Handle to server returned by httpd_start

esp_err_t httpd_queue_work(httpd_handle_t handle, httpd_work_fn_t work, void *arg)

Queue execution of a function in HTTPD’s context.

This API queues a work function for asynchronous execution

Note
Some protocols require that the web server generate some asynchronous data and send it to the persistently opened connection. This facility is for use by such protocols.
Return
  • ESP_OK : On successfully queueing the work
  • ESP_FAIL : Failure in ctrl socket
  • ESP_ERR_INVALID_ARG : Null arguments
Parameters
  • handle: Handle to server returned by httpd_start
  • work: Pointer to the function to be executed in the HTTPD’s context
  • arg: Pointer to the arguments that should be passed to this function

void *httpd_sess_get_ctx(httpd_handle_t handle, int sockfd)

Get session context from socket descriptor.

Typically if a session context is created, it is available to URI handlers through the httpd_req_t structure. But, there are cases where the web server’s send/receive functions may require the context (for example, for accessing keying information etc). Since the send/receive function only have the socket descriptor at their disposal, this API provides them with a way to retrieve the session context.

Return
  • void* : Pointer to the context associated with this session
  • NULL : Empty context / Invalid handle / Invalid socket fd
Parameters
  • handle: Handle to server returned by httpd_start
  • sockfd: The socket descriptor for which the context should be extracted.

esp_err_t httpd_trigger_sess_close(httpd_handle_t handle, int sockfd)

Trigger an httpd session close externally.

Note
Calling this API is only required in special circumstances wherein some application requires to close an httpd client session asynchronously.
Return
  • ESP_OK : On successfully initiating closure
  • ESP_FAIL : Failure to queue work
  • ESP_ERR_NOT_FOUND : Socket fd not found
  • ESP_ERR_INVALID_ARG : Null arguments
Parameters
  • handle: Handle to server returned by httpd_start
  • sockfd: The socket descriptor of the session to be closed

esp_err_t httpd_sess_update_timestamp(httpd_handle_t handle, int sockfd)

Update timestamp for a given socket.

Timestamps are internally associated with each session to monitor how recently a session exchanged traffic. When LRU purge is enabled, if a client is requesting for connection but maximum number of sockets/sessions is reached, then the session having the earliest timestamp is closed automatically.

Updating the timestamp manually prevents the socket from being purged due to the Least Recently Used (LRU) logic, even though it might not have received traffic for some time. This is useful when all open sockets/session are frequently exchanging traffic but the user specifically wants one of the sessions to be kept open, irrespective of when it last exchanged a packet.

Note
Calling this API is only necessary if the LRU Purge Enable option is enabled.
Return
  • ESP_OK : Socket found and timestamp updated
  • ESP_ERR_NOT_FOUND : Socket not found
  • ESP_ERR_INVALID_ARG : Null arguments
Parameters
  • handle: Handle to server returned by httpd_start
  • sockfd: The socket descriptor of the session for which timestamp is to be updated

Structures

struct httpd_config

HTTP Server Configuration Structure.

Note
Use HTTPD_DEFAULT_CONFIG() to initialize the configuration to a default value and then modify only those fields that are specifically determined by the use case.

Public Members

unsigned task_priority

Priority of FreeRTOS task which runs the server

size_t stack_size

The maximum stack size allowed for the server task

uint16_t server_port

TCP Port number for receiving and transmitting HTTP traffic

uint16_t ctrl_port

UDP Port number for asynchronously exchanging control signals between various components of the server

uint16_t max_open_sockets

Max number of sockets/clients connected at any time

uint16_t max_uri_handlers

Maximum allowed uri handlers

uint16_t max_resp_headers

Maximum allowed additional headers in HTTP response

uint16_t backlog_conn

Number of backlog connections

bool lru_purge_enable

Purge “Least Recently Used” connection

uint16_t recv_wait_timeout

Timeout for recv function (in seconds)

uint16_t send_wait_timeout

Timeout for send function (in seconds)

struct httpd_req

HTTP Request Data Structure.

Public Members

httpd_handle_t handle

Handle to server instance

int method

The type of HTTP request, -1 if unsupported method

const char uri[HTTPD_MAX_URI_LEN+1]

The URI of this request (1 byte extra for null termination)

size_t content_len

Length of the request body

void *aux

Internally used members

void *user_ctx

User context pointer passed during URI registration.

void *sess_ctx

Session Context Pointer

A session context. Contexts are maintained across ‘sessions’ for a given open TCP connection. One session could have multiple request responses. The web server will ensure that the context persists across all these request and responses.

By default, this is NULL. URI Handlers can set this to any meaningful value.

If the underlying socket gets closed, and this pointer is non-NULL, the web server will free up the context by calling free(), unless free_ctx function is set.

httpd_free_sess_ctx_fn_t free_ctx

Pointer to free context hook

Function to free session context

If the web server’s socket closes, it frees up the session context by calling free() on the sess_ctx member. If you wish to use a custom function for freeing the session context, please specify that here.

struct httpd_uri

Structure for URI handler.

Public Members

const char *uri

The URI to handle

httpd_method_t method

Method supported by the URI

esp_err_t (*handler)(httpd_req_t *r)

Handler to call for supported request method. This must return ESP_OK, or else the underlying socket will be closed.

void *user_ctx

Pointer to user context data which will be available to handler

Macros

HTTPD_MAX_REQ_HDR_LEN
HTTPD_MAX_URI_LEN
HTTPD_SOCK_ERR_FAIL
HTTPD_SOCK_ERR_INVALID
HTTPD_SOCK_ERR_TIMEOUT
HTTPD_200

HTTP Response 200

HTTPD_204

HTTP Response 204

HTTPD_207

HTTP Response 207

HTTPD_400

HTTP Response 400

HTTPD_404

HTTP Response 404

HTTPD_408

HTTP Response 408

HTTPD_500

HTTP Response 500

HTTPD_TYPE_JSON

HTTP Content type JSON

HTTPD_TYPE_TEXT

HTTP Content type text/HTML

HTTPD_TYPE_OCTET

HTTP Content type octext-stream

HTTPD_DEFAULT_CONFIG
ESP_ERR_HTTPD_BASE

Starting number of HTTPD error codes

ESP_ERR_HTTPD_HANDLERS_FULL

All slots for registering URI handlers have been consumed

ESP_ERR_HTTPD_HANDLER_EXISTS

URI handler with same method and target URI already registered

ESP_ERR_HTTPD_INVALID_REQ

Invalid request pointer

ESP_ERR_HTTPD_RESULT_TRUNC

Result string truncated

ESP_ERR_HTTPD_RESP_HDR

Response header field larger than supported

ESP_ERR_HTTPD_RESP_SEND

Error occured while sending response packet

ESP_ERR_HTTPD_ALLOC_MEM

Failed to dynamically allocate memory for resource

ESP_ERR_HTTPD_TASK

Failed to launch server task/thread

Type Definitions

typedef void (*httpd_free_sess_ctx_fn_t)(void *sess_ctx)

Function type for freeing context data (if any)

typedef struct httpd_req httpd_req_t

HTTP Request Data Structure.

typedef struct httpd_uri httpd_uri_t

Structure for URI handler.

typedef int (*httpd_send_func_t)(int sockfd, const char *buf, size_t buf_len, int flags)

Prototype for HTTPDs low-level send function.

Note
User specified send function must handle errors internally, depending upon the set value of errno, and return specific HTTPD_SOCK_ERR_ codes, which will eventually be conveyed as return value of httpd_send() function
Return
  • Bytes : The number of bytes sent successfully
  • HTTPD_SOCK_ERR_INVALID : Invalid arguments
  • HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket send()
  • HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket send()

typedef int (*httpd_recv_func_t)(int sockfd, char *buf, size_t buf_len, int flags)

Prototype for HTTPDs low-level recv function.

Note
User specified recv function must handle errors internally, depending upon the set value of errno, and return specific HTTPD_SOCK_ERR_ codes, which will eventually be conveyed as return value of httpd_req_recv() function
Return
  • Bytes : The number of bytes received successfully
  • 0 : Buffer length parameter is zero / connection closed by peer
  • HTTPD_SOCK_ERR_INVALID : Invalid arguments
  • HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket recv()
  • HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket recv()

typedef void *httpd_handle_t

HTTP Server Instance Handle.

Every instance of the server will have a unique handle.

typedef enum http_method httpd_method_t

HTTP Method Type wrapper over “enum http_method” available in “http_parser” library.

typedef struct httpd_config httpd_config_t

HTTP Server Configuration Structure.

Note
Use HTTPD_DEFAULT_CONFIG() to initialize the configuration to a default value and then modify only those fields that are specifically determined by the use case.

typedef void (*httpd_work_fn_t)(void *arg)

Prototype of the HTTPD work function Please refer to httpd_queue_work() for more details.

Parameters
  • arg: The arguments for this work function