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