Hello everyone and thanks in advance for the assistance. I am working on server.c and I have my functions written and was able to test lookup and parse individually and everything seems to be working fine. But every time I try and run the program I get:
GET / HTTP/1.1 HTTP/1.1 301 Moved Permanently
and the webpage says:
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies.
I have no idea why this is happening. Any advice would be appreciated. here is the function
bool parse(const char* line, char* abs_path, char* query)
{
// TODO
//Check method to make sure it is GET, if not "405 Method Not Allowed" return false
if(line[0] != 'G' || line[1] != 'E' || line[2] != 'T' || line[3] != ' ')
{
error(405);
return false;
}
//then check for /, if no slash "501 Not Implemented" and return false
if(line[4] != '/')
{
error(501);
return false;
}
//go through request target, if it contains " return false and 400 bad request
//make int for location of query start
int q = 0;
int i = 4;
int j = 0;
//create new string of just request target
char new2[LimitRequestLine + 1];
//create a string with path and query and mark where the query starts
while(line[i] != ' ')
{
if(line[i] == '?')
{
q = i;
}
if(line[i] == '"')
{
error(400);
return false;
}
new2[j] = line[i];
j++;
i++;
}
//add null term to new2
new2[j+1] = '\0';
// add one to i to account for the space
i++;
// check each char after the space and make sure it is HTTP/1.1
if(line[i] != 'H' || line[i+1] != 'T' || line[i+2] != 'T' || line[i+3] != 'P' || line[i+4] != '/' || line[i+5] != '1' || line[i+6] != '.' || line[i+7] != '1')
{
error(505);
return false;
}
//if no question mark was in request, update abs path and query
if(q == 0)
{
int o = 0;
for(int length = strlen(new2); o < length; o++)
{
abs_path[o] = new2[o];
}
//add null term to abs path
abs_path[o+1] = '\0';
char* quotes = "\"\"";
int p = 0;
for(int length = strlen(quotes); p < length; p++)
{
query[p] = quotes[p];
}
//add null term to query
query[p+1] = '\0';
}
if(q != 0 && line[q+1] == ' ')
{
int o = 0;
for( int length = strlen(new2); o < length; o++)
{
abs_path[o] = new2[o];
}
//add null term to abs path
abs_path[o+1] = '\0';
char* quotes = "\"\"";
//query = quotes;
int p = 0;
for(int length = strlen(quotes); p < length; p++)
{
query[p] = quotes[p];
}
//add null term to query
query[p+1] = '\0';
}
//if question mark was found, copy up to that mark
if(q != 0)
{
char new_minus_query[LimitRequestLine + 1];
strncpy(new_minus_query, new2, q-4);
//update abs path value
// abs_path = new_minus_query;
int o = 0;
for(int length = strlen(new_minus_query); o < length; o++)
{
abs_path[o] = new_minus_query[o];
}
abs_path[o+1] = '\0';
////update query
// if(line[q+1] == ' ')
//{
// query = "";
//}
//else
//{
//copy everything past ? to new string
char new_only_query[LimitRequestLine + 1];
int k = q+1;
int l = 0;
while(line[k] != ' ')
{
new_only_query[l] = line[k];
k++;
l++;
}
abs_path[l+1] = '\0';
//update query
// query = new_only_query;
int r = 0;
for(int length = strlen(new_only_query); r < length; r++)
{
query[r] = new_only_query[r];
}
query[r+1] = '\0';
}
return true;
}
I added null terminators to all the strings and I am still seeing: * Error in `./server': munmap_chunk(): invalid pointer: 0x0000000002198240 * Aborted
I updated the code with my changes to add null terms. I dont know why I cant figure this out. I have been able to work through the rest of the course with little guidance. I am pulling my hair out here. If you wouldn't mind taking another look.
I also tried adding +1 to the strlen calls and removing it from the line that adds the null term. like this
int o = 0; for(int length = strlen(new2+1); o < length; o++) { abs_path[o] = new2[o]; } //add null term to abs path abs_path[o] = '\0';
It fixed the string error I've been getting but I'm back to the 301 error. :(