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:
- Open a socket on port 80.
- Wait for a client to make a request.
- Read the request (i.e., this person wants page "contact-us.html").
- Find and read "contact-us.html".
- Send an html header, then send the content of "contact-us.html"
- 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:
Fun Considerations
Things to consider for extra fun:
Have fun. It's a very cool thing to look at.
Other (Simpler) Suggestions
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).