2018-07-03 10:57:41 +00:00
|
|
|
/* Simple HTTP Server Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <esp_wifi.h>
|
2018-11-20 16:42:37 +00:00
|
|
|
#include <esp_event.h>
|
2018-07-03 10:57:41 +00:00
|
|
|
#include <esp_log.h>
|
|
|
|
#include <esp_system.h>
|
|
|
|
#include <nvs_flash.h>
|
|
|
|
#include <sys/param.h>
|
2018-11-20 16:42:37 +00:00
|
|
|
#include "nvs_flash.h"
|
2019-08-31 14:19:21 +00:00
|
|
|
#include "esp_netif.h"
|
2018-11-20 16:42:37 +00:00
|
|
|
#include "esp_eth.h"
|
|
|
|
#include "protocol_examples_common.h"
|
2018-07-03 10:57:41 +00:00
|
|
|
|
2018-10-24 09:52:15 +00:00
|
|
|
#include <esp_http_server.h>
|
2018-07-03 10:57:41 +00:00
|
|
|
|
|
|
|
/* A simple example that demonstrates how to create GET and POST
|
|
|
|
* handlers for the web server.
|
2018-11-20 16:42:37 +00:00
|
|
|
*/
|
2018-07-03 10:57:41 +00:00
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
static const char *TAG = "example";
|
2018-07-03 10:57:41 +00:00
|
|
|
|
|
|
|
/* An HTTP GET handler */
|
2018-11-20 16:42:37 +00:00
|
|
|
static esp_err_t hello_get_handler(httpd_req_t *req)
|
2018-07-03 10:57:41 +00:00
|
|
|
{
|
|
|
|
char* buf;
|
|
|
|
size_t buf_len;
|
|
|
|
|
|
|
|
/* Get header value string length and allocate memory for length + 1,
|
|
|
|
* extra byte for null termination */
|
|
|
|
buf_len = httpd_req_get_hdr_value_len(req, "Host") + 1;
|
|
|
|
if (buf_len > 1) {
|
|
|
|
buf = malloc(buf_len);
|
|
|
|
/* Copy null terminated value string into buffer */
|
|
|
|
if (httpd_req_get_hdr_value_str(req, "Host", buf, buf_len) == ESP_OK) {
|
|
|
|
ESP_LOGI(TAG, "Found header => Host: %s", buf);
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-2") + 1;
|
|
|
|
if (buf_len > 1) {
|
|
|
|
buf = malloc(buf_len);
|
|
|
|
if (httpd_req_get_hdr_value_str(req, "Test-Header-2", buf, buf_len) == ESP_OK) {
|
|
|
|
ESP_LOGI(TAG, "Found header => Test-Header-2: %s", buf);
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-1") + 1;
|
|
|
|
if (buf_len > 1) {
|
|
|
|
buf = malloc(buf_len);
|
|
|
|
if (httpd_req_get_hdr_value_str(req, "Test-Header-1", buf, buf_len) == ESP_OK) {
|
|
|
|
ESP_LOGI(TAG, "Found header => Test-Header-1: %s", buf);
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read URL query string length and allocate memory for length + 1,
|
|
|
|
* extra byte for null termination */
|
|
|
|
buf_len = httpd_req_get_url_query_len(req) + 1;
|
|
|
|
if (buf_len > 1) {
|
|
|
|
buf = malloc(buf_len);
|
|
|
|
if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
|
|
|
|
ESP_LOGI(TAG, "Found URL query => %s", buf);
|
|
|
|
char param[32];
|
|
|
|
/* Get value of expected key from query string */
|
|
|
|
if (httpd_query_key_value(buf, "query1", param, sizeof(param)) == ESP_OK) {
|
|
|
|
ESP_LOGI(TAG, "Found URL query parameter => query1=%s", param);
|
|
|
|
}
|
|
|
|
if (httpd_query_key_value(buf, "query3", param, sizeof(param)) == ESP_OK) {
|
|
|
|
ESP_LOGI(TAG, "Found URL query parameter => query3=%s", param);
|
|
|
|
}
|
|
|
|
if (httpd_query_key_value(buf, "query2", param, sizeof(param)) == ESP_OK) {
|
|
|
|
ESP_LOGI(TAG, "Found URL query parameter => query2=%s", param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set some custom headers */
|
|
|
|
httpd_resp_set_hdr(req, "Custom-Header-1", "Custom-Value-1");
|
|
|
|
httpd_resp_set_hdr(req, "Custom-Header-2", "Custom-Value-2");
|
|
|
|
|
|
|
|
/* Send response with custom headers and body set as the
|
|
|
|
* string passed in user context*/
|
|
|
|
const char* resp_str = (const char*) req->user_ctx;
|
|
|
|
httpd_resp_send(req, resp_str, strlen(resp_str));
|
|
|
|
|
|
|
|
/* After sending the HTTP response the old HTTP request
|
|
|
|
* headers are lost. Check if HTTP request headers can be read now. */
|
|
|
|
if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
|
|
|
|
ESP_LOGI(TAG, "Request headers lost");
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
static const httpd_uri_t hello = {
|
2018-07-03 10:57:41 +00:00
|
|
|
.uri = "/hello",
|
|
|
|
.method = HTTP_GET,
|
|
|
|
.handler = hello_get_handler,
|
|
|
|
/* Let's pass response string in user
|
|
|
|
* context to demonstrate it's usage */
|
|
|
|
.user_ctx = "Hello World!"
|
|
|
|
};
|
|
|
|
|
|
|
|
/* An HTTP POST handler */
|
2018-11-20 16:42:37 +00:00
|
|
|
static esp_err_t echo_post_handler(httpd_req_t *req)
|
2018-07-03 10:57:41 +00:00
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
int ret, remaining = req->content_len;
|
|
|
|
|
|
|
|
while (remaining > 0) {
|
|
|
|
/* Read the data for the request */
|
|
|
|
if ((ret = httpd_req_recv(req, buf,
|
2018-10-09 12:37:38 +00:00
|
|
|
MIN(remaining, sizeof(buf)))) <= 0) {
|
|
|
|
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
|
|
|
|
/* Retry receiving if timeout occurred */
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-03 10:57:41 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send back the same data */
|
|
|
|
httpd_resp_send_chunk(req, buf, ret);
|
|
|
|
remaining -= ret;
|
|
|
|
|
|
|
|
/* Log data received */
|
|
|
|
ESP_LOGI(TAG, "=========== RECEIVED DATA ==========");
|
|
|
|
ESP_LOGI(TAG, "%.*s", ret, buf);
|
|
|
|
ESP_LOGI(TAG, "====================================");
|
|
|
|
}
|
|
|
|
|
|
|
|
// End response
|
|
|
|
httpd_resp_send_chunk(req, NULL, 0);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
static const httpd_uri_t echo = {
|
2018-07-03 10:57:41 +00:00
|
|
|
.uri = "/echo",
|
|
|
|
.method = HTTP_POST,
|
|
|
|
.handler = echo_post_handler,
|
|
|
|
.user_ctx = NULL
|
|
|
|
};
|
|
|
|
|
2019-02-01 13:11:46 +00:00
|
|
|
/* This handler allows the custom error handling functionality to be
|
|
|
|
* tested from client side. For that, when a PUT request 0 is sent to
|
|
|
|
* URI /ctrl, the /hello and /echo URIs are unregistered and following
|
|
|
|
* custom error handler http_404_error_handler() is registered.
|
|
|
|
* Afterwards, when /hello or /echo is requested, this custom error
|
|
|
|
* handler is invoked which, after sending an error message to client,
|
|
|
|
* either closes the underlying socket (when requested URI is /echo)
|
|
|
|
* or keeps it open (when requested URI is /hello). This allows the
|
|
|
|
* client to infer if the custom error handler is functioning as expected
|
|
|
|
* by observing the socket state.
|
|
|
|
*/
|
|
|
|
esp_err_t http_404_error_handler(httpd_req_t *req, httpd_err_code_t err)
|
|
|
|
{
|
|
|
|
if (strcmp("/hello", req->uri) == 0) {
|
|
|
|
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "/hello URI is not available");
|
|
|
|
/* Return ESP_OK to keep underlying socket open */
|
|
|
|
return ESP_OK;
|
|
|
|
} else if (strcmp("/echo", req->uri) == 0) {
|
|
|
|
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "/echo URI is not available");
|
|
|
|
/* Return ESP_FAIL to close underlying socket */
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
/* For any other URI send 404 and close socket */
|
|
|
|
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Some 404 error message");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
2018-07-03 10:57:41 +00:00
|
|
|
/* An HTTP PUT handler. This demonstrates realtime
|
|
|
|
* registration and deregistration of URI handlers
|
|
|
|
*/
|
2018-11-20 16:42:37 +00:00
|
|
|
static esp_err_t ctrl_put_handler(httpd_req_t *req)
|
2018-07-03 10:57:41 +00:00
|
|
|
{
|
|
|
|
char buf;
|
|
|
|
int ret;
|
|
|
|
|
2018-10-09 12:37:38 +00:00
|
|
|
if ((ret = httpd_req_recv(req, &buf, 1)) <= 0) {
|
|
|
|
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
|
|
|
|
httpd_resp_send_408(req);
|
|
|
|
}
|
2018-07-03 10:57:41 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf == '0') {
|
2019-02-01 13:11:46 +00:00
|
|
|
/* URI handlers can be unregistered using the uri string */
|
2018-07-03 10:57:41 +00:00
|
|
|
ESP_LOGI(TAG, "Unregistering /hello and /echo URIs");
|
|
|
|
httpd_unregister_uri(req->handle, "/hello");
|
|
|
|
httpd_unregister_uri(req->handle, "/echo");
|
2019-02-01 13:11:46 +00:00
|
|
|
/* Register the custom error handler */
|
|
|
|
httpd_register_err_handler(req->handle, HTTPD_404_NOT_FOUND, http_404_error_handler);
|
2018-07-03 10:57:41 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ESP_LOGI(TAG, "Registering /hello and /echo URIs");
|
|
|
|
httpd_register_uri_handler(req->handle, &hello);
|
|
|
|
httpd_register_uri_handler(req->handle, &echo);
|
2019-02-01 13:11:46 +00:00
|
|
|
/* Unregister custom error handler */
|
|
|
|
httpd_register_err_handler(req->handle, HTTPD_404_NOT_FOUND, NULL);
|
2018-07-03 10:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Respond with empty body */
|
|
|
|
httpd_resp_send(req, NULL, 0);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
static const httpd_uri_t ctrl = {
|
2018-07-03 10:57:41 +00:00
|
|
|
.uri = "/ctrl",
|
|
|
|
.method = HTTP_PUT,
|
|
|
|
.handler = ctrl_put_handler,
|
|
|
|
.user_ctx = NULL
|
|
|
|
};
|
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
static httpd_handle_t start_webserver(void)
|
2018-07-03 10:57:41 +00:00
|
|
|
{
|
|
|
|
httpd_handle_t server = NULL;
|
|
|
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
|
|
|
|
|
|
|
// Start the httpd server
|
2018-09-02 21:01:44 +00:00
|
|
|
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
|
2018-07-03 10:57:41 +00:00
|
|
|
if (httpd_start(&server, &config) == ESP_OK) {
|
|
|
|
// Set URI handlers
|
|
|
|
ESP_LOGI(TAG, "Registering URI handlers");
|
|
|
|
httpd_register_uri_handler(server, &hello);
|
|
|
|
httpd_register_uri_handler(server, &echo);
|
|
|
|
httpd_register_uri_handler(server, &ctrl);
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Error starting server!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
static void stop_webserver(httpd_handle_t server)
|
2018-07-03 10:57:41 +00:00
|
|
|
{
|
|
|
|
// Stop the httpd server
|
|
|
|
httpd_stop(server);
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
static void disconnect_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
2018-07-03 10:57:41 +00:00
|
|
|
{
|
2018-11-20 16:42:37 +00:00
|
|
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
|
|
|
if (*server) {
|
|
|
|
ESP_LOGI(TAG, "Stopping webserver");
|
|
|
|
stop_webserver(*server);
|
|
|
|
*server = NULL;
|
2018-07-03 10:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
static void connect_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
2018-07-03 10:57:41 +00:00
|
|
|
{
|
2018-11-20 16:42:37 +00:00
|
|
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
|
|
|
if (*server == NULL) {
|
|
|
|
ESP_LOGI(TAG, "Starting webserver");
|
|
|
|
*server = start_webserver();
|
|
|
|
}
|
2018-07-03 10:57:41 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
void app_main(void)
|
2018-07-03 10:57:41 +00:00
|
|
|
{
|
|
|
|
static httpd_handle_t server = NULL;
|
2018-11-20 16:42:37 +00:00
|
|
|
|
2018-07-03 10:57:41 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2019-11-29 09:54:02 +00:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 16:42:37 +00:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
|
|
|
|
/* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
|
|
|
|
* and re-start it upon connection.
|
|
|
|
*/
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
|
|
|
|
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
|
|
|
|
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
|
|
|
|
|
|
|
|
/* Start the server for the first time */
|
|
|
|
server = start_webserver();
|
2018-07-03 10:57:41 +00:00
|
|
|
}
|