Merge branch 'master' of github.com:cesanta/mongoose

This commit is contained in:
Sergey Lyubka 2021-09-27 13:52:59 +01:00
commit ea85f93e37
10 changed files with 537 additions and 410 deletions

File diff suppressed because it is too large Load Diff

View File

@ -32,28 +32,10 @@ static void forward_request(struct mg_http_message *hm,
(int) hm->uri.len, hm->uri.ptr));
}
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void forward_fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
struct mg_connection *c2 = fn_data;
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (c->label[0] != 'B' && c2 == NULL) {
// Client request, create backend connection Note that we're passing
// client connection `c` as fn_data for the created backend connection.
// This way, we tie together these two connections via `fn_data` pointer:
// client's c->fn_data points to the backend connection, and backend's
// c->fn_data points to the client connection
c2 = mg_connect(c->mgr, s_backend_url, fn, c);
c->fn_data = c2;
if (c2 != NULL) {
c2->is_hexdumping = 1;
c2->label[0] = 'B'; // Mark this connection as backend
} else {
c->is_closing = 1;
}
}
if (c2 != NULL && c2->label[0] == 'B') forward_request(hm, c2);
} else if (ev == MG_EV_READ) {
if (c->label[0] == 'B' && c2 != NULL) {
if (ev == MG_EV_READ) {
if (c2 != NULL) {
// All incoming data from the backend, forward to the client
mg_send(c2, c->recv.buf, c->recv.len);
mg_iobuf_del(&c->recv, 0, c->recv.len);
@ -63,8 +45,31 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
struct mg_tls_opts opts = {.ca = "ca.pem"};
mg_tls_init(c, &opts);
}
}
(void)ev_data;
}
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
struct mg_connection *c2 = fn_data;
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
// Client request, create backend connection Note that we're passing
// client connection `c` as fn_data for the created backend connection.
c2 = mg_connect(c->mgr, s_backend_url, forward_fn, c);
c->fn_data = c2;
if (c2 != NULL) {
forward_request(hm, c2);
} else {
c->is_closing = 1;
}
} else if (ev == MG_EV_CONNECT) {
if (mg_url_is_ssl(s_backend_url)) {
struct mg_tls_opts opts = {.ca = "ca.pem"};
mg_tls_init(c, &opts);
}
} else if (ev == MG_EV_CLOSE) {
if (c->label[0] != 'B' && c2 != NULL) c2->is_closing = 1;
if (c2 != NULL) c2->is_closing = 1;
c->fn_data = NULL;
}
}

View File

@ -0,0 +1,8 @@
NAME ?= $(notdir $(CURDIR))
build:
git clone --depth 1 https://github.com/mongoose-examples/$(NAME)
make -C $(NAME) build
clean:
rm -rf $(NAME)

View File

