Build a simple HTTP server in C

CHttpserver

C Problem Overview


I need to build a simple HTTP server in C. Any guidance? Links? Samples?

C Solutions


Solution 1 - C

I'd recommend that you take a look at: A Practical Guide to Writing Clients and Servers

What you have to implement in incremental steps is:

  1. Get your basic TCP sockets layer running (listen on port/ports, accept client connections and send/receive data).
  2. Implement a buffered reader so that you can read requests one line (delimited by CRLF) at a time.
  3. Read the very first line. Parse out the method, the request version and the path.
  4. Implement header parsing for the "Header: value" syntax. Don't forget unfolding folded headers.
  5. Check the request method, content type and content size to determine how/if the body will be read.
  6. Implement decoding of content based on content type.
  7. If you're going to support HTTP 1.1, implement things like "100 Continue", keep-alive, chunked transfer.
  8. Add robustness/security measures like detecting incomplete requests, limiting max number of clients etc.
  9. Shrink wrap your code and open-source it :)

Solution 2 - C

I suggest you take a look at http://tinyhttpd.sourceforge.net/">tiny httpd. If you want to write it from scratch, then you'll want to thoroughly read https://www.rfc-editor.org/rfc/rfc2616">RFC 2616. Use http://en.wikipedia.org/wiki/Berkeley_sockets">BSD sockets to access the network at a really low level.

Solution 3 - C

An HTTP server is conceptually simple:

  • Open port 80 for listening
  • When contact is made, gather a little information (get mainly - you can ignore the rest for now)
  • Translate the request into a file request
  • Open the file and spit it back at the client

It gets more difficult depending on how much of HTTP you want to support - POST is a little more complicated, scripts, handling multiple requests, etc.

But the base is very simple.

Solution 4 - C

Mongoose (Formerly Simple HTTP Daemon) is pretty good. In particular, it's embeddable and compiles under Windows, Windows CE, and UNIX.

Solution 5 - C

Open a TCP socket on port 80, start listening for new connections, implement this. Depending on your purposes, you can ignore almost everything. At the easiest, you can send the same response for every request, which just involves writing text to the socket.

Solution 6 - C

Look at nweb (Nigel's Web Server), "a tiny, safe web server [...] with only 200 lines of C source code":

https://drive.google.com/file/d/0B3msld7qnNOhN1NXaFIwSFU2Mjg/view?usp=sharing&resourcekey=0-ngY0neP78dxJKlFv0PJoDQ http://www.ibm.com/developerworks/systems/library/es-nweb/</strike>

The article includes pseudocode, explanations, and comments.

EDIT: IBM's link has died. I have saved a PDF of the webpage to Google Drive. Here is the code download:

https://drive.google.com/file/d/0B3msld7qnNOhSGZGdDJJMmY0VHM/view?usp=sharing&resourcekey=0-xkbf4mv0gN1sZrhBjt86UQ

@ankushagarwal has made a few changes and uploaded his version on GitHub: https://github.com/ankushagarwal/nweb

Solution 7 - C

I have written my own that you can use. This one works has sqlite, is thread safe and is in C++ for UNIX.

You should be able to pick it apart and use the C compatible code.

http://code.google.com/p/mountain-cms/

Solution 8 - C

The HTTP spec and Firebug were very useful for me when I had to do it for my homework.

Good luck with yours. :)

Solution 9 - C

I'd suggest looking at the source to something like lighthttpd.

Solution 10 - C

http://www.manning.com/hethmon/ -- "Illustrated Guide to HTTP by Paul S. Hethmon" from Manning is a very good book to learn HTTP protocol and will be very useful to someone implementing it /extending it.

Solution 11 - C

Use platform specific socket functions to encapsulate the HTTP protocol, just like guys behind Apache did.

Solution 12 - C

There is a duplicate with more responses.

One candidate not mentioned yet is spserver.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathan HView Question on Stackoverflow
Solution 1 - CAtes GoralView Answer on Stackoverflow
Solution 2 - CAdam RosenfieldView Answer on Stackoverflow
Solution 3 - CAdam DavisView Answer on Stackoverflow
Solution 4 - CBob NadlerView Answer on Stackoverflow
Solution 5 - CEclipseView Answer on Stackoverflow
Solution 6 - CXP1View Answer on Stackoverflow
Solution 7 - CDaniel A. WhiteView Answer on Stackoverflow
Solution 8 - COmer van KloetenView Answer on Stackoverflow
Solution 9 - CwarrenView Answer on Stackoverflow
Solution 10 - CanjanbView Answer on Stackoverflow
Solution 11 - CarulView Answer on Stackoverflow
Solution 12 - CakauppiView Answer on Stackoverflow