My parse and lookup function(Got it corrected through check50 test 1) Updated parse function
/**
* Returns MIME type for supported extensions, else NULL.
*/
const char* lookup(const char* path)
{
// http://www.tutorialspoint.com/c_standard_library/c_function_strchr.htm
const char ch = '.';
//Stores string after the .
char* end = strchr(path,ch);
//Initialize string for storing the return value
char* ret;
//Check the file extension
if(strcasecmp(end,".css") == 0)
{
ret = "text/css";
return ret;
}
if(strcasecmp(end,".html") == 0)
{
ret = "text/html";
return ret;
}
if(strcasecmp(end,".gif") == 0)
{
ret = "image/gif";
return ret;
}
if(strcasecmp(end,".ico") == 0)
{
ret = "image/x-icon";
return ret;
}
if(strcasecmp(end,".jpg") == 0)
{
ret = "image/jpeg";
return ret;
}
if(strcasecmp(end,".js") == 0)
{
ret = "text/javascript";
return ret;
}
if(strcasecmp(end,".php") == 0)
{
ret = "text/x-php";
return ret;
}
if(strcasecmp(end,".png") == 0)
{
ret = "image/png";
return ret;
}
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)
{
// TODO
//Request-line consists of method SP request-target SP HTTP-version CRLF*/
//The request target consists of the absolute path and the query in this form : absolute-path [ "?" query ]
//We will use strtok to store tokens in different strings
//Referenced from stackoverflow and tutorialspoint.com
//Copy the line into a new string
char* l =(char* )malloc(sizeofstrlen(line) + 1);
l = strdup(line);
//Token to store method
char* method = strtokstrcat(l, " ""\0");
//TokenAllocating tomemory storefor method,request targetand version
char *method = malloc(strlen(line) + 1);
char *request = strtokmalloc(NULL,"strlen(line) "+ 1);
char* http =
malloc(strlen(line) + 1);
char *abs_path1;
char *query1;
//Store the HTTP versionToken into astore tokenmethod
char* httpmethod = strtok(NULLl,"\r\n " ");
//Store the absoluteToken pathto instore arequest tokentarget
char* abs_path1request = strtok(requestNULL,"? ");
//Store the query afterHTTP ?version in a token
char* query1http = strtok(NULL,"?"\r\n ");
if
//Store the absolute path in a token abs_path1 = strtok(request,"?");
//Store the query after ? in a token query1 = strtok(NULL,"?"); if(query1 == NULL) { query1 = " "; strcat(query1,"\0"); } strcat(query1,"\0");
strcat(query1 == NULL)
query1 = "";
strcpy(query,query1"\0");
strcat(queryabs_path1,"\0");
strcpy(abs_pathquery,abs_path1query1);
strcatstrcpy(abs_path,"\0"abs_path1);
if (strcmp(method, "GET") != 0)
{
error(405);
return false;
}
if (strchr(query, '"'))
{
error(400);
return false;
}
if (request[0] != '/')
{
error(501);
return false;
}
if (strstr(request, "/ ") != 0)
{
error(404);
return false;
}
if (strcmp(http, "HTTP/1.1") != 0)
{
error(505);
return false;
}
free(l);
return true;
}