• Advertisement
Sign in to follow this  

C++ Apache HTTP server?

This topic is 3458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, I'm trying to build a HTTP server using libCURL. I find the tutorials quite lacking (or I haven't found the tutorial pages...!), so I was wondering if anyone had a very quick example code which would return text if someone accesses the server (using http://localhost for example). Or could anyone link me to any resource site explaining the use of cURL and how one could build a server using it. I've compiled it successfully, and a download script (client-side) which I found on these forums works excellent... Thanks! EDIT: or would there be a better library for this? [Edited by - Decrius on August 4, 2008 1:05:17 PM]

Share this post


Link to post
Share on other sites
Advertisement
libCURL is client-side, not server-side. It is not suitable for implementing an HTTP server.
Quote:
EDIT: or would there be a better library for this?
Try Apache.

Share this post


Link to post
Share on other sites
A HTTP server is TCP/IP connection handler. If you can write TCP/IP server, you can write HTTP server.

HTTP in itself is documented, and very mundane. What is of interest to you is the document encoding, and request/response generation.

A trivial implementation of this functionality can be found as boost asio example.

You could also look at libcURL, to see how it handles the request/response, and then reverse that.

But main problem with lack of tutorials really is in the fact that the process is incredibly boring, and, if it is intended to be fully HTTP compliant, fairly extensive.

Share this post


Link to post
Share on other sites
Aye, had a feeling for that.

Would like to use boost.asio, looks like it has some nice examples :)
Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by Decrius
Would like to use boost.asio, looks like it has some nice examples :)
Thanks!

Seriously, use Apache unless you have a damn good reason not to. Writing a barebones HTTP server from scratch is simple; writing a good one is very hard.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by Decrius
Would like to use boost.asio, looks like it has some nice examples :)
Thanks!

Seriously, use Apache unless you have a damn good reason not to. Writing a barebones HTTP server from scratch is simple; writing a good one is very hard.


True, but I saw some HTTP examples over there, but they give very weird compiling errors.

How would I use Apache? Is it a library, or should I run it and connect to it using C/C++? I'm really looking for a small server...

Or could I somehow write an extension to Apache? How does PHP do this? *confused*

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Try Apache.
This may sound like a stupid advice at first, but seriously... do it! It's the absolutely best advice.
Apache is something that "just works", and it works really well. There may be one or the other solution that performs a bit better in some special cases, but so what. Apache works, fullstop.

On what is considered low-low-end hardware today, Apache will physically saturate the ethernet cable with ease, unless you do a dozen database queries and excessive scripting for every page hit. And if you do that, other solutions won't be much faster either.

To use Apache, you install it on your server, tweak a few configuration settings, and launch the server process. That's it.
If you need something else than static pages, you can write CGI/fastCGI apps, or you could just use PHP or some other scripting language. PHP is surprisingly fast, more than one would expect (especially when using an accelerator/cache).

Share this post


Link to post
Share on other sites
Quote:
Original post by samoth
Quote:
Original post by Sneftel
Try Apache.
This may sound like a stupid advice at first, but seriously... do it! It's the absolutely best advice.
Apache is something that "just works", and it works really well. There may be one or the other solution that performs a bit better in some special cases, but so what. Apache works, fullstop.

On what is considered low-low-end hardware today, Apache will physically saturate the ethernet cable with ease, unless you do a dozen database queries and excessive scripting for every page hit. And if you do that, other solutions won't be much faster either.

To use Apache, you install it on your server, tweak a few configuration settings, and launch the server process. That's it.
If you need something else than static pages, you can write CGI/fastCGI apps, or you could just use PHP or some other scripting language. PHP is surprisingly fast, more than one would expect (especially when using an accelerator/cache).


Thanks for the advice. I'm willing to use Apache, but I wonder how I ever could use C to send and receive data from a client. I simply want to 'replace' PHP in this case, I should be able send HTML and receive GET and POST values. Apache would be THE solution, since I can use Mod Rewrite then aswell.

So, does anyone have an example of it? Or a resources site which describes how one can connect to apache?

Share this post


Link to post
Share on other sites
What you are looking for is CGI or fastCGI. It will let you use just about any language out there to do web programming. try googling cgi c++

Share this post


