reverse: redirect if path without trailing slash is detected
if for example: ReversePath = "/foo/" and user requests "http://tinyproxy/foo" the common behaviour for HTTP servers is to send a http 301 redirect to the correct url. we now do the same.
This commit is contained in:
parent
bc87de3482
commit
2529597ea0
@ -383,10 +383,17 @@ BAD_REQUEST_ERROR:
|
||||
* we'll be closing anyway.
|
||||
*/
|
||||
char *reverse_url;
|
||||
int reverse_status;
|
||||
|
||||
reverse_url = reverse_rewrite_url (connptr, hashofheaders, url);
|
||||
reverse_url = reverse_rewrite_url (connptr, hashofheaders, url, &reverse_status);
|
||||
|
||||
if (reverse_url != NULL) {
|
||||
if (reverse_status == 301) {
|
||||
char buf[PATH_MAX];
|
||||
snprintf (buf, sizeof buf, "Location: %s\r\n", reverse_url);
|
||||
send_http_headers (connptr, 301, "Moved Permanently", buf);
|
||||
goto fail;
|
||||
}
|
||||
safefree (url);
|
||||
url = reverse_url;
|
||||
} else if (config->reverseonly) {
|
||||
|
@ -93,10 +93,16 @@ void reversepath_add (const char *path, const char *url,
|
||||
*/
|
||||
struct reversepath *reversepath_get (char *url, struct reversepath *reverse)
|
||||
{
|
||||
size_t l, lu, lp;
|
||||
while (reverse) {
|
||||
if (strstr (url, reverse->path) == url)
|
||||
lu = strlen (url);
|
||||
lp = strlen (reverse->path);
|
||||
if ((
|
||||
(l = lu) == lp-1 ||
|
||||
(l = lp) <= lu
|
||||
) &&
|
||||
!memcmp(url, reverse->path, l))
|
||||
return reverse;
|
||||
|
||||
reverse = reverse->next;
|
||||
}
|
||||
|
||||
@ -122,23 +128,30 @@ void free_reversepath_list (struct reversepath *reverse)
|
||||
* Rewrite the URL for reverse proxying.
|
||||
*/
|
||||
char *reverse_rewrite_url (struct conn_s *connptr, orderedmap hashofheaders,
|
||||
char *url)
|
||||
char *url, int *status)
|
||||
{
|
||||
char *rewrite_url = NULL;
|
||||
char *cookie = NULL;
|
||||
char *cookieval;
|
||||
struct reversepath *reverse = NULL;
|
||||
|
||||
*status = 0;
|
||||
|
||||
/* Reverse requests always start with a slash */
|
||||
if (*url == '/') {
|
||||
/* First try locating the reverse mapping by request url */
|
||||
reverse = reversepath_get (url, config->reversepath_list);
|
||||
if (reverse) {
|
||||
rewrite_url = (char *)
|
||||
safemalloc (strlen (url) + strlen (reverse->url) +
|
||||
1);
|
||||
strcpy (rewrite_url, reverse->url);
|
||||
strcat (rewrite_url, url + strlen (reverse->path));
|
||||
size_t lu = strlen (url);
|
||||
size_t lrp = strlen (reverse->path);
|
||||
if (lrp > lu) {
|
||||
rewrite_url = safestrdup (reverse->path);
|
||||
*status = 301;
|
||||
} else {
|
||||
rewrite_url = safemalloc (
|
||||
strlen (reverse->url) + lu + 1);
|
||||
sprintf (rewrite_url, "%s%s", reverse->url, url + lrp);
|
||||
}
|
||||
} else if (config->reversemagic
|
||||
&& (cookie = orderedmap_find (hashofheaders,
|
||||
"cookie"))) {
|
||||
|
@ -38,6 +38,7 @@ extern struct reversepath *reversepath_get (char *url,
|
||||
struct reversepath *reverse);
|
||||
void free_reversepath_list (struct reversepath *reverse);
|
||||
extern char *reverse_rewrite_url (struct conn_s *connptr,
|
||||
orderedmap hashofheaders, char *url);
|
||||
orderedmap hashofheaders, char *url,
|
||||
int *status);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user