Was wondering if anybody could take a look at my code and find any issues. I ran check50 after implementing lookup and parse, and I passed everything, so I am assuming the problem is in my load and/or indexes function. When I manually check my code with curl, I can request all the files in the public directory and receive a 200 OK response for each one. When I try to upload it to check50 to check50, it always stops uploading and gives an "unexpected end of input" error message. I fixed a couple bugs, but can't find any more that would cause this problem.
bool load(FILE* file, BYTE** content, size_t* length)
{
int c;
*length = 0;
BYTE* buffer = malloc(1000 * sizeof(BYTE));
if (buffer == NULL)
{
free(buffer);
return false;
}
// read file char by char until end of file
while ((c = fgetc(file)) != EOF)
{
// store char from file in buffer
buffer[*length] = c;
// increase length counter
*length += 1;
// reallocate memory if necessary
if (*length % 1000 == 0)
{
buffer = realloc(buffer, (*length + 1000) * sizeof(BYTE));
if (buffer == NULL)
{
free(buffer);
return false;
}
}
}
// reallocate memory for exact amount needed
buffer = realloc(buffer, *length * sizeof(BYTE));
if (buffer == NULL)
{
free(buffer);
return false;
}
*content = buffer;
return true;
}
and here is my indexes function:
char* indexes(const char* path)
{
// create strings to append to path
char* php = "/index.php";
char* html = "/index.html";
// create memory for indexed path
char* path_index = malloc(sizeof(path) + sizeof(html) + 1);
if (path_index == NULL)
{
free(path_index);
return NULL;
}
// concatonate path string and php index string
strcpy(path_index, path);
strcat(path_index, php);
// return path if /index.php exists
if (access(path_index, F_OK) == 0)
{
return path_index;
}
else
{
// concatonate path string and html index string
strcpy(path_index, path);
strcat(path_index, html);
// return path if /index.html exists
if (access(path_index, F_OK) == 0)
{
return path_index;
}
// return NULL if none exist
else
return NULL;
}
}
EDIT:
Finally was able to upload to check50, got this back:
:) 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 \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
:( Requesting directory containing index.php outputs index.php \ expected output, not standard error of "======= Backtrace: =========\n======= M..."
:) Requesting two files in a row (cat.html then cat.jpg) succeeds
Can anybody point me in the right direction as to where to look in my code to fix these errors?