HTTP Server – How to Write an HTTP Server

httplanguage-agnosticserver

As the title says, I would like to write a HTTP server. My question is this, how do I do this? I know this sounds VERY general and too "high level", but there is a method to my madness. An answer to this question should be, I believe, language agnostic; meaning, no matter what language I use (e.g., C, C++, Java, etc.) the answer should be the same. I have a general idea of how this is supposed to work:

  1. Open a socket on port 80.
  2. Wait for a client to make a request.
  3. Read the request (i.e., this person wants page "contact-us.html").
  4. Find and read "contact-us.html".
  5. Send an html header, then send the content of "contact-us.html"
  6. Done

Like I said, I believe this is the process, but I am not 100% sure. This leads me to the heart of my question. How or where does a person find out this information?

What if I didn't want to write just an HTTP server, what if I wanted to write an FTP server, a chat server, an image viewer, etc.? How does a person find out the exact steps/process needed to create a working HTTP server?

A co-worker told me about the html header, so I would have NEVER know this without him. He also said something about handing off each request to a new thread. Is there some big book of how things work? Is there some manual of what it takes to be an HTTP server?

I tried googling "how does a HTTP server work", but the only answers I could find were geared towards your average Joe, and not towards a person wanting to program a HTTP server.

Best Answer

Use the RFC2616, Luke!

You read the RFC 2616 on HTTP/1.1, and you go for it.

That was actually a project in my 3rd year in engineering school, and that's pretty much the project description.

Tools

Your tools are:

  • basic networking stuff (socket management, binding, understand addresses),
  • good understanding of I/O streams,
  • a lot patience to get some shady parts of the RFC (mime-types are fun).

Fun Considerations

Things to consider for extra fun:

  • plug-in architecture to add CGI / mod support,
  • configuration files for, well, many things,
  • lots of experimentation on how to optimize transfers,
  • lots of experimentation to see how to manage load in terms of CPU and memory, and to pick a dispatch model (big fat even loop, single accept dispatch, multi-thread, multi-process, etc...).

Have fun. It's a very cool thing to look at.

Other (Simpler) Suggestions

  • FTP client/server (mostly RFC959 but there are older versions and also some extensions)
  • IRC client/server (mostly RFC1459, but there are extensions)

They're way easier to tackle first, and their RFCs are a lot easier to digest (well, the IRC one has some odd parts, but the FTP one is pretty clear).

Language Choice

Of course, some implementation details will be highly dependant on the language and stack you use to implement it. I approached all that in C, but I'm sure it can be fun just as well in other languages (ok, maybe not as much fun, but still fun).

Related Topic