I’m currently working on a Computer Science degree, and I want to build my own Web Server library. The problem? Every search I’ve done on interacting with web sites points to “Use this library that was built by someone else” – instead of explaining the basics of sending and receiving information.
I’m not asking for a lot – I just want to enter a single alpha-numeric character on a web page, hit a button, and have that one byte of information sent to a C++ program, without the aid of a non-standard library.
4
Boost.Asio is currently being converted to be (one of?) the future C++ standard way to do networking. (Here is one of the papers: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3388.pdf )
It’s really the most basic portable asynchronous communication system you will find using modern C++, as it abstract away other socket libraries or platform provided ones.
All the modern C++ http networking libraries (CppNetlib and CPPCMS) relies on it. I suggest you build your library around it provide basics for sockets and asynchronous (and synchronous) comunication but don’t setup a full system for you. Maybe in a few years you can even replace Boost.ASIO by the C++ standard equivalent.
Also, the documentation of Boost.Asio provide insight on how communication works in this kind of context. So reading it might answer a lot of your questions.
And if you want to understand the lower level, read the Asio code. It’s a bit hard to read if you’re not used to C++ meta-programming and other C++ idioms but you would learn a lot by reading it.
Some of tye info that you want is here – http://en.wikipedia.org/wiki/Common_Gateway_Interface . It might not sound like what you are trying to do is very complicated, but it’s a bit more complicated than you might think. As Dominic said the tricky part will be opening the port and reading from the socket – quite difficult in c++ land.
I would recommend that to start with, use an existing web server and write your own CGI module. The web server will take care of the sockets side of things, and basically you’re just reading data from environment variables or command line arguments (if it’s a GET request) or from the stdin if it’s a POST (I think, this is data from my memory that’s about 15 years old). What you want is something like this:
#include <stdio.h>
#include <string.h>
int main(){
char *s=getenv("CONTENT_LENGTH");
int i=atoi(getenv("CONTENT_LENGTH"));
printf("Content-type: text/htmlnn");
printf("%sn<br />",s); //Shows you CONTENT_LENGTH works
printf("%dn<br />",i); //Shows you it was converted to int
char *tmp = new char[100];
fread(tmp,i,1,stdin); //read from stdin something of i bytes to tmp
printf("%sn<br />",tmp);
return 0;
}
BTW as a lesson for the reader this application has a buffer overflow bug.. you probably want to fix that 🙂
Correct me if I’m wrong, but it sounds like you want to program your own web server from scratch, using only standard C++ components.
First of all you are in a bit of a bind as there is no standard tcp/ip socket library, so you’ll need to use Windows sockets, or Posix sockets depending on platform.
Then you’ll need to research the format of requests/responses. I’m sure with a bit of searching you’ll find a resource on the http protocol.
After that you’ll need to create a server that listens for connections, responds to requests and sends responses.
If that isn’t what you are asking, then MichaelT’s suggestion sounds reasonable.