I've been grinding on this pset6 problem for days. I've implemented all four functions thus far. After I had only implemented lookup and parse, I passed the server1 check50 so I assume both of those are not the issue.
However, I can't seem to get images to load correctly since completing the load and indexes functions. I do however get a 200 ok response from the server when running it myself. Here is my output from the server2 check50.
:) 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
:( Requesting two files in a row (cat.html then cat.jpg) succeeds
Here is the source code for my load function:
bool load(FILE* file, BYTE** content, size_t* length)
{
//create variables for number in buffer, capacity of buffer and current count in buffer
unsigned int count = 0;
unsigned int capacity = 0;
char* buffer = NULL;
int c;
for (c = fgetc(file); c != EOF; c = fgetc(file))
{
//determine the new capacity
if (count + 1 > capacity)
{
if (capacity == 0)
capacity = 2;
else
capacity *= 2;
//extend the capacity of the buffer
char* temp = realloc(buffer, capacity * sizeof(char));
if (temp == NULL)
{
free(buffer);
return false;
}
buffer = temp;
}
buffer[count] = c;
count++;
}
//minimize the buffer to compensate for doubling
char* storage = malloc((count + 1) * sizeof(char));
strncpy(storage, buffer, count);
free(buffer);
//set content and lenth to storage and count
*content = storage;
*length = count;
return true;
}
And here is the code for my index function
char* indexes(const char* path)
{
//get the path length
int pathLength = strlen(path);
//malloc enough space on heap for php path
char* phpIndexPath = malloc((pathLength + 11) * sizeof(char));
//check to make sure malloc succeeded
if (phpIndexPath == NULL)
{
return NULL;
}
//append index extension
strcpy(phpIndexPath, path);
strcat(phpIndexPath, "/index.php");
if (access(phpIndexPath, F_OK) == -1)
free(phpIndexPath);
else
return phpIndexPath;
char* htmlIndexPath = malloc((pathLength + 12) * sizeof(char));
//check to make sure malloc succeeded
if (htmlIndexPath == NULL)
{
return NULL;
}
//append index extension
strcpy(htmlIndexPath, path);
strcat(htmlIndexPath, "/index.html");
if (access(htmlIndexPath, F_OK) == -1)
{
free(htmlIndexPath);
return NULL;
}
else
{
return htmlIndexPath;
}
}