I am in pset6. I am getting an error of 501 not implemented. I do no think this error comes from the validating line part because a couple of days ago i was having a 403 error. So up to that point everything was fine, hence the 501 error has to deal with the extension. The path is both readable and exists since now 403 and 404 errors are not returned. However, I cant see why or how my extension is being wrongly stored. Thanks for helping me in advance
//define elements to be checked
int length = strlen(line);
char method_checker[4];
method_checker[3] = '\0';
//Check if method is get
for (int i = 0; i < 3; i++)
{
method_checker[i] = line[i];
}
//continue man page http://www.tutorialspoint.com/cprogramming/c_continue_statement.htm
if (strcmp(method_checker, "GET") != 0)
{
error(405);
continue;
}
//the first char of request is 5 element in line
if (line[4] != '/'){
error(501);
continue;
}
//Finds second space in request line
int second_space = 0;
for (int i = 4; i < (length-4); i++)
{
if(line[i] == ' ')
{
second_space = i;
}
}
//Check the request target
int request_end= second_space - 1;
int counter =0;
for (int i = 5; i < request_end; i++)
{
if (line[i] == '.')
{
counter += 1;
}
if (line[i] == '\"')
{
error(400);
continue;
}
}
if (counter==0)
{
error(501);
continue;
}
//Check the http protocol
int http_start= second_space +1;
char protocol[9];
protocol[8]= '\0';
for (int i = http_start; i < http_start + 8; i++)
{
protocol[(i-http_start)] = line[i];
}
char* http= "HTTP/1.1";
if (strcmp(protocol, http) != 0)
{
error(505);
continue;
}
// extract query from request-target
//Find the length of the request
//Subtract position of second space with first space, then add 1 to account for null terminator
int request_length = second_space - 4;
char request[request_length];
request[request_length] = '\0';
strncpy(request, line + 4, second_space - 4 + 1);
//create variable that will keep track of start of query
int index = 0;
//find start of query
for (int i = 0; i < request_length; i++)
{
if (request[i] == '?')
{
index = i;
}
}
char query[request_length -index +1];
//if there is no query start then set size of query to one byte
if (index == 0)
{
query[0] = '\0';
}
//copy the query from line to query
//for third param request_length-1 -index +1
strncpy(query, request+index, request_length - index);
//concatenate root and absolute-path
//create variable to hold absolute path
char path[index+1];
//copy absolute path from request to absolute path
strncpy(path, request + 0, index - 0 + 1);
path[index+1]= '\0';
strcat(root, path);
//ensure path exists
//Man page for access http://linux.die.net/man/2/access
if (access(path, F_OK) == -1)
{
error(404);
continue;
}
// ensure path is readable
//Man page for access http://linux.die.net/man/2/access
if (access(path, R_OK) == -1)
{
error(403);
continue;
}
// extract path's extension
int path_length = strlen(path);
int dot_index = 0;
for (int i = 0; i < path_length; i++)
{
if (path[i] == '.')
{
dot_index = i;
}
}
//set extension initially to null
char extension[path_length-dot_index + 2];
strncpy(extension, path + dot_index + 1, path_length - (dot_index + 1) + 1);
extension[path_length-dot_index+2] = '\0';