:( Requesting cat.exe returns error code 501 \ expected output, not an exit code of 0 :( Requesting non-existant file returns error code 404 \ expected output, not an exit code of 0
code for lookup and parse-
/**
* Returns MIME type for supported extensions, else NULL.
*/
const char* lookup(const char* path)
{
char* ext = strstr(path,".");
if (strcasecmp(ext,".css") == 0)
{
return "text/css";
}
else if (strcasecmp(ext,".html") == 0)
{
return "text/html";
}
else if (strcasecmp(ext,".gif") == 0)
{
return "image/gif";
}
else if (strcasecmp(ext,".ico") == 0)
{
return "image/x-icon";
}
else if (strcasecmp(ext,".jpg") == 0)
{
return "image/jpeg";
}
else if (strcasecmp(ext,".js") == 0)
{
return "text/javascript";
}
else if (strcasecmp(ext,".php") == 0)
{
return "text/x-php";
}
else if (strcasecmp(ext,".png") == 0)
{
return "image/png";
}
else
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 (strstr(line,"HTTP/1.1") == NULL)
{
error(505);
return false;
}
int blnk_count = 0;
int x = 0;
while (line[x] != '\0')
{
if (line[x] == '"')
{
error(400);
return false;
}
if (line[x] == ' ')
{
blnk_count++;
}
x++;
}
if (blnk_count != 2)
{
error(400);
return false;
}
if (strncmp(line,"GET ",4) != 0)
{
error(405);
return false;
}
if (strncmp(line,"GET /",5) != 0)
{
error(501);
return false;
}
char* ptr1 = strchr(line,'/');
char* ptr2 = strchr(line,'?');
if (ptr2 == NULL)
{
ptr2 = strrchr(line,' ');
}
char path[ptr2 - ptr1 + 1];
int n = ptr2 - ptr1;
for (int i=0;i<n;i++)
{
path[i]=*ptr1;
ptr1++;
}
path[n]='\0';
strcpy(abs_path,path);
free(path);
if (*ptr1 == '?')
{
if (*(ptr1+1) ==' ')
{
query = "\0";
}
else
{
ptr1 = ptr1+1;
ptr2 = (strchr(line,' '));
char temp_query[ptr2 - ptr1 + 1];
int m = ptr2 - ptr1;
for (int i=0;i<n;i++)
{
temp_query[i] = *ptr1;
ptr1++;
}
temp_query[m]='\0';
strcpy(query,temp_query);
free(temp_query);
}
}
else if(*ptr1 == ' ')
{
query = "\0";
}
return true;
}