samedi 1 décembre 2018

Image in HTTP Response using C?

I am writing a web server in C. I am able to send my html code in the response body but I tried an JPG image but I get "http://localhost:port/image.jpg" cannot be displayed because it contains errors.

this is a snippet from my webserver code:

this is my response header:

char * responseheader= "HTTP/1.1 200 OK\r\n "Content-Type: image/jpg\r\n" "Content-Length: 110000\r\n" "Content-Type: text\html\r\n\r\n";

//read the file imagefile and store it in the buffer and append it to response header.

for (i=0;(i<(sizeof(buffer))&&((ch=fgetc(imagefile))!=EOF)&&(ch!='\n')); i++) { printf("%c", ch) buffer[i] = ch; } strcat(responseheader, buffer);

if (strncmp(requestbuff, "GET /image.jpg", 15) { printf("server: loading image...\n"); send(socketfd, responseheader, sizeof(responseheader), 0); }

output:

on my webrowser firefox, I type localhost:port/index.html and the page loads fine. once my server reads the request GET /image.jpg..., I send the buffer above. Now, when my page loads, the image doesn't show up but as a small error box and when I right click on it and view image, it states the error above.

I'm not really sure how I can append the image with my response header. I tried sending the file directly to the web browser once I used open() after sending the response; same story. One thing I do know is that the image is "binary data", full of characters that I can see when I use notepad.

I've looked for hours, browsing the university library, and looking for books, but I haven't seen any implementation in HTTP/C to properly display an image.

Inside my index.html I used <img src=/image.jpg></img> which triggers the browser request to my web server.

Also, is there a book that any of you would recommend concerning HTTP and C web servers that covers this material? Thanks.

Note: I understand this may be easier to implement in PHP/JAVA/JAVASCRIPT; I can only write this in C at the moment

Best Regards.




Aucun commentaire:

Enregistrer un commentaire