I'm convinced that the issues lie within my parse and/or load function but can't seem to identify them. Maybe I'm incorrectly allocating memory?? I'm not sure. Can someone please help me out?
Here's my parse:
bool parse(const char* line, char* abs_path, char* query)
{
// Extract 'method' string from 'line' string
int line_len = strlen(line);
char* temp = malloc(line_len);
char* method = malloc(line_len);
strcpy(temp, line);
strcpy(method, temp);
method = strtok(temp, " ");
// method spec
if((strlen(method)) != 3)
{
error(405);
return false;
}
if((strcasecmp(method, "get")) != 0)
{
error(405);
return false;
}
// HTTP Version spec
char* version = strcasestr(line, "HTTP/1.1");
if(version == NULL)
{
error(505);
return false;
}
// .exe is not valid
char* exe_file = strcasestr(line, ".exe");
if(exe_file != NULL)
{
error(501);
return false;
}
// Extract 'request-target' string from 'line' string
char* temp1 = malloc(line_len);
char* req_target = malloc(line_len);
strcpy(temp1, line);
strcpy(req_target, temp1);
req_target = strtok(temp1, " ");
req_target = strtok(NULL, " ");
int tar_len = strlen(req_target) + 1;
char* temp2 = malloc(tar_len);
strcpy(temp2, req_target);
char* nul = malloc(1);
strcpy(nul, "\0");
strcat(temp2, nul);
// req_target spec
if(temp2[0] != '/')
{
error(501);
return false;
}
// filename must not have "
for(int i = 0; i < line_len; i++)
{
if (line[i] == '"')
{
error(400);
return false;
}
}
bool val = true;
while(true)
{
// check if 'file is valid' spec
char* valid_file = (strcasestr(abs_path, ".css"));
if(valid_file == NULL)
{
val = false;
}
else
{
val = true;
break;
}
valid_file = (strcasestr(abs_path, ".jpg"));
if(valid_file == NULL)
{
val = false;
}
else
{
val = true;
break;
}
valid_file = (strcasestr(abs_path, ".gif"));
if(valid_file == NULL)
{
val = false;
}
else
{
val = true;
break;
}
valid_file = (strcasestr(abs_path, ".ico"));
if(valid_file == NULL)
{
val = false;
}
else
{
val = true;
break;
}
valid_file = (strcasestr(abs_path, ".js"));
if(valid_file == NULL)
{
val = false;
}
else
{
val = true;
break;
}
valid_file = (strcasestr(abs_path, ".php"));
if(valid_file == NULL)
{
val = false;
}
else
{
val = true;
break;
}
valid_file = (strcasestr(abs_path, ".png"));
if(valid_file == NULL)
{
val = false;
}
else
{
val = true;
break;
}
valid_file = (strcasestr(abs_path, ".html"));
if(valid_file == NULL)
{
val = false;
}
else
{
val = true;
break;
}
if(val == false)
{
error(404);
return false;
}
break;
}
// CRCF spec
char* CRCF = strstr(line, "\r\n");
if(CRCF == NULL)
{
error(400);
return false;
}
// set new abs_path
strcpy(abs_path, temp2);
// Extract 'query' string from 'line' string
char* temp3 = malloc(line_len);
char* the_query = malloc(line_len);
char* qry = query;
strcpy(temp3, line);
strcpy(the_query, temp3);
char* quest_m = strchr(the_query, '?');
if(quest_m != NULL)
{
the_query = strtok(temp3, " ");
the_query = strtok(NULL, "/");
the_query = strtok(NULL, "?");
the_query = strtok(NULL, " ");
}
// check if query exists
char* char_eq = strchr(the_query, '=');
if(char_eq != NULL)
{
char* actual_query = malloc(line_len);
strcpy(actual_query, "?");
strcat(actual_query, the_query);
strcat(actual_query, nul);
strcpy(qry, actual_query);
}
else
{
strcpy(qry, "\0");
}
return true;
}
and then here's my Load:
bool load(FILE* file, BYTE** content, size_t* length)
{
// don't go further if empty
if(file == NULL)
{
return false;
}
BYTE *buffer;
size_t filelen;
// Jump to the end of the file
fseek(file, 0, SEEK_END);
// Get the current byte offset in the file
filelen = ftell(file);
// Jump back to the beginning of the file
rewind(file);
// Enough memory for file + \0
buffer = (BYTE *)malloc((filelen+1)*sizeof(BYTE));
fread(buffer, filelen, 1, file); // Read in the entire file
fclose(file); // Close the file
*length = filelen;
*content = buffer;
return true;
}