Link to post
Share on other sites
Quote:
Original post by Decrius
Quote:
Original post by Sneftel
Quote:
Original post by Decrius
Would like to use boost.asio, looks like it has some nice examples :)
Thanks!

Seriously, use Apache unless you have a damn good reason not to. Writing a barebones HTTP server from scratch is simple; writing a good one is very hard.


True, but I saw some HTTP examples over there, but they give very weird compiling errors.

How would I use Apache? Is it a library, or should I run it and connect to it using C/C++? I'm really looking for a small server...

Or could I somehow write an extension to Apache? How does PHP do this? *confused*
The latter. As for how to do it, there's a few ways. The simplest is to make a CGI program, referred to by the Apache configuration; Apache receives a request, passes it along to your program, and your program responds with whatever data you want. It really is as simple as that. Since CGI programs are just regular programs, you can write them in whatever language you like.

For greater efficiency and control over the server, you can write an Apache module which is invoked directly. See the Apache documentation for details. If you start out with a CGI program, it's straightforward to transition to a module later if needed, so don't bother with the module stuff right out of the gate.

Share this post


Link to post
Share on other sites
It can be added that writing Apache modules is really nice and easy, much thanks to the frameworks involved. If you download the Apache source code, you'll probably find samples named mod_hello or mod_cpphello, which IIRC outputs data directly to the client.

Share this post


Link to post
Share on other sites
I'm still not quite sure what your web server part is supposed to do.

Do you already have a server, and you want to connect to it to get statistics and set management information? Then turning your server into an Apache module is not the right answer. Hack your own in 200 lines and call it good.

Do you want to set up a server for a web-page game? If so, I suggest using Apache, or some other web server, and writing the game itself in Flash or Java.

Do you want to write a client/server application, and use HTTP for transport for network communications? If so, I suggest using an application server, or Java servlet server, such as Tomcat or one of the J2EE servers.

Share this post


Link to post
Share on other sites
hplus, I'm looking into a way where I can replace PHP. I want to do a few tests to see if using a native language will make much difference, I'm also looking into a way where I can use simply put up a game server which allows http connections as well (for configuring in-game things). Well...I don't really have a goal, I simply want to get known with HTTP server building...using C++ to send HTML to the client...:)

I think Apache would be nice, I've also downloaded a simply Win32 socket HTTP server class, it works very nice and uses forms and authentication etc. But it's windows only, while Apache is cross platform.

CGI, looked into that, could not get it to work when I quickly tried it :S, I'll see how building a module goes :).

Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by Decrius
hplus, I'm looking into a way where I can replace PHP.


A HTTP server is passive, when it receives an HTTP request, it generates some response. While various push technologies have been developed, today's AJAX uses same concept. Even though it appears async, server still merely responds to queries.

As such, almost all application frameworks operate on exactly the same concept. You implement some function, which receives a Request object. Your code then produces a Response, and optionally writes something to the stream.

PHP does just that. The URL identifies the script, which is then executed, and resulting text is sent to client.

Quote:
which allows http connections as well (for configuring in-game things).


Here you will run into problems if you try to do it yourself. While various performance improvements were added to HTTP (re-use of connections instead of one connection per request), HTTP is still session-less. As such, various techniques need to be used to keep track of which HTTP connection belongs to which client. After taking into consideration that browsers are free to open multiple connections, that they may time-out or expire, and that there's several options of how to track users (cookies, URL-encoded), as well as possible browser incompatibilities, this really is a task that's by far best suited for any given Web App framework.

Quote:
Well...I don't really have a goal, I simply want to get known with HTTP server building...using C++ to send HTML to the client...:)


1) Write a TCP/IP server than can handle multiple clients
2) Read the HTTP documentation on the requests (GET, later POST), the URL and MIME encodings
3) Static HTTP server then accepts a connection, and reads the request fully.
4) Extract the URL from request, and map it to file system (www.example.com/foo/bar.txt would map to /home/myself/wwwroot/foo/bar.txt)
5) On success, load the file from disk, send it to client (may need proper MIME encoding) formatted as response
6) On failure, see the documentation on what to send

Quote:
I think Apache would be nice, I've also downloaded a simply Win32 socket HTTP server class, it works very nice and uses forms and authentication etc. But it's windows only, while Apache is cross platform.


If you haven't yet, install apache, set up Wiki or some other web app, and mess around with that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement