I need to build a simple HTTP server in C. Any guidance? Links? Samples?
This question is related to
c
httpserver
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:
Use platform specific socket functions to encapsulate the HTTP protocol, just like guys behind Apache did.
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.
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
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:
https://drive.google.com/file/d/0B3msld7qnNOhSGZGdDJJMmY0VHM/view?usp=sharing
@ankushagarwal has made a few changes and uploaded his version on GitHub: https://github.com/ankushagarwal/nweb
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.
An HTTP server is conceptually simple:
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.
Mongoose (Formerly Simple HTTP Daemon) is pretty good. In particular, it's embeddable and compiles under Windows, Windows CE, and UNIX.
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.
I'd suggest looking at the source to something like lighthttpd.
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.
An HTTP server is conceptually simple:
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.
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.
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:
An HTTP server is conceptually simple:
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.
Use platform specific socket functions to encapsulate the HTTP protocol, just like guys behind Apache did.
There is a duplicate with more responses.
One candidate not mentioned yet is spserver.
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.
Mongoose (Formerly Simple HTTP Daemon) is pretty good. In particular, it's embeddable and compiles under Windows, Windows CE, and UNIX.
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.
I'd suggest looking at the source to something like lighthttpd.
Mongoose (Formerly Simple HTTP Daemon) is pretty good. In particular, it's embeddable and compiles under Windows, Windows CE, and UNIX.
There is a duplicate with more responses.
One candidate not mentioned yet is spserver.
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:
Use platform specific socket functions to encapsulate the HTTP protocol, just like guys behind Apache did.
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.
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
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:
https://drive.google.com/file/d/0B3msld7qnNOhSGZGdDJJMmY0VHM/view?usp=sharing
@ankushagarwal has made a few changes and uploaded his version on GitHub: https://github.com/ankushagarwal/nweb
I'd suggest looking at the source to something like lighthttpd.
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.
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:
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.
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.
I'd suggest looking at the source to something like lighthttpd.
Use platform specific socket functions to encapsulate the HTTP protocol, just like guys behind Apache did.
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.
Source: Stackoverflow.com