Page 1 of 1

HTTP server in Gambas with

Posted: Monday 28th December 2020 2:21pm
by ocoquet
Hi all from France

For my personal use, I decided to publish web pages from Gambas without any external server.

For this, I have explored and I see a sample use for server socket.

Here is my question: why nobody use to incorporate a simple http server directly in Gambas code ?

This seem to work perfectly (after corrected some error in sample code :) )


Re: HTTP server in Gambas with

Posted: Tuesday 29th December 2020 9:20am
by ocoquet
Hi from France,

If somebody is interested, I've wrote a class file :idea: in Gambas for http server.
It can be incorporated in a program and start just with a single instantiation.
Actually, it can:

Listen on a specified port
Accept requests (http 1.0)
Serv html pages which are in a specified directory (pages can incorporate images).
Accept parameters from FORM and store them in a string array for future usage.


Re: HTTP server in Gambas with

Posted: Wednesday 30th December 2020 9:27am
by ocoquet
Hi All from France,
Moved to Project showcase....thanks

Here it is my first version of httpd class to create an http minimal server with Gambas basic (v3.15.2)
To run, you must add component in the project components properties.
For example, this class is given with a minimalist form which display arguments from a form.
This server is approximately conform to http1.0 rules, except in response header which is always ok200 !

There is no default index page, the first page must be given in the url, in the sample given the url is:
without rerouting port:
With rerouting port:

As you can see, there is a html page with link to a ...gbp page, *.gbp are specials pages which can be modified by your program.
All pages are situated in the web-docs folder in the application folder.
Port listened is 1200 but you can change it in public var in header of class file.
If you want to listen on port 80, I Re-command you to use iptable to reroute port 80 to 1200, port 1 to 1024 are root reserved.

Below are the shell command to do this:
To make Port 80 routing to port 1200 (external):
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 1200
To Make port 80 routing to port 1200 (internal ex: localhost):
sudo iptables -t nat -I OUTPUT -p tcp -d --dport 80 -j REDIRECT --to-ports 1200

If You use it, try to let the class file intact and make modification page function in your main program.

Advantages; Run on a fresh linux with gambas basic installed without other programs.

All suggestions are welcome.

Enjoy and Happy new year !!!!

Re: HTTP server in Gambas with

Posted: Friday 01st January 2021 6:11pm
by ocoquet
Version 0.0.11 (Happy New Year From France :D )
Now moved in projects forum.

Last Changes:

- if "er404.html" exist, server answer this page when requested page is not found, else a simple message ERREUR 404 is answered
- if no page is requested and index.html exist, server send this page as response, else ERREUR 404 is answered.
- Header "cache-control" is added for better contrôle of navigator cache ( public var for cache-contrôle is added in head of class file).
- if file end with .gbp, server load the page, give the control to master program function with page(string) and parameters (array of string) before answering. This give a chance to modify page programmatically before sending it to navigator. This is something like php... :P


Re: HTTP server in Gambas with

Posted: Saturday 09th January 2021 1:40pm
by BruceSteers

Thanks for the example and for sharing :)

I don't have a use for it sorry so i have not tried it.
( I assume the same for most other people here as you have had no replies)

But Benoit is interested so keep at it and we may find it to be a part of gambas one day :)

BruceS :)

Re: HTTP server in Gambas with

Posted: Sunday 10th January 2021 12:23pm
by ocoquet
Reproduction of ML message sent today by me:
HTTP Gambas Server Class v0.0.15


I take the time to give you some information about my Gambas http server.

Originally, I wrote this class to make my home automation program accessible from a tablet or a smartphone.
Some researches had told me that to achieve this, I needed to install a web server, the php module and some other stuff. I also had to configure all this to work together. Last but not least, all these installations were dependent on linux distribution and therefore not very easily transportable.
So I decided to write something simple, easily transportable and above all not dependent on anything but Gambas itself.
That's how this http server class was born.
The first versions only accepted browser connections and sent a pure html page, but as time went by and needs changed, I enriched it with more advanced features: get requests, *.gbp pages equivalent to php etc.
Today, the class can answer GET and POST requests, redirect pages, call a procedure in the main program when a *.gbp page is requested, manage encrypted logins and passwords, manage, through cookies, the validity of a session, etc....
Due to its relatively simple principle, any program can become a "communicating web" by just copying and pasting the class, creating a processing procedure in the main program and instantiating the class.
I've added some examples in the web-docs folder in which I demonstrate the verification of a login and password, the verification of the validity of a session if the page is protected, the sending and processing of information entered in a form, etc....

Do not hesitate to ask me if you are interested in additional functions.

For your information, this class is the first one I am writing, 25 days ago I had no idea how classes work, a demonstration of how simple Gambas Basic is.
Maybe the next step will be to transform all this into a component if I can do it and understand how to build a component....
Friendship to all
Olivier Coquet.

PS: excuse me for English, i'm French and as all French peoples, I speak English "like a Spanish caw", this page is a translation made with deepl :)

Translated with (free version)