Page 1 of 9

atomic web server threads

Posted: Tue Mar 12, 2024 4:43 am
by idle
A simple modification to the atomic webserver to thread processing
provides a simple way to host static or dynamic web content or spider basic apps
PB 6.03 - 6.20 windows
https://github.com/idle-PB/atomic_webserver_3

test site
https://atomicwebserver.com

version 3.1.0b15 6.03-6.20 uses PB TLS libs 6.20 or 6.03-6.12 uses the TLS Dlls
version 3.1.1b3 6.20 only uses native TLS but no reverse proxy

version 3.1.0b12 PB 6.12 reverse proxy working
version 3.1.1b1 PB 6.20 reverse proxy broken

version 3.1.0b9
Added TLS support to reverse proxy

version 3.1.0b7
build libreTLs 3.9.2 windows x86 tls.dll, ssl.dll, crypto.dll
fixed bug in tls.pbi that was preventing x86 to bind the tls context

version 3.1.0b6
fixed up receive to handle posts larger than buffer

version 3.1.0b5
moved the logging
fixed a time out bug in send
added cache age on init, uri handlers and preprocessed pages are un cached.
pages with .pbh or .htm are passed to the preprocessor
added option to SetCookie to create a session cookie with bRandom flag uses CryptRandomData sha2

version 3.1.0b3
fixed close connection on no data received

Version 3.1.0b2
Added the virtual file system, this is pretty much it feature wise and can now undergo refinements. The virtual file system will eventually be replaced with a streamlined version if and when time allows.
example of use is in single_server_localhost.pb repo has prepacked site in the .ezp

Version 3.0.10b1
Reverted to single threads per client as multiple request threads per client were getting into contention on linux and causing a high cpu load.

Version 3.0.9b1
Added TLS libreSSL
windows x86 / x64
linux x64
ssl_server.pb example shows how to use it.

v3.0.8b3
Added header fields and cookies
Request header fields are read via map *request\RequestHeaders()
you can also add additional client Response headers via Atomic_Server_SetResponceHeader(*request.Atomic_Server_Request,key.s,value.s)

Cookies are extracted and mapped to *client\Cookies()
you can set cookies in a callback via
Atomic_Server_SetCookie(*request.Atomic_Server_Request,Cookie.s,value.s)

The cookies are available in callbacks to GET POST URIHandlers and Preprocessor functions

v 3.0.8b2
added deflate support

v 3.0.8b1
added reverse proxy
examples
test site

v 3.0.7b1
Added Tag Preprocessor
no example of it as yet, as it assumes you have an html file with .pbh extension but it works fine
it calls runtime procedures so you can fill in content in an html file without the need for server side scripts

v 3,0,6b4
fixed thread clean up

v 3.0.6b3
changed scope of lock, increased buffer size

v 3.0.6b2
adjusted buffer size and redid mutex

v.3.0.6b
tarted up landing pages added chr function and favico

v.3.0.5b
rearranged a bit and added persistence for clients with timeout in preparation for SSL, clients can still spawn multiple threads, might add a thread pool later but it's still a dumb spawn a request thread so the servers can deal with multiple clients making multiple async requests.

v3.0.5a
bug fix, seems I was missing #PB_File_SharedRead on file access, despite being led on a wild goose chase :oops:
not to self make sure #PB_File_SharedRead is set on threaded file access.

v3,0.4b
removed internal windows.

v3.04a
changed so you can run multiple servers with IPV4 or IPV6

v3.0.3
added own window option

v3.0.3b
Added post support
with post and get callbacks

v3.0.2
Added Index and error handlers
v3.0.1
Added mime types
v3.0
Added URI handler currently only gets are supported

you add them as
URIHandlers("foo")\pt = @URIfoo()
and then you can navigate to
http://127.0.0.1/foo?foo=1234&bar=5678

Re: atomic web server threads

Posted: Tue Mar 12, 2024 12:36 pm
by skywalk
Thanks! I'll try this out.

Re: atomic web server threads

Posted: Tue Mar 12, 2024 2:39 pm
by skinkairewalker
I'm encountering somewhat "strange" behavior...
I tried to load an application made in spiderbasic, but it keeps loading infinitely.

example : https://mega.nz/file/u8pVSJRb#J_Uylw9Ic ... 08eFsi-keg

Re: atomic web server threads

Posted: Tue Mar 12, 2024 2:53 pm
by useful
skinkairewalker wrote: Tue Mar 12, 2024 2:39 pm ...
I tried to load an application made in spiderbasic, but it keeps loading infinitely.
...
I have the same behavior

Re: atomic web server threads

Posted: Tue Mar 12, 2024 3:14 pm
by dige
tried it with a simple index.html ... same problem

Re: atomic web server threads

Posted: Tue Mar 12, 2024 3:36 pm
by useful
dige wrote: Tue Mar 12, 2024 3:14 pm tried it with a simple index.html ... same problem
With a simple index.html if you put it in a folder ./www is working

Re: atomic web server threads

Posted: Tue Mar 12, 2024 9:07 pm
by idle
fixed so it will warn you if no www directory exists

Re: atomic web server threads

Posted: Tue Mar 12, 2024 9:26 pm
by idle
skinkairewalker wrote: Tue Mar 12, 2024 2:39 pm I'm encountering somewhat "strange" behavior...
I tried to load an application made in spiderbasic, but it keeps loading infinitely.

example : https://mega.nz/file/u8pVSJRb#J_Uylw9Ic ... 08eFsi-keg
The issue is the variable ?t=1710106237" in the index page loading the spiderbasic.js
<script type="text/javascript" src="spiderbasic.js?t=1710106237"></script>

I haven't added parameter support or any checks for them yet

Try this index,html and it should load

