HTTP server that doesn't serve HTML?

Hey! I just finished the base “Build your own HTTP server” challenge. This was my very first encounter with Rust. I figured it would be fairly simple and super rewarding to finally see my own website being served by an HTTP server I built from scratch.

Weeeell, that’s exactly where the problem popped up. The challenge didn’t actually include serving basic files like html, js, css, and jpg with the correct MIME types. I was soooo disappointed by that!

So, I decided to handle it myself. Even with zero prior Rust experience, adding support for html, js, and css was actually quite easy. Returning jpg/png correctly? That was a bit harder :stuck_out_tongue: But after a long, brutal war with ownership, I finally pulled it off, and the moment I saw my own website being served by my own HTTP server in the browser, in all its glory, was unbelievably satisfying!

I’m honestly really surprised this isn’t the main goal of the challenge. Because when I see “HTTP server”, the very first thing I think of is serving HTML pages.

Hi, thanks for your post!

I’m currently out of the office and will be back on March 23. I’ll get back to you as soon as possible once I return.

Deducing the correct MIME type for a file can be harder than it seems, as you can’t always trust the file extension or that there is one. Also, once you do figure out the MIME type, extracting or setting it from the HTTP headers is just a few lines of code, so not really anything fancy or difficult there.

There is one exception, though: when the client bundles multiple types into a multipart/form-data and you have to split it into its individual components on the server side. For example, suppose you have a website that creates thumbnails and need to receive in a single request both the original image (binary, need to handle various image types) and the requested thumbnail size (plaintext or JSON).

But, I think the main purpose of this challenge was how to concurrently read/write data to/from multiple TCP streams, and for that the bare-bones HTTP server that we already wrote is enough for that.

Yeah, that’s fair. In a real production environment, just checking only the file extension wouldn’t be enough, especially from a security perspective where backend file validation really matters. But for this challenge, I don’t think it needs to go that deep. If the stage doesn’t require one exact way of figuring out MIME types, people can just handle it however they want.

What you described with multipart/form-data is actually a fantastic idea for yet another advanced extension! However, for simply a basic static website, supporting just a few basic MIME types and correctly returning images is more than enough to get that satisfying result.

And yep, you’re totally right about the main goal of the challenge. That’s actually exactly how it’s described on the website, so I probably just let my imagination run a bit too wild :stuck_out_tongue:
Still, it doesn’t change the fact that I’d absolutely love to see this added as an official extension! :slight_smile: