0

I have already made a first part of this pset, but I have several problems that I can not resolve. My code pass almost all of the tests except these:

:( Requesting cat.exe returns error code 501

:( Requesting non-existant file returns error code 404

... and I can not figure out why I have these errors. Here is my parse function:

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

{

char* tmp = malloc(sizeof(char) * strlen(line));
strcpy(tmp, line);

char* method = strtok(tmp, " ");
char* request = strtok(NULL, " ");
char* httpVer = strtok(NULL, "\r");

printf("method:%s;\nrequest:%s;\nhttp:%s\n", method, request, httpVer);
printf("requestlen:%lu\n", strlen(request));
if (strcmp(method, "GET"))
{
    printf("after1:\n");
    error(405);
    printf("after1:\n");
    return false;
}
if (request[0] != '/')
{
    printf("after2:\n");
    error(501);
    return false;
}
if (strchr(request, '\"'))
{
    printf("after3:\n");
    error(400);
    return false;
}
if (strcmp(httpVer, "HTTP/1.1"))
{
    error(505);
    return false;
}

if (strcmp(request, "?") == 0)
{
    abs_path = strtok(request, "?");
    query = "";
    printf("request2:%s; abs_path: %s\n", request, abs_path);
    return true;
}
if (strcmp(request, "q=") == 0)
{
    abs_path = strtok(request, "q");
    query = strchr(request, 'q');
    printf("request1:%s; abs_path: %s\n", request, abs_path);
    return true;
}

// error(501);
// return false;
abs_path = request;
query[0] = '\0';

return true;

}

I run a server and type the URL like "https://ide50-username.cs50.io/ffff.jpg" I can see in my terminal window a few lines of "HTTP/1.1 301 Moved Permanently". Why this happens ?

1

Scope. Pointers. Suggest you rewatch Week 4 > Section > Pointers.

When program control is returned to main, abs_path has the same contents as before the call to parse; it's still pointing to the "blue" memory.

where is pointer?

Review how you populated tmp. You want to populate abs_path similarly.

That is the main problem causing the check50/server1. You have other problems that will appear in check50/server 2 because of query. What will strcmp(request, "?") return? strcmp compares the whole string. You want to know if request contains a '?' not whether it is "?" exactly. And this
if (strcmp(request, "q=") == 0) is, frankly, meaningless. What if the request line is /hello.php?name=Alice?

As you work through the corrections, remember that gdb is an invaluable troubleshooting tool. It lets you inspect variables anywhere in the program (eg in main when you return from parse) without having to add printfs.

0

You must log in to answer this question.

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