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.
|
* we'll be closing anyway.
|
||||||
*/
|
*/
|
||||||
char *reverse_url;
|
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_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);
|
safefree (url);
|
||||||
url = reverse_url;
|
url = reverse_url;
|
||||||
} else if (config->reverseonly) {
|
} 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)
|
struct reversepath *reversepath_get (char *url, struct reversepath *reverse)
|
||||||
{
|
{
|
||||||
|
size_t l, lu, lp;
|
||||||
while (reverse) {
|
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;
|
return reverse;
|
||||||
|
|
||||||
reverse = reverse->next;
|
reverse = reverse->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,23 +128,30 @@ void free_reversepath_list (struct reversepath *reverse)
|
|||||||
* Rewrite the URL for reverse proxying.
|
* Rewrite the URL for reverse proxying.
|
||||||
*/
|
*/
|
||||||
char *reverse_rewrite_url (struct conn_s *connptr, orderedmap hashofheaders,
|
char *reverse_rewrite_url (struct conn_s *connptr, orderedmap hashofheaders,
|
||||||
char *url)
|
char *url, int *status)
|
||||||
{
|
{
|
||||||
char *rewrite_url = NULL;
|
char *rewrite_url = NULL;
|
||||||
char *cookie = NULL;
|
char *cookie = NULL;
|
||||||
char *cookieval;
|
char *cookieval;
|
||||||
struct reversepath *reverse = NULL;
|
struct reversepath *reverse = NULL;
|
||||||
|
|
||||||
|
*status = 0;
|
||||||
|
|
||||||
/* Reverse requests always start with a slash */
|
/* Reverse requests always start with a slash */
|
||||||
if (*url == '/') {
|
if (*url == '/') {
|
||||||
/* First try locating the reverse mapping by request url */
|
/* First try locating the reverse mapping by request url */
|
||||||
reverse = reversepath_get (url, config->reversepath_list);
|
reverse = reversepath_get (url, config->reversepath_list);
|
||||||
if (reverse) {
|
if (reverse) {
|
||||||
rewrite_url = (char *)
|
size_t lu = strlen (url);
|
||||||
safemalloc (strlen (url) + strlen (reverse->url) +
|
size_t lrp = strlen (reverse->path);
|
||||||
1);
|
if (lrp > lu) {
|
||||||
strcpy (rewrite_url, reverse->url);
|
rewrite_url = safestrdup (reverse->path);
|
||||||
strcat (rewrite_url, url + strlen (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
|
} else if (config->reversemagic
|
||||||
&& (cookie = orderedmap_find (hashofheaders,
|
&& (cookie = orderedmap_find (hashofheaders,
|
||||||
"cookie"))) {
|
"cookie"))) {
|
||||||
|
@ -38,6 +38,7 @@ extern struct reversepath *reversepath_get (char *url,
|
|||||||
struct reversepath *reverse);
|
struct reversepath *reverse);
|
||||||
void free_reversepath_list (struct reversepath *reverse);
|
void free_reversepath_list (struct reversepath *reverse);
|
||||||
extern char *reverse_rewrite_url (struct conn_s *connptr,
|
extern char *reverse_rewrite_url (struct conn_s *connptr,
|
||||||
orderedmap hashofheaders, char *url);
|
orderedmap hashofheaders, char *url,
|
||||||
|
int *status);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user