@ -2966,6 +2966,8 @@ static void mg_set_non_blocking_mode(SOCKET fd) {
setsockopt(fd, 0, FREERTOS_SO_SNDTIMEO, &off, sizeof(off));
#elif MG_ARCH == MG_ARCH_FREERTOS_LWIP
lwip_fcntl(fd, F_SETFL, O_NONBLOCK);
#elif MG_ARCH == MG_ARCH_AZURERTOS
fcntl(fd, F_SETFL, O_NONBLOCK);
#else
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK, FD_CLOEXEC);
#endif
@ -3011,7 +3013,7 @@ SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
#endif
bind(fd, &usa.sa, slen) == 0 &&
// NOTE(lsm): FreeRTOS uses backlog value as a connection limit
(type == SOCK_DGRAM || listen(fd, 128) == 0)) {
(type == SOCK_DGRAM || listen(fd, MG_SOCK_LISTEN_BACKLOG_SIZE) == 0)) {
// In case port was set to 0, get the real port number
if (getsockname(fd, &usa.sa, &slen) == 0) {
addr->port = usa.sin.sin_port;
@ -3136,7 +3138,7 @@ static void close_conn(struct mg_connection *c) {
}
static void setsockopts(struct mg_connection *c) {
#if MG_ARCH == MG_ARCH_FREERTOS_TCP
#if MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_AZURERTOS
(void) c;
#else
int on = 1;
@ -3210,6 +3212,12 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
socklen_t sa_len = sizeof(usa);
SOCKET fd = accept(FD(lsn), &usa.sa, &sa_len);
if (fd == INVALID_SOCKET) {
#if MG_ARCH == MG_ARCH_AZURERTOS
// AzureRTOS, in non-block socket mode can mark listening socket readable
// even it is not. See comment for 'select' func implementation in nx_bsd.c
// That's not an error, just should try later
if (MG_SOCK_ERRNO != EAGAIN)
#endif
LOG(LL_ERROR, ("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO));
#if (!defined(_WIN32) && (MG_ARCH != MG_ARCH_FREERTOS_TCP))
} else if ((long) fd >= FD_SETSIZE) {
@ -4457,7 +4465,7 @@ double mg_time(void) {
((int64_t) ftime.dwHighDateTime << 32)) /
10000000.0) -
11644473600;
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_AZURERTOS
return mg_millis() / 1000.0;
#else
struct timeval tv;
@ -4473,6 +4481,8 @@ void mg_usleep(unsigned long usecs) {
ets_delay_us(usecs);
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP
vTaskDelay(pdMS_TO_TICKS(usecs / 1000));
#elif MG_ARCH == MG_ARCH_AZURERTOS
tx_thread_sleep((usecs / 1000) * TX_TIMER_TICKS_PER_SECOND);
#else
usleep((useconds_t) usecs);
#endif
@ -4487,6 +4497,8 @@ unsigned long mg_millis(void) {
return xTaskGetTickCount() * portTICK_PERIOD_MS;
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP
return xTaskGetTickCount() * portTICK_PERIOD_MS;
#elif MG_ARCH == MG_ARCH_AZURERTOS
return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND);
#else
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);

View File

@ -88,6 +88,10 @@ extern "C" {
#define MG_PATH_MAX PATH_MAX
#endif
#ifndef MG_SOCK_LISTEN_BACKLOG_SIZE
#define MG_SOCK_LISTEN_BACKLOG_SIZE 128
#endif
#define MG_ARCH_CUSTOM 0
#define MG_ARCH_UNIX 1
@ -96,6 +100,7 @@ extern "C" {
#define MG_ARCH_ESP8266 4
#define MG_ARCH_FREERTOS_TCP 5
#define MG_ARCH_FREERTOS_LWIP 6
#define MG_ARCH_AZURERTOS 7
#if !defined(MG_ARCH)
#if defined(__unix__) || defined(__APPLE__)
@ -108,6 +113,8 @@ extern "C" {
#define MG_ARCH MG_ARCH_ESP32
#elif defined(FREERTOS_IP_H)
#define MG_ARCH MG_ARCH_FREERTOS_TCP
#elif defined(AZURE_RTOS_THREADX)
#define MG_ARCH MG_ARCH_AZURERTOS
#endif
#if !defined(MG_ARCH)
@ -135,6 +142,44 @@ extern "C" {
#if MG_ARCH == MG_ARCH_AZURERTOS
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <stdio.h>
#include <tx_api.h>
#include <fx_api.h>
#include <tx_port.h>
#include <nx_port.h>
#include <nx_api.h>
#include <nx_bsd.h>
#ifdef __REDLIB__
#define va_copy(d,s)__builtin_va_copy(d,s)
#endif
#define PATH_MAX FX_MAXIMUM_PATH
#define MG_DIRSEP '\\'
#define socklen_t int
#define closesocket(x) soc_close(x)
#define gmtime_r(a, b) gmtime(a)
#define MG_INT64_FMT "%lld"
static __inline struct tm *localtime_r(time_t *t, struct tm *tm) {
(void) tm;
return localtime(t);
}
#undef FOPEN_MAX
#endif
#if MG_ARCH == MG_ARCH_ESP32
#include <ctype.h>

View File

@ -7,6 +7,7 @@
#define MG_ARCH_ESP8266 4
#define MG_ARCH_FREERTOS_TCP 5
#define MG_ARCH_FREERTOS_LWIP 6
#define MG_ARCH_AZURERTOS 7
#if !defined(MG_ARCH)
#if defined(__unix__) || defined(__APPLE__)
@ -19,6 +20,8 @@
#define MG_ARCH MG_ARCH_ESP32
#elif defined(FREERTOS_IP_H)
#define MG_ARCH MG_ARCH_FREERTOS_TCP
#elif defined(AZURE_RTOS_THREADX)
#define MG_ARCH MG_ARCH_AZURERTOS
#endif
#if !defined(MG_ARCH)

38
src/arch_azurertos.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
#if MG_ARCH == MG_ARCH_AZURERTOS
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <stdio.h>
#include <tx_api.h>
#include <fx_api.h>
#include <tx_port.h>
#include <nx_port.h>
#include <nx_api.h>
#include <nx_bsd.h>
#ifdef __REDLIB__
#define va_copy(d,s)__builtin_va_copy(d,s)
#endif
#define PATH_MAX FX_MAXIMUM_PATH
#define MG_DIRSEP '\\'
#define socklen_t int
#define closesocket(x) soc_close(x)
#define gmtime_r(a, b) gmtime(a)
#define MG_INT64_FMT "%lld"
static __inline struct tm *localtime_r(time_t *t, struct tm *tm) {
(void) tm;
return localtime(t);
}
#undef FOPEN_MAX
#endif

View File

@ -62,3 +62,7 @@
#ifndef MG_PATH_MAX
#define MG_PATH_MAX PATH_MAX
#endif
#ifndef MG_SOCK_LISTEN_BACKLOG_SIZE
#define MG_SOCK_LISTEN_BACKLOG_SIZE 128
#endif

View File

@ -120,6 +120,8 @@ static void mg_set_non_blocking_mode(SOCKET fd) {
setsockopt(fd, 0, FREERTOS_SO_SNDTIMEO, &off, sizeof(off));
#elif MG_ARCH == MG_ARCH_FREERTOS_LWIP
lwip_fcntl(fd, F_SETFL, O_NONBLOCK);
#elif MG_ARCH == MG_ARCH_AZURERTOS
fcntl(fd, F_SETFL, O_NONBLOCK);
#else
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK, FD_CLOEXEC);
#endif
@ -165,7 +167,7 @@ SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
#endif
bind(fd, &usa.sa, slen) == 0 &&
// NOTE(lsm): FreeRTOS uses backlog value as a connection limit
(type == SOCK_DGRAM || listen(fd, 128) == 0)) {
(type == SOCK_DGRAM || listen(fd, MG_SOCK_LISTEN_BACKLOG_SIZE) == 0)) {
// In case port was set to 0, get the real port number
if (getsockname(fd, &usa.sa, &slen) == 0) {
addr->port = usa.sin.sin_port;
@ -290,7 +292,7 @@ static void close_conn(struct mg_connection *c) {
}
static void setsockopts(struct mg_connection *c) {
#if MG_ARCH == MG_ARCH_FREERTOS_TCP
#if MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_AZURERTOS
(void) c;
#else
int on = 1;
@ -364,6 +366,12 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
socklen_t sa_len = sizeof(usa);
SOCKET fd = accept(FD(lsn), &usa.sa, &sa_len);
if (fd == INVALID_SOCKET) {
#if MG_ARCH == MG_ARCH_AZURERTOS
// AzureRTOS, in non-block socket mode can mark listening socket readable
// even it is not. See comment for 'select' func implementation in nx_bsd.c
// That's not an error, just should try later
if (MG_SOCK_ERRNO != EAGAIN)
#endif
LOG(LL_ERROR, ("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO));
#if (!defined(_WIN32) && (MG_ARCH != MG_ARCH_FREERTOS_TCP))
} else if ((long) fd >= FD_SETSIZE) {

View File

@ -314,7 +314,7 @@ double mg_time(void) {
((int64_t) ftime.dwHighDateTime << 32)) /
10000000.0) -
11644473600;
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_AZURERTOS
return mg_millis() / 1000.0;
#else
struct timeval tv;
@ -330,6 +330,8 @@ void mg_usleep(unsigned long usecs) {
ets_delay_us(usecs);
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP
vTaskDelay(pdMS_TO_TICKS(usecs / 1000));
#elif MG_ARCH == MG_ARCH_AZURERTOS
tx_thread_sleep((usecs / 1000) * TX_TIMER_TICKS_PER_SECOND);
#else
usleep((useconds_t) usecs);
#endif
@ -344,6 +346,8 @@ unsigned long mg_millis(void) {
return xTaskGetTickCount() * portTICK_PERIOD_MS;
#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP
return xTaskGetTickCount() * portTICK_PERIOD_MS;
#elif MG_ARCH == MG_ARCH_AZURERTOS
return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND);
#else
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);