From 35f02fdaad30139ca34ff61f1a5b50d2aecf29ef Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Wed, 27 Oct 2021 19:18:19 +0100 Subject: [PATCH] Use small IO buffer --- examples/http-reverse-proxy/Makefile | 2 +- examples/http-reverse-proxy/main.c | 41 +++++++++++----------------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/examples/http-reverse-proxy/Makefile b/examples/http-reverse-proxy/Makefile index 2c97607e..6ec568ba 100644 --- a/examples/http-reverse-proxy/Makefile +++ b/examples/http-reverse-proxy/Makefile @@ -1,5 +1,5 @@ PROG ?= example -CFLAGS ?= -DMG_IO_SIZE=8192 +CFLAGS ?= -DMG_IO_SIZE=64 MBEDTLS_DIR ?= ifeq "$(MBEDTLS_DIR)" "" diff --git a/examples/http-reverse-proxy/main.c b/examples/http-reverse-proxy/main.c index c6d7d320..3023edd1 100644 --- a/examples/http-reverse-proxy/main.c +++ b/examples/http-reverse-proxy/main.c @@ -32,21 +32,13 @@ static void forward_request(struct mg_http_message *hm, (int) hm->uri.len, hm->uri.ptr)); } -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_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); - } - } 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); - } +static void fn2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_READ && fn_data != NULL) { + // All incoming data from the backend, forward to the client + mg_send((struct mg_connection *) fn_data, c->recv.buf, c->recv.len); + mg_iobuf_del(&c->recv, 0, c->recv.len); } - (void)ev_data; + (void) ev_data; } static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { @@ -55,18 +47,17 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { 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); + c2 = mg_connect(c->mgr, s_backend_url, fn2, c); + if (c2 == NULL) { + mg_error(c, "Cannot create backend connection"); } 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); + if (mg_url_is_ssl(s_backend_url)) { + struct mg_tls_opts opts = {.ca = "ca.pem"}; + mg_tls_init(c2, &opts); + } + c->fn_data = c2; + forward_request(hm, c2); + // c2->is_hexdumping = 1; } } else if (ev == MG_EV_CLOSE) { if (c2 != NULL) c2->is_closing = 1;