Select one of the symbols to view example projects that use it.
 
Outline
#include <errno.h>
#include <esp_log.h>
#include <esp_err.h>
#include <esp_http_server.h>
#include "esp_httpd_priv.h"
#include <netinet/tcp.h>
TAG
httpd_sess_set_send_override(httpd_handle_t, int, httpd_send_func_t)
httpd_sess_set_recv_override(httpd_handle_t, int, httpd_recv_func_t)
httpd_sess_set_pending_override(httpd_handle_t, int, httpd_pending_func_t)
httpd_send(httpd_req_t *, const char *, size_t)
httpd_send_all(httpd_req_t *, const char *, size_t)
httpd_recv_pending(httpd_req_t *, char *, size_t)
httpd_recv_with_opt(httpd_req_t *, char *, size_t, bool)
httpd_recv(httpd_req_t *, char *, size_t)
httpd_unrecv(struct httpd_req *, const char *, size_t)
httpd_resp_set_hdr(httpd_req_t *, const char *, const char *)
httpd_resp_set_status(httpd_req_t *, const char *)
httpd_resp_set_type(httpd_req_t *, const char *)
httpd_resp_send(httpd_req_t *, const char *, ssize_t)
httpd_resp_send_chunk(httpd_req_t *, const char *, ssize_t)
httpd_resp_send_err(httpd_req_t *, httpd_err_code_t, const char *)
httpd_resp_send_custom_err(httpd_req_t *, const char *, const char *)
httpd_register_err_handler(httpd_handle_t, httpd_err_code_t, httpd_err_handler_func_t)
httpd_req_handle_err(httpd_req_t *, httpd_err_code_t)
httpd_req_recv(httpd_req_t *, char *, size_t)
httpd_req_async_handler_begin(httpd_req_t *, httpd_req_t **)
httpd_req_async_handler_complete(httpd_req_t *)
httpd_req_to_sockfd(httpd_req_t *)
httpd_sock_err(const char *, int)
httpd_default_send(httpd_handle_t, int, const char *, size_t, int)
httpd_default_recv(httpd_handle_t, int, char *, size_t, int)
httpd_socket_send(httpd_handle_t, int, const char *, size_t, int)
httpd_socket_recv(httpd_handle_t, int, char *, size_t, int)
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/esp_http_server/src/httpd_txrx.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <errno.h> #include <esp_log.h> #include <esp_err.h> #include <esp_http_server.h> #include "esp_httpd_priv.h" #include <netinet/tcp.h>6 includes static const char *TAG = "httpd_txrx"; esp_err_t httpd_sess_set_send_override(httpd_handle_t hd, int sockfd, httpd_send_func_t send_func) { struct sock_db *sess = httpd_sess_get(hd, sockfd); if (!sess) { return ESP_ERR_INVALID_ARG; }{...} sess->send_fn = send_func; return ESP_OK; }{ ... } esp_err_t httpd_sess_set_recv_override(httpd_handle_t hd, int sockfd, httpd_recv_func_t recv_func) { struct sock_db *sess = httpd_sess_get(hd, sockfd); if (!sess) { return ESP_ERR_INVALID_ARG; }{...} sess->recv_fn = recv_func; return ESP_OK; }{ ... } esp_err_t httpd_sess_set_pending_override(httpd_handle_t hd, int sockfd, httpd_pending_func_t pending_func) { struct sock_db *sess = httpd_sess_get(hd, sockfd); if (!sess) { return ESP_ERR_INVALID_ARG; }{...} sess->pending_fn = pending_func; return ESP_OK; }{ ... } int httpd_send(httpd_req_t *r, const char *buf, size_t buf_len) { if (r == NULL || buf == NULL) { return HTTPD_SOCK_ERR_INVALID; }{...} if (!httpd_valid_req(r)) { return HTTPD_SOCK_ERR_INVALID; }{...} struct httpd_req_aux *ra = r->aux; int ret = ra->sd->send_fn(ra->sd->handle, ra->sd->fd, buf, buf_len, 0); if (ret < 0) { ESP_LOGD(TAG, LOG_FMT("error in send_fn")); return ret; }{...} return ret; }{ ... } static esp_err_t httpd_send_all(httpd_req_t *r, const char *buf, size_t buf_len) { struct httpd_req_aux *ra = r->aux; int ret; while (buf_len > 0) { ret = ra->sd->send_fn(ra->sd->handle, ra->sd->fd, buf, buf_len, 0); if (ret < 0) { ESP_LOGD(TAG, LOG_FMT("error in send_fn")); return ESP_FAIL; }{...} ESP_LOGD(TAG, LOG_FMT("sent = %d"), ret); buf += ret; buf_len -= ret; }{...} return ESP_OK; }{ ... } static size_t httpd_recv_pending(httpd_req_t *r, char *buf, size_t buf_len) { struct httpd_req_aux *ra = r->aux; size_t offset = sizeof(ra->sd->pending_data) - ra->sd->pending_len; /* buf_len must not be greater than remaining_len */ buf_len = MIN(ra->sd->pending_len, buf_len); memcpy(buf, ra->sd->pending_data + offset, buf_len); ra->sd->pending_len -= buf_len; return buf_len; }{ ... } int httpd_recv_with_opt(httpd_req_t *r, char *buf, size_t buf_len, bool halt_after_pending) { ESP_LOGD(TAG, LOG_FMT("requested length = %"NEWLIB_NANO_COMPAT_FORMAT), NEWLIB_NANO_COMPAT_CAST(buf_len)); size_t pending_len = 0; struct httpd_req_aux *ra = r->aux; /* First fetch pending data from local buffer */ if (ra->sd->pending_len > 0) { ESP_LOGD(TAG, LOG_FMT("pending length = %"NEWLIB_NANO_COMPAT_FORMAT), NEWLIB_NANO_COMPAT_CAST(ra->sd->pending_len)); pending_len = httpd_recv_pending(r, buf, buf_len); buf += pending_len; buf_len -= pending_len; /* If buffer filled then no need to recv. * If asked to halt after receiving pending data then * return with received length *//* ... */ if (!buf_len || halt_after_pending) { return pending_len; }{...} }{...} /* Receive data of remaining length */ int ret = ra->sd->recv_fn(ra->sd->handle, ra->sd->fd, buf, buf_len, 0); if (ret < 0) { ESP_LOGD(TAG, LOG_FMT("error in recv_fn")); if ((ret == HTTPD_SOCK_ERR_TIMEOUT) && (pending_len != 0)) { /* If recv() timeout occurred, but pending data is * present, return length of pending data. * This behavior is similar to that of socket recv() * function, which, in case has only partially read the * requested length, due to timeout, returns with read * length, rather than error *//* ... */ return pending_len; }{...} return ret; }{...} ESP_LOGD(TAG, LOG_FMT("received length = %"NEWLIB_NANO_COMPAT_FORMAT), NEWLIB_NANO_COMPAT_CAST((ret + pending_len))); return ret + pending_len; }{ ... } int httpd_recv(httpd_req_t *r, char *buf, size_t buf_len) { return httpd_recv_with_opt(r, buf, buf_len, false); }{ ... } size_t httpd_unrecv(struct httpd_req *r, const char *buf, size_t buf_len) { struct httpd_req_aux *ra = r->aux; /* Truncate if external buf_len is greater than pending_data buffer size */ ra->sd->pending_len = MIN(sizeof(ra->sd->pending_data), buf_len); /* Copy data into internal pending_data buffer with the exact offset * such that it is right aligned inside the buffer *//* ... */ size_t offset = sizeof(ra->sd->pending_data) - ra->sd->pending_len; memcpy(ra->sd->pending_data + offset, buf, ra->sd->pending_len); ESP_LOGD(TAG, LOG_FMT("length = %"NEWLIB_NANO_COMPAT_FORMAT), NEWLIB_NANO_COMPAT_CAST(ra->sd->pending_len)); return ra->sd->pending_len; }{ ... } /** * This API appends an additional header field-value pair in the HTTP response. * But the header isn't sent out until any of the send APIs is executed. *//* ... */ esp_err_t httpd_resp_set_hdr(httpd_req_t *r, const char *field, const char *value) { if (r == NULL || field == NULL || value == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (!httpd_valid_req(r)) { return ESP_ERR_HTTPD_INVALID_REQ; }{...} struct httpd_req_aux *ra = r->aux; struct httpd_data *hd = (struct httpd_data *) r->handle; /* Number of additional headers is limited */ if (ra->resp_hdrs_count >= hd->config.max_resp_headers) { return ESP_ERR_HTTPD_RESP_HDR; }{...} /* Assign header field-value pair */ ra->resp_hdrs[ra->resp_hdrs_count].field = field; ra->resp_hdrs[ra->resp_hdrs_count].value = value; ra->resp_hdrs_count++; ESP_LOGD(TAG, LOG_FMT("new header = %s: %s"), field, value); return ESP_OK; }{ ... } /** * This API sets the status of the HTTP response to the value specified. * But the status isn't sent out until any of the send APIs is executed. *//* ... */ esp_err_t httpd_resp_set_status(httpd_req_t *r, const char *status) { if (r == NULL || status == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (!httpd_valid_req(r)) { return ESP_ERR_HTTPD_INVALID_REQ; }{...} struct httpd_req_aux *ra = r->aux; ra->status = (char *)status; return ESP_OK; }{ ... } /** * This API sets the method/type of the HTTP response to the value specified. * But the method isn't sent out until any of the send APIs is executed. *//* ... */ esp_err_t httpd_resp_set_type(httpd_req_t *r, const char *type) { if (r == NULL || type == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (!httpd_valid_req(r)) { return ESP_ERR_HTTPD_INVALID_REQ; }{...} struct httpd_req_aux *ra = r->aux; ra->content_type = (char *)type; return ESP_OK; }{ ... } esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, ssize_t buf_len) { if (r == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (!httpd_valid_req(r)) { return ESP_ERR_HTTPD_INVALID_REQ; }{...} struct httpd_req_aux *ra = r->aux; const char *httpd_hdr_str = "HTTP/1.1 %s\r\nContent-Type: %s\r\nContent-Length: %d\r\n"; const char *colon_separator = ": "; const char *cr_lf_seperator = "\r\n"; if (buf_len == HTTPD_RESP_USE_STRLEN) { buf_len = strlen(buf); }{...} /* Request headers are no longer available */ ra->req_hdrs_count = 0; /* Size of essential headers is limited by scratch buffer size */ if (snprintf(ra->scratch, sizeof(ra->scratch), httpd_hdr_str, ra->status, ra->content_type, buf_len) >= sizeof(ra->scratch)) { return ESP_ERR_HTTPD_RESP_HDR; }{...} /* Sending essential headers */ if (httpd_send_all(r, ra->scratch, strlen(ra->scratch)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} /* Sending additional headers based on set_header */ for (unsigned i = 0; i < ra->resp_hdrs_count; i++) { /* Send header field */ if (httpd_send_all(r, ra->resp_hdrs[i].field, strlen(ra->resp_hdrs[i].field)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} /* Send ': ' */ if (httpd_send_all(r, colon_separator, strlen(colon_separator)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} /* Send header value */ if (httpd_send_all(r, ra->resp_hdrs[i].value, strlen(ra->resp_hdrs[i].value)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} /* Send CR + LF */ if (httpd_send_all(r, cr_lf_seperator, strlen(cr_lf_seperator)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} }{...} /* End header section */ if (httpd_send_all(r, cr_lf_seperator, strlen(cr_lf_seperator)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} esp_http_server_dispatch_event(HTTP_SERVER_EVENT_HEADERS_SENT, &(ra->sd->fd), sizeof(int)); /* Sending content */ if (buf && buf_len) { if (httpd_send_all(r, buf, buf_len) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} }{...} esp_http_server_event_data evt_data = { .fd = ra->sd->fd, .data_len = buf_len, }{...}; esp_http_server_dispatch_event(HTTP_SERVER_EVENT_SENT_DATA, &evt_data, sizeof(esp_http_server_event_data)); return ESP_OK; }{ ... } esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, ssize_t buf_len) { if (r == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (!httpd_valid_req(r)) { return ESP_ERR_HTTPD_INVALID_REQ; }{...} if (buf_len == HTTPD_RESP_USE_STRLEN) { buf_len = strlen(buf); }{...} struct httpd_req_aux *ra = r->aux; const char *httpd_chunked_hdr_str = "HTTP/1.1 %s\r\nContent-Type: %s\r\nTransfer-Encoding: chunked\r\n"; const char *colon_separator = ": "; const char *cr_lf_seperator = "\r\n"; /* Request headers are no longer available */ ra->req_hdrs_count = 0; if (!ra->first_chunk_sent) { /* Size of essential headers is limited by scratch buffer size */ if (snprintf(ra->scratch, sizeof(ra->scratch), httpd_chunked_hdr_str, ra->status, ra->content_type) >= sizeof(ra->scratch)) { return ESP_ERR_HTTPD_RESP_HDR; }{...} /* Sending essential headers */ if (httpd_send_all(r, ra->scratch, strlen(ra->scratch)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} /* Sending additional headers based on set_header */ for (unsigned i = 0; i < ra->resp_hdrs_count; i++) { /* Send header field */ if (httpd_send_all(r, ra->resp_hdrs[i].field, strlen(ra->resp_hdrs[i].field)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} /* Send ': ' */ if (httpd_send_all(r, colon_separator, strlen(colon_separator)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} /* Send header value */ if (httpd_send_all(r, ra->resp_hdrs[i].value, strlen(ra->resp_hdrs[i].value)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} /* Send CR + LF */ if (httpd_send_all(r, cr_lf_seperator, strlen(cr_lf_seperator)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} }{...} /* End header section */ if (httpd_send_all(r, cr_lf_seperator, strlen(cr_lf_seperator)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} ra->first_chunk_sent = true; }{...} /* Sending chunked content */ char len_str[10]; snprintf(len_str, sizeof(len_str), "%lx\r\n", (long)buf_len); if (httpd_send_all(r, len_str, strlen(len_str)) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} if (buf) { if (httpd_send_all(r, buf, (size_t) buf_len) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} }{...} /* Indicate end of chunk */ if (httpd_send_all(r, "\r\n", strlen("\r\n")) != ESP_OK) { return ESP_ERR_HTTPD_RESP_SEND; }{...} esp_http_server_event_data evt_data = { .fd = ra->sd->fd, .data_len = buf_len, }{...}; esp_http_server_dispatch_event(HTTP_SERVER_EVENT_SENT_DATA, &evt_data, sizeof(esp_http_server_event_data)); return ESP_OK; }{ ... } esp_err_t httpd_resp_send_err(httpd_req_t *req, httpd_err_code_t error, const char *usr_msg) { esp_err_t ret; const char *msg; const char *status; switch (error) { case HTTPD_501_METHOD_NOT_IMPLEMENTED: status = "501 Method Not Implemented"; msg = "Server does not support this method"; break;... case HTTPD_505_VERSION_NOT_SUPPORTED: status = "505 Version Not Supported"; msg = "HTTP version not supported by server"; break;... case HTTPD_400_BAD_REQUEST: status = "400 Bad Request"; msg = "Bad request syntax"; break;... case HTTPD_401_UNAUTHORIZED: status = "401 Unauthorized"; msg = "No permission -- see authorization schemes"; break;... case HTTPD_403_FORBIDDEN: status = "403 Forbidden"; msg = "Request forbidden -- authorization will not help"; break;... case HTTPD_404_NOT_FOUND: status = "404 Not Found"; msg = "Nothing matches the given URI"; break;... case HTTPD_405_METHOD_NOT_ALLOWED: status = "405 Method Not Allowed"; msg = "Specified method is invalid for this resource"; break;... case HTTPD_408_REQ_TIMEOUT: status = "408 Request Timeout"; msg = "Server closed this connection"; break;... case HTTPD_414_URI_TOO_LONG: status = "414 URI Too Long"; msg = "URI is too long"; break;... case HTTPD_411_LENGTH_REQUIRED: status = "411 Length Required"; msg = "Client must specify Content-Length"; break;... case HTTPD_413_CONTENT_TOO_LARGE: status = "413 Content Too Large"; msg = "Content is too large"; break;... case HTTPD_431_REQ_HDR_FIELDS_TOO_LARGE: status = "431 Request Header Fields Too Large"; msg = "Header fields are too long"; break;... case HTTPD_500_INTERNAL_SERVER_ERROR: default: status = "500 Internal Server Error"; msg = "Server has encountered an unexpected error";... }{...} /* If user has provided custom message, override default message */ msg = usr_msg ? usr_msg : msg; ESP_LOGW(TAG, LOG_FMT("%s - %s"), status, msg); /* Set error code in HTTP response */ httpd_resp_set_status(req, status); httpd_resp_set_type(req, HTTPD_TYPE_TEXT); #ifdef CONFIG_HTTPD_ERR_RESP_NO_DELAY /* Use TCP_NODELAY option to force socket to send data in buffer * This ensures that the error message is sent before the socket * is closed *//* ... */ struct httpd_req_aux *ra = req->aux; int nodelay = 1; if (setsockopt(ra->sd->fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) < 0) { /* If failed to turn on TCP_NODELAY, throw warning and continue */ ESP_LOGW(TAG, LOG_FMT("error calling setsockopt : %d"), errno); nodelay = 0; }{...} /* ... */#endif /* Send HTTP error message */ ret = httpd_resp_send(req, msg, HTTPD_RESP_USE_STRLEN); #ifdef CONFIG_HTTPD_ERR_RESP_NO_DELAY /* If TCP_NODELAY was set successfully above, time to disable it */ if (nodelay == 1) { nodelay = 0; if (setsockopt(ra->sd->fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) < 0) { /* If failed to turn off TCP_NODELAY, throw error and * return failure to signal for socket closure *//* ... */ ESP_LOGE(TAG, LOG_FMT("error calling setsockopt : %d"), errno); return ESP_ERR_INVALID_STATE; }{...} }{...} #endif/* ... */ esp_http_server_dispatch_event(HTTP_SERVER_EVENT_ERROR, &error, sizeof(httpd_err_code_t)); return ret; }{ ... } esp_err_t httpd_resp_send_custom_err(httpd_req_t *req, const char *status, const char *msg) { ESP_LOGW(TAG, LOG_FMT("%s - %s"), status, msg); /* Set error code in HTTP response */ httpd_resp_set_status(req, status); httpd_resp_set_type(req, HTTPD_TYPE_TEXT); #ifdef CONFIG_HTTPD_ERR_RESP_NO_DELAY /* Use TCP_NODELAY option to force socket to send data in buffer * This ensures that the error message is sent before the socket * is closed *//* ... */ struct httpd_req_aux *ra = req->aux; int nodelay = 1; if (setsockopt(ra->sd->fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) < 0) { /* If failed to turn on TCP_NODELAY, throw warning and continue */ ESP_LOGW(TAG, LOG_FMT("error calling setsockopt : %d"), errno); nodelay = 0; }{...} /* ... */#endif /* Send HTTP error message */ esp_err_t ret = httpd_resp_send(req, msg, HTTPD_RESP_USE_STRLEN); #ifdef CONFIG_HTTPD_ERR_RESP_NO_DELAY /* If TCP_NODELAY was set successfully above, time to disable it */ if (nodelay == 1) { nodelay = 0; if (setsockopt(ra->sd->fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) < 0) { /* If failed to turn off TCP_NODELAY, throw error and * return failure to signal for socket closure *//* ... */ ESP_LOGE(TAG, LOG_FMT("error calling setsockopt : %d"), errno); return ESP_ERR_INVALID_STATE; }{...} }{...} #endif/* ... */ return ret; }{ ... } esp_err_t httpd_register_err_handler(httpd_handle_t handle, httpd_err_code_t error, httpd_err_handler_func_t err_handler_fn) { if (handle == NULL || error >= HTTPD_ERR_CODE_MAX) { return ESP_ERR_INVALID_ARG; }{...} struct httpd_data *hd = (struct httpd_data *) handle; hd->err_handler_fns[error] = err_handler_fn; return ESP_OK; }{ ... } esp_err_t httpd_req_handle_err(httpd_req_t *req, httpd_err_code_t error) { struct httpd_data *hd = (struct httpd_data *) req->handle; esp_err_t ret; /* Invoke custom error handler if configured */ if (hd->err_handler_fns[error]) { ret = hd->err_handler_fns[error](req, error); /* If error code is 500, force return failure * irrespective of the handler's return value *//* ... */ ret = (error == HTTPD_500_INTERNAL_SERVER_ERROR ? ESP_FAIL : ret); }{...} else { /* If no handler is registered for this error default * behavior is to send the HTTP error response and * return failure for closure of underlying socket *//* ... */ httpd_resp_send_err(req, error, NULL); ret = ESP_FAIL; }{...} return ret; }{ ... } int httpd_req_recv(httpd_req_t *r, char *buf, size_t buf_len) { if (r == NULL || buf == NULL) { return HTTPD_SOCK_ERR_INVALID; }{...} if (!httpd_valid_req(r)) { ESP_LOGW(TAG, LOG_FMT("invalid request")); return HTTPD_SOCK_ERR_INVALID; }{...} struct httpd_req_aux *ra = r->aux; ESP_LOGD(TAG, LOG_FMT("remaining length = %"NEWLIB_NANO_COMPAT_FORMAT), NEWLIB_NANO_COMPAT_CAST(ra->remaining_len)); if (buf_len > ra->remaining_len) { buf_len = ra->remaining_len; }{...} if (buf_len == 0) { return buf_len; }{...} int ret = httpd_recv(r, buf, buf_len); if (ret < 0) { ESP_LOGD(TAG, LOG_FMT("error in httpd_recv")); return ret; }{...} ra->remaining_len -= ret; ESP_LOGD(TAG, LOG_FMT("received length = %d"), ret); esp_http_server_event_data evt_data = { .fd = ra->sd->fd, .data_len = ret, }{...}; esp_http_server_dispatch_event(HTTP_SERVER_EVENT_ON_DATA, &evt_data, sizeof(esp_http_server_event_data)); return ret; }{ ... } esp_err_t httpd_req_async_handler_begin(httpd_req_t *r, httpd_req_t **out) { if (r == NULL || out == NULL) { return ESP_ERR_INVALID_ARG; }{...} // alloc async req httpd_req_t *async = malloc(sizeof(httpd_req_t)); if (async == NULL) { return ESP_ERR_NO_MEM; }{...} memcpy(async, r, sizeof(httpd_req_t)); // alloc async aux async->aux = malloc(sizeof(struct httpd_req_aux)); if (async->aux == NULL) { free(async); return ESP_ERR_NO_MEM; }{...} memcpy(async->aux, r->aux, sizeof(struct httpd_req_aux)); // Copy response header block struct httpd_data *hd = (struct httpd_data *) r->handle; struct httpd_req_aux *async_aux = (struct httpd_req_aux *) async->aux; struct httpd_req_aux *r_aux = (struct httpd_req_aux *) r->aux; async_aux->resp_hdrs = calloc(hd->config.max_resp_headers, sizeof(struct resp_hdr)); if (async_aux->resp_hdrs == NULL) { free(async_aux); free(async); return ESP_ERR_NO_MEM; }{...} memcpy(async_aux->resp_hdrs, r_aux->resp_hdrs, hd->config.max_resp_headers * sizeof(struct resp_hdr)); // Prevent the main thread from reading the rest of the request after the handler returns. r_aux->remaining_len = 0; // mark socket as "in use" r_aux->sd->for_async_req = true; *out = async; return ESP_OK; }{ ... } esp_err_t httpd_req_async_handler_complete(httpd_req_t *r) { if (r == NULL) { return ESP_ERR_INVALID_ARG; }{...} struct httpd_req_aux *ra = r->aux; ra->sd->for_async_req = false; free(ra->resp_hdrs); free(r->aux); free(r); return ESP_OK; }{ ... } int httpd_req_to_sockfd(httpd_req_t *r) { if (r == NULL) { return -1; }{...} if (!httpd_valid_req(r)) { ESP_LOGW(TAG, LOG_FMT("invalid request")); return -1; }{...} struct httpd_req_aux *ra = r->aux; return ra->sd->fd; }{ ... } static int httpd_sock_err(const char *ctx, int sockfd) { int errval; ESP_LOGW(TAG, LOG_FMT("error in %s : %d"), ctx, errno); switch (errno) { case EAGAIN: case EINTR: errval = HTTPD_SOCK_ERR_TIMEOUT; break;... case EINVAL: case EBADF: case EFAULT: case ENOTSOCK: errval = HTTPD_SOCK_ERR_INVALID; break;... default: errval = HTTPD_SOCK_ERR_FAIL;... }{...} return errval; }{ ... } int httpd_default_send(httpd_handle_t hd, int sockfd, const char *buf, size_t buf_len, int flags) { (void)hd; if (buf == NULL) { return HTTPD_SOCK_ERR_INVALID; }{...} int ret = send(sockfd, buf, buf_len, flags); if (ret < 0) { return httpd_sock_err("send", sockfd); }{...} return ret; }{ ... } int httpd_default_recv(httpd_handle_t hd, int sockfd, char *buf, size_t buf_len, int flags) { (void)hd; if (buf == NULL) { return HTTPD_SOCK_ERR_INVALID; }{...} int ret = recv(sockfd, buf, buf_len, flags); if (ret < 0) { return httpd_sock_err("recv", sockfd); }{...} return ret; }{ ... } int httpd_socket_send(httpd_handle_t hd, int sockfd, const char *buf, size_t buf_len, int flags) { struct sock_db *sess = httpd_sess_get(hd, sockfd); if (!sess) { return HTTPD_SOCK_ERR_INVALID; }{...} if (!sess->send_fn) { return HTTPD_SOCK_ERR_INVALID; }{...} return sess->send_fn(hd, sockfd, buf, buf_len, flags); }{ ... } int httpd_socket_recv(httpd_handle_t hd, int sockfd, char *buf, size_t buf_len, int flags) { struct sock_db *sess = httpd_sess_get(hd, sockfd); if (!sess) { return HTTPD_SOCK_ERR_INVALID; }{...} if (!sess->recv_fn) { return HTTPD_SOCK_ERR_INVALID; }{...} return sess->recv_fn(hd, sockfd, buf, buf_len, flags); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.