My server seems to be working fine, except when I submit form in hello.html and load hello.php. It results in Error 500 (Internal Server Error).
Also, when I use check50 it results in 'Unexpected end of input'.
Please help. I have been trying to fix it for 3 days now.
load() function -
bool load(FILE* file, BYTE** content, size_t* length)
{
BYTE* data = malloc(128);
unsigned int cap = 128;
unsigned int l = 0;
BYTE c;
if (file == NULL)
{
return false;
}
while(1)
{
if (l >= (cap-1))
{
cap = (cap*2);
data = realloc(data,cap);
}
c = fgetc(file);
if (feof(file))
{
break;
}
else
{
fseek(file,-1,SEEK_CUR);
c = fgetc(file);
*(data+l) = c;
l++;
}
}
*length = (size_t)l;
*content = data;
return true;
}
lookup() function -
const char* lookup(const char* path)
{
int l = strlen(path);
char* path1 = malloc(l + 1);
int i;
for(i=0; i<=l; i++)
{
if ((*(path+i) >= 'A') && (*(path+i) <= 'Z'))
{
*(path1+i) = (*(path+i) + 32);
}
else
{
*(path1+i) = *(path+i);
}
}
for(i=l;i>0;i--)
{
if (*(path1+i) == '.')
{
i++;
break;
}
}
char* ext = malloc(6);
int j=0;
for(;i<=l;i++)
{
if (*(path1+i) == '\0')
{
*(ext+j) = '\0';
break;
}
else
{
*(ext+j) = *(path1+i);
j++;
}
}
if (!strcmp(ext,"css"))
{
return "text/css";
}
else if (!strcmp(ext,"html"))
{
return "text/html";
}
else if (!strcmp(ext,"gif"))
{
return "image/gif";
}
else if (!strcmp(ext,"ico"))
{
return "image/x-icon";
}
else if (!strcmp(ext,"jpg"))
{
return "image/jpeg";
}
else if (!strcmp(ext,"js"))
{
return "text/javascript";
}
else if (!strcmp(ext,"php"))
{
return "text/x-php";
}
else if (!strcmp(ext,"png"))
{
return "image/png";
}
return NULL;
}
parse() function
bool parse(const char* line, char* abs_path, char* query)
{
char* method = malloc(4);
int i,j;
int l = strlen(line);
for (i=0;i<l;i++)
{
if (*(line+i) == ' ')
{
*(method+i) = '\0';
i++;
break;
}
else
{
*(method+i) = *(line+i);
}
}
if(strcmp(method,"GET"))
{
error(405);
return false;
}
j=0;
char* target = malloc(LimitRequestLine + 100);
for(;i<l;i++)
{
if (*(line+i) == ' ')
{
*(target+j) = '\0';
i++;
break;
}
else
{
*(target+j) = *(line+i);
j++;
}
}
if(*(target) != '/')
{
error(501);
return false;
}
for(j=0;j<strlen(target);j++)
{
if (*(target+j) == '"')
{
error(400);
return false;
}
}
j=0;
char* version = malloc(9);
for(;;i++)
{
if ((*(line+i) == '\n') || (*(line+i) == '\r'))
{
*(version+j) = '\0';
break;
}
else
{
*(version+j) = *(line+i);
j++;
}
}
if(strcmp(version,"HTTP/1.1"))
{
error(505);
return false;
}
i=0; j=0;
char* pth = malloc(LimitRequestLine + 100);
while(1)
{
if ((*(target+i) == '?') || (*(target+i) == '\0'))
{
*(pth+j) = '\0';
break;
}
else
{
*(pth+j) = *(target+i);
j++;
i++;
}
}
char* q = malloc(LimitRequestLine +100);
if (*(target+i) == '?')
{
j=0;
i++;
while(1)
{
if (*(target+i) == '\0')
{
*(q+j) = '\0';
strcpy(abs_path,pth);
strcpy(query,q);
return true;
break;
}
else
{
*(q+j) = *(target+i);
i++;
j++;
}
}
}
else
{
*q = '\0';
strcpy(query,q);
strcpy(abs_path,pth);
return true;
}
}
indexes() function -
char* indexes(const char* path)
{
int l = strlen(path);
char* temp = malloc(l+12);
*temp = '\0';
strcat(temp,path);
strcat(temp,"/index.php");
FILE* f1 = fopen(temp,"r");
if (f1 == NULL)
{
char* temp2 = malloc(l+12);
*temp2 = '\0';
strcat(temp2,path);
strcat(temp2,"/index.html");
FILE* f2 = fopen(temp2,"r");
if (f2 == NULL)
{
return NULL;
}
else
{
return temp2;
}
}
else
{
return temp;
}
}