0

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. :(

1

Before you go changing cookies and settings, it his most likely that abs_path is not properly created and/or null terminated. This post has helpful info about viewing abs_path in main(). Ditto this. Are you using strncpy to create abs_path? Check out this post. If you are using strspn, read this post about why you cannot. And make sure you are not doing any kind of malloc on abs_path in parse. (Here's why). If none of this info leads you to an answer, consider adding your parse function to your post. Or playing around with cookies and settings.


EDIT(after code post)
Short answer: abs_path is not null terminated.

Part of the problem is declaring char arrays (e.g. char new2[LimitRequestLine + 1];) and then using string functions on them. I am not suggesting that you change them to char*, because that is not likely to make things better viz. memory leaks, seg faults etc.

Here is the crux of the problem:

for(int o = 0, length = strlen(new2); o < length; o++) 
    {  
        abs_path[o] = new2[o];  
    }
  1. strlen(new2): new2 is not string data and is not null terminated, so results are unpredicitble (read: unreliable and very often wrong).
  2. even if new2 was valid string data, strlen does NOT include the null terminator, so abs_path is not null terminated. Full stop.

To fix this code, you will need to manually add '\0' at the appropriate spots. You should have all the numbers (positions) you need to do that. When you're done with abs_path, you will need to make the same corrections to query. Also double check: it looks like in your q!=0 loop that abs_path will still have the query attached.

I am not going to comment on why your test program works. You need server.c to work so you can submit it for grading.

You will get this working, you're almost there. Let me suggest that when you are done celebrating your perfect score on pset6, that you take some time to get more knowledge and experience with the string functions. While you certainly can do this pos by pos, you would create more elegant, readable code if you used functions like strcpy, strncpy, strchr, strrchr, strcat, etc.


EDIT after new code changes.

This strlen(new2+1) is a problem. I don't think the parens are where you want them. If you want to loop for one longer than the length of new2, then strlen(new2) + 1 would be correct. See if that helps.

7
  • Thanks for the help. I think im narrowing it down. I pulled the function out and wrote a program to test it. It seems to be working great. Then i insert it back into server.c and when i run it i get this error: Using /home/ubuntu/workspace/pset6/public for server's root Listening on port 8080 GET / HTTP/1.1 / *** Error in `./server': munmap_chunk(): invalid pointer: 0x0000000000f6b240 *** Aborted
    – Bogricia
    Mar 30 '16 at 21:34
  • I also tried adding +1 to all the strlens, and removing them from the line that adds null terms like this
    – Bogricia
    Mar 31 '16 at 16:30
  • Hey Dino, I think I have parse working now. It seems to be returning the correct abs_path and query. Thank you so much for your help. I have now come across another invalid pointer error in my indexes function. Trying to correct that now.
    – Bogricia
    Mar 31 '16 at 18:44
  • Almost time to celebrate! Mar 31 '16 at 18:56
  • I posted my indexes function, i cant see the problem here.
    – Bogricia
    Mar 31 '16 at 19:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .