I have almost completed pset 6 but am having trouble with the php part in terms of the query not going through properly to the dynamic content string. Specifically the name. I copy the query to memory but then it starts to get overwritten at some point and by the time it reaches the dynamic content, it's some garbage value. Any suggestions on how to fix my code would be much appreciated, code below:
// Extract query from request-target
char* check_query = strchr(request_target, '?');
char* query = "\0";
// If there is no "?" or anything after a "?", do the following
if(check_query == 0 || check_query + 1 == NULL)
{
query[0] = '\0';
}
else
{
int queryLen = strlen(check_query);
char querytemp[queryLen];
// copy query part to query[]
strncpy(querytemp, check_query + 1, queryLen);
querytemp[queryLen] = '\0';
query = querytemp;
int charlenToCopy = (strlen(requestTarget) - strlen(querytemp) - 1);
requestTarget = strncpy(requestTarget, requestTarget, charlenToCopy);
requestTarget[charlenToCopy] = '\0';
}
// Concatenate root and absolute-path
char path[strlen(root) + strlen(requestTarget)];
strcpy(path, root);
strcat(path, request_target);
// Ensure path exists
if (access(path, F_OK) != 0)
{
printf("404 Not Found\n");
continue;
}
// Ensure path is executable
if (access(path, X_OK) != 0)
{
printf("403 Forbidden\n");
continue;
}
// Extract path's extension
char* request_target2 = strchr(request_target, '\0');
char extension[request_target2 - check_stop];
strncpy(extension, check_stop + 1, strlen(request_target2 - strlen(check_stop)));