Code: Select all

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="user-scalable=no, width=device-width, height=device-height, initial-scale=1, minimum-scale=1, maximum-scale=1, shrink-to-fit=no">

<script type="text/javascript">var spider = {}; spider.nbModules = 1; spider.nbLoadedModules = 0;</script>

<title>SpiderApp</title>

<script type="text/javascript">function onLoad() { spiderCheckBrowser(); }</script>
<script type="text/javascript" src="libs/platform.js"></script>
<script type="text/javascript" src="libs/init.js"></script>
<script type="text/javascript">dojoConfig.baseUrl = 'libs/'; </script>
<script type="text/javascript" src="libs/dojo/dojo.js"></script>
<script type="text/javascript" src="libs/main.js"></script>
<script type="text/javascript" src="libs/debug.js"></script>
<link rel="stylesheet" href="libs/dojo/themes/flat/flat.css" />
<link rel="stylesheet" href="libs/dojo/dgrid/css/dgrid.css" />
<script type="text/javascript" src="libs/xdate.min.js"></script>
<link rel="stylesheet" title="Default" href="libs/themes/flat/window.css" type="text/css"/>
<script type="text/javascript" src="spiderbasic.js"></script>

</head>

<body oncontextmenu="return false;" class="flat" id="spiderbody" onload="onLoad()">

</body>
</html>

Re: atomic web server threads

Posted: Wed Mar 13, 2024 1:40 am
by idle
Added a URIhandler

so you add them as
URIHandlers("foo")\pt = @URIfoo()
and then you can navigate to
http://127.0.0.1/foo?foo=1234&bar=5678

Code: Select all

;-Example URIHandler 
Procedure URIfoo(*request.client,*contentType) 
  
  ForEach *request\parameters() 
    Debug MapKey(*request\parameters()) + " " + *request\parameters() 
  Next 
  
  Protected *data
  Protected sout.s = "<!DOCTYPE html><html><head><title>Atomic Web Server</title></head><title>Atomic Web Server</title></head>" 
  sout + "<body><h1 style='text-align:center';>Atomic Web Server</h1>"
  ForEach *request\parameters() 
    sout + "<p style='text-align:center'><font color='red'>" + MapKey(*request\parameters()) + "=" + *request\parameters() +"</p>" 
  Next 
  sout + "<body></html>"
  
  *data = UTF8(sout) 
  
  PokeS(*contentType,"text/html") 
  
  ProcedureReturn *data  
  
EndProcedure   

URIHandlers("foo")\pt = @URIfoo() 


Re: atomic web server threads

Posted: Wed Mar 13, 2024 12:12 pm
by Caronte3D
Nice, thanks! :D

Re: atomic web server threads

Posted: Wed Mar 13, 2024 4:19 pm
by skinkairewalker
awesome ! works fine .

Re: atomic web server threads

Posted: Wed Mar 13, 2024 9:54 pm
by idle
Added Index and Error page handlers so if there's no index.html or error.html it will default to the handlers


Todo
POST support
Turn into a module

under consideration, add serve from virtual directory as an option, this will require a bit of thought to make it an optional dependency but offers a very useful way to pack you assets into a datasection which is exactly the same as the file system

Re: atomic web server threads

Posted: Thu Mar 14, 2024 4:05 am
by idle
hacked in Post support, still need to test post in uri handlers
but added two callbacks for post and gets

Code: Select all


;-Example URIHandler for dynamic generated content  
Procedure URIfoo(*request.Atomic_Server_client,*contentType) 
    
  Protected *data
  
  Debug *request\Request
  
  Protected sout.s = "<!DOCTYPE html><html><head><title>" + Atomic_Server\Title +"</title></head><title>" + Atomic_Server\Title + "</title></head>" 
  sout + "<body><h1 style='text-align:center';>Atomic Web Server</h1>"
  ForEach *request\parameters() 
    sout + "<p style='text-align:center'><font color='red'>" + MapKey(*request\parameters()) + "=" + *request\parameters() +"</p>" 
  Next 
  sout + "<body></html>"
  
  *data = UTF8(sout) 
  
  PokeS(*contentType,"text/html") ;Set the contentType
  
  ProcedureReturn *data  
  
EndProcedure   

;-Example Post Callback
Procedure CBPost(*request.Atomic_Server_client)
    
  Debug "POST " + *request\RequestedFile 
  
  ForEach *request\parameters() 
    Debug MapKey(*request\parameters()) + "=" + *request\parameters()  
  Next 
  
EndProcedure   

;-Example GET Callback
Procedure CBGet(*request.Atomic_Server_client)
    
  Debug "GET " + *request\RequestedFile 
  
  ForEach *request\parameters() 
    Debug MapKey(*request\parameters()) + "=" + *request\parameters()  
  Next 
  
EndProcedure   

Atomic_Server_Init("MyAtomic_Webserver",80,"www/",@CBPost(),@CBGet())
Atomic_Server_Add_Handler("foo",@URIfoo()) ;navigate to http://127.0.0.1/foo?foo=1234&bar=5678
Atomic_Server_start()



Re: atomic web server threads

Posted: Thu Mar 14, 2024 3:22 pm
by skinkairewalker
awesome !!! :mrgreen: :mrgreen: :mrgreen: :mrgreen:

Re: atomic web server threads

Posted: Fri Mar 15, 2024 8:14 am
by PBJim
idle wrote: Tue Mar 12, 2024 9:07 pm fixed so it will warn you if no www directory exists
This didn't happen for me just now — I created an index.html file in the folder of the server, which the server ignored, although its status window showed "Server Running on port 80" and then "Client IP x.x.x.x load index.html client 33984320", suggesting it had found the file "index.html", but in fact it hadn't.

When I realised my mistake and put the index.html file inside a www sub-folder, it worked, but I didn't see an error beforehand.