How to write crosslatform HTTP Server in Juce?


#1

I want to write my own HTTP server. Should I use the StreamingSocket or InterprocessConnectionServer? Actually I need to have a function, which receives all get and post data and can transfer text data to display in a client browser.

Is there any example of the simplest http server implementation in Juce (so it would be crossplatform like all other Juce programs), or can you give me any advice on it? I tried to, but was lost in different socket classes… :?

I guess I need to create a class HttpServer. Should it inherit any other socket-based juce class?

I think if we could create a good HttpServer class, it may be helpful for all folks :slight_smile: And who knows, if we follow all Juce rules strictly, Julian may add it to the main Lib compilation (or at least to the “juce demo” :roll: )


#2

How about a website! That’s as cross platform as you can get.

  1. Get some website hosting for as low as $4 a month (Google It!).
  2. Make a website.
  3. Call it from a JUCE app using the JUCE URL class.

#3

I have a dedicated server for a long time, with tens of hosted websites.

I mean a local http server written in C++ using juce. The program which listens on port 80 and responses to the requests from browser, say, Firefox, with “http://localhost:80/12345” URL.


#4

Sounds like an interesting project, though my knowledge of http isn’t really deep enough to make any good suggestions here!


#5

Well, I think I’ve got something to show you :slight_smile:

Based purely on Juce lib, I have tested in on windows only, gonna test on Ubuntu, then I’ll publish it here.


#6

Merry Christmas, Happy Hanukkah and Happy New Year to everybody :smiley:

Here is a class HttpServer.

All you need to do is to inherit this class and implement the function processHttpRequest. This function is called when browser requests a page from your server. For example:

class MyCoolServer: public HttpServer{ public: void processHttpRequest(HttpServerRequest *R){ R->echo(T("<html><head><title>Hi from the Cool Server!</title></head>")); R->echo(T("<body><b>Cool Server</b> is ready to serve you!</body>")); } };
To use youe class, do the following:

...
private:
   MyCoolServer the_server;
public:
   void buttonClicked(Button* buttonThatWasClicked){
		 if(buttonThatWasClicked == startHttpdButton){
				the_server.setup(
							80 /* or any other port*/, 
							"Cool Server" /* the server name is required, it is sent to browser in headers */
						);
				the_server.startServer();
		 }
	}

Once it is started, you can visit “http://localhost:XX/path.html”, where XX is a given port (not required if you use the default HTTP port 80).

Server can prepare path, host, parses GET and POST vars (text only).

This server lacks:

  • ability to send binary files to the browser (images, audio, etc)
  • hanging audio/video streams
  • sending streams
  • parse incoming binary data (file POST)
  • and many more…

But now it is easy to add these functions, the hardest part is done :wink:
I am going to do some of them and publish it here.

Files:
juce_HttpServer.h - the base class for your server
SomeHttpServer.h - a simple example of a server implementation. Demonstrates some server capabilities.
SomeHttpServer.exe - compiled Windows binary.

WARNING: The class I wrote is just an example of how to create a web server using Juce. It is not aimed to handle hard loads or to be secure. Please don’t install if for a public access on your hosted or dedicated machines. For these purposes you have Apache, IIS, nginx and other mature http servers.

Use this class, for a local desktop purposes or as an example for your own web server, written from scratch.