From 205d4c6832fe5eb8d9642de24af8a5e327be0cbc Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Mon, 9 Feb 2026 14:42:31 -0300 Subject: [PATCH] provide local info, fix redirect example --- mongoose.c | 2 +- src/sock.c | 2 +- tutorials/http/redirect-to-https/main.c | 9 +++++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/mongoose.c b/mongoose.c index cdd23be4..0e93a582 100644 --- a/mongoose.c +++ b/mongoose.c @@ -10769,7 +10769,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { setsockopts(c); c->is_accepted = 1; c->is_hexdumping = lsn->is_hexdumping; - c->loc = lsn->loc; + setlocaddr(fd, &c->loc); // set local addr to where the client connected to c->pfn = lsn->pfn; c->pfn_data = lsn->pfn_data; c->fn = lsn->fn; diff --git a/src/sock.c b/src/sock.c index 6b2bd5f3..753f8345 100644 --- a/src/sock.c +++ b/src/sock.c @@ -475,7 +475,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { setsockopts(c); c->is_accepted = 1; c->is_hexdumping = lsn->is_hexdumping; - c->loc = lsn->loc; + setlocaddr(fd, &c->loc); // set local addr to where the client connected to c->pfn = lsn->pfn; c->pfn_data = lsn->pfn_data; c->fn = lsn->fn; diff --git a/tutorials/http/redirect-to-https/main.c b/tutorials/http/redirect-to-https/main.c index 8db3d211..be12d356 100644 --- a/tutorials/http/redirect-to-https/main.c +++ b/tutorials/http/redirect-to-https/main.c @@ -52,8 +52,13 @@ static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; char buf[256]; - mg_snprintf(buf, sizeof(buf), "Location: %s%.*s\r\n", s_https_addr, - hm->uri.len, hm->uri.buf); + mg_snprintf(buf, sizeof(buf), "Location: https://%M:%u%.*s\r\n", + mg_print_ip, &c->loc, mg_url_port(s_https_addr), hm->uri.len, + hm->uri.buf); + // If you want to redirect to a name, you must provide a redirection URL: + // - static const char *s_https_redirect_addr = "https://yourname:port" + // - mg_snprintf(buf, sizeof(buf), "Location: %s%.*s\r\n", + // s_https_redirect_addr, hm->uri.len, hm->uri.buf); mg_http_reply(c, 302, buf, "%s", buf); // 302 redirect } }