I need to build a simple HTTP server in C. Any guidance? Links? Samples?
-
I got the same question and somehow I got incomplete but very simple implementation(incomplete) using C, which compiles under Ubuntu linux thought it would be valuable if I tag it here, https://blog.abhi.host/blog/2010/04/15/very-simple-http-server-writen-in-c/ – Pulathisi Bandara Feb 04 '18 at 19:39
12 Answers
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:
- Get your basic TCP sockets layer running (listen on port/ports, accept client connections and send/receive data).
- Implement a buffered reader so that you can read requests one line (delimited by CRLF) at a time.
- Read the very first line. Parse out the method, the request version and the path.
- Implement header parsing for the "Header: value" syntax. Don't forget unfolding folded headers.
- Check the request method, content type and content size to determine how/if the body will be read.
- Implement decoding of content based on content type.
- If you're going to support HTTP 1.1, implement things like "100 Continue", keep-alive, chunked transfer.
- Add robustness/security measures like detecting incomplete requests, limiting max number of clients etc.
- Shrink wrap your code and open-source it :)

- 137,716
- 26
- 137
- 190
-
3
-
5Thank you for providing concepts rather than prebuilt solutions or links to RFC and Sockets. Reading the RFC and learning about Sockets isn't enough to build your own web server if you don't have these concepts in mind. – Stanley Sathler Dec 13 '20 at 15:13
-
1Reading requests line by line using "*a buffered reader*" (hence storing the line in a buffer until we find a CRLF), makes me anxoius about attacks that send gigabytes of data without CRLF. For this, I usually read using 'states', re-allocate the buffers on the go with the data, check for limits every read and send 414 appropriately. Except point #2, I agree with all the other points. – Example person Feb 20 '22 at 08:20
I suggest you take a look at tiny httpd. If you want to write it from scratch, then you'll want to thoroughly read RFC 2616. Use BSD sockets to access the network at a really low level.

- 1
- 1

- 390,455
- 97
- 512
- 589
-
3
-
28Don't read [RFC 2616](http://tools.ietf.org/html/rfc2616) now, it obsoleted by: [RFC 7230](http://tools.ietf.org/html/rfc7230), [RFC 7231](http://tools.ietf.org/html/rfc7231), [RFC 7232](http://tools.ietf.org/html/rfc7232), [RFC 7233](http://tools.ietf.org/html/rfc7233), [RFC 7234](http://tools.ietf.org/html/rfc7234), [RFC 7235](http://tools.ietf.org/html/rfc7235) – songhir Jun 10 '14 at 12:53
-
4`tiny httpd` is also on github here: https://github.com/larryhe/tinyhttpd – Purplejacket Aug 30 '17 at 21:25
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.

- 23,478
- 6
- 59
- 81

- 91,931
- 60
- 264
- 330
-
8Thank you for providing concepts rather than prebuilt solutions or links to RFC and Sockets. Reading the RFC and learning about Sockets isn't enough to build your own web server if you don't have these concepts in mind. – Stanley Sathler Dec 13 '20 at 15:11
Mongoose (Formerly Simple HTTP Daemon) is pretty good. In particular, it's embeddable and compiles under Windows, Windows CE, and UNIX.

- 24,066
- 8
- 71
- 57

- 2,755
- 24
- 20
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/
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:
@ankushagarwal has made a few changes and uploaded his version on GitHub: https://github.com/ankushagarwal/nweb

- 6,910
- 8
- 54
- 61
-
Eh up voted without checking link :/ I'm looking for the exact thing you mentioned, if you find something can you ping me? Txt – samayo Jun 30 '16 at 18:59
-
1
-
Here's a mirror by archive.org - which will hopefully be around for years to come: https://web.archive.org/web/20140905115151/http://www.ibm.com/developerworks/systems/library/es-nweb/ – james246 Oct 21 '16 at 10:50
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.

- 187,200
- 47
- 362
- 445
The HTTP spec and Firebug were very useful for me when I had to do it for my homework.
Good luck with yours. :)

- 11,800
- 9
- 42
- 53
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.

- 12,999
- 18
- 77
- 106