I think my parse and lookup are working fine, but check50 is not happy. Maybe anyone has an idea. here my functions:
const char* lookup(const char* path)
{
// if there is a path, compare and give correct output if matched
char* s = strrchr(path, '.');
if(strcmp(s, "CSS")== 0)
{
return "text/css";
}
else if(strcmp(s, "HTML")== 0)
{
return "text/html";
}
else if(strcmp(s, "GIF") == 0)
{
return "image/gif";
}
else if(strcmp(s, "ICO")== 0)
{
return "image/x-icon";
}
else if(strcmp(s, "JPG")== 0)
{
return "image/jpeg";
}
else if(strcmp(s, "JS")== 0)
{
return "text/javescript";
}
else if(strcmp(s, "PHP")== 0)
{
return "text/x-php";
}
else if(strcmp(s, "PNG")== 0)
{
return "image/png";
}
return NULL;
}
/**
* Parses a request-line, storing its absolute-path at abs_path
* and its query string at query, both of which are assumed
* to be at least of length LimitRequestLine + 1.
*/
bool parse(const char* line, char* abs_path, char* query)
{
//if method is not get, respond with error
if(strncmp(line, "GET", 4) !=0)
{
error(405);
return false;
}
//if requested target doesn't begin with /, respond with error
if(line[4] != '/')
{
error(501);
return false;
}
//get requested tagert
char* haystack = strchr(line, '/');
char* needle = strchr(haystack, ' ');
//after the requested 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 absolut path
haystack = target;
needle = strchr(haystack, '?');
//check for query
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;
}
and the check 50 output:
:) server.c exists
:) server compiles
:( HTTP/1.0 returns error code 505
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:) Method of 'abcGET' returns error code 405
:) Method of 'GETabc' returns error code 405
:( request-target without starting '/' returns error code 501
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( request-target of abc/hello.php returns error code 501
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( Requesting cat.exe returns error code 501
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( Requesting non-existant file returns error code 404
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( Requesting request-target with " returns error code 400
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:) Two spaces after GET returns error code
:) A space within the request target returns error code
:) Two spaces before HTTP/1.1 returns error code