Skip to content

Commit 205d4c6

Browse files
committed
provide local info, fix redirect example
1 parent 18cea94 commit 205d4c6

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

mongoose.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10769,7 +10769,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
1076910769
setsockopts(c);
1077010770
c->is_accepted = 1;
1077110771
c->is_hexdumping = lsn->is_hexdumping;
10772-
c->loc = lsn->loc;
10772+
setlocaddr(fd, &c->loc); // set local addr to where the client connected to
1077310773
c->pfn = lsn->pfn;
1077410774
c->pfn_data = lsn->pfn_data;
1077510775
c->fn = lsn->fn;

src/sock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
475475
setsockopts(c);
476476
c->is_accepted = 1;
477477
c->is_hexdumping = lsn->is_hexdumping;
478-
c->loc = lsn->loc;
478+
setlocaddr(fd, &c->loc); // set local addr to where the client connected to
479479
c->pfn = lsn->pfn;
480480
c->pfn_data = lsn->pfn_data;
481481
c->fn = lsn->fn;

tutorials/http/redirect-to-https/main.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
5252
if (ev == MG_EV_HTTP_MSG) {
5353
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
5454
char buf[256];
55-
mg_snprintf(buf, sizeof(buf), "Location: %s%.*s\r\n", s_https_addr,
56-
hm->uri.len, hm->uri.buf);
55+
mg_snprintf(buf, sizeof(buf), "Location: https://%M:%u%.*s\r\n",
56+
mg_print_ip, &c->loc, mg_url_port(s_https_addr), hm->uri.len,
57+
hm->uri.buf);
58+
// If you want to redirect to a name, you must provide a redirection URL:
59+
// - static const char *s_https_redirect_addr = "https://yourname:port"
60+
// - mg_snprintf(buf, sizeof(buf), "Location: %s%.*s\r\n",
61+
// s_https_redirect_addr, hm->uri.len, hm->uri.buf);
5762
mg_http_reply(c, 302, buf, "%s", buf); // 302 redirect
5863
}
5964
}

0 commit comments

Comments
 (0)