0

3 hopefully 'quick' issues if someone could help? [Edit: full code now here for reference - http://pastebin.com/d1jTs1jV]

1) My query parser for php isn't working. I thought believe I've done it right, so I'd appreciate some help here. CS50 check one passed fine, but check 2 gives me errors

Requesting hello.php? returns 200, text/html, and correct output \ expected output, not an exit code of 0

:( Requesting hello.php?name=Alice returns 200, text/html, and correct output \ expected output, not an exit code of 0

Here's the code for my parse (can remove once answered)

if (strchr(line, '?') == NULL)
// look for a query; if there is none - set it equal to null terminator as spec'ed
{
    *query = '\0';
    return true; 

}
char* query_ptr = strchr(line, '?');
// otherwise, you should have ? and a valid pointer to it with strchr
if (query_ptr == NULL)
{
// make sure you get a valid pointer;
    error(501);
    return false;

}

else if (*(query_ptr + 1) == ' ')
// check if there is nothing after the question mark - if so, again query is ""
{
    *query = '\0';
}
else
// otherwise, start from 1 after the question mark and copy the lowercase query into the index and null terminate
{
    query_ptr++;
    int query_idx = 0;
    while(*(query_ptr + query_idx) != ' ')
    {
        *(query + query_idx) = tolower(*(query_ptr + query_idx));
        query_idx++;
    }
    *(query + query_idx + 1) = '\0';
    return true;
}

2. cat2.html + cat3.html are no longer in the directories I think I saw someone say to email someone with the code to check, is that the case?

**3. I can't load 2 files ** Not sure what's going on here - I can load cat.html and cat.jpg successfully separately.

Thanks for any help!

5
  • Is this code block a piece of the parse() function or the whole function? It is hard for me to understand the code block. Can you provide the full code for the parse() function? Commented Nov 11, 2016 at 13:15
  • What is cat2.html and cat3.thml? I don't think I got those when I unzipped my pset6 folder. Commented Nov 11, 2016 at 13:17
  • Can you provide the full code of the load() function to help you with the third issue? Commented Nov 11, 2016 at 13:18
  • cs50.stackexchange.com/questions/21734/… there's a discussion of my load function. The commenter said it should work, but I guess it's not.
    – borker
    Commented Nov 11, 2016 at 17:22
  • Thanks, I am going to work with the pastebin code you provided and see if I can be of any help. Commented Nov 11, 2016 at 18:45

1 Answer 1

1

If the query is name=Alice, you should return name=Alice, but you're setting it to lower case and returning name=alice. Why?

The second problem I'll let you see for yourself. See your code in action here and pay attention to where the null terminator is:

https://goo.gl/8Gj8o1

Wait some seconds for it to load, then keep pressing the forward button.

PS: I can't see you handling abs_path anywhere in that snippet you've posted (the pastebin link doesn't work), but I'll assume you're handling it somewhere.


EDIT to check updated code:

1st Problem:

Look at this:

char method[6];
char * get_method = "GET ";
do
{
    method[i] = *(line + i);
    i++;
}
while(method[i] != ' ' && i < 6);

When you first get to while(method[i] != ' ' && i < 6);, the variable i is equal to 1 (since you've just incremented it with i++ right before), so you're comparing method[1] to ' '.

But method[1] is yet uninitialized (you've only filled method[0] with 'G' yet and nothing more)! You'll compare whatever garbage value inside method[1] to ' ', which causes unpredictable behavior.


2nd problem
Then look here:

//count the size of the request line
int tgt_size = 0;
// Copy using pointers to the absolute path limit
while (*(target_ptr + tgt_size) != ' ' && *(target_ptr + tgt_size) != '?' && tgt_size < LimitRequestLine)
{
    *(abs_path + tgt_size) = tolower(*(target_ptr + tgt_size));
    tgt_size++;
}
//null terminate abs path
*(abs_path + tgt_size + 1 ) = '\0';

In the last iteration of your loop, tgt_size++ will be incremented. When you do that + 1 here *(abs_path + tgt_size + 1 ) = '\0';, you're placing the '\0' one byte further than it should be and you end up with something like this:

enter image description here


3rd problem:
Other than that, you're again setting abs_path to lower case with tolower(), but you shouldn't.


4th problem:
And finally, you forgot to return true inside this block, after setting query to "". If you don't do this, you function will go to the end and return error(501) and false:

else if (*(query_ptr + 1) == ' ')
// check if there is nothing after the question mark - if so, again query is ""
{
    *query = '\0';
}
4
  • Thanks for the help, but I'm still not passing the same tests when I fixed the lowercase error (had missed a matching earlier, and was overzealous and fixing it)... any chance you could take a look on the abs path? pastebin.com/a4sG6HjP
    – borker
    Commented Nov 16, 2016 at 6:18
  • Ok. You still have 4 problems... all of them in parse(). I have updated the answer, take a look. Commented Nov 16, 2016 at 12:50
  • 1
    I've been banging my head against the wall on this for 2 weeks now - tough without being able to readily unblock yourself. Thanks a ton for clearing it up! Lifesaver!
    – borker
    Commented Nov 17, 2016 at 18:09
  • The good news is that from now on you won't need to use C to deal with strings anymore. Easier ways will come with the new languages you'll learn! Commented Nov 17, 2016 at 23:01

You must log in to answer this question.

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