Lookup:
const char* lookup(const char* path)
{
// To return MIME types
char* MIME_type = strrchr(path, '.');
if (strcasecmp(MIME_type, ".css") == 0)
return "text/css";
else if (strcasecmp(MIME_type, ".html") == 0)
return "text/html";
else if (strcasecmp(MIME_type, ".gif") == 0)
return "image/gif";
else if (strcasecmp(MIME_type, ".ico") == 0)
return "image/x-icon";
else if (strcasecmp(MIME_type, ".jpg") == 0)
return "image/jpg";
else if(strcasecmp(MIME_type, ".js") == 0)
return "text/javascript";
else if(strcasecmp(MIME_type, ".php") == 0)
return "text/x-php";
else if(strcasecmp(MIME_type, ".png") == 0)
return "image/png";
else
return NULL;
}
Here is parse:
bool parse(const char* line, char* abs_path, char* query)
{
// Initialize abs_path and query.
memset(abs_path, '\0', LimitRequestLine + 1);
memset(query, '\0', LimitRequestLine + 1);
// Find where /,? and space are.
char* first_space = strchr(line, ' ');
char* question_mark = strchr(line, '?');
char* HTTP_version = strstr(line, "HTTP");
char* target_end = HTTP_version - 1;
// Extract absolute-path and query.
if (question_mark == NULL)
{
memcpy(abs_path, (first_space + 1), target_end - (first_space + 1));
}
else
{
memcpy(abs_path, (first_space + 1), question_mark - (first_space + 1));
memcpy(query, (question_mark + 1), target_end - (question_mark + 1));
}
// Check line's consistency with the rules.
// find substrings in line.
char* get = strstr(line, "GET ");
if (get == NULL || get != line)
{
error(405);
return false;
}
if(strstr(line, " ") != NULL)
{
error(400);
return false;
}
if (strncmp(HTTP_version, "HTTP/1.1", 8) != 0)
{
error(505);
return false;
}
char* quotation = strchr(line, '"');
if(first_space < quotation && quotation < target_end)
{
error(400);
return false;
}
if (* (first_space + 1) != '/')
{
error(501);
return false;
}
return true;
}
and indexes:
char* indexes(const char* path)
{
// return if index.php is present.
char* indexphp = "/index.php";
char* path_php = malloc(strlen(path) + strlen(indexphp) + 1);
memset(path_php, '\0', strlen(path) + strlen(indexphp) + 1);
strcpy(path_php, path);
strcat(path_php, indexphp);
if (access(path_php, F_OK) == 0)
{
return path_php;
}
else
{
char* indexhtml = "/index.html";
char* path_html = malloc(strlen(path) + strlen(indexhtml) + 1);
memset(path_html, '\0', strlen(path) + strlen(indexhtml) + 1);
strcpy(path_html, path);
strcat(path_html, indexhtml);
if (access(path_html, F_OK) == 0)
{
return path_html;
}
else
return NULL;
}
}