From a4c668e37ae2f10e869254940278b65453c4750c Mon Sep 17 00:00:00 2001 From: Marko Mikulicic Date: Thu, 27 Oct 2016 13:01:47 +0100 Subject: [PATCH] Expose reverse proxy API for dynamic mounts PUBLISHED_FROM=00772400bce7c15368d91741092ebc8ab0842e19 --- docs/c-api/http_server.h/intro.md | 1 + .../http_server.h/mg_http_reverse_proxy.md | 19 +++++++++++++++++++ mongoose.c | 13 ++++++++++--- mongoose.h | 16 ++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 docs/c-api/http_server.h/mg_http_reverse_proxy.md diff --git a/docs/c-api/http_server.h/intro.md b/docs/c-api/http_server.h/intro.md index 6e1c49f4..80665232 100644 --- a/docs/c-api/http_server.h/intro.md +++ b/docs/c-api/http_server.h/intro.md @@ -8,6 +8,7 @@ items: - { name: mg_get_http_var.md } - { name: mg_http_check_digest_auth.md } - { name: mg_http_parse_header.md } + - { name: mg_http_reverse_proxy.md } - { name: mg_http_send_error.md } - { name: mg_http_send_redirect.md } - { name: mg_http_serve_file.md } diff --git a/docs/c-api/http_server.h/mg_http_reverse_proxy.md b/docs/c-api/http_server.h/mg_http_reverse_proxy.md new file mode 100644 index 00000000..c0c83835 --- /dev/null +++ b/docs/c-api/http_server.h/mg_http_reverse_proxy.md @@ -0,0 +1,19 @@ +--- +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. + diff --git a/mongoose.c b/mongoose.c index d0e45954..60ec1014 100644 --- a/mongoose.c +++ b/mongoose.c @@ -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, - struct mg_str mount, struct mg_str upstream) { +void mg_http_reverse_proxy(struct mg_connection *nc, + const struct http_message *hm, struct mg_str mount, + struct mg_str upstream) { struct mg_connection *be; char burl[256], *purl = burl; char *addr = NULL; @@ -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); 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); } @@ -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) { if (mg_strncmp(a, hm->uri, a.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; } } diff --git a/mongoose.h b/mongoose.h index e4cfb936..336c2eba 100644 --- a/mongoose.h +++ b/mongoose.h @@ -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, ...); +#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 } #endif /* __cplusplus */