I have been working on server.c in Problem Set 6, and it seems to work. I think it runs the server, but for some reason the second Check50 fails. When I run it, I get:
:) 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
:) Requesting hello.php?name=Alice returns 200, text/html, and correct output
:( Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html
:( Requesting directory containing index.php outputs index.php \ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..."
:) Requesting two files in a row (cat.html then cat.jpg) succeeds
I am not sure how to test for which part of the program is wrong, and therefore, I have been unable to fix it.
These are my four functions:
Lookup
const char* lookup(const char* path)
{
char* extension = strrchr(path, '.');
if (extension != NULL) {
if (strcasecmp(extension, ".css") == 0) {
return "text/css";
} else if (strcasecmp(extension, ".html") == 0) {
return "text/html";
} else if (strcasecmp(extension, ".gif") == 0) {
return "image/gif";
} else if (strcasecmp(extension, ".ico") == 0) {
return "image/x-icon";
} else if (strcasecmp(extension, ".jpg") == 0) {
return "image/jpeg";
} else if (strcasecmp(extension, ".js") == 0) {
return "text/javascript";
} else if (strcasecmp(extension, ".php") == 0) {
return "text/x-php";
} else if (strcasecmp(extension, ".png") == 0) {
return "image/png";
} else {
return NULL;
}
} else {
return NULL;
}
}
Parse
bool parse(const char* line, char* abs_path, char* query)
{
char* temp = (char*) line;
if (strncmp(temp, "GET ", 4) != 0) {
error(405);
return false;
}
temp = temp + 4;
char target[LimitRequestLine + 1];
int targetIndex;
for (targetIndex = 0; *temp != ' '; temp++, targetIndex++) {
target[targetIndex] = *temp;
}
target[targetIndex] = '\0';
if(target[0] != '/') {
error(501);
return false;
}
for (int i = 0; i < LimitRequestLine + 1; i++) {
if (target[i] == '"') {
error(400);
return false;
}
}
temp++;
char version[LimitRequestLine + 1];
int versionIndex;
for (versionIndex = 0; *temp != '\0'; temp++, versionIndex++) {
version[versionIndex] = *temp;
}
version[versionIndex] = '\0';
if (strncmp(version, "HTTP/1.1", 8) != 0) {
error(505);
return false;
}
if (strcmp(version, "HTTP/1.1\r\n") != 0) {
error(400);
return false;
}
int j;
for (j = 0; target[j] != '?' && target[j] != ' ' && target[j] != '\0'; j++) {
abs_path[j] = target[j];
}
abs_path[j] = '\0';
char* question = strchr(target, '?');
if (question != NULL) {
question++;
strcpy(query, question);
}
return true;
}
Load
bool load(FILE* file, BYTE** content, size_t* length)
{
*length = 0;
*content = NULL;
if (file == NULL) {
error(400);
return false;
}
BYTE buffer[BYTES];
for (size_t i = fread(buffer, 1, BYTES, file); i != 0; i = fread(buffer, 1, 512, file)) {
*content = realloc(*content, *length + i + 1);
if (content == NULL) {
return false;
}
memcpy(*content + *length, buffer, i);
*length = *length + i;
}
return true;
}
Indexes
char* indexes(const char* path)
{
char* html = "/index.html";
char* php = "/index.php";
char* indexhtml = malloc(strlen(html) + strlen(path) + 1);
strcpy(indexhtml, path);
strcat(indexhtml, html);
char* indexphp = malloc(strlen(php) + strlen(path) + 1);
strcpy(indexphp, path);
strcat(indexphp, php);
if(access(indexhtml, F_OK) == 1) {
return indexhtml;
}
else if(access(indexphp, F_OK) == 1) {
return indexphp;
}
return NULL;
}
Thank you so much!