参考来源:百度百科 HTTP服务器
参考例程:Simple HTTPD Server Example
注意:ESP32开发板作为HTTP服务器;ApiPost作为客户端
实验流程大概为:
查看get注册参数以及处理函数代码(详细代码请参考例程)。
static const httpd_uri_t hello = {
.uri = "/hello", //具体路径,如 http://192.168.3.173:80/hello
.method = HTTP_GET, //方法method
.handler = hello_get_handler, //处理函数
/* Let's pass response string in user
* context to demonstrate it's usage */
.user_ctx = "Hello World!i am esp32 2021.06.25" //给客户端的响应
};
/* An HTTP GET handler */
static esp_err_t hello_get_handler(httpd_req_t *req)
{
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, "Accept-Language") + 1;
if (buf_len > 1) {
buf = malloc(buf_len);
if (httpd_req_get_hdr_value_str(req, "Accept-Language", buf, buf_len) == ESP_OK) {
ESP_LOGI(TAG, "Found header => Accept-Language: %s", buf);
}
free(buf);
}
buf_len = httpd_req_get_hdr_value_len(req, "User-Agent") + 1;
if (buf_len > 1) {
buf = malloc(buf_len);
if (httpd_req_get_hdr_value_str(req, "User-Agent", buf, buf_len) == ESP_OK) {
ESP_LOGI(TAG, "Found header => User-Agent: %s", buf);
}
free(buf);
}
buf_len = httpd_req_get_hdr_value_len(req, "Connection") + 1;
if (buf_len > 1) {
buf = malloc(buf_len);
if (httpd_req_get_hdr_value_str(req, "Connection", buf, buf_len) == ESP_OK) {
ESP_LOGI(TAG, "Found header => Connection: %s", buf);
}
free(buf);
}
buf_len = httpd_req_get_hdr_value_len(req, "content-type") + 1;
if (buf_len > 1) {
buf = malloc(buf_len);
if (httpd_req_get_hdr_value_str(req, "content-type", buf, buf_len) == ESP_OK) {
ESP_LOGI(TAG, "Found header => content-type: %s", buf);
}
free(buf);
}
//获取query并打印出来,再分类
/* 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-11", "Custom-Value-1");
httpd_resp_set_hdr(req, "Custom-Header-22", "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;
}
关于HTTP报文接收请参考:这里
static const httpd_uri_t echo = {
.uri = "/echo", //具体路径,如 http://192.168.3.173:80/echo
.method = HTTP_POST, //方法method
.handler = echo_post_handler, //处理函数
.user_ctx = NULL
};
/* An HTTP POST handler */
static esp_err_t echo_post_handler(httpd_req_t *req)
{
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,
MIN(remaining, sizeof(buf)))) <= 0) {
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
/* Retry receiving if timeout occurred */
continue;
}
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;
}
/* An HTTP PUT handler. This demonstrates realtime
* registration and deregistration of URI handlers
*/
static esp_err_t ctrl_put_handler(httpd_req_t *req)
{
char buf;
int ret;
if ((ret = httpd_req_recv(req, &buf, 1)) <= 0) {
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
httpd_resp_send_408(req);
}
return ESP_FAIL;
}
if (buf == '0') {
/* URI handlers can be unregistered using the uri string */
ESP_LOGI(TAG, "Unregistering /hello and /echo URIs");
httpd_unregister_uri(req->handle, "/hello");
httpd_unregister_uri(req->handle, "/echo");
/* Register the custom error handler */
httpd_register_err_handler(req->handle, HTTPD_404_NOT_FOUND, http_404_error_handler);
}
else {
ESP_LOGI(TAG, "Registering /hello and /echo URIs");
httpd_register_uri_handler(req->handle, &hello);
httpd_register_uri_handler(req->handle, &echo);
/* Unregister custom error handler */
httpd_register_err_handler(req->handle, HTTPD_404_NOT_FOUND, NULL);
}
/* Respond with empty body */
httpd_resp_send(req, NULL, 0);
return ESP_OK;
}
static const httpd_uri_t ctrl = {
.uri = "/ctrl",
.method = HTTP_PUT,
.handler = ctrl_put_handler,
.user_ctx = NULL
};