Updated my parse and load now. I am dying to find out why 301 error now:( Must be happening in redirect function.
typedef char BYTE;
#define LimitRequestLine 8190
bool parse(const char* line, char* abs_path, char* query) {
//abs_path start pointer
char* array = malloc(LimitRequestLine + 1);
//creating a method string together with a NULL terminator
char* method = NULL;
method = realloc(method,sizeof(char));
method[strlen(method)] = '\0';
//creating an array to check for '\'
//array that will be a buffer for an abs_path
char array3[LimitRequestLine + 1];
char array4[LimitRequestLine + 1];
//pointer for HTTP-version
char* http = malloc(sizeof(LimitRequestLine + 1));
char* check =malloc(sizeof(LimitRequestLine + 1));
//copying method into a pointer method and checking whether it matches "GET"
char* pastMetEnd = NULL;
pastMetEnd = realloc(pastMetEnd,sizeof(BYTE));
pastMetEnd = strchr(line,' ');
strncpy(method,line,(strlen(line) - strlen(pastMetEnd)));
method[strlen(method) + 1] = '\0';
// printf("%s",method);
//check for '/' in front of the abs_path
int num = (strlen(line) - strlen(pastMetEnd))+1;
//check for '/' in front of the abs_path
if(line[num]!=47)
{
error(501);
return false;
}
//checking for GET
printf("%s",method);
if(strcmp(method,"GET") != 0)
{
printf("dupa");
error(405);
return false;
}
//getting the start of http ver
check = strchr(line,'/');
http = strchr(check,' ');
//checking for the correct http ver
if(strcmp(http," HTTP/1.1\r\n")!=0 )
{
printf("lel");
error(505);
return false;
}
//getting the start of an abs_path
array = (strchr(line,'/'));
// populating a abs_path buffer
for(int j = 0;array[j]!= 'q' && array[j] != ' ' ; j++)
{
array3[j] = array[j];
}
//check for " " or ' " ' inside of abs_path buffer
for(int z = 0; z < strlen(array3);z++)
{
// printf("%c",array3[z]);
if(array3[z] == 32 || array3[z] == 34)
{
error(400);
return false;
}
}
//populating abs_path
abs_path=array3;
abs_path[strlen(abs_path)+1] = '\0';
printf("%s", abs_path);
//getting array to point to the start of the querry
if(strchr(line,'q') != NULL){
array = strchr(line,'q');
for(int i = 0; array[i] != 32; i++)
{
array4[i] = array[i];
}
query = array4;
printf("%s" , abs_path);
for(int z = 0; z < strlen(array4);z++)
{
if(array4[z] == 32 || array4[z] == 34)
{
error(400);
return false;
}
}
}
else
{
*query = ' ';
}
query[strlen(query)+1] = '\0';
// printf("%s" , query);
//error(501);
return true;
} } bool checkEnd(const char* droga, char* end) { //char* temp = malloc(strlen(end + 1)); char* temp = strrchr(droga,end[0]); if(strcmp(end,temp) == 0) {
return true;
}
//free(temp);
return false;
}
const char* lookup(const char* path)
{
//creating arrays of endings and mime's
char* array1[8] = {".css", ".html", ".gif", ".ico", ".jpg", ".js", ".php", ".png"};
char* array2[8] = {"text/css", "text/html", "image/gif", "image/x-icon", "image/jpg"
,"text/javascript", "text/x-php", "image/png"};
//checking if path contains the ending from array1 and if so returning array2 equivalent
for(int i = 0; i < 8; i++)
{
if(checkEnd(path,array1[i]))
{
return array2[i];
}
}
return "No such file available";
}
bool load(FILE* file, BYTE** content, size_t* length)
{
if(file == NULL)
{
printf("File is empty and therefore can not be read!!");
error(404);
return false;
}
BYTE array[sizeof(int)];
BYTE* storedBytes = NULL;
storedBytes = realloc(storedBytes,sizeof(BYTE));
int i = 0;
//load all of the bytes into and array and count the bytes
while(fread(array,sizeof(BYTE),1,file))
{
storedBytes[i] = array[0];
*length = *length + 1;
i++;
}
*content = storedBytes;
//printf("%d", (int)*length);
//printf("\n%s", storedBytes);
return true;
}Now indexes
char* indexes( char* path)
{
char* address = malloc(100));
//setting up a buffer to hold that path
char array[strlen(path) + 11];
char* indexes[2] = {"index.php","index.html"};
//poplating first buffer
for(int i = 0; i < strlen(path);i++)
{
array[i] = path[i];
}
//checking for a match
for(int n = 0; n < 2; n++){
strcat(array,indexes[n]);
address = array;
FILE* fp = fopen(address,"r");
if(fp != NULL )
{
printf("FOUND");
return address;
}
else
{
for(int i = 0; i < strlen(array);i++)
{
array[i] = path[i];
}
}
}
//if no match return NULL
return NULL;