Moohar Archive

Testcard F

13th February 2024

Doh. The problem with the UTF-8 was nothing wrong with the C code, but I forgot to set the charset in the HTTP response headers, I should have got that sooner. It makes sense as I’m not manipulating the content, just reading it from a file and sending it to a socket. The only function I’m really using on the temporary string is strlen which in this case will return the number of bytes, which is what I need, and not specifically the number of characters (beacuse some of them are multiple bytes long), which I don’t particularly care about. As it happens I didn’t even need to use strlen, I’ll come back to why in a moment.

I’m actually setting the character set in two places now. First, in the HTTP headers I have set Content-Type: text/html; charset=utf-8. Second, I’ve added the HTML header <meta charset="utf-8">. The second one is more out of habit than anything. I was not sure if it is strictly necessary, a review of the spec suggests not, but every boilerplate HTML example I found includes it, so I’ve left it there.

I’ve updated the server to be able to serve more than a single page. I was worried about accidentally granting everyone access to all the files on my test box, so I’ve taken what I believe to be quite a defensive approach. When the program starts, it scans the current directory and recursively builds a list of files available. Then when the server receives a request for a file it will only respond if that file is its list. This does means that if I add a file I have to restart the server, which is fine by me until I build up my confidence in validating client requests. Also I’m not a total noob, I’m running the server with an ID that has very limited access.

I created a new function that based on the extension of the file works out the mime type for the HTTP header, I assumed there was a far smarter way to do this but a cursory glance suggests not. I’ve intentionally left room for future improvement by using a big laddered if statement. One day I will replace this with a proper list of file extensions to mime types and a lookup or something.

As for sending images over HTTP, the only real change I needed to make was to stop treating the data I was reading from the files as a null terminated string and instead as an array of bytes. Most of my code was already set up for this, so it took about 30 seconds to fix it once I realised my error in thinking. This is another example of my lack of experience coding with C. I saw errors the other day using strlen to work out the content length and fixed it by null terminating the string when I should have realised I already knew the length from when I read the file. Fixed now. Clearly more practice is needed.

Oh you want proof, ok, here is an image.

Testcard F

TC