Skip to content

Commit e085728

Browse files
authored
Merge pull request #1601 from pi-hole/fix/redirect_slash
Slash the slash
2 parents b1c2c29 + 55785f1 commit e085728

File tree

3 files changed

+100
-30
lines changed

3 files changed

+100
-30
lines changed

src/api/docs/content/specs/queries.yaml

+6-5
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ components:
210210
description: Time until the response was received (ms, negative if N/A)
211211
ttl:
212212
type: integer
213-
description: Remaining Time-To-Live (0 if N/A)
214-
regex:
213+
description: Query's Time-To-Live (TTL) value (`-1` if N/A)
214+
regex_id:
215215
type: integer
216216
description: ID of blocking regex (`-1` if N/A)
217217
upstream:
@@ -231,7 +231,8 @@ components:
231231
reply:
232232
type: "IP"
233233
time: 19
234-
regex_idx: -1
234+
ttl: -1
235+
regex_id: -1
235236
upstream: "localhost#5353"
236237
dbid: 112421354
237238
- time: 1581907871.583821
@@ -246,8 +247,8 @@ components:
246247
reply:
247248
type: "IP"
248249
time: 12.3
249-
response_time: 7
250-
regex_idx: -1
250+
ttl: 7
251+
regex_id: -1
251252
upstream: "localhost#5353"
252253
dbid: 112421355
253254
cursor:

src/webserver/lua_web.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ int request_handler(struct mg_connection *conn, void *cbdata)
105105
// Check if the request is for the login page
106106
const bool login = (strcmp(req_info->local_uri_raw, login_uri) == 0);
107107

108-
// Check if the request is for a LUA page (every *.lp has already been
109-
// rewritten at this point to *)
108+
// Check if the request is for a LUA page (every XYZ.lp has already been
109+
// rewritten at this point to XYZ)
110110
if(!no_dot)
111111
{
112112
// Not a LUA page - fall back to CivetWeb's default handler

src/webserver/webserver.c

+92-23
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,48 @@ static int redirect_lp_handler(struct mg_connection *conn, void *input)
108108
char *new_uri = calloc(strlen(uri) + query_len, sizeof(char));
109109
// Copy everything from before the ".lp" to the new URI
110110
strncpy(new_uri, uri, pos - uri);
111+
112+
// Append query string to the new URI if present
113+
if(query_len > 0)
114+
{
115+
strcat(new_uri, "?");
116+
strcat(new_uri, query_string);
117+
}
118+
119+
// Send a 301 redirect to the new URI
120+
log_debug(DEBUG_API, "Redirecting %s?%s ==301==> %s",
121+
uri, query_string, new_uri);
122+
mg_send_http_redirect(conn, new_uri, 301);
123+
free(new_uri);
124+
125+
return 1;
126+
}
127+
128+
static int redirect_slash_handler(struct mg_connection *conn, void *input)
129+
{
130+
// Get requested URI
131+
const struct mg_request_info *request = mg_get_request_info(conn);
132+
const char *uri = request->local_uri_raw;
133+
const char *query_string = request->query_string;
134+
const size_t query_len = query_string != NULL ? strlen(query_string) : 0;
135+
136+
// Do not redirect if the new URI is the webhome
137+
if(strcmp(uri, config.webserver.paths.webhome.v.s) == 0)
138+
{
139+
log_debug(DEBUG_API, "Not redirecting %s?%s",
140+
uri, query_string);
141+
142+
// Handle as a normal request
143+
return request_handler(conn, input);
144+
}
145+
146+
// Remove the trailing slash from the URI
147+
char *new_uri = strdup(uri);
148+
new_uri[strlen(new_uri) - 1] = '\0';
149+
150+
// Append query string to the new URI if present
111151
if(query_len > 0)
112152
{
113-
// Append query string ".lp" to the new URI if present
114153
strcat(new_uri, "?");
115154
strcat(new_uri, query_string);
116155
}
@@ -291,52 +330,82 @@ void http_init(void)
291330
// Register **.lp -> ** redirect handler
292331
mg_set_request_handler(ctx, "**.lp$", redirect_lp_handler, NULL);
293332

333+
// Register **/ -> ** redirect handler
334+
mg_set_request_handler(ctx, "**/$", redirect_slash_handler, NULL);
335+
294336
// Register handler for the rest
295337
mg_set_request_handler(ctx, "**", request_handler, NULL);
296338

297339
// Prepare prerequisites for Lua
298340
allocate_lua();
299341
}
300342

