mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-08 04:02:27 +00:00
docs: add a note on required header files for the esp_http_server example
This commit is contained in:
@@ -15,104 +15,9 @@ The HTTP Server component provides an ability for running a lightweight web serv
|
||||
Application Examples
|
||||
--------------------
|
||||
|
||||
.. code-block:: c
|
||||
|
||||
/* 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, HTTPD_RESP_USE_STRLEN);
|
||||
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 content[100];
|
||||
|
||||
/* 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, HTTPD_RESP_USE_STRLEN);
|
||||
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
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
:example:`protocols/http_server/simple` demonstrates how to handle arbitrary content lengths, read request headers and URL query parameters, and set response headers.
|
||||
|
||||
Advanced Testing Example
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
:example:`protocols/http_server/advanced_tests` demonstrates how to use the HTTP server for advanced testing.
|
||||
- :example:`protocols/http_server/simple` demonstrates how to handle arbitrary content lengths, read request headers and URL query parameters, and set response headers.
|
||||
|
||||
- :example:`protocols/http_server/advanced_tests` demonstrates how to use the HTTP server for advanced testing.
|
||||
|
||||
Persistent Connections
|
||||
----------------------
|
||||
|
Reference in New Issue
Block a user