Everything on Socket API now

Drop old Network Code
Rename SSL/TLS to HTTPS
Rename default server to HTTP
And other smal fixes
This commit is contained in:
René Linder 2020-11-28 16:49:22 +01:00
parent 4f81308f22
commit af15bd9dc6
4 changed files with 113 additions and 140 deletions

View file

@ -1,10 +1,12 @@
lweb HTTP & HTTPS Webserver lweb HTTP & HTTPS Webserver
TODO: TODO List:
Switch all Network operations to Standard API DONE: Switch all Network operations to Standard API
POST/PUT with bigger Files and or Contents POST/PUT with bigger Files and or Contents
Sending of bigger Files (Currently everithing in Memory and then send) Sending of bigger Files (Currently everithing in Memory and then send)
error400 handling error400 handling
error Code handling (Different errorcodes and responses) error Code handling (Different errorcodes and responses)
Translate Remarks to english Translate Remarks to english
Translate README.md to english Translate README.md to english
Optimizing Code away from old PureBasic Network Code.
Cleanup unused Variables

201
lweb.pbi
View file

@ -40,13 +40,13 @@ Module lhs_web
Global.i conf_cache_maxsize = 1 ;1 Mbyte Global.i conf_cache_maxsize = 1 ;1 Mbyte
Global.i conf_cache_current = 0 Global.i conf_cache_current = 0
Global.i conf_cache_enable = 0 Global.i conf_cache_enable = 0
Global.i conf_TLS_Port = 8443 Global.i conf_HTTPS_Port = 8443
Global.s conf_TLS_binding = "127.0.0.1" Global.s conf_HTTPS_binding = "127.0.0.1"
Global.i conf_TLS_enable = 0 ;SSL Server Global.i conf_HTTPS_enable = 0 ;SSL Server
Global.s conf_TLS_CA = "" Global.s conf_HTTPS_CA = ""
Global.s conf_TLS_Cert = "" Global.s conf_HTTPS_Cert = ""
Global.s conf_TLS_Key = "" Global.s conf_HTTPS_Key = ""
Global.s conf_TLS_Key_Pass = "" Global.s conf_HTTPS_Key_Pass = ""
Enumeration s_client_do ;client_do_cli Enumeration s_client_do ;client_do_cli
#CLI_DO_NOP ;Keine Arbeit #CLI_DO_NOP ;Keine Arbeit
@ -122,8 +122,10 @@ Module lhs_web
#get_handler_prototype ;Welcher Prototype #get_handler_prototype ;Welcher Prototype
EndEnumeration EndEnumeration
Global.i count_SSL_client Global.i count_HTTP_client
Global.i server_id Global.i count_HTTPS_client
Global.i server_HTTP_id
Global.i server_HTTPS_id
Global.i server_mutex = CreateMutex() ;Dieser Mutex dient zu der Sicherheit der Element Liste. Global.i server_mutex = CreateMutex() ;Dieser Mutex dient zu der Sicherheit der Element Liste.
Global.i file_cache_mutex = CreateMutex() Global.i file_cache_mutex = CreateMutex()
Global.i file_cache_semaphore = CreateSemaphore() Global.i file_cache_semaphore = CreateSemaphore()
@ -150,7 +152,8 @@ Module lhs_web
;* ;*
Declare server(network_server_id.i) Declare server(network_server_id.i)
Declare server_SSL(network_server_id.i) Declare server_HTTP(network_server_id.i)
Declare server_HTTPS(network_server_id.i)
Declare client(network_client_id.i) Declare client(network_client_id.i)
Declare AddFileToCache(MemoryID.i, FileName.s, Size.i) Declare AddFileToCache(MemoryID.i, FileName.s, Size.i)
Declare FileCache(Information.i) Declare FileCache(Information.i)
@ -166,8 +169,8 @@ Module lhs_web
Select parameter Select parameter
Case #conf_port Case #conf_port
conf_port = Val(setting) conf_port = Val(setting)
Case #conf_TLS_Port Case #conf_HTTPS_Port
conf_TLS_Port = Val(setting) conf_HTTPS_Port = Val(setting)
Case #conf_binding Case #conf_binding
If IsIPStringValid(setting) If IsIPStringValid(setting)
conf_binding = setting conf_binding = setting
@ -175,23 +178,23 @@ Module lhs_web
conf_binding = "127.0.0.1" conf_binding = "127.0.0.1"
ProcedureReturn #False ProcedureReturn #False
EndIf EndIf
Case #conf_TLS_Binding Case #conf_HTTPS_Binding
If IsIPStringValid(setting) If IsIPStringValid(setting)
conf_binding = setting conf_binding = setting
Else Else
conf_binding = "127.0.0.1" conf_binding = "127.0.0.1"
ProcedureReturn #False ProcedureReturn #False
EndIf EndIf
Case #conf_TLS_CA Case #conf_HTTPS_CA
conf_TLS_CA = setting conf_HTTPS_CA = setting
Case #conf_TLS_Cert Case #conf_HTTPS_Cert
conf_TLS_Cert = setting conf_HTTPS_Cert = setting
Case #conf_TLS_Key Case #conf_HTTPS_Key
conf_TLS_Key = setting conf_HTTPS_Key = setting
Case #conf_TLS_Key_Pass Case #conf_HTTPS_Key_Pass
conf_TLS_Key_Pass = setting conf_HTTPS_Key_Pass = setting
Case #conf_TLS_Enable Case #conf_HTTPS_Enable
conf_TLS_enable = Val(setting) conf_HTTPS_enable = Val(setting)
Case #conf_defaultfile Case #conf_defaultfile
conf_defaultfile = setting conf_defaultfile = setting
Case #conf_basedir Case #conf_basedir
@ -229,8 +232,8 @@ Module lhs_web
ProcedureReturn Str(conf_server_type) ProcedureReturn Str(conf_server_type)
Case #conf_port Case #conf_port
ProcedureReturn Str(conf_port) ProcedureReturn Str(conf_port)
Case #conf_TLS_Port Case #conf_HTTPS_Port
ProcedureReturn Str(conf_TLS_Port) ProcedureReturn Str(conf_HTTPS_Port)
Default Default
ProcedureReturn "" ProcedureReturn ""
EndSelect EndSelect
@ -240,15 +243,14 @@ Module lhs_web
Global NewMap m_clients.s_clients(conf_max_clients) Global NewMap m_clients.s_clients(conf_max_clients)
Protected tlsresponse.i Protected tlsresponse.i
;TODO: Create SSL Server ;Init HTTP Socket
server_HTTP_id = lsocket::CreateSocket(conf_port, conf_max_clients, lsocket::#SOCK_STREAM, lsocket::#AF_INET, conf_binding)
server_id = CreateNetworkServer(#PB_Any, conf_port, #PB_Network_TCP, conf_binding) If conf_HTTPS_enable = 1
If conf_TLS_enable = 1 server_HTTPS_id = lsocket::CreateSocket(conf_HTTPS_Port, conf_max_clients, lsocket::#SOCK_STREAM, lsocket::#AF_INET, conf_HTTPS_binding)
server_SSL_id = lsocket::CreateSocket(conf_TLS_Port, conf_max_clients, lsocket::#SOCK_STREAM, lsocket::#AF_INET, conf_TLS_binding) If server_HTTPS_id
If server_SSL_id tlsresponse = ltls::InitSimpleTLS(conf_HTTPS_CA, conf_HTTPS_Cert, conf_HTTPS_Key, conf_HTTPS_Key_Pass)
tlsresponse = ltls::InitSimpleTLS(conf_TLS_CA, conf_TLS_Cert, conf_TLS_Key, conf_TLS_Key_Pass)
If tlsresponse > 0 If tlsresponse > 0
CreateThread(@server_SSL(), server_SSL_id) CreateThread(@server_HTTPS(), server_HTTPS_id)
Else Else
Debug "TLS Fehler:"+Str(tlsresponse) Debug "TLS Fehler:"+Str(tlsresponse)
ProcedureReturn #False ProcedureReturn #False
@ -258,9 +260,8 @@ Module lhs_web
ProcedureReturn #False ProcedureReturn #False
EndIf EndIf
EndIf EndIf
If server_HTTP_id
If server_id CreateThread(@server_HTTP(), server_HTTP_id)
CreateThread(@server(), server_id)
ProcedureReturn #True ProcedureReturn #True
Else Else
ProcedureReturn #False ProcedureReturn #False
@ -268,31 +269,30 @@ Module lhs_web
EndProcedure EndProcedure
Procedure server_SSL(network_server_id.i) Procedure server_HTTPS(network_server_id.i)
;************************** ;**************************
;* ;*
;* Hauptthread welcher den Netzwerkport verwaltet und den Datenfluss. ;* Main thread for HTTPS networksocket management. Create the Client Threads.
;* Erstellt die Subthreads für Clients
;* ;*
Protected.i client_id Protected.i client_id
thread_alive = #True thread_alive = #True
Debug "TLS Server Started." Debug "HTTPS Server Started."
Repeat Repeat
client_id = ltls::WaitTLSSocket(network_server_id) client_id = ltls::WaitTLSSocket(network_server_id)
Debug "New TLS Client:"+Str(client_id) Debug "New HTTPS Client:"+Str(client_id)
If client_id > 0 If client_id > 0
If count_client < conf_max_clients If count_HTTPS_client < conf_max_clients
m_clients(Str(client_id))\client_id = client_id m_clients(Str(client_id))\client_id = client_id
m_clients(Str(client_id))\client_do_cli = #CLI_DO_WaitDataReceive m_clients(Str(client_id))\client_do_cli = #CLI_DO_WaitDataReceive
m_clients(Str(client_id))\client_do_srv = #SRV_DO_DataReceive m_clients(Str(client_id))\client_do_srv = #SRV_DO_DataReceive
m_clients(Str(client_id))\client_ssl = 1 m_clients(Str(client_id))\client_ssl = 1
;Thread erstellen ;Thread erstellen
m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id) m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id)
count_client + 1 count_HTTPS_client + 1
Else Else
Debug "Max TLS Clients reached..." Debug "Max HTTPS Clients reached..."
ltls::CloseTLSSocket(client_id) ltls::CloseTLSSocket(client_id)
EndIf EndIf
Else Else
@ -302,85 +302,37 @@ Module lhs_web
ltls::CloseTLS(network_server_id) ltls::CloseTLS(network_server_id)
EndProcedure EndProcedure
Procedure server_HTTP(network_server_id.i)
Procedure server(network_server_id.i)
;************************** ;**************************
;* ;*
;* Hauptthread welcher den Netzwerkport verwaltet und den Datenfluss. ;* Main thread for HTTP networksocket management. Create the Client Threads.
;* Erstellt die Subthreads für Clients
;* ;*
Protected.i network_event , client_id, thread_alive, file_cache_thread Protected.i client_id
Protected.i count_client
If conf_cache_enable = 1 : file_cache_thread = CreateThread(@FileCache(), 1) : EndIf
thread_alive = #True thread_alive = #True
Debug "AllOk"
Debug "HTTP Server Started."
Repeat Repeat
;Ist etwas passiert ? client_id = lsocket::WaitSocket(network_server_id)
;TODO: SSL Server Debug "New HTTP Client:"+Str(client_id)
network_event = NetworkServerEvent(network_server_id) If client_id > 0
Select network_event If count_HTTP_client < conf_max_clients
Case #PB_NetworkEvent_None
;Kein Event kleine bedenkzeit und CPU entlastung für den Thread einlegen.
Delay(1)
Case #PB_NetworkEvent_Connect
client_id = EventClient()
Debug "***** Client Connected:" + Str(client_id)
If count_client <= conf_max_clients
m_clients(Str(client_id))\client_id = client_id m_clients(Str(client_id))\client_id = client_id
m_clients(Str(client_id))\client_do_cli = #CLI_DO_WaitDataReceive m_clients(Str(client_id))\client_do_cli = #CLI_DO_WaitDataReceive
m_clients(Str(client_id))\client_do_srv = #SRV_DO_DataReceive
m_clients(Str(client_id))\client_ssl = 0
;Thread erstellen ;Thread erstellen
m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id) m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id)
count_client + 1 count_HTTP_client + 1
Else Else
Debug "Max Clients reached..." Debug "Max HTTP Clients reached..."
CloseNetworkConnection(client_id) lsocket::CloseSocket(client_id)
EndIf EndIf
Case #PB_NetworkEvent_Data
;Auslesen und in thread speicher geben.
Debug "Client Sent Data:" + Str(client_id)
client_id = EventClient()
If m_clients(Str(client_id))\client_do_cli = #CLI_DO_WaitDataReceive
;Es ist kein Speicher vorhanden... hinzufügen:
m_clients(Str(client_id))\client_do_srv = #SRV_DO_DataReceive
ElseIf m_clients(Str(client_id))\client_do_cli = #CLI_DO_DataWorking
;Clientthread ist noch am Datenverarbeiten.
;Signalisieren das Datenbereitstehen und der Netzwerkthread Blockiert ist...
;10msec Warten bis Thread wieder Arbeiten darf.
m_clients(Str(client_id))\client_do_srv = #SRV_DO_NewDatainBuffer
;Delay()
Else Else
;* Thread nicht existent? Break
If IsThread(m_clients(Str(client_id))\client_thread)
;Thread ist existent... funktioniert jedoch was nicht mer...
CloseNetworkConnection(client_id)
m_clients(Str(client_id))\client_do_srv = #SRV_DO_ClientDisconnect
Else
Debug "Systemabsturz ^.^"
CloseNetworkConnection(client_id)
End
EndIf EndIf
EndIf ForEver
ltls::CloseTLS(network_server_id)
Case #PB_NetworkEvent_Disconnect
;Nur für Debugzweck
Debug "***** Client Disconnected:" + Str(client_id)
client_id = EventClient()
m_clients(Str(client_id))\client_do_srv = #SRV_DO_ClientDisconnect
count_client - 1
EndSelect
Until thread_alive = #False
KillThread(file_cache_thread)
Debug "HTTP Server gestoppt"
EndProcedure EndProcedure
Procedure IsIPStringValid(Adress.s) Procedure IsIPStringValid(Adress.s)
@ -454,7 +406,7 @@ Module lhs_web
If m_clients(Str(thread_cli_id))\client_ssl = 1 If m_clients(Str(thread_cli_id))\client_ssl = 1
temp_receivelength = ltls::ReadTLSSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536) temp_receivelength = ltls::ReadTLSSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
Else Else
temp_receivelength = ReceiveNetworkData(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536) temp_receivelength = lsocket::ReadSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
EndIf EndIf
If temp_receivelength = -1 If temp_receivelength = -1
@ -476,7 +428,7 @@ Module lhs_web
If m_clients(Str(thread_cli_id))\client_ssl = 1 If m_clients(Str(thread_cli_id))\client_ssl = 1
temp_receivelength = ltls::ReadTLSSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536) temp_receivelength = ltls::ReadTLSSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
Else Else
temp_receivelength = ReceiveNetworkData(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536) temp_receivelength = lsocket::ReadSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
EndIf EndIf
m_clients(Str(thread_cli_id))\datenbuffer()\Size = temp_receivelength m_clients(Str(thread_cli_id))\datenbuffer()\Size = temp_receivelength
@ -862,12 +814,12 @@ Module lhs_web
Repeat Repeat
sent = ltls::WriteTLSSocket(thread_cli_id, sent_buffer_address , sent_length) sent = ltls::WriteTLSSocket(thread_cli_id, sent_buffer_address , sent_length)
If sent <> -1 If sent <> -1
Debug "TLS Sent:"+Str(sent)+" bytes" Debug "HTTPS Sent:"+Str(sent)+" bytes"
sent_length - sent sent_length - sent
sent_buffer_address + sent sent_buffer_address + sent
sent_total + sent sent_total + sent
Else Else
Debug "TLS Sent error:"+ltls::ErrorTLSCli(thread_cli_id) Debug "HTTPS Sent error:"+ltls::ErrorTLSCli(thread_cli_id)
EndIf EndIf
Until sent_length <= 0 Until sent_length <= 0
@ -875,7 +827,23 @@ Module lhs_web
thread_alive = #False thread_alive = #False
Else Else
sent = SendNetworkData(thread_cli_id, thread_buffer , thread_data_size+(thread_buffer_offset-thread_buffer)) sent_length = sent_total
sent_buffer_address = thread_buffer
sent_total = 0
Repeat
sent = lsocket::WriteSocket(thread_cli_id, thread_buffer , sent_length)
If sent <> -1
Debug "HTTP Sent:"+Str(sent)+" bytes"
sent_length - sent
sent_buffer_address + sent
sent_total + sent
Else
Debug "HTTP Sent error:"+Str(sent)
EndIf
Until sent_length <= 0
sent = sent_total
thread_alive = #False
EndIf EndIf
If sent = thread_data_size+(thread_buffer_offset-thread_buffer) If sent = thread_data_size+(thread_buffer_offset-thread_buffer)
@ -913,7 +881,10 @@ Module lhs_web
Wend Wend
If m_clients(Str(thread_cli_id))\client_ssl = 1 If m_clients(Str(thread_cli_id))\client_ssl = 1
ltls::CloseTLSSocket(thread_cli_id) ltls::CloseTLSSocket(thread_cli_id)
count_SSL_client - 1 count_HTTPS_client - 1
Else
lsocket::CloseSocket(thread_cli_id)
count_HTTP_client - 1
EndIf EndIf
DeleteMapElement(m_clients(), Str(thread_cli_id)) DeleteMapElement(m_clients(), Str(thread_cli_id))

