MCPcopy Create free account
hub / github.com/cesanta/mongoose / forward_request

Function forward_request

tutorials/http/http-reverse-proxy/main.c:21–37  ·  view source on GitHub ↗

Forward client request to the backend connection, rewriting the Host header

Source from the content-addressed store, hash-verified

19
20// Forward client request to the backend connection, rewriting the Host header
21static void forward_request(struct mg_http_message *hm,
22 struct mg_connection *c) {
23 size_t i, max = sizeof(hm->headers) / sizeof(hm->headers[0]);
24 struct mg_str host = mg_url_host(s_backend_url);
25 mg_printf(c, "%.*s\r\n",
26 (int) (hm->proto.buf + hm->proto.len - hm->message.buf),
27 hm->message.buf);
28 for (i = 0; i < max && hm->headers[i].name.len > 0; i++) {
29 struct mg_str *k = &hm->headers[i].name, *v = &hm->headers[i].value;
30 if (mg_strcmp(*k, mg_str("Host")) == 0) v = &host;
31 mg_printf(c, "%.*s: %.*s\r\n", (int) k->len, k->buf, (int) v->len, v->buf);
32 }
33 mg_send(c, "\r\n", 2);
34 mg_send(c, hm->body.buf, hm->body.len);
35 MG_DEBUG(("FORWARDING: %.*s %.*s", (int) hm->method.len, hm->method.buf,
36 (int) hm->uri.len, hm->uri.buf));
37}
38
39static void fn2(struct mg_connection *c, int ev, void *ev_data) {
40 struct mg_connection *c2 = (struct mg_connection *) c->fn_data;

Callers 1

fnFunction · 0.85

Calls 5

mg_strClass · 0.70
mg_url_hostFunction · 0.50
mg_printfFunction · 0.50
mg_strcmpFunction · 0.50
mg_sendFunction · 0.50

Tested by

no test coverage detected