Select one of the symbols to view example projects that use it.
 
Outline
#include <stdlib.h>
#include <stdbool.h>
#include <esp_log.h>
#include <esp_system.h>
#include <esp_http_server.h>
#include "esp_check.h"
#include "tests.h"
TAG
pre_start_mem
post_stop_mem
async_resp_arg
hello_get_handler(httpd_req_t *)
test_header_get_handler(httpd_req_t *)
hello_type_get_handler(httpd_req_t *)
hello_status_get_handler(httpd_req_t *)
echo_post_handler(httpd_req_t *)
adder_free_func(void *)
adder_post_handler(httpd_req_t *)
leftover_data_post_handler(httpd_req_t *)
generate_async_resp(void *)
async_get_handler(httpd_req_t *)
basic_handlers
basic_handlers_no
register_basic_handlers(httpd_handle_t)
test_httpd_start()
test_httpd_stop(httpd_handle_t)
start_tests()
stop_tests(httpd_handle_t)
Files
loading...
SourceVuESP-IDF Framework and Examplesadvanced_tests samplemain/tests.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#include <stdlib.h> #include <stdbool.h> #include <esp_log.h> #include <esp_system.h> #include <esp_http_server.h> #include "esp_check.h" #include "tests.h"7 includes static const char *TAG = "TESTS"; static int pre_start_mem, post_stop_mem; struct async_resp_arg { httpd_handle_t hd; int fd; }{ ... }; /********************* Basic Handlers Start *******************/ static esp_err_t hello_get_handler(httpd_req_t *req) { #define STR "Hello World!" ESP_LOGI(TAG, "Free Stack for server task: '%d'", uxTaskGetStackHighWaterMark(NULL)); httpd_resp_send(req, STR, HTTPD_RESP_USE_STRLEN); return ESP_OK; #undef STR }{ ... } /* This handler is intended to check what happens in case of empty values of headers. * Here `Header2` is an empty header and `Header1` and `Header3` will have `Value1` * and `Value3` in them. *//* ... */ static esp_err_t test_header_get_handler(httpd_req_t *req) { httpd_resp_set_type(req, HTTPD_TYPE_TEXT); int buf_len; char *buf; buf_len = httpd_req_get_hdr_value_len(req, "Header1"); if (buf_len > 0) { buf = malloc(++buf_len); if (!buf) { ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", buf_len); httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Memory allocation failed"); return ESP_ERR_NO_MEM; }{...} /* Copy null terminated value string into buffer */ if (httpd_req_get_hdr_value_str(req, "Header1", buf, buf_len) == ESP_OK) { ESP_LOGI(TAG, "Header1 content: %s", buf); if (strcmp("Value1", buf) != 0) { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Wrong value of Header1 received"); free(buf); return ESP_ERR_INVALID_ARG; }{...} else { ESP_LOGI(TAG, "Expected value and received value matched for Header1"); }{...} }{...} else { ESP_LOGE(TAG, "Error in getting value of Header1"); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Error in getting value of Header1"); free(buf); return ESP_FAIL; }{...} free(buf); }{...} else { ESP_LOGE(TAG, "Header1 not found"); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Header1 not found"); return ESP_ERR_NOT_FOUND; }{...} buf_len = httpd_req_get_hdr_value_len(req, "Header3"); if (buf_len > 0) { buf = malloc(++buf_len); if (!buf) { ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", buf_len); httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Memory allocation failed"); return ESP_ERR_NO_MEM; }{...} /* Copy null terminated value string into buffer */ if (httpd_req_get_hdr_value_str(req, "Header3", buf, buf_len) == ESP_OK) { ESP_LOGI(TAG, "Header3 content: %s", buf); if (strcmp("Value3", buf) != 0) { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Wrong value of Header3 received"); free(buf); return ESP_ERR_INVALID_ARG; }{...} else { ESP_LOGI(TAG, "Expected value and received value matched for Header3"); }{...} }{...} else { ESP_LOGE(TAG, "Error in getting value of Header3"); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Error in getting value of Header3"); free(buf); return ESP_FAIL; }{...} free(buf); }{...} else { ESP_LOGE(TAG, "Header3 not found"); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Header3 not found"); return ESP_ERR_NOT_FOUND; }{...} buf_len = httpd_req_get_hdr_value_len(req, "Header2"); buf = malloc(++buf_len); if (!buf) { ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", buf_len); httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Memory allocation failed"); return ESP_ERR_NO_MEM; }{...} if (httpd_req_get_hdr_value_str(req, "Header2", buf, buf_len) == ESP_OK) { ESP_LOGI(TAG, "Header2 content: %s", buf); httpd_resp_send(req, buf, HTTPD_RESP_USE_STRLEN); }{...} else { ESP_LOGE(TAG, "Header2 not found"); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Header2 not found"); return ESP_FAIL; }{...} return ESP_OK; }{ ... } static esp_err_t hello_type_get_handler(httpd_req_t *req) { #define STR "Hello World!" httpd_resp_set_type(req, HTTPD_TYPE_TEXT); httpd_resp_send(req, STR, HTTPD_RESP_USE_STRLEN); return ESP_OK; #undef STR }{ ... } static esp_err_t hello_status_get_handler(httpd_req_t *req) { #define STR "Hello World!" httpd_resp_set_status(req, HTTPD_500); httpd_resp_send(req, STR, HTTPD_RESP_USE_STRLEN); return ESP_OK; #undef STR }{ ... } static esp_err_t echo_post_handler(httpd_req_t *req) { ESP_LOGI(TAG, "/echo handler read content length %d", req->content_len); char* buf = malloc(req->content_len + 1); size_t off = 0; int ret; if (!buf) { ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", req->content_len + 1); httpd_resp_send_500(req); return ESP_FAIL; }{...} while (off < req->content_len) { /* Read data received in the request */ ret = httpd_req_recv(req, buf + off, req->content_len - off); if (ret <= 0) { if (ret == HTTPD_SOCK_ERR_TIMEOUT) { httpd_resp_send_408(req); }{...} free (buf); return ESP_FAIL; }{...} off += ret; ESP_LOGI(TAG, "/echo handler recv length %d", ret); }{...} buf[off] = '\0'; if (req->content_len < 128) { ESP_LOGI(TAG, "/echo handler read %s", buf); }{...} /* Search for Custom header field */ char* req_hdr = 0; size_t hdr_len = httpd_req_get_hdr_value_len(req, "Custom"); if (hdr_len) { /* Read Custom header value */ req_hdr = malloc(hdr_len + 1); if (!req_hdr) { ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", hdr_len + 1); httpd_resp_send_500(req); return ESP_FAIL; }{...} httpd_req_get_hdr_value_str(req, "Custom", req_hdr, hdr_len + 1); /* Set as additional header for response packet */ httpd_resp_set_hdr(req, "Custom", req_hdr); }{...} httpd_resp_send(req, buf, req->content_len); free (req_hdr); free (buf); return ESP_OK; }{ ... } static void adder_free_func(void *ctx) { ESP_LOGI(TAG, "Custom Free Context function called"); free(ctx); }{ ... } /* Create a context, keep incrementing value in the context, by whatever was * received. Return the result *//* ... */ static esp_err_t adder_post_handler(httpd_req_t *req) { char buf[10]; char outbuf[50]; int ret; /* Read data received in the request */ ret = httpd_req_recv(req, buf, sizeof(buf)); if (ret <= 0) { if (ret == HTTPD_SOCK_ERR_TIMEOUT) { httpd_resp_send_408(req); }{...} return ESP_FAIL; }{...} buf[ret] = '\0'; int val = atoi(buf); ESP_LOGI(TAG, "/adder handler read %d", val); if (! req->sess_ctx) { ESP_LOGI(TAG, "/adder allocating new session"); req->sess_ctx = malloc(sizeof(int)); ESP_RETURN_ON_FALSE(req->sess_ctx, ESP_ERR_NO_MEM, TAG, "Failed to allocate sess_ctx"); req->free_ctx = adder_free_func; *(int *)req->sess_ctx = 0; }{...} int *adder = (int *)req->sess_ctx; *adder += val; snprintf(outbuf, sizeof(outbuf),"%d", *adder); httpd_resp_send(req, outbuf, HTTPD_RESP_USE_STRLEN); return ESP_OK; }{ ... } static esp_err_t leftover_data_post_handler(httpd_req_t *req) { /* Only echo the first 10 bytes of the request, leaving the rest of the * request data as is. *//* ... */ char buf[11]; int ret; /* Read data received in the request */ ret = httpd_req_recv(req, buf, sizeof(buf) - 1); if (ret <= 0) { if (ret == HTTPD_SOCK_ERR_TIMEOUT) { httpd_resp_send_408(req); }{...} return ESP_FAIL; }{...} buf[ret] = '\0'; ESP_LOGI(TAG, "leftover data handler read %s", buf); httpd_resp_send(req, buf, HTTPD_RESP_USE_STRLEN); return ESP_OK; }{ ... } static void generate_async_resp(void *arg) { char buf[250]; struct async_resp_arg *resp_arg = (struct async_resp_arg *)arg; httpd_handle_t hd = resp_arg->hd; int fd = resp_arg->fd; #define HTTPD_HDR_STR "HTTP/1.1 200 OK\r\n" \ "Content-Type: text/html\r\n" \ "Content-Length: %d\r\n"... #define STR "Hello Double World!" ESP_LOGI(TAG, "Executing queued work fd : %d", fd); snprintf(buf, sizeof(buf), HTTPD_HDR_STR, strlen(STR)); httpd_socket_send(hd, fd, buf, strlen(buf), 0); /* Space for sending additional headers based on set_header */ httpd_socket_send(hd, fd, "\r\n", strlen("\r\n"), 0); httpd_socket_send(hd, fd, STR, strlen(STR), 0); #undef STR free(arg); }{ ... } static esp_err_t async_get_handler(httpd_req_t *req) { #define STR "Hello World!" httpd_resp_send(req, STR, HTTPD_RESP_USE_STRLEN); /* Also register a HTTPD Work which sends the same data on the same * socket again *//* ... */ struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg)); ESP_RETURN_ON_FALSE(resp_arg, ESP_ERR_NO_MEM, TAG, "Failed to allocate resp_arg"); resp_arg->hd = req->handle; resp_arg->fd = httpd_req_to_sockfd(req); if (resp_arg->fd < 0) { return ESP_FAIL; }{...} ESP_LOGI(TAG, "Queuing work fd : %d", resp_arg->fd); httpd_queue_work(req->handle, generate_async_resp, resp_arg); return ESP_OK; #undef STR }{ ... } static const httpd_uri_t basic_handlers[] = { { .uri = "/hello/type_html", .method = HTTP_GET, .handler = hello_type_get_handler, .user_ctx = NULL, }{...}, { .uri = "/test_header", .method = HTTP_GET, .handler = test_header_get_handler, .user_ctx = NULL, }{...}, { .uri = "/hello", .method = HTTP_GET, .handler = hello_get_handler, .user_ctx = NULL, }{...}, { .uri = "/hello/status_500", .method = HTTP_GET, .handler = hello_status_get_handler, .user_ctx = NULL, }{...}, { .uri = "/echo", .method = HTTP_POST, .handler = echo_post_handler, .user_ctx = NULL, }{...}, { .uri = "/echo", .method = HTTP_PUT, .handler = echo_post_handler, .user_ctx = NULL, }{...}, { .uri = "/leftover_data", .method = HTTP_POST, .handler = leftover_data_post_handler, .user_ctx = NULL, }{...}, { .uri = "/adder", .method = HTTP_POST, .handler = adder_post_handler, .user_ctx = NULL, }{...}, { .uri = "/async_data", .method = HTTP_GET, .handler = async_get_handler, .user_ctx = NULL, }{...} }{...}; static const int basic_handlers_no = sizeof(basic_handlers)/sizeof(httpd_uri_t); static void register_basic_handlers(httpd_handle_t hd) { int i; ESP_LOGI(TAG, "Registering basic handlers"); ESP_LOGI(TAG, "No of handlers = %d", basic_handlers_no); for (i = 0; i < basic_handlers_no; i++) { if (httpd_register_uri_handler(hd, &basic_handlers[i]) != ESP_OK) { ESP_LOGW(TAG, "register uri failed for %d", i); return; }{...} }{...} ESP_LOGI(TAG, "Success"); }{ ... } static httpd_handle_t test_httpd_start(void) { pre_start_mem = esp_get_free_heap_size(); httpd_handle_t hd; httpd_config_t config = HTTPD_DEFAULT_CONFIG(); /* Modify this setting to match the number of test URI handlers */ config.max_uri_handlers = 9; config.server_port = 1234; /* This check should be a part of http_server */ config.max_open_sockets = (CONFIG_LWIP_MAX_SOCKETS - 3); if (httpd_start(&hd, &config) == ESP_OK) { ESP_LOGI(TAG, "Started HTTP server on port: '%d'", config.server_port); ESP_LOGI(TAG, "Max URI handlers: '%d'", config.max_uri_handlers); ESP_LOGI(TAG, "Max Open Sessions: '%d'", config.max_open_sockets); ESP_LOGI(TAG, "Max Header Length: '%d'", HTTPD_MAX_REQ_HDR_LEN); ESP_LOGI(TAG, "Max URI Length: '%d'", HTTPD_MAX_URI_LEN); ESP_LOGI(TAG, "Max Stack Size: '%d'", config.stack_size); return hd; }{...} return NULL; }{ ... } static void test_httpd_stop(httpd_handle_t hd) { httpd_stop(hd); post_stop_mem = esp_get_free_heap_size(); ESP_LOGI(TAG, "HTTPD Stop: Current free memory: %d", post_stop_mem); }{ ... } httpd_handle_t start_tests(void) { httpd_handle_t hd = test_httpd_start(); if (hd) { register_basic_handlers(hd); }{...} return hd; }{ ... } void stop_tests(httpd_handle_t hd) { ESP_LOGI(TAG, "Stopping httpd"); test_httpd_stop(hd); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.