summaryrefslogtreecommitdiff
path: root/package/busybox/patches/350-httpd_redir.patch
blob: bbb8eb4aab33896789adde54b8857455ed9f1f61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
--- a/include/usage.h
+++ b/include/usage.h
@@ -1727,6 +1727,7 @@
 	IF_FEATURE_HTTPD_BASIC_AUTH(" [-r REALM]") \
        " [-h HOME]\n" \
        "or httpd -d/-e" IF_FEATURE_HTTPD_AUTH_MD5("/-m") " STRING"
+       " [-R <path> [-H <host>]]"
 #define httpd_full_usage "\n\n" \
        "Listen for incoming HTTP requests\n" \
      "\nOptions:" \
@@ -1744,6 +1745,8 @@
      "\n	-m STRING	MD5 crypt STRING") \
      "\n	-e STRING	HTML encode STRING" \
      "\n	-d STRING	URL decode STRING" \
+     "\n	-R PATH		Redirect target path" \
+     "\n	-H HOST		Redirect target host" \
 
 #define hwclock_trivial_usage \
 	IF_FEATURE_HWCLOCK_LONG_OPTIONS( \
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -250,6 +250,8 @@ struct globals {
 
 	const char *found_mime_type;
 	const char *found_moved_temporarily;
+	const char *redirect_path;
+	const char *redirect_host;
 	Htaccess_IP *ip_a_d;    /* config allow/deny lines */
 
 	IF_FEATURE_HTTPD_BASIC_AUTH(const char *g_realm;)
@@ -296,6 +298,8 @@ struct globals {
 #define index_page        (G.index_page       )
 #define found_mime_type   (G.found_mime_type  )
 #define found_moved_temporarily (G.found_moved_temporarily)
+#define redirect_path     (G.redirect_path    )
+#define redirect_host     (G.redirect_host    )
 #define last_mod          (G.last_mod         )
 #define ip_a_d            (G.ip_a_d           )
 #define g_realm           (G.g_realm          )
@@ -997,8 +1001,11 @@ static void send_headers(int responseNum
 	}
 #endif
 	if (responseNum == HTTP_MOVED_TEMPORARILY) {
-		len += sprintf(iobuf + len, "Location: %s/%s%s\r\n",
+		len += sprintf(iobuf + len, "Location: %s%s%s%s%s%s\r\n",
+				(redirect_host ? "http://" : ""),
+				(redirect_host ? redirect_host : ""),
 				found_moved_temporarily,
+				(redirect_host ? "" : "/"),
 				(g_query ? "?" : ""),
 				(g_query ? g_query : ""));
 	}
@@ -1941,8 +1948,12 @@ static void handle_incoming_and_exit(con
 	} while (*++tptr);
 	*++urlp = '\0';       /* terminate after last character */
 
+	/* redirect active */
+	if (redirect_path && (strncmp(urlcopy, redirect_path, strlen(redirect_path)) != 0))
+		found_moved_temporarily = redirect_path;
+
 	/* If URL is a directory, add '/' */
-	if (urlp[-1] != '/') {
+	if (!redirect_path && (urlp[-1] != '/')) {
 		if (is_directory(urlcopy + 1, 1, NULL)) {
 			found_moved_temporarily = urlcopy;
 		}
@@ -2283,7 +2294,9 @@ static void sighup_handler(int sig UNUSE
 }
 
 enum {
-	c_opt_config_file = 0,
+	R_opt_redirect_path = 0,
+	H_opt_redirect_host,
+	c_opt_config_file,
 	d_opt_decode_url,
 	h_opt_home_httpd,
 	IF_FEATURE_HTTPD_ENCODE_URL_STR(e_opt_encode_url,)
@@ -2332,12 +2345,13 @@ int httpd_main(int argc UNUSED_PARAM, ch
 	/* We do not "absolutize" path given by -h (home) opt.
 	 * If user gives relative path in -h,
 	 * $SCRIPT_FILENAME will not be set. */
-	opt = getopt32(argv, "c:d:h:"
+	opt = getopt32(argv, "R:H:c:d:h:"
 			IF_FEATURE_HTTPD_ENCODE_URL_STR("e:")
 			IF_FEATURE_HTTPD_BASIC_AUTH("r:")
 			IF_FEATURE_HTTPD_AUTH_MD5("m:")
 			IF_FEATURE_HTTPD_SETUID("u:")
 			"p:ifv",
+			&redirect_path, &redirect_host,
 			&opt_c_configFile, &url_for_decode, &home_httpd
 			IF_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode)
 			IF_FEATURE_HTTPD_BASIC_AUTH(, &g_realm)