@@ -108,9 +108,48 @@ static int redirect_lp_handler(struct mg_connection *conn, void *input)
108
108
char * new_uri = calloc (strlen (uri ) + query_len , sizeof (char ));
109
109
// Copy everything from before the ".lp" to the new URI
110
110
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
111
151
if (query_len > 0 )
112
152
{
113
- // Append query string ".lp" to the new URI if present
114
153
strcat (new_uri , "?" );
115
154
strcat (new_uri , query_string );
116
155
}
@@ -291,52 +330,82 @@ void http_init(void)
291
330
// Register **.lp -> ** redirect handler
292
331
mg_set_request_handler (ctx , "**.lp$" , redirect_lp_handler , NULL );
293
332
333
+ // Register **/ -> ** redirect handler
334
+ mg_set_request_handler (ctx , "**/$" , redirect_slash_handler , NULL );
335
+
294
336
// Register handler for the rest
295
337
mg_set_request_handler (ctx , "**" , request_handler , NULL );
296
338
297
339
// Prepare prerequisites for Lua
298
340
allocate_lua ();
299
341
}
300
342
301
- void FTL_rewrite_pattern (char * filename , size_t filename_buf_len )
343
+ static char * append_to_path (char * path , const char * append )
302
344
{
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 )
307
350
{
308
- log_err ("Failed to allocate memory for Lua pattern !" );
309
- return ;
351
+ log_err ("Failed to allocate memory for path !" );
352
+ return NULL ;
310
353
}
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
+ }
313
358
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 ))
316
374
{
317
- log_debug (DEBUG_API , "Rewriting %s ==> %s" , filename , filename_lp );
375
+ log_debug (DEBUG_API , "Rewriting index page: %s ==> %s" , filename , filename_lp );
318
376
strncpy (filename , filename_lp , filename_buf_len );
319
377
free (filename_lp );
320
378
return ;
321
379
}
380
+ free (filename_lp );
322
381
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 ;
325
385
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 ))
331
389
{
332
- log_debug (DEBUG_API , "Rewriting %s ==> %s" , filename , filename_lp );
390
+ log_debug (DEBUG_API , "Rewriting Lua page: %s ==> %s" , filename , filename_lp );
333
391
strncpy (filename , filename_lp , filename_buf_len );
334
392
free (filename_lp );
335
393
return ;
336
394
}
337
395
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
+ }
340
409
free (filename_lp );
341
410
}
342
411
0 commit comments