-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd_main.c
674 lines (602 loc) · 18.6 KB
/
httpd_main.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
/*
SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <esp_log.h>
#include <esp_err.h>
#include <assert.h>
#include <esp_http_server.h>
#include "esp_httpd_priv.h"
#include "tlsserver.h"
typedef struct {
fd_set *fdset;
struct httpd_data *hd;
} process_session_context_t;
static const char *TAG = "httpd";
#define IPAddress unsigned long
IPAddress ip;
static esp_err_t httpd_accept_conn(struct httpd_data *hd, int listen_fd, struct sock_db **newsession)
{
debug("new TLS connection\n");
/* If no space is available for new session, close the least recently used one */
if (hd->config.lru_purge_enable == true) {
if (!httpd_is_sess_available(hd)) {
/* Queue asynchronous closure of the least recently used session */
return httpd_sess_close_lru(hd);
/* Returning from this allowes the main server thread to process
the queued asynchronous control message for closing LRU session.
Since connection request hasn't been addressed yet using accept()
therefore httpd_accept_conn() will be called again, but this time
with space available for one session
*/
}
}
struct sockaddr_in addr_from;
socklen_t addr_from_len = sizeof(addr_from);
int new_fd = accept(listen_fd, (struct sockaddr *)&addr_from, &addr_from_len);
if (new_fd < 0) {
ESP_LOGW(TAG, LOG_FMT("error in accept (%d)"), errno);
return ESP_FAIL;
}
ESP_LOGD(TAG, LOG_FMT("newfd = %d"), new_fd);
struct timeval tv;
/* Set recv timeout of this fd as per config ???*/
tv.tv_sec = 0;
tv.tv_usec = 0;
setsockopt(new_fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(tv));
/* Set send timeout of this fd as per config */
tv.tv_sec = hd->config.send_wait_timeout;
tv.tv_usec = 0;
int val = 1;
setsockopt(new_fd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&tv, sizeof(tv));
setsockopt(new_fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&val, sizeof(int));
int status = fcntl(new_fd, F_SETFL, fcntl(new_fd, F_GETFL, 0) | O_NONBLOCK); // set nonblocking
if (ESP_OK != httpd_sess_new(hd, new_fd, newsession)) {
printf("session creation failed\n");
close(new_fd);
*newsession = NULL;
return ESP_FAIL;
}
printf("TLS session accepted\n");
return ESP_OK;
}
esp_err_t httpd_queue_work(httpd_handle_t handle, httpd_work_fn_t work, void *arg)
{
return ESP_OK;
}
esp_err_t httpd_get_client_list(httpd_handle_t handle, size_t *fds, int *client_fds)
{
struct httpd_data *hd = (struct httpd_data *) handle;
if (hd == NULL || fds == NULL || *fds == 0 || client_fds == NULL) {
return ESP_ERR_INVALID_ARG;
}
size_t max_fds = *fds;
*fds = 0;
for (int i = 0; i < hd->config.max_open_sockets; ++i) {
if (hd->hd_sd[i].fd != -1) {
if (*fds < max_fds) {
client_fds[(*fds)++] = hd->hd_sd[i].fd;
} else {
return ESP_ERR_INVALID_ARG;
}
}
}
return ESP_OK;
}
void *httpd_get_global_user_ctx(httpd_handle_t handle)
{
return ((struct httpd_data *)handle)->config.global_user_ctx;
}
void *httpd_get_global_transport_ctx(httpd_handle_t handle)
{
return ((struct httpd_data *)handle)->config.global_transport_ctx;
}
// Called for each session from httpd_server
static int httpd_process_session(struct sock_db *session, void *context)
{
if ((!session) || (!context)) {
return 0;
}
if (session->fd < 0) {
return 1;
}
printf("processsession\n");
process_session_context_t *ctx = (process_session_context_t *)context;
int fd = session->fd;
if (FD_ISSET(fd, ctx->fdset) || httpd_sess_pending(ctx->hd, session)) {
printf("processing socket %d\n", fd);
if (httpd_sess_process(ctx->hd, session) != ESP_OK) {
printf("delete session\n");
httpd_sess_delete(ctx->hd, session); // Delete session
}
}
return 1;
}
//MY functions
void process(struct httpd_data *hd, struct sock_db *session) {
if (httpd_sess_process(hd, session) != ESP_OK) {
printf("not OK process\n");
httpd_sess_delete(hd, session); // Delete session
}
}
// check if listen socket was connected for new connection
struct sock_db * checkAvail(struct httpd_data *hd)
{
debug("avail\n");
struct sock_db *newsession = NULL;
fd_set read_set;
FD_ZERO(&read_set);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 10000;
if (hd->config.lru_purge_enable || httpd_is_sess_available(hd)) {
/* Only listen for new connections if server has capacity to
handle more (or when LRU purge is enabled, in which case
older connections will be closed) */
FD_SET(hd->listen_fd, &read_set);
}
int tmp_max_fd;
httpd_sess_set_descriptors(hd, &read_set, &tmp_max_fd); ////seems to be necessary???
int maxfd = MAX(hd->listen_fd, tmp_max_fd);
tmp_max_fd = maxfd;
debug("maxfd %d\n",maxfd);
int active_cnt = select(maxfd + 1, &read_set, NULL, NULL, &timeout);
if (active_cnt <= 0) return NULL;
debug("active=%d max=%d\n", active_cnt, maxfd);
if (FD_ISSET(hd->listen_fd, &read_set)) {
debug("processing listen socket %d\n", hd->listen_fd);
if (httpd_accept_conn(hd, hd->listen_fd, &newsession) != ESP_OK) {
printf("error accepting new connection\n");
}
else
{
printf("new broker connection\n");
return newsession;
}
}
return NULL;
}
// checks if there are any (TLS) bytes waiting in TLS buffer
bool pendingData(struct sock_db *session)
{
if (!session) {
return false;
}
struct httpd_data * hd = session->handle;
if (!hd) {
return false;
}
return httpd_sess_pending(hd, session);
}
// checks if there are any (TLS) bytes waiting on socket
int peekData(struct sock_db *session)
{
if (!session) {
return false;
}
struct httpd_data * hd = session->handle;
if (!hd) {
return false;
}
int count = 0;
ioctl(session->fd, FIONREAD, &count);
if (count > 0)
printf("%d socket bytes ready\n", count);
return count;
}
// reads bytes in TLS buffer for socket
int readData(struct sock_db *session, char* buf, size_t buflen)
{
if (!session) {
return false;
}
struct httpd_data * hd = session->handle;
if (!hd) {
return false;
}
int ret = session->recv_fn(session->handle, session->fd, buf, buflen, 0);
if (ret < 0) {
printf("my error in readData on socket %d", session->fd);
httpd_sess_delete(hd, session);
return -1;
}
else
{
/* for (int i = 0; i < ret; i++)
{
printf("%02x", buf[i]);
fflush(stdout);
}*/
}
return ret;
}
// reads bytes in TLS buffer for socket
int writeData(struct sock_db *session, char* buf, size_t buflen)
{
if (!session) {
printf("invalid session\n");
return false;
}
struct httpd_data * hd = session->handle;
if (!hd) {
printf("invalid hd\n");
return false;
}
int ret = session->send_fn(session->handle, session->fd, buf, buflen, 0);
if (ret < 0) {
printf("my error in sendData on socket %d\n", session->fd);
httpd_sess_delete(hd, session);
return -1;
}
else if (ret == 0) {
printf("nothing written\n");
}
else
{
/* for (int i = 0; i < ret; i++)
{
printf("%02x", buf[i]);
fflush(stdout);
}*/
}
return ret;
}
bool canReadData(struct sock_db * session)
{
fd_set read_set;
FD_ZERO(&read_set);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
if (!session) {
printf("canReadData zero session\n");
return false;
}
struct httpd_data * hd = session->handle;
if (!hd) {
printf("canReadData zero hd\n");
return false;
}
if (session->fd < 0) {
printf("canReadData invalid fd\n");
return false;
}
FD_SET(session->fd, &read_set);
int active_cnt = select((session->fd) + 1, &read_set, NULL, NULL, &tv);
debug("canReadData active=%d\n", active_cnt);
if (FD_ISSET(session->fd, &read_set) || httpd_sess_pending(hd, session)) {
debug("data on socket %d\n", session->fd);
return true;
}
return false;
}
/* Manage in-coming connection or data requests */
esp_err_t httpd_server(struct httpd_data * hd)
{
struct sock_db *newsession = NULL;
fd_set read_set;
FD_ZERO(&read_set);
if (hd->config.lru_purge_enable || httpd_is_sess_available(hd)) {
/* Only listen for new connections if server has capacity to
handle more (or when LRU purge is enabled, in which case
older connections will be closed) */
FD_SET(hd->listen_fd, &read_set);
}
int tmp_max_fd;
httpd_sess_set_descriptors(hd, &read_set, &tmp_max_fd);
int maxfd = MAX(hd->listen_fd, tmp_max_fd);
tmp_max_fd = maxfd;
debug("server select\n");
ESP_LOGD(TAG, LOG_FMT("doing select maxfd+1 = %d"), maxfd + 1);
int active_cnt = select(maxfd + 1, &read_set, NULL, NULL, NULL);
debug("server select after\n");
if (active_cnt < 0) {
printf("error in select (%d)\n", errno);
httpd_sess_delete_invalid(hd);
return ESP_OK;
}
/* Case0: Do we have a control message? */
//newsession = checkAvail(hd);
/* Case1: Do we have any activity on the current data
sessions? */
//sess_enum(hd);
process_session_context_t context = {
.fdset = &read_set,
.hd = hd
};
httpd_sess_enum(hd, httpd_process_session, &context);
/* Case2: Do we have any incoming connection requests to
process? */
/*if (FD_ISSET(hd->listen_fd, &read_set)) {
printf("processing listen socket %d\n", hd->listen_fd);
if (httpd_accept_conn(hd, hd->listen_fd, &newsession) != ESP_OK) {
printf("error accepting new connection\n");
}
else printf("new connection\n");
}*/
return ESP_OK;
}
/* The main HTTPD thread */
static void httpd_thread(void *arg)
{
int ret;
struct httpd_data *hd = (struct httpd_data *) arg;
hd->hd_td.status = THREAD_RUNNING;
debug("web server thread started\n");
while (1) {
vTaskDelay(500);
/*ret = httpd_server(hd);
if (ret != ESP_OK) {
break;
}*/
}
ESP_LOGD(TAG, LOG_FMT("web server exiting"));
close(hd->msg_fd);
httpd_sess_close_all(hd);
close(hd->listen_fd);
hd->hd_td.status = THREAD_STOPPED;
httpd_os_thread_delete();
}
static esp_err_t httpd_server_init(struct httpd_data * hd)
{
int fd = socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
printf("error in initial socket (%d)\n", errno);
return ESP_FAIL;
}
struct sockaddr_in serv_addr = {
.sin_family = PF_INET,
.sin_addr = {
.s_addr = ip
},
.sin_port = htons(hd->config.server_port)
};
/* Enable SO_REUSEADDR to allow binding to the same
address and port when restarting the server */
int enable = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0) {
/* This will fail if CONFIG_LWIP_SO_REUSE is not enabled. But
it does not affect the normal working of the HTTP Server */
ESP_LOGW(TAG, LOG_FMT("error enabling SO_REUSEADDR (%d)"), errno);
}
int ret = bind(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (ret < 0) {
ESP_LOGE(TAG, LOG_FMT("error in bind (%d)"), errno);
close(fd);
return ESP_FAIL;
}
ret = listen(fd, hd->config.backlog_conn);
if (ret < 0) {
ESP_LOGE(TAG, LOG_FMT("error in listen (%d)"), errno);
close(fd);
return ESP_FAIL;
}
int msg_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (msg_fd < 0) {
ESP_LOGE(TAG, LOG_FMT("error in creating msg socket (%d)"), errno);
close(fd);
return ESP_FAIL;
}
hd->listen_fd = fd;
hd->msg_fd = msg_fd;
return ESP_OK;
}
static struct httpd_data *httpd_create(const httpd_config_t *config)
{
/* Allocate memory for httpd instance data */
struct httpd_data *hd = calloc(1, sizeof(struct httpd_data));
if (!hd) {
ESP_LOGE(TAG, LOG_FMT("Failed to allocate memory for HTTP server instance"));
return NULL;
}
hd->hd_calls = calloc(config->max_uri_handlers, sizeof(httpd_uri_t *));
if (!hd->hd_calls) {
ESP_LOGE(TAG, LOG_FMT("Failed to allocate memory for HTTP URI handlers"));
free(hd);
return NULL;
}
hd->hd_sd = calloc(config->max_open_sockets, sizeof(struct sock_db));
if (!hd->hd_sd) {
ESP_LOGE(TAG, LOG_FMT("Failed to allocate memory for HTTP session data"));
free(hd->hd_calls);
free(hd);
return NULL;
}
struct httpd_req_aux *ra = &hd->hd_req_aux;
ra->resp_hdrs = calloc(config->max_resp_headers, sizeof(struct resp_hdr));
if (!ra->resp_hdrs) {
ESP_LOGE(TAG, LOG_FMT("Failed to allocate memory for HTTP response headers"));
free(hd->hd_sd);
free(hd->hd_calls);
free(hd);
return NULL;
}
hd->err_handler_fns = calloc(HTTPD_ERR_CODE_MAX, sizeof(httpd_err_handler_func_t));
if (!hd->err_handler_fns) {
ESP_LOGE(TAG, LOG_FMT("Failed to allocate memory for HTTP error handlers"));
free(ra->resp_hdrs);
free(hd->hd_sd);
free(hd->hd_calls);
free(hd);
return NULL;
}
/* Save the configuration for this instance */
hd->config = *config;
return hd;
}
static void httpd_delete(struct httpd_data * hd)
{
struct httpd_req_aux *ra = &hd->hd_req_aux;
/* Free memory of httpd instance data */
free(hd->err_handler_fns);
free(ra->resp_hdrs);
free(hd->hd_sd);
/* Free registered URI handlers */
free(hd->hd_calls);
free(hd);
}
esp_err_t httpd_start(httpd_handle_t *handle, const httpd_config_t *config)
{
debug("starting httpd\n");
if (handle == NULL || config == NULL) {
return ESP_ERR_INVALID_ARG;
}
/* Sanity check about whether LWIP is configured for providing the
maximum number of open sockets sufficient for the server. Though,
this check doesn't guarantee that many sockets will actually be
available at runtime as other processes may use up some sockets.
Note that server also uses 3 sockets for its internal use :
1) listening for new TCP connections
2) for sending control messages over UDP
3) for receiving control messages over UDP
So the total number of required sockets is max_open_sockets + 3
*/
if (CONFIG_LWIP_MAX_SOCKETS < config->max_open_sockets + 3) {
ESP_LOGE(TAG, "Configuration option max_open_sockets is too large (max allowed %d)\n\t"
"Either decrease this or configure LWIP_MAX_SOCKETS to a larger value",
CONFIG_LWIP_MAX_SOCKETS - 3);
return ESP_ERR_INVALID_ARG;
}
struct httpd_data *hd = httpd_create(config);
if (hd == NULL) {
/* Failed to allocate memory */
return ESP_ERR_HTTPD_ALLOC_MEM;
}
if (httpd_server_init(hd) != ESP_OK) {
httpd_delete(hd);
return ESP_FAIL;
}
httpd_sess_init(hd);
if (httpd_os_thread_create(&hd->hd_td.handle, "httpd",
hd->config.stack_size,
hd->config.task_priority,
httpd_thread, hd,
hd->config.core_id) != ESP_OK) {
//* Failed to launch task
httpd_delete(hd);
return ESP_ERR_HTTPD_TASK;
}
*handle = (httpd_handle_t *)hd;
return ESP_OK;
}
esp_err_t httpd_stop(httpd_handle_t handle)
{
struct httpd_data *hd = (struct httpd_data *) handle;
if (hd == NULL) {
return ESP_ERR_INVALID_ARG;
}
ESP_LOGD(TAG, LOG_FMT("sent control msg to stop server"));
while (hd->hd_td.status != THREAD_STOPPED) {
httpd_os_thread_sleep(100);
}
/* Release global user context, if not NULL */
if (hd->config.global_user_ctx) {
if (hd->config.global_user_ctx_free_fn) {
hd->config.global_user_ctx_free_fn(hd->config.global_user_ctx);
} else {
free(hd->config.global_user_ctx);
}
hd->config.global_user_ctx = NULL;
}
/* Release global transport context, if not NULL */
if (hd->config.global_transport_ctx) {
if (hd->config.global_transport_ctx_free_fn) {
hd->config.global_transport_ctx_free_fn(hd->config.global_transport_ctx);
} else {
free(hd->config.global_transport_ctx);
}
hd->config.global_transport_ctx = NULL;
}
ESP_LOGD(TAG, LOG_FMT("server stopped"));
httpd_delete(hd);
return ESP_OK;
}
// loop over all sessions and read data
void sess_enum(struct httpd_data * hd)
{
char buf[128];
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
if ((!hd) || (!hd->hd_sd) || (!hd->config.max_open_sockets)) {
return;
}
fd_set read_set;
FD_ZERO(&read_set);
process_session_context_t context = {
.fdset = &read_set,
.hd = hd
};
struct sock_db *current = hd->hd_sd;
struct sock_db *end = hd->hd_sd + hd->config.max_open_sockets - 1;
while (current <= end) {
//bool data = canReadData(current);
// bool x = httpd_sess_pending(hd, current);
// if (x) printf("---pending \n");
while (canReadData(current))
{
int ret = current->recv_fn(current->handle, current->fd, buf, 128, 0);
if (ret < 0) {
printf("my error in recv_fn %d", current->fd);
httpd_sess_delete(hd, current);
return;
}
else
{
printf("%d bytes read:\n", ret);
for (int i = 0; i < ret; i++)
{
printf("%c", buf[i]);
}
printf("\n");
}
if (ret <= 0) break;
}
current++;
}
httpd_sess_delete_invalid(hd);
}
// loop over all sessions and read data
void xsess_enum(struct httpd_data * hd)
{
char buf[128];
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
if ((!hd) || (!hd->hd_sd) || (!hd->config.max_open_sockets)) {
return;
}
struct sock_db *current = hd->hd_sd;
struct sock_db *end = hd->hd_sd + hd->config.max_open_sockets - 1;
while (current <= end) {
//bool data = canReadData(current);
// bool x = httpd_sess_pending(hd, current);
// if (x) printf("---pending \n");
while ((peekData(current) > 0) || pendingData(current))
{
int ret = readData(current, buf, 128);
if (ret < 0) {
printf("my error in recv_fn %d", current->fd);
httpd_sess_delete(hd, current);
return;
}
else
{
printf("%d bytes read:\n", ret);
for (int i = 0; i < ret; i++)
{
printf("%c", buf[i]);
}
printf("\n");
}
if (ret <= 0) break;
}
current++;
}
httpd_sess_delete_invalid(hd);
}