So far my program passes all but 2 of check50 server2:
:( Requesting cat.jpg returns 200, image/jpeg, and correct image
:( Requesting two files in a row (cat.html then cat.jpg) succeeds
I think they result from the same problem: loading jpg file.
I have gone through some question replies on this website and found out fgetc() can not successfully deal with binary file, such as jpg. So I changed to fread, still does not work.
As far as I know, by using char* buffer, some 0x00 from jpg file make false ends to the string. So I try to imitate how we "fread" jpg in pset 4. I think in pset 4 the program "typedef uint8_t BYTE", so that the 0x00 wouldn't be any problem.
However this time, the program has already set "typedef char BYTE;", so I can not just put " uint8_t* buffer" to store the address in content, which apparently takes only char.
Please, I'd be grateful for advice!
Here is my load function:
/**
* 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)
{
if (file == NULL)
{
return false;
}
char* buffer = malloc(1);
*length = 0;
while (fread(&buffer[*length], 1, 1, file) == 1)
{
*length = *length + 1;
buffer = realloc(buffer, *length + 1);
}
buffer = realloc(buffer, *length);
*content = &buffer[0];
return true;
}