View file

@ -29,13 +29,13 @@ DeclareModule lhs_web
#conf_max_clients #conf_max_clients
#conf_server_type #conf_server_type
#conf_cache_enable #conf_cache_enable
#conf_TLS_CA #conf_HTTPS_CA
#conf_TLS_Cert #conf_HTTPS_Cert
#conf_TLS_Key #conf_HTTPS_Key
#conf_TLS_Key_Pass #conf_HTTPS_Key_Pass
#conf_TLS_Port #conf_HTTPS_Port
#conf_TLS_Binding #conf_HTTPS_Binding
#conf_TLS_Enable #conf_HTTPS_Enable
EndEnumeration EndEnumeration
Enumeration cli_handler 1 Enumeration cli_handler 1

View file

@ -23,13 +23,13 @@ Define counter, v_lweb_srv_stop
lhs_web::set_config(lhs_web::#conf_port, "8081") lhs_web::set_config(lhs_web::#conf_port, "8081")
;lhs_web::set_config(lhs_web::#conf_binding, "127.0.0.1") ;lhs_web::set_config(lhs_web::#conf_binding, "127.0.0.1")
lhs_web::set_config(lhs_web::#conf_binding, "0.0.0.0") lhs_web::set_config(lhs_web::#conf_binding, "0.0.0.0")
lhs_web::set_config(lhs_web::#conf_TLS_CA, "/home/renlin/dev/libressl/sample/test_ca.pem") lhs_web::set_config(lhs_web::#conf_HTTPS_CA, "/home/renlin/dev/libressl/sample/test_ca.pem")
lhs_web::set_config(lhs_web::#conf_TLS_Cert, "/home/renlin/dev/libressl/sample/test_server_cert.pem") lhs_web::set_config(lhs_web::#conf_HTTPS_Cert, "/home/renlin/dev/libressl/sample/test_server_cert.pem")
lhs_web::set_config(lhs_web::#conf_TLS_Key, "/home/renlin/dev/libressl/sample/test_server_key.pem") lhs_web::set_config(lhs_web::#conf_HTTPS_Key, "/home/renlin/dev/libressl/sample/test_server_key.pem")
lhs_web::set_config(lhs_web::#conf_TLS_Key_Pass, "test-server-pass") lhs_web::set_config(lhs_web::#conf_HTTPS_Key_Pass, "test-server-pass")
lhs_web::set_config(lhs_web::#conf_TLS_Binding, "127.0.0.1") lhs_web::set_config(lhs_web::#conf_HTTPS_Binding, "127.0.0.1")
lhs_web::set_config(lhs_web::#conf_TLS_Port, "8444") lhs_web::set_config(lhs_web::#conf_HTTPS_Port, "8444")
lhs_web::set_config(lhs_web::#conf_TLS_Enable, "1") lhs_web::set_config(lhs_web::#conf_HTTPS_Enable, "1")
lhs_web::set_config(lhs_web::#conf_defaultfile, "/index.html") lhs_web::set_config(lhs_web::#conf_defaultfile, "/index.html")
lhs_web::set_config(lhs_web::#conf_basedir, "/home/renlin/testweb/") lhs_web::set_config(lhs_web::#conf_basedir, "/home/renlin/testweb/")
lhs_web::set_config(lhs_web::#conf_error400, "integrated") lhs_web::set_config(lhs_web::#conf_error400, "integrated")
@ -147,7 +147,7 @@ counter = 0
OpenConsole("Test") OpenConsole("Test")
PrintN("Webserver") PrintN("Webserver")
PrintN("HTTP an Port:"+lhs_web::get_config(lhs_web::#conf_port)) PrintN("HTTP an Port:"+lhs_web::get_config(lhs_web::#conf_port))
PrintN("HTTPS an Port:"+lhs_web::get_config(lhs_web::#conf_TLS_Port)) PrintN("HTTPS an Port:"+lhs_web::get_config(lhs_web::#conf_HTTPS_Port))
PrintN("Press Enter to Exit") PrintN("Press Enter to Exit")
Input() Input()