Alright. I'm becoming crazy, so here I come.
I finished the pset, and everything works. At least that's what check50
tells me.
When I launch the server and try each file, they work, except for hello.php, where I get a 500 Internal Server Error
.
First, I tried to change the query handling part of the
parse
function, but the problem definitely doesn't come from there. I'm almost certain it comes from the load
function.
Here it is :
/**
* Loads a file into memory dynamically allocated on heap.
* Stores address thereof in *content and length thereof in *length.
*/
bool load(FILE* file, BYTE** content, size_t* length)
{
struct stat st;
/* Finds out the size of 'file' in bytes */
fstat(fileno(file), &st);
*length = st.st_size;
/* Allocates memory for 'content' */
*content = malloc(*length * sizeof(BYTE));
/* Returns true if fread indeed read 'length' bytes, false otherwise */
return fread(*content, *length, 1, file) == 1 ? true : false;
}
Here's the weird part. The debugger seems to tell me that for every file but hello.php, the length that fstat
finds out is the right one. For hello.php however, with or without a query part, the length is zero.
I tried everything I knew and found on the internet - using a for
loop with putc
, one with fread
, and using fseek
and ftell
. None of this works for hello.php.
EDIT : Okay so I realized the difference between the php file and the others. For the php one, the interpret
function is used and so the file is not opened with fopen
, but with popen
. But still, it's a FILE* stream
, it should work the same, shouldn't it?