Merge pull request #3436 from cesanta/redirect

provide local info, fix redirect example
This commit is contained in:
Sergio R. Caprile authored and GitHub committed 2026-02-09 14:51:03 -03:00
commit cc617d537d
3 files changed
+9 -4

No files matched your search

+1 -1
View File
@@ -10769,7 +10769,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
setsockopts(c); setsockopts(c);
c->is_accepted = 1; c->is_accepted = 1;
c->is_hexdumping = lsn->is_hexdumping; 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 = lsn->pfn;
c->pfn_data = lsn->pfn_data; c->pfn_data = lsn->pfn_data;
c->fn = lsn->fn; c->fn = lsn->fn;
+1 -1
View File
@@ -475,7 +475,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
setsockopts(c); setsockopts(c);
c->is_accepted = 1; c->is_accepted = 1;
c->is_hexdumping = lsn->is_hexdumping; 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 = lsn->pfn;
c->pfn_data = lsn->pfn_data; c->pfn_data = lsn->pfn_data;
c->fn = lsn->fn; c->fn = lsn->fn;
+7 -2
View File
@@ -52,8 +52,13 @@ static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) { if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data; struct mg_http_message *hm = (struct mg_http_message *) ev_data;
char buf[256]; char buf[256];
mg_snprintf(buf, sizeof(buf), "Location: %s%.*s\r\n", s_https_addr, mg_snprintf(buf, sizeof(buf), "Location: https://%M:%u%.*s\r\n",
hm->uri.len, hm->uri.buf); 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 mg_http_reply(c, 302, buf, "%s", buf); // 302 redirect
} }
} }