I spend a lot of time on this pset, and still got some errors which I can not figure out, so please help if you may. Here are errors cs50 checker sends me:
:) server.c exists
:) server compiles
:) Requesting cat.jpg returns 200, image/jpeg, and correct image
:) Requesting cat.html returns 200, text/html, and correct file
:) Requesting cat2.HTML returns 200, text/html, and correct file
:) Requesting cat3.HtMl returns 200, text/html, and correct file
:) Requesting cat.gif returns 200, image/gif, and correct file
:) Requesting favicon.ico returns 200, image/x-icon, and correct file
:) Requesting test.css returns 200, text/css, and correct file
:) Requesting test.js returns 200, text/javascript, and correct file
:) Requesting hello.php returns 200, text/html, and correct output
:( Requesting hello.php? returns 200, text/html, and correct output
\ expected output, not an exit code of 0
:( Requesting hello.php?name=Alice returns 200, text/html, and correct output
\ expected output, not an exit code of 0
:) Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html
:( Requesting directory containing index.php outputs index.php
\ expected output, not an exit code of 0
:) Requesting two files in a row (cat.html then cat.jpg) succeeds
here is load:
bool load(FILE* file, BYTE** content, size_t* length)
{
*length = 0;
char *buffer = malloc(1);
for (unsigned int c = fgetc(file); c != EOF; c = fgetc(file))
{
buffer[*length] = c;
*length = *length + 1;
buffer = realloc(buffer, *length + 1);
}
*content = &buffer[0];
return true;
}
here is lookup:
const char* lookup(const char* path)
{
const char ch = '.';
char* rest = strrchr(path, ch);
if (strcasecmp(rest, ".css") == 0)
return "text/css";
else if (strcasecmp(rest, ".html") == 0)
return "text/html";
else if (strcasecmp(rest, ".gif") == 0)
return "image/gif";
else if (strcasecmp(rest, ".ico") == 0)
return "image/x-icon";
else if (strcasecmp(rest, ".jpg") == 0)
return "image/jpeg";
else if (strcasecmp(rest, ".js") == 0)
return "text/javascript";
else if (strcasecmp(rest, ".php") == 0)
return "text/x-php";
else if (strcasecmp(rest, ".png") == 0)
return "image/png";
else
return NULL;
}
Parse:
bool parse(const char* line, char* abs_path, char* query)
{
//parses line and puts words into array
char buff[strlen(line)];
strcpy(buff, line);
int i = 0;
char *p = strtok (buff, " ");
char *parsed[3];
while (p != NULL)
{
parsed[i++] = p;
p = strtok (NULL, " ");
}
//checks for the method to be GET
if (strcmp(parsed[0], "GET") != 0)
{
error(405);
return false;
}
//checks for the HTTP/1.1
if (strcmp(parsed[2], "HTTP/1.1\r\n") != 0)
{
error(505);
return false;
}
char *request_target = parsed[1];
if (request_target[0] != '/')
{
error(501);
return false;
}
if (strchr(request_target, '"') != NULL)
{
error(400);
return false;
}
strcpy(abs_path, request_target);
query = strchr(abs_path, '?');
return true;
}
and indexes:
char* indexes(const char* path)
{
const char *php = "/index.php";
const char *html = "/index.html";
if (strstr(path, php) != NULL || strstr(path, html) != NULL)
{
char *string = malloc(strlen(path) + 1);
strcpy(string, path);
return string;
}
else
return NULL;
}
appriciate your help!