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 ?