Here are my implemented functions
const char* lookup(const char* path)
{
char ext[6];
int j = 0;
for(int i=0,n=strlen(path);i<n;i++)
{
if(path[i]=='.')
{
ext[j] = path[i];
j++;
}
else if(j>0)
{
ext[j] = path[i];
j++;
}
}
ext[j+1] ='\0';
if(strcmp(ext,".css") == 0)
return "text/css";
else if(strcmp(ext,".html") == 0)
return "text/html";
else if(strcmp(ext,".jpg") == 0)
return "image/jpeg";
else if(strcmp(ext,".gif") == 0)
return "image/gif";
else if(strcmp(ext,".js") == 0)
return "text/javascript";
else if(strcmp(ext,".php") == 0)
return "text/x-php";
else if(strcmp(ext,".png") == 0)
return "image/png";
else if(strcmp(ext,".ico") == 0)
return "image/x-icon";
else
return NULL;
}
bool parse(const char* line, char* abs_path, char* query)
{
char method[5];
strncpy(method,line,4);
method[4]='\0';
if(strcmp(method,"GET ")!=0)
{
error(405);
return false;
}
int i = 4;
int j = 0;
if(line[i]!='/')
{
error(501);
return false;
}
if(line[i+1]==' ')
i++;
else if(line[i+1]!=' ')
{
while(line[i]!='\n' || line[i]!='?')
{
if(line[i]=='\"' || line[i]==' ' )
{
error(400);
return false;
}
abs_path[j] = line[i];
j++;
i++;
}
if(line[i]=='?' && line[i+1]!=' ')
{
int u = 0;
while(line[i]!=' ')
{
query[u] = line[i];
u++;
i++;
}
}
}
char* http = malloc(sizeof(char)*10);
int u = 0;
while(line[i]!='\r')
{
http[u] = line[i];
u++;
i++;
}
http[u+1]='\0';
if(strcmp(http," HTTP/1.1")!=0)
{
error(505);
return false;
}
return true;
}
bool load(FILE* file, BYTE** content, size_t* length)
{
char buffer[1];
char* buffer2 = NULL;
buffer2 = malloc(sizeof(char));
int i = 2;
while(!feof(file))
{
fread(buffer,1,1,file);
buffer2 = realloc(*content,sizeof(char)*i);
i++;
}
fseek(file,0,SEEK_SET);
while(feof(file))
{
fread(buffer2,1,i,file);
}
fclose(file);
return true;
}
char* indexes(const char* path)
{
char* c = NULL;
char* u = strcpy(c,path);
char* r = "index.html";
char* s = "index.php";
char* b = strcat(u,r);
FILE* file = fopen(b,"r");
if(file==NULL)
return NULL;
else
{
fclose(file);
return b;
}
char* n = strcat(u,s);
FILE* file2 = fopen(n,"r");
if(file2==NULL)
return NULL;
else
{
fclose(file2);
return n;
}
}