Add request handler for directories (try /index.php)

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2020-06-15 21:05:09 +02:00
parent dc915c2a60
commit c3d2c87800
2 changed files with 21 additions and 6 deletions
+20 -6
View File
@@ -44,17 +44,31 @@ int ph7_handler(struct mg_connection *conn, void *cbdata)
/* Handler may access the request info using mg_get_request_info */
const struct mg_request_info *req_info = mg_get_request_info(conn);
const char *local_uri = req_info->local_uri + 1u;
// Build full path of PHP script on our machine
const size_t webroot_len = strlen(httpsettings.webroot);
const size_t local_uri_len = strlen(req_info->local_uri + 1u); // +1 to skip the initial '/'
char full_path[webroot_len + local_uri_len + 2];
strcpy(full_path, httpsettings.webroot);
const size_t local_uri_len = strlen(local_uri); // +1 to skip the initial '/'
size_t buffer_len = webroot_len + local_uri_len + 2;
// Check if we can serve an index.php file when the user is looking for a directory
bool append_index = false;
if(local_uri[local_uri_len - 1u] == '/')
{
append_index = true;
buffer_len += 11; // strlen("/index.php")
}
char full_path[buffer_len];
strncpy(full_path, httpsettings.webroot, webroot_len);
full_path[webroot_len] = '/';
strncpy(full_path + webroot_len + 1u, req_info->local_uri + 1u, local_uri_len);
strncpy(full_path + webroot_len + 1u, local_uri, local_uri_len);
full_path[webroot_len + local_uri_len + 1u] = '\0';
if(config.debug & DEBUG_API)
logg("Full path of PHP script: %s", full_path);
if(append_index)
{
strcpy(full_path + webroot_len + local_uri_len, "/index.php");
full_path[webroot_len + local_uri_len + 11u] = '\0';
}
// Compile PHP script into byte-code
// This usually takes only 1-2 msec even for larger scripts on a Raspberry
+1
View File
@@ -127,6 +127,7 @@ void http_init(void)
// Initialize PH7 engine and register PHP request handler
init_ph7();
mg_set_request_handler(ctx, "**/$", ph7_handler, 0);
mg_set_request_handler(ctx, "**.php$", ph7_handler, 0);
}