So, im making a webserver in C that display a sensor temperature and also a plot made with gnuplot in png format. Im having problem sending the http template and the png image. I can send the http template with all the data but cant attach the image in the end.
static const char response_http_template[] = {
"HTTP/1.1 200 OK \
Date: Mon, 27 Jul 2009 12:28:53 GMT \
Server: Apache/2.2.14 (Win32) \
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT \
Content-Length: %lu \
Content-Type: text/html \
Connection: Closed \
\n\n \
%s \
%s"
};
static const char response_page_template[] = {
"<!DOCTYPE html> \
<html> \
<head> \
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> \
<meta charset=\"utf-8\" /> \
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> \
more html code with some %s
}
void send_response_data(uint32_t sock_client,
const char *student_name,
last_temp_t *last_temp)
{
char write_buf[8192] = { 0 };
char out_buf[8192] = { 0 };
struct stat filestat;
char filesize[7];
int fd;
char file_buf[1024] = {0};
char result;
FILE *fp;
if ( ((fd = open ("./sup/data.png", O_RDONLY)) < -1) || (fstat(fd, &filestat) < 0) ) {
printf ("Error in measuring the size of the file");
}
sprintf (filesize, "%zd", filestat.st_size);
printf("File size %s\n", filesize);
fp = fopen ("./sup/data.png", "r");
if (fp == NULL)
{
perror("fopen error");
exit(EXIT_FAILURE);
}
fread(file_buf, sizeof(char), filestat.st_size + 1, fp);
fclose(fp);
sprintf(
write_buf, response_page_template, student_name, last_temp->timestamp, last_temp->temp);
sprintf(out_buf, response_http_template, strlen(write_buf) + strlen(file_buf), write_buf, file_buf);
write(sock_client, out_buf, strlen(out_buf));
}
When i start a client with chrome, i see the http code correct, but the image is not showed, only text like this: �PNG
Any ideas on how to send the binary data? Or if i need to change something in the http templates?
Thanks