Commit a4c668e3 authored by Marko Mikulicic's avatar Marko Mikulicic Committed by Cesanta Bot

Expose reverse proxy API for dynamic mounts

PUBLISHED_FROM=00772400bce7c15368d91741092ebc8ab0842e19
parent 900bbe72
...@@ -8,6 +8,7 @@ items: ...@@ -8,6 +8,7 @@ items:
- { name: mg_get_http_var.md } - { name: mg_get_http_var.md }
- { name: mg_http_check_digest_auth.md } - { name: mg_http_check_digest_auth.md }
- { name: mg_http_parse_header.md } - { name: mg_http_parse_header.md }
- { name: mg_http_reverse_proxy.md }
- { name: mg_http_send_error.md } - { name: mg_http_send_error.md }
- { name: mg_http_send_redirect.md } - { name: mg_http_send_redirect.md }
- { name: mg_http_serve_file.md } - { name: mg_http_serve_file.md }
......
---
title: "mg_http_reverse_proxy()"
decl_name: "mg_http_reverse_proxy"
symbol_kind: "func"
signature: |
void mg_http_reverse_proxy(struct mg_connection *nc,
const struct http_message *hm, struct mg_str mount,
struct mg_str upstream);
---
Proxies a given request to a given upstream http server. The path prefix
in `mount` will be stripped of the path requested to the upstream server,
e.g. if mount is /api and upstream is http://localhost:8001/foo
then an incoming request to /api/bar will cause a request to
http://localhost:8001/foo/bar
EXPERIMENTAL API. Please use http_serve_http + url_rewrites if a static
mapping is good enough.
...@@ -5845,8 +5845,9 @@ static void mg_reverse_proxy_handler(struct mg_connection *nc, int ev, ...@@ -5845,8 +5845,9 @@ static void mg_reverse_proxy_handler(struct mg_connection *nc, int ev,
} }
} }
void mg_handle_reverse_proxy(struct mg_connection *nc, struct http_message *hm, void mg_http_reverse_proxy(struct mg_connection *nc,
struct mg_str mount, struct mg_str upstream) { const struct http_message *hm, struct mg_str mount,
struct mg_str upstream) {
struct mg_connection *be; struct mg_connection *be;
char burl[256], *purl = burl; char burl[256], *purl = burl;
char *addr = NULL; char *addr = NULL;
...@@ -5889,6 +5890,12 @@ void mg_handle_reverse_proxy(struct mg_connection *nc, struct http_message *hm, ...@@ -5889,6 +5890,12 @@ void mg_handle_reverse_proxy(struct mg_connection *nc, struct http_message *hm,
mg_printf(be, "Content-Length: %" SIZE_T_FMT "\r\n", hm->body.len); mg_printf(be, "Content-Length: %" SIZE_T_FMT "\r\n", hm->body.len);
continue; continue;
} }
/* We don't support proxying Expect: 100-continue. */
if (mg_vcasecmp(&hn, "Expect") == 0 &&
mg_vcasecmp(&hv, "100-continue") == 0) {
continue;
}
mg_printf(be, "%.*s: %.*s\r\n", (int) hn.len, hn.p, (int) hv.len, hv.p); mg_printf(be, "%.*s: %.*s\r\n", (int) hn.len, hn.p, (int) hv.len, hv.p);
} }
...@@ -5909,7 +5916,7 @@ static int mg_http_handle_forwarding(struct mg_connection *nc, ...@@ -5909,7 +5916,7 @@ static int mg_http_handle_forwarding(struct mg_connection *nc,
while ((rewrites = mg_next_comma_list_entry(rewrites, &a, &b)) != NULL) { while ((rewrites = mg_next_comma_list_entry(rewrites, &a, &b)) != NULL) {
if (mg_strncmp(a, hm->uri, a.len) == 0) { if (mg_strncmp(a, hm->uri, a.len) == 0) {
if (mg_strncmp(b, p1, p1.len) == 0 || mg_strncmp(b, p2, p2.len) == 0) { if (mg_strncmp(b, p1, p1.len) == 0 || mg_strncmp(b, p2, p2.len) == 0) {
mg_handle_reverse_proxy(nc, hm, a, b); mg_http_reverse_proxy(nc, hm, a, b);
return 1; return 1;
} }
} }
......
...@@ -3502,6 +3502,22 @@ void mg_send_head(struct mg_connection *n, int status_code, ...@@ -3502,6 +3502,22 @@ void mg_send_head(struct mg_connection *n, int status_code,
*/ */
void mg_printf_html_escape(struct mg_connection *nc, const char *fmt, ...); void mg_printf_html_escape(struct mg_connection *nc, const char *fmt, ...);
#if MG_ENABLE_HTTP_URL_REWRITES
/*
* Proxies a given request to a given upstream http server. The path prefix
* in `mount` will be stripped of the path requested to the upstream server,
* e.g. if mount is /api and upstream is http://localhost:8001/foo
* then an incoming request to /api/bar will cause a request to
* http://localhost:8001/foo/bar
*
* EXPERIMENTAL API. Please use http_serve_http + url_rewrites if a static
* mapping is good enough.
*/
void mg_http_reverse_proxy(struct mg_connection *nc,
const struct http_message *hm, struct mg_str mount,
struct mg_str upstream);
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment