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