I'm making a simple http server. So far socket works for html files, now I'm trying to make work images.
This is how I read files:
char * fbuffer = 0;
if ((f = fopen(file, "r")) == NULL)
perror("Error opening file");
fseek (f, 0, SEEK_END);
length = ftell(f);
fseek (f, 0, SEEK_SET);
fbuffer = malloc(length);
int total = 0;
while (total != length)
total += fread(fbuffer, 1, length, f);
snprintf(response, sizeof(response), "HTTP/1.1 200 OK\nContent-Type: %s\nContent-Length: %i\n\n%s", type, (int) strlen(fbuffer), fbuffer);
n = send(newsockfd, response, strlen(response)+1, 0);
The image “http://127.0.0.1:1050/image.gif” cannot be displayed because it contains errors.
The issue here is that
fbuffer contains binary data, but you're attempting to treat is as a string by using functions like
strlen and using the
%s format specifier to print it.
Since binary data may contain a null byte, this prevents string functions from working on them since they use a null byte to mark the end of a string.
You should instead use functions like
memcpy to put data into your output buffer.
char response; int hlen; hlen = snprintf(response, sizeof(response), "HTTP/1.1 200 OK\nContent-Type: %s\nContent-Length: %d\n\n", type, length); memcpy(response + hlen, fbuffer, length); n = send(newsockfd, response, hlen + length, 0);