mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-19 19:40:56 +00:00
Implement port based redirect
PUBLISHED_FROM=798a8bc38cb385027d3caae9b954002c1a10d39c
This commit is contained in:
1 parent
d8f8c1cb99
commit
7c4cda622f
3 files changed
+46
-1
No files matched your search
@@ -74,7 +74,7 @@ int main(int argc, char *argv[]) {
|
|||||||
#endif
|
#endif
|
||||||
} else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc) {
|
} else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc) {
|
||||||
s_http_server_opts.global_auth_file = argv[++i];
|
s_http_server_opts.global_auth_file = argv[++i];
|
||||||
} else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
|
} else if (strcmp(argv[i], "-A") == 0 && i + 1 < argc) {
|
||||||
s_http_server_opts.per_directory_auth_file = argv[++i];
|
s_http_server_opts.per_directory_auth_file = argv[++i];
|
||||||
} else if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) {
|
} else if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) {
|
||||||
s_http_server_opts.url_rewrites = argv[++i];
|
s_http_server_opts.url_rewrites = argv[++i];
|
||||||
|
|||||||
+38
@@ -4488,6 +4488,12 @@ void mg_send_response_line(struct mg_connection *nc, int status_code,
|
|||||||
case 206:
|
case 206:
|
||||||
status_message = "Partial Content";
|
status_message = "Partial Content";
|
||||||
break;
|
break;
|
||||||
|
case 301:
|
||||||
|
status_message = "Moved";
|
||||||
|
break;
|
||||||
|
case 302:
|
||||||
|
status_message = "Found";
|
||||||
|
break;
|
||||||
case 416:
|
case 416:
|
||||||
status_message = "Requested range not satisfiable";
|
status_message = "Requested range not satisfiable";
|
||||||
break;
|
break;
|
||||||
@@ -5502,6 +5508,34 @@ MG_INTERNAL int find_index_file(char *path, size_t path_len, const char *list,
|
|||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int send_port_based_redirect(struct mg_connection *c,
|
||||||
|
struct http_message *hm,
|
||||||
|
const struct mg_serve_http_opts *opts) {
|
||||||
|
const char *rewrites = opts->url_rewrites;
|
||||||
|
struct mg_str a, b;
|
||||||
|
char local_port[20] = {'%'};
|
||||||
|
|
||||||
|
#ifndef MG_ESP8266
|
||||||
|
mg_sock_to_str(c->sock, local_port + 1, sizeof(local_port) - 1,
|
||||||
|
MG_SOCK_STRINGIFY_PORT);
|
||||||
|
#else
|
||||||
|
/* TODO(lsm): remove when mg_sock_to_str() is implemented in LWIP codepath */
|
||||||
|
snprintf(local_port, sizeof(local_port), "%s", "%0");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
while ((rewrites = mg_next_comma_list_entry(rewrites, &a, &b)) != NULL) {
|
||||||
|
if (mg_vcmp(&a, local_port) == 0) {
|
||||||
|
mg_send_response_line(c, 301, NULL);
|
||||||
|
mg_printf(c, "Content-Length: 0\r\nLocation: %.*s%.*s\r\n\r\n",
|
||||||
|
(int) b.len, b.p, (int) (hm->proto.p - hm->uri.p - 1),
|
||||||
|
hm->uri.p);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static void uri_to_path(struct http_message *hm, char *buf, size_t buf_len,
|
static void uri_to_path(struct http_message *hm, char *buf, size_t buf_len,
|
||||||
const struct mg_serve_http_opts *opts) {
|
const struct mg_serve_http_opts *opts) {
|
||||||
char uri[MG_MAX_PATH];
|
char uri[MG_MAX_PATH];
|
||||||
@@ -6132,6 +6166,10 @@ void mg_serve_http(struct mg_connection *nc, struct http_message *hm,
|
|||||||
char path[MG_MAX_PATH];
|
char path[MG_MAX_PATH];
|
||||||
struct mg_str *hdr;
|
struct mg_str *hdr;
|
||||||
|
|
||||||
|
if (send_port_based_redirect(nc, hm, &opts)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (opts.document_root == NULL) {
|
if (opts.document_root == NULL) {
|
||||||
opts.document_root = ".";
|
opts.document_root = ".";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1772,6 +1772,13 @@ struct mg_serve_http_opts {
|
|||||||
* If uri_pattern starts with `@` symbol, then Mongoose compares it with the
|
* If uri_pattern starts with `@` symbol, then Mongoose compares it with the
|
||||||
* HOST header of the request. If they are equal, Mongoose sets document root
|
* HOST header of the request. If they are equal, Mongoose sets document root
|
||||||
* to `file_or_directory_path`, implementing virtual hosts support.
|
* to `file_or_directory_path`, implementing virtual hosts support.
|
||||||
|
* Example: `@foo.com=/document/root/for/foo.com`
|
||||||
|
*
|
||||||
|
* If `uri_pattern` starts with `%` symbol, then Mongoose compares it with
|
||||||
|
* the listening port. If they match, then Mongoose issues a 301 redirect.
|
||||||
|
* For example, to redirect all HTTP requests to the
|
||||||
|
* HTTPS port, do `%80=https://my.site.com`. Note that the request URI is
|
||||||
|
* automatically appended to the redirect location.
|
||||||
*/
|
*/
|
||||||
const char *url_rewrites;
|
const char *url_rewrites;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user