0

I only have this one problem left in check50. https://sandbox.cs50.net/checks/e3382282d5e64259adec1cc4f51b17de

I'm pretty sure the problem is in "load" (but, I could be wrong). I know it is tricky to write user input to pipe, but, if we could read bytes and store it in *content & *length, shouldn't there be a way (an easy one, I hope) to write into it? I've looked around for ideas from the forum, but it remains unclear. Here's a post where this problem was also mentioned.

I feel like I'm missing a step in load to write user input to ".php?". Can anyone please refer me to some readings where I can learn to do this? I don't think it is mentioned anywhere in materials cs50 gave us for edX students. It makes it difficult for someone like me who has NULL background of computer science to complete this pset with flying colours.

Anyway, here's my "load" codes:

Code removed per academic honesty<<

Thanks beforehand.

Edited: Now with 'parse':

bool parse(const char* line, char* abs_path, char* query)

{

    // put line into tokens 
    char req_line[strlen(line) + 1];
    strcpy(req_line, line);

    char* method = strtok(req_line, " ");
    char* req_tar = strtok(NULL, " ");
    char* version = strtok(NULL, "\r\n\r\n");
    char* absolute_path = strtok(req_tar, "?");
    strcpy(abs_path, absolute_path);

    for(int i = 0, n = strlen(line); i < n; i++)
    {
        if(strcmp(req_tar, abs_path) != 0)
            query = strstr(req_tar, abs_path);

        if(query[i] == '?' && strlen(query) == 1)
            query[0] = '\0';

        if(strcmp(method, "GET") != 0)
        {
            error(405);
            return false;
        }

        if(req_tar[0] != '/')
        {   
            error(501);
            return false;
        }

        if(req_tar[i] == '"')
        {
            error(400);
            return false;
        }

        if(line[i] == ' ' && line[i + 1] == ' ')
        {
            error(400);
            return false;
        }

        if(strcmp(version, "HTTP/1.1") != 0)
        {
            error(505);
            return false;
        }
    }
    return true;
}

and here is the check50 server1 which shows everything is good up to some stage https://sandbox.cs50.net/checks/caad31fda59b4ca0964cd3c0710c55dc

2
  • Your load() is functional. Your problem is most likely with parse(). You'll need to paste it instead of load() – Yuri Laguardia Nov 20 '16 at 1:11
  • I've uploaded my parse code and the check50 server1 result. Not sure how to scramble it for the code ethic thingy. You are welcome to do so, if necessary. Thanks – MTreacy Nov 20 '16 at 1:58
2

The strtok() call here:

char* absolute_path = strtok(req_tar, "?");

will crop req_tar at the ? and make it always be equal to abs_path.

Because of this, the following condition will never be true and you'll never extract query:

if(strcmp(req_tar, abs_path) != 0)
    query = strstr(req_tar, abs_path);

But even if that condition triggered, I don't think you understand what query should be.

Given a line "GET /hello.php?name=Alice HTTP/1.1\r\n", this is what you should extract:

  • abs_path should be /hello.php
  • query should be name=Alice

And this query = strstr(req_tar, abs_path); wouldn't make query become this name=Alice.

Here you can see the first part of your approach in action and what it does to req_tar: https://goo.gl/OzkIXX (wait some seconds for the code to load and then use the forward button to step through your code)

3
  • Thanks for the tips. I'll work on it and 'll get back to you soon. – MTreacy Nov 20 '16 at 2:44
  • 1
    It works!! Now I've managed to pass all check50 (server1 and 2) without any grumpy face!! Thanks again, Yuri for the tips. Didn't realize tackling query can be really tricky...:-) – MTreacy Nov 20 '16 at 10:06
  • Great! Please accept the answer checking it with the green mark so we can close this! – Yuri Laguardia Nov 20 '16 at 11:14

You must log in to answer this question.

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