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:
parent
4f81308f22
commit
af15bd9dc6
4 changed files with 113 additions and 140 deletions
6
TODO.txt
6
TODO.txt
|
@ -1,10 +1,12 @@
|
|||
lweb HTTP & HTTPS Webserver
|
||||
|
||||
TODO:
|
||||
Switch all Network operations to Standard API
|
||||
TODO List:
|
||||
DONE: Switch all Network operations to Standard API
|
||||
POST/PUT with bigger Files and or Contents
|
||||
Sending of bigger Files (Currently everithing in Memory and then send)
|
||||
error400 handling
|
||||
error Code handling (Different errorcodes and responses)
|
||||
Translate Remarks to english
|
||||
Translate README.md to english
|
||||
Optimizing Code away from old PureBasic Network Code.
|
||||
Cleanup unused Variables
|
||||
|
|
201
lweb.pbi
201
lweb.pbi
|
@ -40,13 +40,13 @@ Module lhs_web
|
|||
Global.i conf_cache_maxsize = 1 ;1 Mbyte
|
||||
Global.i conf_cache_current = 0
|
||||
Global.i conf_cache_enable = 0
|
||||
Global.i conf_TLS_Port = 8443
|
||||
Global.s conf_TLS_binding = "127.0.0.1"
|
||||
Global.i conf_TLS_enable = 0 ;SSL Server
|
||||
Global.s conf_TLS_CA = ""
|
||||
Global.s conf_TLS_Cert = ""
|
||||
Global.s conf_TLS_Key = ""
|
||||
Global.s conf_TLS_Key_Pass = ""
|
||||
Global.i conf_HTTPS_Port = 8443
|
||||
Global.s conf_HTTPS_binding = "127.0.0.1"
|
||||
Global.i conf_HTTPS_enable = 0 ;SSL Server
|
||||
Global.s conf_HTTPS_CA = ""
|
||||
Global.s conf_HTTPS_Cert = ""
|
||||
Global.s conf_HTTPS_Key = ""
|
||||
Global.s conf_HTTPS_Key_Pass = ""
|
||||
|
||||
Enumeration s_client_do ;client_do_cli
|
||||
#CLI_DO_NOP ;Keine Arbeit
|
||||
|
@ -122,8 +122,10 @@ Module lhs_web
|
|||
#get_handler_prototype ;Welcher Prototype
|
||||
EndEnumeration
|
||||
|
||||
Global.i count_SSL_client
|
||||
Global.i server_id
|
||||
Global.i count_HTTP_client
|
||||
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 file_cache_mutex = CreateMutex()
|
||||
Global.i file_cache_semaphore = CreateSemaphore()
|
||||
|
@ -150,7 +152,8 @@ Module lhs_web
|
|||
;*
|
||||
|
||||
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 AddFileToCache(MemoryID.i, FileName.s, Size.i)
|
||||
Declare FileCache(Information.i)
|
||||
|
@ -166,8 +169,8 @@ Module lhs_web
|
|||
Select parameter
|
||||
Case #conf_port
|
||||
conf_port = Val(setting)
|
||||
Case #conf_TLS_Port
|
||||
conf_TLS_Port = Val(setting)
|
||||
Case #conf_HTTPS_Port
|
||||
conf_HTTPS_Port = Val(setting)
|
||||
Case #conf_binding
|
||||
If IsIPStringValid(setting)
|
||||
conf_binding = setting
|
||||
|
@ -175,23 +178,23 @@ Module lhs_web
|
|||
conf_binding = "127.0.0.1"
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
Case #conf_TLS_Binding
|
||||
Case #conf_HTTPS_Binding
|
||||
If IsIPStringValid(setting)
|
||||
conf_binding = setting
|
||||
Else
|
||||
conf_binding = "127.0.0.1"
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
Case #conf_TLS_CA
|
||||
conf_TLS_CA = setting
|
||||
Case #conf_TLS_Cert
|
||||
conf_TLS_Cert = setting
|
||||
Case #conf_TLS_Key
|
||||
conf_TLS_Key = setting
|
||||
Case #conf_TLS_Key_Pass
|
||||
conf_TLS_Key_Pass = setting
|
||||
Case #conf_TLS_Enable
|
||||
conf_TLS_enable = Val(setting)
|
||||
Case #conf_HTTPS_CA
|
||||
conf_HTTPS_CA = setting
|
||||
Case #conf_HTTPS_Cert
|
||||
conf_HTTPS_Cert = setting
|
||||
Case #conf_HTTPS_Key
|
||||
conf_HTTPS_Key = setting
|
||||
Case #conf_HTTPS_Key_Pass
|
||||
conf_HTTPS_Key_Pass = setting
|
||||
Case #conf_HTTPS_Enable
|
||||
conf_HTTPS_enable = Val(setting)
|
||||
Case #conf_defaultfile
|
||||
conf_defaultfile = setting
|
||||
Case #conf_basedir
|
||||
|
@ -229,8 +232,8 @@ Module lhs_web
|
|||
ProcedureReturn Str(conf_server_type)
|
||||
Case #conf_port
|
||||
ProcedureReturn Str(conf_port)
|
||||
Case #conf_TLS_Port
|
||||
ProcedureReturn Str(conf_TLS_Port)
|
||||
Case #conf_HTTPS_Port
|
||||
ProcedureReturn Str(conf_HTTPS_Port)
|
||||
Default
|
||||
ProcedureReturn ""
|
||||
EndSelect
|
||||
|
@ -240,15 +243,14 @@ Module lhs_web
|
|||
Global NewMap m_clients.s_clients(conf_max_clients)
|
||||
Protected tlsresponse.i
|
||||
|
||||
;TODO: Create SSL Server
|
||||
|
||||
server_id = CreateNetworkServer(#PB_Any, conf_port, #PB_Network_TCP, conf_binding)
|
||||
If conf_TLS_enable = 1
|
||||
server_SSL_id = lsocket::CreateSocket(conf_TLS_Port, conf_max_clients, lsocket::#SOCK_STREAM, lsocket::#AF_INET, conf_TLS_binding)
|
||||
If server_SSL_id
|
||||
tlsresponse = ltls::InitSimpleTLS(conf_TLS_CA, conf_TLS_Cert, conf_TLS_Key, conf_TLS_Key_Pass)
|
||||
;Init HTTP Socket
|
||||
server_HTTP_id = lsocket::CreateSocket(conf_port, conf_max_clients, lsocket::#SOCK_STREAM, lsocket::#AF_INET, conf_binding)
|
||||
If conf_HTTPS_enable = 1
|
||||
server_HTTPS_id = lsocket::CreateSocket(conf_HTTPS_Port, conf_max_clients, lsocket::#SOCK_STREAM, lsocket::#AF_INET, conf_HTTPS_binding)
|
||||
If server_HTTPS_id
|
||||
tlsresponse = ltls::InitSimpleTLS(conf_HTTPS_CA, conf_HTTPS_Cert, conf_HTTPS_Key, conf_HTTPS_Key_Pass)
|
||||
If tlsresponse > 0
|
||||
CreateThread(@server_SSL(), server_SSL_id)
|
||||
CreateThread(@server_HTTPS(), server_HTTPS_id)
|
||||
Else
|
||||
Debug "TLS Fehler:"+Str(tlsresponse)
|
||||
ProcedureReturn #False
|
||||
|
@ -258,9 +260,8 @@ Module lhs_web
|
|||
ProcedureReturn #False
|
||||
EndIf
|
||||
EndIf
|
||||
|
||||
If server_id
|
||||
CreateThread(@server(), server_id)
|
||||
If server_HTTP_id
|
||||
CreateThread(@server_HTTP(), server_HTTP_id)
|
||||
ProcedureReturn #True
|
||||
Else
|
||||
ProcedureReturn #False
|
||||
|
@ -268,31 +269,30 @@ Module lhs_web
|
|||
|
||||
EndProcedure
|
||||
|
||||
Procedure server_SSL(network_server_id.i)
|
||||
Procedure server_HTTPS(network_server_id.i)
|
||||
;**************************
|
||||
;*
|
||||
;* Hauptthread welcher den Netzwerkport verwaltet und den Datenfluss.
|
||||
;* Erstellt die Subthreads für Clients
|
||||
;* Main thread for HTTPS networksocket management. Create the Client Threads.
|
||||
;*
|
||||
Protected.i client_id
|
||||
|
||||
thread_alive = #True
|
||||
|
||||
Debug "TLS Server Started."
|
||||
Debug "HTTPS Server Started."
|
||||
Repeat
|
||||
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 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_do_cli = #CLI_DO_WaitDataReceive
|
||||
m_clients(Str(client_id))\client_do_srv = #SRV_DO_DataReceive
|
||||
m_clients(Str(client_id))\client_ssl = 1
|
||||
;Thread erstellen
|
||||
m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id)
|
||||
count_client + 1
|
||||
count_HTTPS_client + 1
|
||||
Else
|
||||
Debug "Max TLS Clients reached..."
|
||||
Debug "Max HTTPS Clients reached..."
|
||||
ltls::CloseTLSSocket(client_id)
|
||||
EndIf
|
||||
Else
|
||||
|
@ -302,85 +302,37 @@ Module lhs_web
|
|||
ltls::CloseTLS(network_server_id)
|
||||
EndProcedure
|
||||
|
||||
|
||||
Procedure server(network_server_id.i)
|
||||
Procedure server_HTTP(network_server_id.i)
|
||||
;**************************
|
||||
;*
|
||||
;* Hauptthread welcher den Netzwerkport verwaltet und den Datenfluss.
|
||||
;* Erstellt die Subthreads für Clients
|
||||
;* Main thread for HTTP networksocket management. Create the Client Threads.
|
||||
;*
|
||||
Protected.i network_event , client_id, thread_alive, file_cache_thread
|
||||
Protected.i count_client
|
||||
|
||||
If conf_cache_enable = 1 : file_cache_thread = CreateThread(@FileCache(), 1) : EndIf
|
||||
Protected.i client_id
|
||||
|
||||
thread_alive = #True
|
||||
Debug "AllOk"
|
||||
|
||||
Debug "HTTP Server Started."
|
||||
Repeat
|
||||
;Ist etwas passiert ?
|
||||
;TODO: SSL Server
|
||||
network_event = NetworkServerEvent(network_server_id)
|
||||
Select network_event
|
||||
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
|
||||
client_id = lsocket::WaitSocket(network_server_id)
|
||||
Debug "New HTTP Client:"+Str(client_id)
|
||||
If client_id > 0
|
||||
If count_HTTP_client < conf_max_clients
|
||||
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_srv = #SRV_DO_DataReceive
|
||||
m_clients(Str(client_id))\client_ssl = 0
|
||||
;Thread erstellen
|
||||
m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id)
|
||||
count_client + 1
|
||||
count_HTTP_client + 1
|
||||
Else
|
||||
Debug "Max Clients reached..."
|
||||
CloseNetworkConnection(client_id)
|
||||
Debug "Max HTTP Clients reached..."
|
||||
lsocket::CloseSocket(client_id)
|
||||
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
|
||||
;* Thread nicht existent?
|
||||
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
|
||||
Break
|
||||
EndIf
|
||||
EndIf
|
||||
|
||||
|
||||
|
||||
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"
|
||||
ForEver
|
||||
ltls::CloseTLS(network_server_id)
|
||||
EndProcedure
|
||||
|
||||
Procedure IsIPStringValid(Adress.s)
|
||||
|
@ -454,7 +406,7 @@ Module lhs_web
|
|||
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)
|
||||
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
|
||||
|
||||
If temp_receivelength = -1
|
||||
|
@ -476,7 +428,7 @@ Module lhs_web
|
|||
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)
|
||||
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
|
||||
|
||||
m_clients(Str(thread_cli_id))\datenbuffer()\Size = temp_receivelength
|
||||
|
@ -862,12 +814,12 @@ Module lhs_web
|
|||
Repeat
|
||||
sent = ltls::WriteTLSSocket(thread_cli_id, sent_buffer_address , sent_length)
|
||||
If sent <> -1
|
||||
Debug "TLS Sent:"+Str(sent)+" bytes"
|
||||
Debug "HTTPS Sent:"+Str(sent)+" bytes"
|
||||
sent_length - sent
|
||||
sent_buffer_address + sent
|
||||
sent_total + sent
|
||||
Else
|
||||
Debug "TLS Sent error:"+ltls::ErrorTLSCli(thread_cli_id)
|
||||
Debug "HTTPS Sent error:"+ltls::ErrorTLSCli(thread_cli_id)
|
||||
EndIf
|
||||
|
||||
Until sent_length <= 0
|
||||
|
@ -875,7 +827,23 @@ Module lhs_web
|
|||
thread_alive = #False
|
||||
|
||||
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
|
||||
|
||||
If sent = thread_data_size+(thread_buffer_offset-thread_buffer)
|
||||
|
@ -913,7 +881,10 @@ Module lhs_web
|
|||
Wend
|
||||
If m_clients(Str(thread_cli_id))\client_ssl = 1
|
||||
ltls::CloseTLSSocket(thread_cli_id)
|
||||
count_SSL_client - 1
|
||||
count_HTTPS_client - 1
|
||||
Else
|
||||
lsocket::CloseSocket(thread_cli_id)
|
||||
count_HTTP_client - 1
|
||||
EndIf
|
||||
|
||||
DeleteMapElement(m_clients(), Str(thread_cli_id))
|
||||
|
|
|
@ -29,13 +29,13 @@ DeclareModule lhs_web
|
|||
#conf_max_clients
|
||||
#conf_server_type
|
||||
#conf_cache_enable
|
||||
#conf_TLS_CA
|
||||
#conf_TLS_Cert
|
||||
#conf_TLS_Key
|
||||
#conf_TLS_Key_Pass
|
||||
#conf_TLS_Port
|
||||
#conf_TLS_Binding
|
||||
#conf_TLS_Enable
|
||||
#conf_HTTPS_CA
|
||||
#conf_HTTPS_Cert
|
||||
#conf_HTTPS_Key
|
||||
#conf_HTTPS_Key_Pass
|
||||
#conf_HTTPS_Port
|
||||
#conf_HTTPS_Binding
|
||||
#conf_HTTPS_Enable
|
||||
EndEnumeration
|
||||
|
||||
Enumeration cli_handler 1
|
||||
|
|
|
@ -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_binding, "127.0.0.1")
|
||||
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_TLS_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_TLS_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_TLS_Port, "8444")
|
||||
lhs_web::set_config(lhs_web::#conf_TLS_Enable, "1")
|
||||
lhs_web::set_config(lhs_web::#conf_HTTPS_CA, "/home/renlin/dev/libressl/sample/test_ca.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_HTTPS_Key, "/home/renlin/dev/libressl/sample/test_server_key.pem")
|
||||
lhs_web::set_config(lhs_web::#conf_HTTPS_Key_Pass, "test-server-pass")
|
||||
lhs_web::set_config(lhs_web::#conf_HTTPS_Binding, "127.0.0.1")
|
||||
lhs_web::set_config(lhs_web::#conf_HTTPS_Port, "8444")
|
||||
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_basedir, "/home/renlin/testweb/")
|
||||
lhs_web::set_config(lhs_web::#conf_error400, "integrated")
|
||||
|
@ -147,7 +147,7 @@ counter = 0
|
|||
OpenConsole("Test")
|
||||
PrintN("Webserver")
|
||||
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")
|
||||
Input()
|
||||
|
||||
|
|
Loading…
Reference in a new issue