HTTP server in Gambas with gb.net

For questions about Gambas web tools.
Post Reply
ocoquet
Posts: 36
Joined: Friday 18th December 2020 7:56am

HTTP server in Gambas with gb.net

Post 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 gb.net and I see a sample use for server socket.

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

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

Regards
Olivier
Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
ocoquet
Posts: 36
Joined: Friday 18th December 2020 7:56am

Re: HTTP server in Gambas with gb.net

Post 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.

Regards
Olivier
Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
ocoquet
Posts: 36
Joined: Friday 18th December 2020 7:56am

Re: HTTP server in Gambas with gb.net

Post 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 gb.net 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:
http://localhost:1200/test.html
With rerouting port:
http://localhost/test.html

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 127.0.0.1 --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 !!!!
Regards
Olivier
Last edited by ocoquet on Monday 4th January 2021 4:58pm, edited 1 time in total.
Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
ocoquet
Posts: 36
Joined: Friday 18th December 2020 7:56am

Re: HTTP server in Gambas with gb.net

Post 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


Regardsr
Olivier
Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
User avatar
BruceSteers
Posts: 1884
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: HTTP server in Gambas with gb.net

Post by BruceSteers »

Interesting..

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 :)
If at first you don't succeed , try doing something differently.
BruceS
ocoquet
Posts: 36
Joined: Friday 18th December 2020 7:56am

Re: HTTP server in Gambas with gb.net

Post by ocoquet »

Reproduction of ML message sent today by me:
HTTP Gambas Server Class v0.0.15

Hello,

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 www.DeepL.com/Translator (free version)
Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
Jo.2a
Posts: 1
Joined: Sunday 6th August 2023 9:44am

Re: HTTP server in Gambas with gb.net

Post by Jo.2a »

Hello
where ti is possible to download your good example ?
merci
Jo.2a (From France)
Post Reply