8

I'm currently working on PSET6 and have implemented the parse() and lookup() functions. When I try to test my code using CS50 (the first part), I get all checks minus the last one, "Requesting two files in a row (cat.html then cat.jpg) succeeds"

Doesn't the server request files one at a time? I figured it would run the parse function every time a file is needed. If you think I'm going in the wrong direction or want to offer any criticism, I'm all ears. Thanks in advance!


bool parse(const char* line, char* abs_path, char* query)
{
    // check if method is of type GET
    if (strncmp(line, "GET ", 4) != 0)
    {
        error(405);
        return false;
    } 

    // check if request-target starts with '/'
    if (line[4] != '/')
    {
        error(501);
        return false;       
    }

    // get request target
    char* haystack = strchr(line, '/');
    char* needle = strchr(haystack, ' ');

    // will be used to check what comes after the request-target
    char* http_version = needle;

    // check for extra targets
    char* errchk = strchr(http_version + 1, ' ');
    if (errchk != NULL)
    {
        error(505);
        return false;   
    }

    char target[needle - haystack];
    strncpy(target, haystack, needle - haystack);
    target[needle - haystack] = '\0'; 

    errchk = strchr(target, '\"');
    if (errchk != NULL)
    {
        error(400);
        return false;
    }

    // parse absolute path
    haystack = target;
    needle = strchr(haystack, '?');

    // check if a query is present
    if (needle == NULL)
    {
        strcpy(abs_path, haystack);
        query[0] = '\0';
    }
    else
    {
        // if so, remove query
        char path[needle - haystack + 1];
        strncpy(path, haystack, needle - haystack);
        path[needle - haystack + 1] = '\0'; 

        strcpy(abs_path, path);

        // parse query
        strcpy(query, needle + 1);
    }

    // check for HTTP version
    needle = strstr(http_version, "HTTP/1.1");

    if (needle == NULL)
    {
        error(505);
        return false;
    }

    return true;
}

2 Answers 2

4

I just completed this pset successfully.. I had the same issue and i moved on implementing the next function because if you see in the sandbox link you will find under that error written TODO..which means it will be covered in the remaining TODO functions.

3
  • I didn't notice that! It's a bit confusing. I wish they would have just eliminated that test if it doesn't apply. Commented Mar 21, 2016 at 22:42
  • Well thats how it is.. And i have passed the all the final checks just like that... Good Luck! Commented Mar 22, 2016 at 1:30
  • Your approach to parse() is interesting and very different from mine. I like it better. In mine I created separate variables for (1) the start of the method, (2) the end of method, (3) the start of the request (based on the end of the method), (4) the end of the request, (5) the start of the version (based on the end of the request) and (6) the end of the version. It involved a lot more code but helped me wrap my head around progressing through the string better. Your method seems a lot more efficient!
    – craig
    Commented Mar 23, 2016 at 19:45
1

Doesn't the server request files one at a time?

the server does not request files at all. the client (e.g., telnet, curl, a browser, etc) does. and it does request one file at a time.

however, if we take cat.html as an example, you'll find that it has an <img> element with a src attribute whose value is the path to cat.jpg.

so when requesting cat.html with a browser, the browser automatically requests cat.jpg as well after requesting cat.html in order to render cat.jpg in place of the aforementioned <img> tag in cat.html.

regarding your code:

char target[needle - haystack];
strncpy(target, haystack, needle - haystack);
target[needle - haystack] = '\0';

are you sure, target is big enough? are you sure the last available index in an array of length needle - haystack is needle - haystack?

same thing applies for

path[needle - haystack + 1] = '\0';

I recommend you avoid using check50 for now and use a client (perhaps a browser) to test your server, which may be running under gdb. set breakpoints, and check whether you have the values you expect stored in your variables.

2
  • Thanks for the comments! @Kareem, I tried using gdb but when I run the code and the server starts, I cannot run any debug commands. As for 'strcpy(query, needle + 1);', since the haystack only includes everything from the first "\" to the following " ", I thought copying the remaing chars after "?" would be safe. Commented Mar 21, 2016 at 22:35
  • @SuperGuajiro you're right about that assuming the target size problem fixed. missed the line where haystack is set to target. sorry! answer edited!
    – kzidane
    Commented Mar 22, 2016 at 9:24

You must log in to answer this question.

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