Merge pull request #1601 from pi-hole/fix/redirect_slash
Slash the slash
This commit is contained in:
commit
e085728e3e
|
@ -210,8 +210,8 @@ components:
|
|||
description: Time until the response was received (ms, negative if N/A)
|
||||
ttl:
|
||||
type: integer
|
||||
description: Remaining Time-To-Live (0 if N/A)
|
||||
regex:
|
||||
description: Query's Time-To-Live (TTL) value (`-1` if N/A)
|
||||
regex_id:
|
||||
type: integer
|
||||
description: ID of blocking regex (`-1` if N/A)
|
||||
upstream:
|
||||
|
@ -231,7 +231,8 @@ components:
|
|||
reply:
|
||||
type: "IP"
|
||||
time: 19
|
||||
regex_idx: -1
|
||||
ttl: -1
|
||||
regex_id: -1
|
||||
upstream: "localhost#5353"
|
||||
dbid: 112421354
|
||||
- time: 1581907871.583821
|
||||
|
@ -246,8 +247,8 @@ components:
|
|||
reply:
|
||||
type: "IP"
|
||||
time: 12.3
|
||||
response_time: 7
|
||||
regex_idx: -1
|
||||
ttl: 7
|
||||
regex_id: -1
|
||||
upstream: "localhost#5353"
|
||||
dbid: 112421355
|
||||
cursor:
|
||||
|
|
|
@ -105,8 +105,8 @@ int request_handler(struct mg_connection *conn, void *cbdata)
|
|||
// Check if the request is for the login page
|
||||
const bool login = (strcmp(req_info->local_uri_raw, login_uri) == 0);
|
||||
|
||||
// Check if the request is for a LUA page (every *.lp has already been
|
||||
// rewritten at this point to *)
|
||||
// Check if the request is for a LUA page (every XYZ.lp has already been
|
||||
// rewritten at this point to XYZ)
|
||||
if(!no_dot)
|
||||
{
|
||||
// Not a LUA page - fall back to CivetWeb's default handler
|
||||
|
|
|
@ -108,9 +108,48 @@ static int redirect_lp_handler(struct mg_connection *conn, void *input)
|
|||
char *new_uri = calloc(strlen(uri) + query_len, sizeof(char));
|
||||
// Copy everything from before the ".lp" to the new URI
|
||||
strncpy(new_uri, uri, pos - uri);
|
||||
|
||||
// Append query string to the new URI if present
|
||||
if(query_len > 0)
|
||||
{
|
||||
strcat(new_uri, "?");
|
||||
strcat(new_uri, query_string);
|
||||
}
|
||||
|
||||
// Send a 301 redirect to the new URI
|
||||
log_debug(DEBUG_API, "Redirecting %s?%s ==301==> %s",
|
||||
uri, query_string, new_uri);
|
||||
mg_send_http_redirect(conn, new_uri, 301);
|
||||
free(new_uri);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int redirect_slash_handler(struct mg_connection *conn, void *input)
|
||||
{
|
||||
// Get requested URI
|
||||
const struct mg_request_info *request = mg_get_request_info(conn);
|
||||
const char *uri = request->local_uri_raw;
|
||||
const char *query_string = request->query_string;
|
||||
const size_t query_len = query_string != NULL ? strlen(query_string) : 0;
|
||||
|
||||
// Do not redirect if the new URI is the webhome
|
||||
if(strcmp(uri, config.webserver.paths.webhome.v.s) == 0)
|
||||
{
|
||||
log_debug(DEBUG_API, "Not redirecting %s?%s",
|
||||
uri, query_string);
|
||||
|
||||
// Handle as a normal request
|
||||
return request_handler(conn, input);
|
||||
}
|
||||
|
||||
// Remove the trailing slash from the URI
|
||||
char *new_uri = strdup(uri);
|
||||
new_uri[strlen(new_uri) - 1] = '\0';
|
||||
|
||||
// Append query string to the new URI if present
|
||||
if(query_len > 0)
|
||||
{
|
||||
// Append query string ".lp" to the new URI if present
|
||||
strcat(new_uri, "?");
|
||||
strcat(new_uri, query_string);
|
||||
}
|
||||
|
@ -291,6 +330,9 @@ void http_init(void)
|
|||
// Register **.lp -> ** redirect handler
|
||||
mg_set_request_handler(ctx, "**.lp$", redirect_lp_handler, NULL);
|
||||
|
||||
// Register **/ -> ** redirect handler
|
||||
mg_set_request_handler(ctx, "**/$", redirect_slash_handler, NULL);
|
||||
|
||||
// Register handler for the rest
|
||||
mg_set_request_handler(ctx, "**", request_handler, NULL);
|
||||
|
||||
|
@ -298,45 +340,72 @@ void http_init(void)
|
|||
allocate_lua();
|
||||
}
|
||||
|
||||
static char *append_to_path(char *path, const char *append)
|
||||
{
|
||||
const size_t path_len = strlen(path);
|
||||
const size_t append_len = strlen(append);
|
||||
const size_t total_len = path_len + append_len + 1;
|
||||
char *new_path = calloc(total_len, sizeof(char));
|
||||
if(new_path == NULL)
|
||||
{
|
||||
log_err("Failed to allocate memory for path!");
|
||||
return NULL;
|
||||
}
|
||||
strncpy(new_path, path, total_len);
|
||||
strncat(new_path, append, total_len);
|
||||
return new_path;
|
||||
}
|
||||
|
||||
void FTL_rewrite_pattern(char *filename, size_t filename_buf_len)
|
||||
{
|
||||
// Construct full path with ".lp" appended
|
||||
const size_t filename_lp_len = strlen(filename) + 4;
|
||||
char *filename_lp = calloc(filename_lp_len, sizeof(char));
|
||||
if(filename_lp == NULL)
|
||||
{
|
||||
log_err("Failed to allocate memory for Lua pattern!");
|
||||
return;
|
||||
}
|
||||
strncpy(filename_lp, filename, filename_lp_len);
|
||||
strncat(filename_lp, ".lp", filename_lp_len);
|
||||
const bool trailing_slash = filename[strlen(filename) - 1] == '/';
|
||||
char *filename_lp = NULL;
|
||||
|
||||
// Check if the file exists. If so, rewrite the filename
|
||||
if(file_readable(filename_lp))
|
||||
// Try index pages first
|
||||
if(trailing_slash)
|
||||
// If there is a trailing slash, append "index.lp"
|
||||
filename_lp = append_to_path(filename, "index.lp");
|
||||
else
|
||||
// If there is no trailing slash, append "/index.lp"
|
||||
filename_lp = append_to_path(filename, "/index.lp");
|
||||
|
||||
// Check if the file exists. If so, rewrite the filename and return
|
||||
if(filename_lp != NULL && file_readable(filename_lp))
|
||||
{
|
||||
log_debug(DEBUG_API, "Rewriting %s ==> %s", filename, filename_lp);
|
||||
log_debug(DEBUG_API, "Rewriting index page: %s ==> %s", filename, filename_lp);
|
||||
strncpy(filename, filename_lp, filename_buf_len);
|
||||
free(filename_lp);
|
||||
return;
|
||||
}
|
||||
free(filename_lp);
|
||||
|
||||
log_debug(DEBUG_API, "Not rewriting %s ==> %s, no such file",
|
||||
filename, filename_lp);
|
||||
// If there is a trailing slash, we are done
|
||||
if(trailing_slash)
|
||||
return;
|
||||
|
||||
// Try full path with ".lp" appended
|
||||
filename_lp = append_to_path(filename, ".lp");
|
||||
if(filename_lp != NULL && file_readable(filename_lp))
|
||||
{
|
||||
log_debug(DEBUG_API, "Rewriting Lua page: %s ==> %s", filename, filename_lp);
|
||||
strncpy(filename, filename_lp, filename_buf_len);
|
||||
free(filename_lp);
|
||||
return;
|
||||
}
|
||||
|
||||
// Change last occurrence of "/" to "-" (if any)
|
||||
char *last_slash = strrchr(filename_lp, '/');
|
||||
if(last_slash != NULL)
|
||||
*last_slash = '-';
|
||||
if(file_readable(filename_lp))
|
||||
{
|
||||
log_debug(DEBUG_API, "Rewriting %s ==> %s", filename, filename_lp);
|
||||
strncpy(filename, filename_lp, filename_buf_len);
|
||||
free(filename_lp);
|
||||
return;
|
||||
*last_slash = '-';
|
||||
if(file_readable(filename_lp))
|
||||
{
|
||||
log_debug(DEBUG_API, "Rewriting Lua page (settings page): %s ==> %s", filename, filename_lp);
|
||||
strncpy(filename, filename_lp, filename_buf_len);
|
||||
free(filename_lp);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
log_debug(DEBUG_API, "Not rewriting %s ==> %s, no such file",
|
||||
filename, filename_lp);
|
||||
free(filename_lp);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue