@curiouskiwi, Thanks a lot....your response helped a lot. I wonder why I missed out such little detail. However guys, I have another problem which I believe am missing out something but can not seem to figure out what am missing out. The problem is that I am getting "HTTP/1.1 501 Not Implemented" when I run 192.#.#.#:port#/cat.jpg. I am trying to implement the question "if absolute-path does not contain a . (and thus a file extension), respond to the browser with 501 Not Implemented." and below is my code. I am VERY certain that check_request[1] is "/cat.jpg " as I have used printf to poke around my code but I still keep getting the 501 not implemented error message. Please another help would be much appreciated.
if(strchr(check_request[1], '?') != NULL)
{
check_request[1] = strtok(check_request[1], "?");
if(strstr(check_request[1], ".") == NULL)
{
error(501);
continue;
}
}
else if(strstr(check_request[1], ".") == NULL)
{
error(501);
continue;
}
@Cliff B, I had to abandon this pset and move on to pset7 but am done and am back and still experiencing the same issue but can't seem to figure out what were the error is coming from. I have spent hours poking around with printf but still no luck. Below is my code as requested please help look through as i may be missing out something but can seem to figure out what is missing. PLEASE any one out there with a helping answer can please help as am loosing steam
// TODO: validate request-line
char* check_request[5] = {NULL};
int i = 1;
//store the method type
check_request[0] = strtok(line, " ");
//store the request-target and http version
while(i < 4)
{
check_request[i] = strtok(NULL, " ");
i++;
}
char temp[strlen(check_request[1])]; //declared to store absolute path
strcpy(temp, check_request[1]); // Storing absolute
//Ensure the method type is get
if(strcasecmp(check_request[0], "GET") != 0)
{
error(405);
continue;
}
//Check if the request target begins with "/" character
if(strstr(&line[4], "/") == NULL)
{
error(501);
continue;
}
//Check if the request-target contains '"'
if(strchr(check_request[1], '"') != NULL)
{
error(400);
continue;
}
char http_version[9];
for(int i = 0; i < 8; i++) //copy the http version into memory
{
http_version[i] = check_request[2][i];
}
http_version[8] = '\0'; //Add the required NULL to terminate the copied string
//Now is to check if the http version is "HTTP/1.1"
if(strncasecmp(check_request[2], http_version, 8) != 0)
{
error(505);
continue;
}
if(strchr(check_request[1], '?') != NULL)
{
check_request[1] = strtok(check_request[1], "?");
if(strstr(check_request[1], ".") == NULL)
{
error(501);
continue;
}
}
else if(strstr(check_request[1], ".") == NULL)
{
error(501);
continue;
}
// TODO: extract query from request-target
check_request[3] = strtok(check_request[1], "?");
check_request[4] = strchr(temp, '?');
char query[1];
if((strchr(check_request[1], '?') == NULL) || (strstr(check_request[3], ".php") == NULL))
{
query[0] = '\0';
}
if(check_request[4] != NULL)
{
char query[strlen(check_request[4]) + 1];
strcpy(query, check_request[4]);
}
// TODO: concatenate root and absolute-path
strcat(root, check_request[3]);
char path[strlen(temp)];
strcpy(path, root);
// TODO: ensure path exists
if(access(path, F_OK) != 0)
{
error(404);
continue;
}
// TODO: ensure path is readable
if(access(path, R_OK) != 0)
{
error(403);
continue;
}
// TODO: extract path's extension
check_request[1] = strrchr(path, '.');
check_request[2] = strtok(check_request[1], ".");
char extension[strlen(check_request[1])];
strcpy(extension, check_request[1]);