My server fails the last check in check50-server2 - "request two files in a row" Because it only fails that test I'm assuming the problem is somewhere in the parse or lookup functions. Valgrind shows that there are no memory leaks but that there are uninitialized variables, but as far as i can tell they are initialized before i use them. I'm not sure wha the problem is. Any ideas or suggestion would be really helpful! thank you!
Here is the code for the lookup function:
const char* lookup(const char* path)
{
const char ch = '.';
char* dot = strrchr(path, ch);
// test that dot is not NULL pointer "\0"
if (dot == NULL)
{
return NULL;
}
dot++;
char ch2 = '\0';
char* term = strrchr(path, ch2);
int len = term - dot - 1;
char* suffix = malloc(sizeof(char) * (len + 1));
/** get the part after the last . operator into a new string
char suffix[5];
int i = 0;
*/
int i = 0;
while (*(dot + i) != '\0')
{
*(suffix + i) = *(dot + i);
i++;
}
*(suffix + i) = '\0';
const char* css = "text/css";
const char* html = "text/html";
const char* js = "text/javascript";
const char* php = "text/x-php";
const char* gif = "image/gif";
const char* ico = "image/x-icon";
const char * jpg = "image/jpeg";
const char* png = "image/png";
// compare the new string to the path options
if (strcasecmp(suffix, "css") == 0)
{
free(suffix);
return css;
}
else if (strcasecmp(suffix, "html") == 0)
{
free(suffix);
return html;
}
else if (strcasecmp(suffix, "js") == 0)
{
free(suffix);
return js;
}
else if (strcasecmp(suffix, "php") == 0)
{
free(suffix);
return php;
}
else if (strcasecmp(suffix, "gif") == 0)
{
free(suffix);
return gif;
}
else if (strcasecmp(suffix, "ico") == 0)
{
free(suffix);
return ico;
}
else if (strcasecmp(suffix, "jpg") == 0)
{
free(suffix);
return jpg;
}
else if (strcasecmp(suffix, "png") == 0)
{
free(suffix);
return png;
}
else
{
free(suffix);
return NULL;
}
}
here is the code for the parse function:
bool parse(const char* line, char* abs_path, char* query)
{
char* start = strchr(line, ' ');
char* end = strrchr(line, ' ');
char* question = strchr(line, '?');
char* car = strchr(line, '\r');
// check for all the characters above
if ((start == NULL) || (end == NULL) || (car == NULL) || (start == end))
{
error(400);
return false;
}
//set up i, j variables
int i = 0;
int j= 0;
// set method
int l = start - line + 1;
char* method = calloc(l + 1, sizeof(char));
for (i = 0; i < l; i++)
{
*(method + i) = *(line + i);
}
*(method + i + 1) = '\0';
// check compatability of method
if (strcmp(method, "GET ") != 0)
{
error(405);
free(method);
return false;
}
free(method);
// set up abs_path
// check for extra spaces
if (*(line + i) == ' ')
{
error(400);
return false;
}
// check for predicate '/'
if (*(line + i) != '/')
{
error(501);
return false;
}
// check if there is a question mark
if (question == NULL)
{
// set query to \0
*query = '\0';
// populate abs_ path unti next space
while (*(line + i) != ' ')
{
if (*(line + i) == '"')
{
error(400);
return false;
}
else
{
*(abs_path + j) = *(line + i);
i++;
j++;
}
}
*(abs_path + j) = '\0';
}
// if there is ? -> populate *abs_path until ?
else
{
while(*(line + i) != '?')
{
if (*(line + i) == '"')
{
error(400);
return false;
}
else
{
*(abs_path + j) = *(line + i);
i++;
j++;
}
}
*(abs_path + j) = '\0';
// skip the ?
i++;
// initialize j
j = 0;
while (*(line + i) != ' ')
{
// check for ' " '
if (*(line + i) == '"')
{
error(400);
return false;
}
else
{
*(query + j) = *(line + i);
i++;
j++;
}
}
*(query + j) = '\0';
}
// set up HTTP Version
i++;
j = 0;
int l2 = car - end - 1;
char* http = malloc(sizeof(char) * (l2 + 1));
while ((line + i) != car)
{
*(http +j) = *(line + i);
i++;
j++;
}
*(http + l2) = '\0';
// check http for compatability with version 1.1
if (strcmp(http, "HTTP/1.1") != 0)
{
error(505);
free(http);
return false;
}
free(http);
return true;
}