301-
void FTL_rewrite_pattern(char *filename, size_t filename_buf_len)
343+
static char *append_to_path(char *path, const char *append)
302344
{
303-
// Construct full path with ".lp" appended
304-
const size_t filename_lp_len = strlen(filename) + 4;
305-
char *filename_lp = calloc(filename_lp_len, sizeof(char));
306-
if(filename_lp == NULL)
345+
const size_t path_len = strlen(path);
346+
const size_t append_len = strlen(append);
347+
const size_t total_len = path_len + append_len + 1;
348+
char *new_path = calloc(total_len, sizeof(char));
349+
if(new_path == NULL)
307350
{
308-
log_err("Failed to allocate memory for Lua pattern!");
309-
return;
351+
log_err("Failed to allocate memory for path!");
352+
return NULL;
310353
}
311-
strncpy(filename_lp, filename, filename_lp_len);
312-
strncat(filename_lp, ".lp", filename_lp_len);
354+
strncpy(new_path, path, total_len);
355+
strncat(new_path, append, total_len);
356+
return new_path;
357+
}
313358

314-
// Check if the file exists. If so, rewrite the filename
315-
if(file_readable(filename_lp))
359+
void FTL_rewrite_pattern(char *filename, size_t filename_buf_len)
360+
{
361+
const bool trailing_slash = filename[strlen(filename) - 1] == '/';
362+
char *filename_lp = NULL;
363+
364+
// Try index pages first
365+
if(trailing_slash)
366+
// If there is a trailing slash, append "index.lp"
367+
filename_lp = append_to_path(filename, "index.lp");
368+
else
369+
// If there is no trailing slash, append "/index.lp"
370+
filename_lp = append_to_path(filename, "/index.lp");
371+
372+
// Check if the file exists. If so, rewrite the filename and return
373+
if(filename_lp != NULL && file_readable(filename_lp))
316374
{
317-
log_debug(DEBUG_API, "Rewriting %s ==> %s", filename, filename_lp);
375+
log_debug(DEBUG_API, "Rewriting index page: %s ==> %s", filename, filename_lp);
318376
strncpy(filename, filename_lp, filename_buf_len);
319377
free(filename_lp);
320378
return;
321379
}
380+
free(filename_lp);
322381

323-
log_debug(DEBUG_API, "Not rewriting %s ==> %s, no such file",
324-
filename, filename_lp);
382+
// If there is a trailing slash, we are done
383+
if(trailing_slash)
384+
return;
325385

326-
// Change last occurrence of "/" to "-" (if any)
327-
char *last_slash = strrchr(filename_lp, '/');
328-
if(last_slash != NULL)
329-
*last_slash = '-';
330-
if(file_readable(filename_lp))
386+
// Try full path with ".lp" appended
387+
filename_lp = append_to_path(filename, ".lp");
388+
if(filename_lp != NULL && file_readable(filename_lp))
331389
{
332-
log_debug(DEBUG_API, "Rewriting %s ==> %s", filename, filename_lp);
390+
log_debug(DEBUG_API, "Rewriting Lua page: %s ==> %s", filename, filename_lp);
333391
strncpy(filename, filename_lp, filename_buf_len);
334392
free(filename_lp);
335393
return;
336394
}
337395

338-
log_debug(DEBUG_API, "Not rewriting %s ==> %s, no such file",
339-
filename, filename_lp);
396+
// Change last occurrence of "/" to "-" (if any)
397+
char *last_slash = strrchr(filename_lp, '/');
398+
if(last_slash != NULL)
399+
{
400+
*last_slash = '-';
401+
if(file_readable(filename_lp))
402+
{
403+
log_debug(DEBUG_API, "Rewriting Lua page (settings page): %s ==> %s", filename, filename_lp);
404+
strncpy(filename, filename_lp, filename_buf_len);
405+
free(filename_lp);
406+
return;
407+
}
408+
}
340409
free(filename_lp);
341410
}
342411

0 commit comments

Comments
 (0)