0

I respond to the client with the following function call:

dprintf(cfd, "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: %i\r\nContent-Type: %s\r\n\r\n%s", length, type, body);

This works, mostly, but the jpg file is giving me an ERR_CONTENT_LENGTH_MISMATCH in the status on the developer Network panel.

Upon further inspection, I found that the jpg was labeled as having a content-length of 26860, which is the total Bytes in the cat.jpg file, BUT the "Size Content" column on the network panel says

Size

Content

100 B

13 B

For cat.html, it says:

Size

Content

240 B

156 B

and 156 B is the size of my actual .html, so for some reason that one works just fine but the .jpg does not.

The biggest problem I am having is FINDING the particular bug here. I did not touch the code in load, and I have no code handling body differently dependent on what's inside it.

In an attempt to debug, I used printf to spit out the file contents of each file being passed in, and what I found was my HTML file has random characters (sometimes letters, sometimes weird boxes that my terminal makes difficult to discern) added after the </html> which doesn't seem to affect the output, but I'm wondering if these extra bits have anything to do with my issues with the .jpg file.

1 Answer 1

0

I have found the solution!

To anyone else who has issues with this, my dprintf statement was not the correct solution. By that call, body is being sent as a string, but it needs to be more carefully handled. instead, took that off of the dprintf command and used awrite command to send it through, a la the pre-loaded error function.

3
  • Hi Michael, I've found your comments and questions on server.c very helpful as I follow not far behind. I have just come across this issue myself, despite implementing write. Am I correct in thinking that first the MIME type needs to be checked, headers then removed and length recalculated in order for write to function correctly? Thanks a lot Commented Jan 28, 2015 at 11:42
  • The MIME type needs to be checked and the length calculated, but I don't believe you're supposed to chop off the header. If you remove the header, the file that's sent by the server would lack the bytes that indicate what sort of file is being sent. For me, I just took the body global and sent it through via write without making any changes to what the load() function did to it.
    – Michael F
    Commented Jan 31, 2015 at 22:41
  • Thanks for coming back to me Michael. If turns out I made a school boy error of omitting the second /r/n! Commented Feb 6, 2015 at 11:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .