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;
}