2020-11-03 19:09:17 +01:00
;********************************
;*
2020-12-01 15:17:48 +01:00
;* lweb.pbi
2020-11-03 19:09:17 +01:00
;*
2020-11-30 09:52:27 +01:00
;* LiHaSo Webserver Main module.
2020-11-03 19:09:17 +01:00
;*
2020-11-30 09:52:27 +01:00
;* It is usable as standalone Webserver, look at lwebd.pb
2020-11-03 19:09:17 +01:00
;*
2023-03-29 10:46:56 +02:00
XIncludeFile "lhs_lib/NET/lhs_net_tls.pbi"
XIncludeFile "lhs_lib/NET/lhs_net_socket.pbi"
2023-07-18 08:59:36 +02:00
XIncludeFile "inc/lweb_http_post_decoder.pbi"
2020-11-27 11:52:35 +01:00
2020-11-03 19:09:17 +01:00
Module lhs_web
;********************************
;*
;* WebServer Variabeln / Parameter
;*
;{
2021-05-22 12:09:31 +02:00
;*
;* Configuration Structures
;*
;{
2023-03-29 10:46:56 +02:00
XIncludeFile "inc/lweb_header_privat.pbi"
2021-05-22 12:09:31 +02:00
;}
;*
2020-11-03 19:09:17 +01:00
;*
;* Identifikation des Servers.
;*
2021-05-22 12:09:31 +02:00
Global configuration.server
configuration\version = "V0.9"
configuration\identifikation = "LiHaSo Webserver " + configuration\version
2023-03-29 10:46:56 +02:00
configuration\config_file("status_xml")\name = "cfg/default_http_status_codes.xml"
configuration\config_file("status_xml")\type = "file"
configuration\config_file("hosts_dir")\name = "cfg/server/"
configuration\config_file("hosts_dir")\type = "dir"
2020-11-03 19:09:17 +01:00
;*
;* Diese Parameter müssen entsprechend angepasst sein.
;* Später ausgelagert in ein nicht synchronisiertes lweb-cfg.pbi
;* Folgende Parameter müssen im Hauptprogramm definiert sein.
2023-03-29 10:46:56 +02:00
2021-05-22 12:09:31 +02:00
configuration\type = 0
2023-03-29 10:46:56 +02:00
configuration\global_max_cli_memory = 0 ; Unlimited
configuration\global_max_cli_threads = 0 ; Unlimited
2021-05-22 12:09:31 +02:00
2023-03-29 10:46:56 +02:00
2021-05-22 12:09:31 +02:00
2020-11-03 19:09:17 +01:00
2020-11-08 17:31:26 +01:00
Enumeration s_client_do ;client_do_cli
#CLI_DO_NOP ;Keine Arbeit
#CLI_DO_DataWorking ;Datenverarbeitung
#CLI_DO_DataToSend ;Daten zum Senden vorhanden
#CLI_DO_WaitDataReceive ;Wartet auf Datenempfang
#CLI_DO_WaitDataSend ;Wartet auf gesendete Daten
2020-11-03 19:09:17 +01:00
EndEnumeration
2020-11-08 17:31:26 +01:00
Enumeration s_server_do ;client_do_srv
#SRV_DO_NOP ;Keine Arbeit
#SRV_DO_NewDatainBuffer ;Neue Daten im incoming Buffer zur bearbeitung.
#SRV_DO_MoreDatainBuffer ;Weitere Daten im incoming Buffer
#SRV_DO_DataReceive ;Datem Empfangen Client Thread muss sich darum Kümmern
#SRV_DO_DataReceiveCompleted ;Empfang der Daten Abgeschlossen
#SRV_DO_DataReceiveFailes ;Beim Empfangen der Daten ist ein fehler passiert.
#SRV_DO_DataSendOK ;Daten erfolgreich gesendet, Thread kann weiterarbeiten.
#SRV_DO_DataSendNOK ;Daten nicht erfolgreich gesendet.
#SRV_DO_ClientDisconnect ;Beenden des Threads Client hat verbindung getrennt.
2020-11-03 19:09:17 +01:00
EndEnumeration
2020-12-01 08:53:53 +01:00
Enumeration e_type_client 1
#client_HTTP
#client_HTTPS
EndEnumeration
#client_add = 1
#client_remove = -1
2023-03-29 10:46:56 +02:00
#http_method_get = "GET"
2020-11-03 19:09:17 +01:00
#http_method_post = "POST"
2023-03-29 10:46:56 +02:00
#http_method_put = "PUT"
2024-03-15 13:27:20 +01:00
#http_method_connect = "CONNECT"
#http_method_delete = "DELETE"
#http_method_options = "OPTIONS"
#http_method_trace = "TRACE"
#http_method_patch = "PATCH"
2020-11-03 19:09:17 +01:00
;***********************
;* s_lweb_client Struktur Jeder Clientthread muss in die Liste eingetragen werden.:
;*
;* client_id = Client ID von EventClient()
;* client_mutex = Client Thread Blockierer für den Hauptthread um gefahrenlos Daten in den Buffer Speichern zu können. (Vorsichtshalber drinn)
;* client_datenbuffer = 128KByte Speicherblock in diesen Speicher Schreibt nur der Hauptthread.
;* client_output_datenbuffer = 128KByte Speicherblock in diesen Speicher Schreibt nur der Clientthread.
;* client_datenbuffer = 128KByte Speicherblock für die Module achtung dieser Speicherblock wird unter umständen von den Modulen
;* vergrössert wird nach abgeschlossener Arbeit wieder auf Default gesetzt.
;*
Structure s_client_memory
2020-11-27 11:52:35 +01:00
*Buffer
2020-11-03 19:09:17 +01:00
Initialized.b
2020-11-08 01:31:57 +01:00
Size.i
2020-11-03 19:09:17 +01:00
EndStructure
Structure s_file_cache
2020-11-27 11:52:35 +01:00
*Buffer
2020-11-03 19:09:17 +01:00
Size.i
Timer.i
Is.i
EndStructure
2023-03-29 10:46:56 +02:00
Structure PORT_used
UUID.s
EndStructure
Structure IP_used
Map PortS.PORT_used()
EndStructure
2020-11-03 19:09:17 +01:00
Structure s_clients
client_id.i
client_do_cli.i
client_do_srv.i
2020-12-01 08:53:53 +01:00
client_type.i ;What type of Client.
2020-11-03 19:09:17 +01:00
client_mutex.i
client_thread.i
List datenbuffer.s_client_memory()
client_test_cli.i
client_test_srv.i
2020-11-27 11:52:35 +01:00
client_cctx.i
2023-03-29 10:46:56 +02:00
server_uuid.s ;On which Server it is received.
host_id.s
client_timeout.i ;Individual timeout per Server to cleanup Client connections
2020-11-03 19:09:17 +01:00
EndStructure
2020-11-08 01:31:57 +01:00
Structure s_request_handler
call.i
type.i
routetype.i
2023-03-29 20:06:01 +02:00
host.s
route.s
library.s
perm.i
2020-11-08 01:31:57 +01:00
EndStructure
2023-03-29 10:46:56 +02:00
Structure s_server_runs
server_connection_id.i
UUID.s
configuration_map_id.s
EndStructure
2024-09-23 05:59:11 +02:00
Structure s_header_functions
header_name.s
call.i
type.i
library.s
perm.i
EndStructure
2020-11-08 01:31:57 +01:00
Enumeration cli_handler_infos 1
#get_handler_procedure ;Funktion die Aufgerufen werden muss
#get_handler_prototype ;Welcher Prototype
2023-03-29 20:06:01 +02:00
#get_handler_library ;Library to open
#get_handler_library_perm ;Library permanent in memory?
2020-11-08 01:31:57 +01:00
EndEnumeration
2020-11-28 16:49:22 +01:00
Global.i count_HTTP_client
Global.i count_HTTPS_client
Global.i server_HTTP_id
Global.i server_HTTPS_id
2020-11-03 19:09:17 +01:00
Global.i server_mutex = CreateMutex() ;Dieser Mutex dient zu der Sicherheit der Element Liste.
2023-03-29 10:46:56 +02:00
Global.i server_start_semaphore = CreateSemaphore()
2020-12-01 08:53:53 +01:00
Global.i count_HTTP_mutex = CreateMutex()
Global.i count_HTTPS_mutex = CreateMutex()
2020-11-03 19:09:17 +01:00
Global.i file_cache_mutex = CreateMutex()
Global.i file_cache_semaphore = CreateSemaphore()
Global.i file_cache_semaphore_thread = CreateSemaphore()
Global NewMap m_file_cache_map.i()
Global NewMap m_file_cache.s_file_cache()
2023-03-29 10:46:56 +02:00
Global NewMap m_clients.s_clients()
2020-11-08 01:31:57 +01:00
Global NewMap m_request.s_request_handler()
2023-03-29 10:46:56 +02:00
Global NewMap server_used.IP_used()
Global NewMap m_server_running.s_server_runs()
2020-11-03 19:09:17 +01:00
;}
2020-11-08 01:31:57 +01:00
;********************************
;*
;* Handler Prototypen
;*
2023-03-29 20:06:01 +02:00
PrototypeC WebHandler_Get(handler_Map_JSON.s)
PrototypeC WebHandler_Post(handler_Map_JSON.s, ContentString.s)
PrototypeC WebHandler_Universal(handler_Map_JSON.s, ContentString.s)
2023-03-30 09:46:13 +02:00
PrototypeC.s WebHandler_i_Get(handler_Map_JSON.s)
PrototypeC.s WebHandler_i_Post(handler_Map_JSON.s, ContentString.s)
PrototypeC.s WebHandler_i_Universal(handler_Map_JSON.s, ContentString.s)
2023-07-17 15:47:38 +02:00
PrototypeC WebSocket_Handler_String(handler_Map_JSON.s, Back_Semaphore.i)
2020-12-01 15:17:48 +01:00
;********************************
;*
;* Used Librarys
;*
XIncludeFile "inc/lweb_http_header.pbi"
2023-03-29 10:46:56 +02:00
;XIncludeFile "inc/lweb_file_cache_header.pbi"
XIncludeFile "inc/lweb_config_header.pbi"
2020-12-01 15:17:48 +01:00
2020-11-03 19:09:17 +01:00
;********************************
;*
;* Proceduren Deklarierung
;*
2020-11-28 16:49:22 +01:00
Declare server_HTTP(network_server_id.i)
Declare server_HTTPS(network_server_id.i)
2020-11-03 19:09:17 +01:00
Declare client(network_client_id.i)
2023-03-29 10:46:56 +02:00
Declare.s file_check(thread_requested.s, hostid.s)
2023-03-29 20:06:01 +02:00
Declare.s call_request_string(RequestString.s, Info.i=#get_handler_procedure)
2020-11-08 01:31:57 +01:00
Declare call_request(RequestString.s, Info.i=#get_handler_procedure)
2023-03-30 09:46:13 +02:00
Declare.s call_function(ToCallType.i, ToCall.i, Map Header.s(), PostMapString.s ="")
2023-03-29 20:06:01 +02:00
Declare advanced_register_handler(RequesterString.s, Permament.i = 0, Library.s = "", Host.s = "")
2023-07-18 08:59:36 +02:00
;Declare.s Work_Post_ToJSON_multipart_form_data(ContentLength.i, MemorSize.i, Memory.i)
;Declare.s Work_Post_ToJSON_x_www_form_urlencoded(ContentLength.i, MemorSize.i, Memory.i)
2020-12-01 08:53:53 +01:00
Declare count_client(Type.i, Countchange.i)
2020-11-03 19:09:17 +01:00
2020-12-01 15:17:48 +01:00
XIncludeFile "inc/lweb_IP.pbi"
XIncludeFile "inc/lweb_http.pbi"
XIncludeFile "inc/lweb_helper.pbi"
2023-03-29 10:46:56 +02:00
;XIncludeFile "inc/lweb_file_cache.pbi" Currently in dev.
XIncludeFile "inc/lweb_config.pbi"
IncludeFile "lhs_lib/SYS/lhs_uuid.pbi" ;It is needed as local function.
2020-12-01 15:17:48 +01:00
2023-03-29 10:46:56 +02:00
XIncludeFile "inc/lweb_server_cfg.pbi"
2023-03-29 20:06:01 +02:00
;XIncludeFile "inc/lweb_dynamic.pbi"
2023-03-29 10:46:56 +02:00
Procedure server_reload(UUID.s)
2020-11-03 19:09:17 +01:00
ProcedureReturn #True
EndProcedure
2023-03-29 10:46:56 +02:00
IncludeFile "inc/lweb_http_status.pbi"
2020-11-03 19:09:17 +01:00
2023-03-29 10:46:56 +02:00
Procedure.s http_server_prepare(http_port.i = 8080, http_binding.s = "127.0.0.1", https_port.i=8443, https_binding.s="127.0.0.1", https_enabled.b = #False)
;CreateUUID
Protected UUID.s
UUID = CreateUUID()
configuration\hosts(UUID)\defaultfile = "index.html"
configuration\hosts(UUID)\basedir = "/srv/lweb-srv/"
configuration\hosts(UUID)\http\max_clients = 10 ; Max sametime HTTP connections.
configuration\hosts(UUID)\http\port = 8080
configuration\hosts(UUID)\http\binding = "127.0.0.1"
configuration\hosts(UUID)\https\max_clients = 100
configuration\hosts(UUID)\https\port = 8443
configuration\hosts(UUID)\https\binding = "127.0.0.1"
configuration\hosts(UUID)\https\enabled = 0
configuration\hosts(UUID)\https\CA = ""
configuration\hosts(UUID)\https\Certs = ""
configuration\hosts(UUID)\https\key = ""
configuration\hosts(UUID)\https\key_pass = ""
configuration\hosts(UUID)\cache\enable = 0 ; Enable / Disable Cached Server
configuration\hosts(UUID)\cache\time = 120 ; TTL of cached files
configuration\hosts(UUID)\cache\maxsize = 1 ; Max Cache
configuration\hosts(UUID)\cache\current = 0 ;
configuration\hosts(UUID)\mem\MaxFileSize = 524288 ; Default 512 KiB
configuration\hosts(UUID)\mem\DefaultBlockSize = 65536 ; Default Blocksize 64 KiB
ProcedureReturn UUID
2020-11-03 19:09:17 +01:00
EndProcedure
2023-03-29 10:46:56 +02:00
Procedure main_server(id.i)
2024-03-13 09:23:28 +01:00
ldl::Logging("main Server Loading")
2023-03-29 10:46:56 +02:00
status_defaults()
EndProcedure
Procedure server_start()
Protected server_id_HTTP
Protected server_id_HTTPS
Protected server_UUID_HTTP.s
Protected server_UUID_HTTPS.s
Protected counter.i, id.i
Global NewMap m_clients.s_clients(configuration\global_max_cli_threads) ;configuration\http\max_clients+configuration\https\max_clients
2020-11-27 11:52:35 +01:00
Protected tlsresponse.i
2023-03-29 20:06:01 +02:00
Protected current_lib.i
Protected current_lib_function.i, current_lib_perm.i
Protected current_lib_close.i
Protected current_register_type.i
Protected current_register_react.s
Protected current_register_proto.i
2023-03-29 10:46:56 +02:00
;Initialize all Servers in configuration\hosts()
ResetMap(configuration\hosts())
While NextMapElement(configuration\hosts())
server_id_HTTP = lsocket::CreateSocket(configuration\hosts()\http\port, configuration\hosts()\http\max_clients, lsocket::#SOCK_STREAM, lsocket::#AF_INET, configuration\hosts()\http\binding)
If server_id_HTTP
;Check for HTTPS
If configuration\hosts()\https\enabled = 1
server_id_HTTPS = lsocket::CreateSocket(configuration\hosts()\https\port, configuration\hosts()\https\max_clients, lsocket::#SOCK_STREAM, lsocket::#AF_INET, configuration\hosts()\https\binding)
If server_id_HTTPS
;Now Create TLS Socket
tlsresponse = tls::InitSimpleTLS(configuration\hosts()\https\CA,configuration\hosts()\https\Certs, configuration\hosts()\https\key, configuration\hosts()\https\key_pass)
If tlsresponse > 0
m_server_running(Str(server_id_HTTPS))\configuration_map_id = MapKey(configuration\hosts())
configuration\hosts()\https\thread_id = CreateThread(@server_HTTPS(), server_id_HTTPS)
counter + 1
Else
ldl::Logging("HTTPS Socket configuration failed with error:"+Str(tlsresponse))
End
EndIf
Else
ldl::Logging("HTTPS Socket ["+configuration\hosts()\https\port+"] could Not opened.")
End
EndIf
2020-11-27 11:52:35 +01:00
EndIf
2023-03-29 10:46:56 +02:00
;HTTP server start
m_server_running(Str(server_id_HTTP))\configuration_map_id = MapKey(configuration\hosts())
configuration\hosts()\http\thread_id = CreateThread(@server_HTTP(), server_id_HTTP)
counter + 1
configuration\hosts()\log\AccesslogUUID = lhs_log_ext::Create("Accesslog "+configuration\hosts()\description)
configuration\hosts()\log\ErrorlogUUID = lhs_log_ext::Create("Errorlog "+configuration\hosts()\description)
2023-03-30 15:40:27 +02:00
lhs_log_ext::SetLogFile(configuration\hosts()\log\AccesslogUUID, configuration\hosts()\log\AccesslogFile)
lhs_log_ext::SetLogFile(configuration\hosts()\log\ErrorlogUUID, configuration\hosts()\log\ErrorlogFile)
2023-03-29 10:46:56 +02:00
lhs_log_ext::Init(configuration\hosts()\log\AccesslogUUID)
lhs_log_ext::Init(configuration\hosts()\log\ErrorlogUUID)
2023-03-30 15:40:27 +02:00
ldl::Register(lhs_log_ext::@Out(), configuration\hosts()\log\AccesslogUUID, 0,configuration\hosts()\log\AccesslogUUID ,ldl::#AdvancedLog)
ldl::Register(lhs_log_ext::@Out(), configuration\hosts()\log\ErrorlogUUID, 0,configuration\hosts()\log\ErrorlogUUID ,ldl::#AdvancedLog)
2023-03-29 10:46:56 +02:00
ldl::Logging("Accesslog UUID:"+configuration\hosts()\log\AccesslogUUID)
ldl::Logging("Errorlog UUID:"+configuration\hosts()\log\ErrorlogUUID)
2023-03-29 20:06:01 +02:00
;Register Handlers
ResetMap(configuration\hosts()\dynamichandler())
While NextMapElement(configuration\hosts()\dynamichandler())
ldl::Logging("Register Handler:"+MapKey(configuration\hosts()\dynamichandler()))
ldl::Logging("Parameters library:"+configuration\hosts()\dynamichandler()\file+" Type:"+configuration\hosts()\dynamichandler()\functiontype+" uri:"+configuration\hosts()\dynamichandler()\url+" extension:"+configuration\hosts()\dynamichandler()\extension)
If FindMapElement(configuration\hosts()\usedlibs(), configuration\hosts()\dynamichandler()\file)
current_lib = configuration\hosts()\usedlibs(configuration\hosts()\dynamichandler()\file)\lib_id
Else
current_lib = OpenLibrary(#PB_Any, configuration\hosts()\dynamichandler()\file)
EndIf
If current_lib
current_lib_function = GetFunction(current_lib, configuration\hosts()\dynamichandler()\proc)
If current_lib_function
ldl::Logging("Function "+configuration\hosts()\dynamichandler()\proc+" found")
If FindMapElement(configuration\hosts()\usedlibs(), configuration\hosts()\dynamichandler()\file)
If configuration\hosts()\usedlibs(configuration\hosts()\dynamichandler()\file)\permlib = "false" And configuration\hosts()\dynamichandler()\permlib = "false"
current_lib_close = 1
current_lib_perm = 0
ElseIf configuration\hosts()\usedlibs(configuration\hosts()\dynamichandler()\file)\permlib = "false" And configuration\hosts()\dynamichandler()\permlib = "true"
configuration\hosts()\usedlibs(configuration\hosts()\dynamichandler()\file)\permlib = configuration\hosts()\dynamichandler()\permlib
configuration\hosts()\usedlibs(configuration\hosts()\dynamichandler()\file)\lib_id = current_lib
current_lib_close = 0
current_lib_perm = 1
EndIf
Else
configuration\hosts()\usedlibs(configuration\hosts()\dynamichandler()\file)\permlib = configuration\hosts()\dynamichandler()\permlib
If configuration\hosts()\usedlibs(configuration\hosts()\dynamichandler()\file)\permlib = "false"
current_lib_close = 1
current_lib_perm = 0
Else
configuration\hosts()\usedlibs(configuration\hosts()\dynamichandler()\file)\lib_id = current_lib
current_lib_close = 0
current_lib_perm = 1
EndIf
EndIf
;Now register the function
Select configuration\hosts()\dynamichandler()\routetype
Case "handler_only"
current_register_type = #handler_only
current_register_react = configuration\hosts()\dynamichandler()\url
Case "handler_sub"
current_register_type = #handler_sub
current_register_react = configuration\hosts()\dynamichandler()\url
Case "handler_type"
current_register_type = #handler_type
current_register_react = configuration\hosts()\dynamichandler()\extension
EndSelect
Select configuration\hosts()\dynamichandler()\functiontype
Case "GET"
current_register_proto = #handler_proto_get
Case "POST"
current_register_proto = #handler_proto_post
Case "Universal"
current_register_proto = #handler_proto_universal
2023-03-30 09:46:13 +02:00
Case "GET_int"
current_register_proto = #handler_proto_i_get
Case "POST_int"
current_register_proto = #handler_proto_i_post
Case "Universal_int"
current_register_proto = #handler_proto_i_universal
2023-03-29 20:06:01 +02:00
EndSelect
ldl::Logging("react:"+current_register_react+" file:"+configuration\hosts()\dynamichandler()\file+" Host ID:"+MapKey(configuration\hosts()))
2023-03-30 09:46:13 +02:00
If FindString(configuration\hosts()\dynamichandler()\functiontype, "_int")
register_client_handler(current_register_react, current_lib_function, current_register_proto, current_register_type, MapKey(configuration\hosts()))
Else
register_client_handler(current_register_react, current_lib_function, current_register_proto, current_register_type, MapKey(configuration\hosts()))
advanced_register_handler(current_register_react, current_lib_perm, configuration\hosts()\dynamichandler()\file, MapKey(configuration\hosts()))
EndIf
2023-03-29 20:06:01 +02:00
If current_lib_close = 1
CloseLibrary(current_lib)
EndIf
current_lib_close = 0
Else
ldl::Logging("Error: Function "+configuration\hosts()\dynamichandler()\proc+" not found")
If configuration\hosts()\dynamichandler()\permlib = "false"
CloseLibrary(current_lib)
EndIf
EndIf
Else
ldl::Logging("Error: Library "+configuration\hosts()\dynamichandler()\file+" could not be opened")
EndIf
Wend
2020-11-27 11:52:35 +01:00
Else
2023-03-29 10:46:56 +02:00
ldl::Logging("HTTP Socket ["+configuration\hosts()\http\port+"] could Not be opened.")
End
2020-11-27 11:52:35 +01:00
EndIf
2023-03-29 10:46:56 +02:00
Wend
;Initalize Loggers:
For id = 1 To counter
SignalSemaphore(server_start_semaphore)
Next
ProcedureReturn #True
EndProcedure
2020-11-03 19:09:17 +01:00
2023-03-29 10:46:56 +02:00
Procedure server_stop(UUID.s = "all")
;Current easy kill HTTP and HTTPS thread.
;Must be changed if Caching is active !
; If configuration\https\enabled = 1 And configuration\https\thread_id <> 0
; KillThread(configuration\https\thread_id)
; EndIf
; If configuration\http\thread_id <> 0
; KillThread(configuration\http\thread_id)
; EndIf
2020-11-03 19:09:17 +01:00
EndProcedure
2023-03-29 10:46:56 +02:00
2020-11-28 16:49:22 +01:00
Procedure server_HTTPS(network_server_id.i)
2020-11-27 11:52:35 +01:00
;**************************
;*
2020-11-28 16:49:22 +01:00
;* Main thread for HTTPS networksocket management. Create the Client Threads.
2020-11-27 11:52:35 +01:00
;*
Protected.i client_id
2023-03-29 10:46:56 +02:00
Protected.i server_host.s = m_server_running(Str(network_server_id))\configuration_map_id
2020-11-27 11:52:35 +01:00
thread_alive = #True
2023-03-29 10:46:56 +02:00
WaitSemaphore(server_start_semaphore)
ldl::Logging("HTTPS Server Started.")
2020-11-27 11:52:35 +01:00
Repeat
2023-03-29 10:46:56 +02:00
client_id = tls::WaitTLSSocket(network_server_id)
ldl::Logging("New HTTPS Client:"+Str(client_id))
2020-11-27 11:52:35 +01:00
If client_id > 0
2020-12-04 21:03:03 +01:00
If m_clients(Str(client_id))\client_id = client_id
2023-03-29 10:46:56 +02:00
ldl::Logging("Client Thread for socket already exist :"+Str(client_id))
2020-11-27 11:52:35 +01:00
Else
2023-03-29 10:46:56 +02:00
If count_HTTPS_client < 128
2020-12-04 21:03:03 +01:00
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_type = #client_HTTPS
2023-03-29 10:46:56 +02:00
m_clients(Str(client_id))\host_id = server_host
2020-12-04 21:03:03 +01:00
;Thread erstellen
m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id)
count_client(#client_HTTPS, #client_add)
Else
2023-03-29 10:46:56 +02:00
ldl::Logging("Max HTTPS Clients reached...")
tls::CloseTLSSocket(client_id)
2020-12-04 21:03:03 +01:00
EndIf
EndIf
2020-11-27 11:52:35 +01:00
Else
Break
EndIf
ForEver
2023-03-29 10:46:56 +02:00
tls::CloseTLS(network_server_id)
2020-11-27 11:52:35 +01:00
EndProcedure
2020-11-28 16:49:22 +01:00
Procedure server_HTTP(network_server_id.i)
2020-11-03 19:09:17 +01:00
;**************************
;*
2020-11-28 16:49:22 +01:00
;* Main thread for HTTP networksocket management. Create the Client Threads.
2020-11-03 19:09:17 +01:00
;*
2020-11-28 16:49:22 +01:00
Protected.i client_id
2023-03-29 10:46:56 +02:00
Protected.i server_host.s = m_server_running(Str(network_server_id))\configuration_map_id
2020-11-28 16:49:22 +01:00
2023-03-29 10:46:56 +02:00
thread_alive = #True
WaitSemaphore(server_start_semaphore)
ldl::Logging("HTTP Server Started.")
2020-11-03 19:09:17 +01:00
Repeat
2020-11-28 16:49:22 +01:00
client_id = lsocket::WaitSocket(network_server_id)
2023-03-29 10:46:56 +02:00
ldl::Logging("New HTTP Client:"+Str(client_id))
2020-11-28 16:49:22 +01:00
If client_id > 0
2020-12-04 21:03:03 +01:00
If m_clients(Str(client_id))\client_id = client_id
2023-03-29 10:46:56 +02:00
ldl::Logging("Client Thread for socket already exist :"+Str(client_id))
2020-11-28 16:49:22 +01:00
Else
2023-03-29 10:46:56 +02:00
If count_HTTP_client < 128
2020-12-04 21:03:03 +01:00
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_type = #client_HTTP
2023-03-29 10:46:56 +02:00
m_clients(Str(client_id))\host_id = server_host
2020-12-04 21:03:03 +01:00
;Thread erstellen
m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id)
count_client(#client_HTTP, #client_add)
Else
2023-03-29 10:46:56 +02:00
ldl::Logging("Max HTTP Clients reached...")
2020-12-04 21:03:03 +01:00
lsocket::CloseSocket(client_id)
EndIf
EndIf
2020-11-28 16:49:22 +01:00
Else
Break
EndIf
ForEver
2020-11-03 19:09:17 +01:00
EndProcedure
2023-03-31 12:46:49 +02:00
Procedure server_WebSocket(network_client_id)
Protected Quit = 1
Protected.i thread_cli_id = network_client_id
Repeat
; Check if we get data from client
If m_clients(Str(thread_cli_id))\client_type = #client_HTTPS
temp_receivelength = tls::ReadTLSSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
Else
temp_receivelength = lsocket::ReadSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
EndIf
; Check if Library has data to send
Until Quit = 1
EndProcedure
2020-11-03 19:09:17 +01:00
Procedure client(network_client_id.i)
2020-11-27 11:52:35 +01:00
Protected thread_cli_id = network_client_id, sent
2020-11-08 23:26:35 +01:00
Protected MyThreadJSON, ToCall, ToCallType
Protected thread_temp_cache.s, thread_temp_cache_memory, temp_receivelength, thread_temp_decode_memory
2022-02-17 13:23:42 +01:00
Protected thread_reasign, thread_oversized_file.b, thread_data_to_read.i, thread_data_readed.i
2023-03-29 20:06:01 +02:00
Protected thread_data_size, thread_file_handle, Handle_Response_Address
2020-11-08 01:31:57 +01:00
Protected.s thread_requested, thread_type, thread_date, thread_header, thread_work, JSONStringToMap, Handler_Response, response_status, PostMapString
2020-11-03 19:09:17 +01:00
Protected thread_buffer, thread_buffer_offset, thread_buffer_length, buffer_sent
2020-11-27 21:43:38 +01:00
Protected sent_length, sent_buffer_address, sent_total
2022-02-18 16:05:48 +01:00
Protected.b thread_alive = #True, error_message = #False, thread_redirect = #False
2023-03-29 10:46:56 +02:00
Protected.s default_file = configuration\hosts(m_clients(Str(thread_cli_id))\host_id)\defaultfile
Protected.s home_dir = configuration\hosts(m_clients(Str(thread_cli_id))\host_id)\basedir
Protected.s AccessLog = configuration\hosts(m_clients(Str(thread_cli_id))\host_id)\log\AccesslogUUID
Protected.s ErrorLog = configuration\hosts(m_clients(Str(thread_cli_id))\host_id)\log\ErrorlogUUID
2023-03-29 20:06:01 +02:00
Protected.s Host_ID = m_clients(Str(thread_cli_id))\host_id
Protected.s Host_call = "SpecServer:["+Host_ID+"]"
Protected.s Header_string = ""
Protected Library_ID, LibraryToCall.s
Define NewMap Header.s()
2020-11-08 01:31:57 +01:00
Define NewMap Response.s()
Define NewMap Post.s()
2020-12-01 08:53:53 +01:00
2023-03-29 10:46:56 +02:00
ldl::Logging("Client Thread Started. ID:" + Str(network_client_id))
2020-11-03 19:09:17 +01:00
Repeat
;Prüfen ob der Thread was zu tun hat.
;{ Main Server Triggered Things
Select m_clients(Str(thread_cli_id))\client_do_srv
Case #SRV_DO_NOP
;Ressourcen freigeben und Threadzeit reduzieren
Delay(1)
Case #SRV_DO_DataReceive
LastElement(m_clients(Str(thread_cli_id))\datenbuffer())
AddElement(m_clients(Str(thread_cli_id))\datenbuffer())
m_clients(Str(thread_cli_id))\datenbuffer()\Buffer = AllocateMemory(131072)
If m_clients(Str(thread_cli_id))\datenbuffer()\Buffer
m_clients(Str(thread_cli_id))\datenbuffer()\Initialized = #True
Else
2023-03-29 10:46:56 +02:00
ldl::Logging("Buffer Initialisierung fehlgeschlagen.")
2020-11-03 19:09:17 +01:00
Break
EndIf
2020-12-01 08:53:53 +01:00
If m_clients(Str(thread_cli_id))\client_type = #client_HTTPS
2023-03-29 10:46:56 +02:00
temp_receivelength = tls::ReadTLSSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
2020-11-27 11:52:35 +01:00
Else
2020-11-28 16:49:22 +01:00
temp_receivelength = lsocket::ReadSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
2020-11-27 11:52:35 +01:00
EndIf
2020-11-03 19:09:17 +01:00
If temp_receivelength = -1
2023-03-29 10:46:56 +02:00
ldl::Logging("Empfangsfehler.")
2020-11-03 19:09:17 +01:00
Break
ElseIf temp_receivelength = 65536
2020-11-08 01:31:57 +01:00
m_clients(Str(thread_cli_id))\datenbuffer()\Size = temp_receivelength
2020-11-03 19:09:17 +01:00
counter_mem_buffers = 2
Repeat
AddElement(m_clients(Str(thread_cli_id))\datenbuffer())
m_clients(Str(thread_cli_id))\datenbuffer()\Buffer = AllocateMemory(131072)
If m_clients(Str(thread_cli_id))\datenbuffer()\Buffer
m_clients(Str(thread_cli_id))\datenbuffer()\Initialized = #True
Else
2023-03-29 10:46:56 +02:00
ldl::Logging("Buffer Initialisierung fehlgeschlagen.")
2020-11-03 19:09:17 +01:00
Break 2
EndIf
2020-12-01 08:53:53 +01:00
If m_clients(Str(thread_cli_id))\client_type = #client_HTTPS
2023-03-29 10:46:56 +02:00
temp_receivelength = tls::ReadTLSSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
2020-11-27 11:52:35 +01:00
Else
2020-11-28 16:49:22 +01:00
temp_receivelength = lsocket::ReadSocket(thread_cli_id, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, 65536)
2020-11-27 11:52:35 +01:00
EndIf
2020-11-08 01:31:57 +01:00
m_clients(Str(thread_cli_id))\datenbuffer()\Size = temp_receivelength
2020-11-03 19:09:17 +01:00
counter_mem_buffers + 1
If temp_receivelength = -1
Break 2
EndIf
Until temp_receivelength < 65536
2020-11-08 01:31:57 +01:00
Else
m_clients(Str(thread_cli_id))\datenbuffer()\Size = temp_receivelength
2020-11-03 19:09:17 +01:00
EndIf
;Alle Daten empfangen.
2023-03-29 10:46:56 +02:00
ldl::Logging("Anzahl Buffer:" + Str(ListSize(m_clients(Str(thread_cli_id))\datenbuffer())))
2020-11-03 19:09:17 +01:00
m_clients(Str(thread_cli_id))\client_do_srv = #SRV_DO_NOP
m_clients(Str(thread_cli_id))\client_do_cli = #CLI_DO_DataWorking
Case #SRV_DO_ClientDisconnect
;Thread beenden
2023-03-29 10:46:56 +02:00
ldl::Logging("#SRV_DO_ClientDisconnect")
2020-11-03 19:09:17 +01:00
thread_alive = #False
;* Alles IO
Default
;Ressourcen freigeben und Threadzeit reduzieren
Delay(1)
2023-03-29 10:46:56 +02:00
ldl::Logging("--------------------------------------------------------- FAILING ???")
2020-11-03 19:09:17 +01:00
EndSelect
;}
;{ Client Side Triggered Things
If thread_alive = #True
Select m_clients(Str(thread_cli_id))\client_do_cli
Case #CLI_DO_DataWorking
2021-04-29 09:41:41 +02:00
;Receive done.
;Do Buffer work
;Change Thread to Work mode.
2020-11-08 01:31:57 +01:00
2020-11-03 19:09:17 +01:00
;{
2023-03-29 10:46:56 +02:00
ldl::Logging("Data received, working...")
2020-11-03 19:09:17 +01:00
m_clients(Str(thread_cli_id))\client_do_cli = #CLI_DO_DataWorking
2021-04-29 09:41:41 +02:00
;Work on Answer.
2020-11-03 19:09:17 +01:00
ResetList(m_clients(Str(thread_cli_id))\datenbuffer())
2021-04-29 09:41:41 +02:00
;TODO: Only first Buffer have a Header.
2023-03-29 10:46:56 +02:00
2020-11-03 19:09:17 +01:00
While NextElement(m_clients(Str(thread_cli_id))\datenbuffer())
2023-03-29 10:46:56 +02:00
2020-11-08 01:31:57 +01:00
thread_work = PeekS(m_clients(Str(thread_cli_id))\datenbuffer()\Buffer, m_clients(Str(thread_cli_id))\datenbuffer()\Size, #PB_Ascii)
2021-04-29 09:41:41 +02:00
;Move Header to Map
2023-03-29 10:46:56 +02:00
ldl::Logging("Preheaderwork:"+ thread_work)
2020-11-03 19:09:17 +01:00
JSONStringToMap = Work_Header_to_JSONMap(thread_work)
If JSONStringToMap <> #error_string
MyThreadJSON = ParseJSON(#PB_Any, JSONStringToMap)
If MyThreadJSON
ClearMap(Header())
ExtractJSONMap(JSONValue(MyThreadJSON), Header())
FreeJSON(MyThreadJSON)
Else
2021-04-29 09:41:41 +02:00
;Should not be a case.
Break 2 ; Kill Thread.
2020-11-03 19:09:17 +01:00
EndIf
Else
error_message = #True
EndIf
2023-03-29 10:46:56 +02:00
;TODO: Url with GET Parameter (Also by POST/PUT & Co.)
;? Wen da abschneiden
2020-11-03 19:09:17 +01:00
2023-03-29 10:46:56 +02:00
ldl::Logging("JSONString:"+ JSONStringToMap)
2020-11-08 01:31:57 +01:00
thread_type = ""
2023-03-30 15:40:27 +02:00
If AccessLog <> ""
2021-05-25 10:05:41 +02:00
If m_clients(Str(thread_cli_id))\client_type = #client_HTTPS
2023-03-30 15:40:27 +02:00
ldl::Logging("Connect:"+lsocket::GetSocketIP(tls::GetSocket(thread_cli_id)) + " " + Header(#http_head_method) + " " + Header(#http_head_request), AccessLog)
ldl::Logging("Connect:"+lsocket::GetSocketIP(tls::GetSocket(thread_cli_id)) + " " + Header(#http_head_method) + " " + Header(#http_head_request))
2021-05-25 10:05:41 +02:00
Else
2023-03-30 15:40:27 +02:00
ldl::Logging("Connect:"+lsocket::GetSocketIP(thread_cli_id) + " " + Header(#http_head_method) + " " + Header(#http_head_request), AccessLog)
ldl::Logging("Connect:"+lsocket::GetSocketIP(thread_cli_id) + " " + Header(#http_head_method) + " " + Header(#http_head_request))
2021-05-25 10:05:41 +02:00
EndIf
2023-03-30 15:40:27 +02:00
Else
ldl::Logging("Empty accesslog:["+AccessLog+"]")
2021-05-25 10:05:41 +02:00
EndIf
2024-09-23 05:59:11 +02:00
;TODO: Header Handler
; ex. Cookie: #http_head_cookie
2020-11-08 01:31:57 +01:00
Select Header(#http_head_method)
Case #http_method_get
;********************************
;*
;* Default GET
;*
;{
2023-03-29 10:46:56 +02:00
ldl::Logging(#http_method_get)
2022-02-18 16:05:48 +01:00
2023-03-31 10:40:32 +02:00
;*
;* Detect websocket upgrade request
;*
2022-02-18 16:05:48 +01:00
2023-03-31 10:40:32 +02:00
If LCase(Header(lhs_web_helper::#http_head_connection)) = lhs_web_helper::#http_connection_upgrade
;Whe have a Upgrade Request.
;Current Disconnect it with not supported
response_status = lhs_web_helper::#http_state_501
thread_type = lhs_web_helper::mimetype("html")
thread_temp_cache_memory = AllocateMemory(StringByteLength("Unsuported"))
PokeS(thread_temp_cache_memory, "Unsuported")
thread_data_size = StringByteLength("Unsuported")
If response_status = "" ;Remove if finished...
;detect a route to there.
If Left(Header(#http_head_request),2) = "ws"
;Whe have a websocket request.
;Detect if whe have a library who react to there:
ToCallType = call_request(Host_call + thread_requested, #get_handler_prototype)
If ToCallType = #handler_proto_i_get Or ToCallType = #handler_proto_get
2023-03-31 12:46:49 +02:00
;Call Websocket
2023-03-29 20:06:01 +02:00
EndIf
EndIf
EndIf
2023-03-31 10:40:32 +02:00
2020-11-08 01:31:57 +01:00
Else
2023-03-31 10:40:32 +02:00
If Header(#http_head_request) = "/"
2023-03-29 10:46:56 +02:00
thread_requested = default_file
2022-02-18 16:05:48 +01:00
Else
2023-03-31 10:40:32 +02:00
thread_requested = Header(#http_head_request)
2023-03-29 20:06:01 +02:00
EndIf
2023-03-31 10:40:32 +02:00
ldl::Logging("Requested:"+thread_requested)
ToCallType = call_request(Host_call + thread_requested, #get_handler_prototype)
If ToCallType = #handler_proto_universal Or ToCallType = #handler_proto_get Or ToCallType = #handler_proto_i_universal Or ToCallType = #handler_proto_i_get
ToCall = call_request(Host_call + thread_requested)
If call_request(Host_call + thread_requested, #get_handler_library_perm) = 0
LibraryToCall = call_request_string(Host_call + thread_requested, #get_handler_library_perm)
If Len(LibraryToCall) > 0
Library_ID = OpenLibrary(#PB_Any, LibraryToCall)
If Library_ID
2024-03-15 13:27:20 +01:00
ldl::Logging("Library is opened:"+LibraryToCall)
2023-03-31 10:40:32 +02:00
Else
ldl::Logging("Library could not be opened")
EndIf
EndIf
EndIf
2020-11-03 19:09:17 +01:00
Else
2023-03-31 10:40:32 +02:00
ToCall = 0
2020-11-08 01:31:57 +01:00
EndIf
2023-03-31 10:40:32 +02:00
If ToCall = 0
If Right(thread_requested,1) = "/"
thread_requested = default_file
Else
thread_requested = thread_requested
2022-02-18 16:05:48 +01:00
EndIf
2023-03-31 10:40:32 +02:00
EndIf
If ToCall > 0 ;Dann ist eine Funktion hinterlegt und zulässig aufgerufen zu werden.
;{ Dynamischer WebHandler
Handler_Response = call_function(ToCallType, ToCall, Header())
If call_request(Host_call + thread_requested, #get_handler_library_perm) = 0 And Library_ID <> 0
CloseLibrary(Library_ID)
EndIf
ldl::Logging("Main Client Response :"+Handler_Response)
MyThreadJSON = ParseJSON(#PB_Any, Handler_Response)
If MyThreadJSON
ClearMap(Response())
ExtractJSONMap(JSONValue(MyThreadJSON), Response())
FreeJSON(MyThreadJSON)
2022-02-18 16:05:48 +01:00
Else
2023-03-31 10:40:32 +02:00
;WTF ???
ldl::Logging("Fehler Absturz")
Break 2 ; Thread abschiessen
2022-02-18 16:05:48 +01:00
EndIf
2023-03-31 10:40:32 +02:00
ldl::Logging("Response Content:"+Response(#cha_R_ResponseContentType))
Select Response(#cha_R_ResponseContentType)
Case #response_Memory
ldl::Logging("Response Memory")
thread_data_size = Val(Response(#cha_R_MemorySize))
thread_temp_cache_memory = Val(Response(#cha_R_MemoryAdress))
thread_type = Response(#cha_R_ResponseType)
Case #response_string
ldl::Logging("Response String")
thread_temp_decode_memory = AllocateMemory(StringByteLength(Response(#cha_R_StringBase64)))
thread_data_size = Base64Decoder(Response(#cha_R_StringBase64), thread_temp_decode_memory, StringByteLength(Response(#cha_R_StringBase64)))
thread_temp_cache_memory = AllocateMemory(thread_data_size)
CopyMemory(thread_temp_decode_memory, thread_temp_cache_memory, thread_data_size)
FreeMemory(thread_temp_decode_memory)
thread_type = Response(#cha_R_ResponseType)
Default
;Solte ja nicht passieren.
EndSelect
ldl::Logging("Content Finished")
;}
; ElseIf configuration\cache\enable = 1
; ;{ Cached File Handling BUGGY!!!!!!!
; thread_temp_cache_memory = AllocateMemory(1024)
; thread_temp_cache = GetFileFromCache(thread_requested, thread_temp_cache_memory)
; If thread_temp_cache = #error_string
; thread_file_handle = ReadFile(#PB_Any, configuration\basedir + thread_requested,#PB_File_SharedRead)
;
; If thread_file_handle
; ;Alles Ok
; Else
; thread_file_handle = ReadFile(#PB_Any, configuration\basedir + configuration\defaultfile,#PB_File_SharedRead)
; ldl::Logging("FileDir:" + configuration\basedir + configuration\defaultfile)
; If Not thread_file_handle
; thread_file_handle = ReadFile(#PB_Any, "error.html")
; EndIf
;
; EndIf
; thread_data_size = Lof(thread_file_handle)
; thread_temp_cache_memory = ReAllocateMemory(thread_temp_cache_memory, thread_data_size)
; ReadData(thread_file_handle, thread_temp_cache_memory, thread_data_size)
; CloseFile(thread_file_handle)
; AddFileToCache(thread_temp_cache_memory, thread_requested, thread_data_size)
;
; Else
; thread_data_size = Val(StringField(thread_temp_cache, 1, ":"))
; thread_temp_cache_memory = Val(StringField(thread_temp_cache, 2, ":"))
; EndIf
; ;}
2022-02-17 13:23:42 +01:00
Else
2023-03-31 10:40:32 +02:00
;{ Uncached file sems to be stable tested up to 200 clients and 100 requests.
If file_check(thread_requested, m_clients(Str(thread_cli_id))\host_id) = thread_requested
thread_file_handle = ReadFile(#PB_Any, home_dir + thread_requested,#PB_File_SharedRead)
If Not thread_file_handle
thread_file_handle = ReadFile(#PB_Any, home_dir + default_file ,#PB_File_SharedRead)
ldl::Logging("FileDir:" + home_dir + default_file, ErrorLog)
If Not thread_file_handle
ldl::Logging("Error file set", ErrorLog)
thread_file_handle = ReadFile(#PB_Any, "error.html")
EndIf
EndIf
thread_data_size = Lof(thread_file_handle)
; Is File bigger than MaxFileSize in Memory allowed ?
If thread_data_size >= configuration\hosts(m_clients(Str(thread_cli_id))\host_id)\mem\MaxFileSize
;Do Handle the File another way.
thread_temp_cache_memory = AllocateMemory(configuration\hosts(m_clients(Str(thread_cli_id))\host_id)\mem\MaxFileSize)
thread_temp_file_readed = configuration\hosts(m_clients(Str(thread_cli_id))\host_id)\mem\DefaultBlockSize
thread_data_readed = ReadData(thread_file_handle, thread_temp_cache_memory, thread_temp_file_readed)
thread_data_to_read = thread_data_size - thread_data_readed
thread_oversized_file = #True
Else
thread_temp_cache_memory = AllocateMemory(thread_data_size)
ReadData(thread_file_handle, thread_temp_cache_memory, thread_data_size)
CloseFile(thread_file_handle)
thread_oversized_file = #False
EndIf
Else
thread_redirect = #True
EndIf
;}
2022-02-17 13:23:42 +01:00
EndIf
2023-03-31 10:40:32 +02:00
EndIf
2020-11-08 01:31:57 +01:00
;}
Case #http_method_post
2020-11-03 19:09:17 +01:00
2020-11-08 01:31:57 +01:00
;********************************
;*
;* POST
;*
2021-05-21 07:45:23 +02:00
;* Current Finaly Implementet is only application/x-www-form-urlencoded
;* Error Handling must be Optimized.
;*
2023-03-29 10:46:56 +02:00
ldl::Logging(#http_method_post)
2021-05-21 07:45:23 +02:00
;{ POST Content Type Decoder
2020-11-08 01:31:57 +01:00
If LCase(Header(#http_head_content_type)) = #http_content_type_application_x_www_form_urlencoded
2023-07-18 08:59:36 +02:00
PostMapString = lhs_web_post::mem_x_www_form_urlencoded(Val(Header(#http_head_content_length)), m_clients(Str(thread_cli_id))\datenbuffer()\Size, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer)
2020-11-08 01:31:57 +01:00
MyThreadJSON = ParseJSON(#PB_Any, PostMapString)
If MyThreadJSON
ClearMap(Post())
ExtractJSONMap(JSONValue(MyThreadJSON), Post())
FreeJSON(MyThreadJSON)
Else
2020-11-30 09:52:27 +01:00
;Should not be possible.
2023-03-29 10:46:56 +02:00
ldl::Logging("Error in thread, JSON Convert didn't Work")
2020-11-30 09:52:27 +01:00
Break 2 ; Kill Thread
2020-11-08 01:31:57 +01:00
EndIf
2021-05-21 07:45:23 +02:00
ElseIf Left(LCase(Header(#http_head_content_type)), Len(#http_content_type_multipart_form_data)) = #http_content_type_multipart_form_data
2023-07-18 08:59:36 +02:00
PostMapString = lhs_web_post::mem_x_www_form_urlencoded(Val(Header(#http_head_content_length)), m_clients(Str(thread_cli_id))\datenbuffer()\Size, m_clients(Str(thread_cli_id))\datenbuffer()\Buffer)
2021-05-21 07:45:23 +02:00
MyThreadJSON = ParseJSON(#PB_Any, PostMapString)
If MyThreadJSON
ClearMap(Post())
ExtractJSONMap(JSONValue(MyThreadJSON), Post())
FreeJSON(MyThreadJSON)
Else
;Someting wrong in the POST
2023-03-29 10:46:56 +02:00
ldl::Logging("Error with Content. JSON Convert didn't Work")
2021-05-21 07:45:23 +02:00
Break 2; Kill Thread
EndIf
2020-11-03 19:09:17 +01:00
EndIf
2021-05-21 07:45:23 +02:00
;}
2020-11-03 19:09:17 +01:00
2020-11-08 01:31:57 +01:00
If Header(#http_head_request) = "/"
2023-03-29 10:46:56 +02:00
thread_requested = default_file
2020-11-08 01:31:57 +01:00
Else
thread_requested = Header(#http_head_request)
EndIf
2023-03-29 10:46:56 +02:00
ldl::Logging("Requested:"+thread_requested)
2023-03-29 20:06:01 +02:00
ToCallType = call_request(Host_call+thread_requested, #get_handler_prototype)
2023-03-31 10:40:32 +02:00
If ToCallType = #handler_proto_universal Or ToCallType = #handler_proto_post Or ToCallType = #handler_proto_i_universal Or ToCallType = #handler_proto_i_post
2023-03-29 20:06:01 +02:00
ToCall = call_request(Host_call + thread_requested)
ldl::Logging("A To Call is found:"+Str(ToCall))
If call_request(Host_call + thread_requested, #get_handler_library_perm) = 0
LibraryToCall = call_request_string(Host_call + thread_requested, #get_handler_library)
If Len(LibraryToCall) > 0
Library_ID = OpenLibrary(#PB_Any, LibraryToCall)
If Library_ID
Else
ldl::Logging("Library could not be opened")
EndIf
EndIf
EndIf
2020-11-08 01:31:57 +01:00
Else
ToCall = 0
EndIf
2020-11-03 19:09:17 +01:00
2020-11-30 09:52:27 +01:00
If ToCall > 0 ;A dynamic webhandler is available, call.
;{ Dynamic webhandler.
2020-11-08 01:31:57 +01:00
2023-03-30 09:46:13 +02:00
Handler_Response = call_function(ToCallType, ToCall, Header(), PostMapString)
2023-03-29 20:06:01 +02:00
If call_request(Host_call + thread_requested, #get_handler_library_perm) = 0 And Library_ID <> 0
CloseLibrary(Library_ID)
EndIf
2023-03-29 10:46:56 +02:00
ldl::Logging("Main Client Response :"+Handler_Response)
2020-11-08 01:31:57 +01:00
MyThreadJSON = ParseJSON(#PB_Any, Handler_Response)
If MyThreadJSON
ClearMap(Response())
ExtractJSONMap(JSONValue(MyThreadJSON), Response())
FreeJSON(MyThreadJSON)
Else
;WTF ???
2023-03-29 10:46:56 +02:00
ldl::Logging("Fehler Absturz")
2020-11-08 01:31:57 +01:00
Break 2 ; Thread abschiessen
EndIf
2023-03-29 10:46:56 +02:00
ldl::Logging("Response Content:"+Response(#cha_R_ResponseContentType))
2020-11-08 01:31:57 +01:00
Select Response(#cha_R_ResponseContentType)
Case #response_Memory
thread_data_size = Val(Response(#cha_R_MemorySize))
thread_temp_cache_memory = Val(Response(#cha_R_MemoryAdress))
thread_type = Response(#cha_R_ResponseType)
Case #response_string
thread_temp_decode_memory = AllocateMemory(StringByteLength(Response(#cha_R_StringBase64)))
thread_data_size = Base64Decoder(Response(#cha_R_StringBase64), thread_temp_decode_memory, StringByteLength(Response(#cha_R_StringBase64)))
thread_temp_cache_memory = AllocateMemory(thread_data_size)
CopyMemory(thread_temp_decode_memory, thread_temp_cache_memory, thread_data_size)
FreeMemory(thread_temp_decode_memory)
thread_type = Response(#cha_R_ResponseType)
Default
;Solte ja nicht passieren.
EndSelect
2023-03-29 10:46:56 +02:00
ldl::Logging("Content Finished")
2020-11-08 01:31:57 +01:00
;}
ElseIf conf_cache_enable = 1
2023-03-29 10:46:56 +02:00
; ;{ Cached File Handling BUGGY!!!!!!!
; thread_temp_cache_memory = AllocateMemory(1024)
; thread_temp_cache = GetFileFromCache(thread_requested, thread_temp_cache_memory)
; If thread_temp_cache = #error_string
; thread_file_handle = ReadFile(#PB_Any, "srv/www" + thread_requested,#PB_File_SharedRead)
;
; If thread_file_handle
; ;Alles Ok
; Else
; thread_file_handle = ReadFile(#PB_Any, "srv/www" + "index.html",#PB_File_SharedRead)
; ldl::Logging("FileDir:" + "srv/www" + "index.html")
; If Not thread_file_handle
; thread_file_handle = ReadFile(#PB_Any, "error.html")
; EndIf
;
; EndIf
; thread_data_size = Lof(thread_file_handle)
; thread_temp_cache_memory = ReAllocateMemory(thread_temp_cache_memory, thread_data_size)
; ReadData(thread_file_handle, thread_temp_cache_memory, thread_data_size)
; CloseFile(thread_file_handle)
; AddFileToCache(thread_temp_cache_memory, thread_requested, thread_data_size)
;
; Else
; thread_data_size = Val(StringField(thread_temp_cache, 1, ":"))
; thread_temp_cache_memory = Val(StringField(thread_temp_cache, 2, ":"))
; EndIf
; ;}
2020-11-08 01:31:57 +01:00
Else
2020-11-30 09:52:27 +01:00
;{ Uncached file sems to be stable tested up to 200 clients and 100 requests.
2023-03-29 10:46:56 +02:00
If file_check(thread_requested, m_clients(Str(thread_cli_id))\host_id) = thread_requested
thread_file_handle = ReadFile(#PB_Any, home_dir+ thread_requested,#PB_File_SharedRead)
2022-02-18 16:05:48 +01:00
If Not thread_file_handle
2023-03-29 10:46:56 +02:00
thread_file_handle = ReadFile(#PB_Any, home_dir + default_file ,#PB_File_SharedRead)
ldl::Logging("FileDir:" + home_dir + default_file)
2022-02-18 16:05:48 +01:00
If Not thread_file_handle
2023-03-29 10:46:56 +02:00
ldl::Logging("Error file set")
2022-02-18 16:05:48 +01:00
thread_file_handle = ReadFile(#PB_Any, "error.html")
EndIf
EndIf
2020-11-08 01:31:57 +01:00
2022-02-18 16:05:48 +01:00
thread_data_size = Lof(thread_file_handle)
; Is File bigger than MaxFileSize in Memory allowed ?
2023-03-29 10:46:56 +02:00
If thread_data_size >= configuration\hosts(m_clients(Str(thread_cli_id))\host_id)\mem\MaxFileSize
2022-02-18 16:05:48 +01:00
;Do Handle the File another way.
2023-03-29 10:46:56 +02:00
thread_temp_cache_memory = AllocateMemory(configuration\hosts(m_clients(Str(thread_cli_id))\host_id)\mem\MaxFileSize)
thread_temp_file_readed = configuration\hosts(m_clients(Str(thread_cli_id))\host_id)\mem\DefaultBlockSize
2022-02-18 16:05:48 +01:00
thread_data_readed = ReadData(thread_file_handle, thread_temp_cache_memory, thread_temp_file_readed)
thread_data_to_read = thread_data_size - thread_data_readed
thread_oversized_file = #True
Else
thread_temp_cache_memory = AllocateMemory(thread_data_size)
ReadData(thread_file_handle, thread_temp_cache_memory, thread_data_size)
CloseFile(thread_file_handle)
thread_oversized_file = 0
EndIf
2022-02-17 13:23:42 +01:00
Else
2022-02-18 16:05:48 +01:00
;Send Redirect:
thread_redirect = #True
2022-02-17 13:23:42 +01:00
EndIf
2020-11-08 01:31:57 +01:00
2022-02-18 16:05:48 +01:00
;}
2020-11-08 01:31:57 +01:00
EndIf
2020-11-08 17:31:26 +01:00
2022-02-18 16:05:48 +01:00
2020-11-08 01:31:57 +01:00
Default
;************************************
;*
;* Not a supported Command in HTTP Header Cleanup.
;*
;* Read Buffer to Memory and Clear Buffer to Zero until the complete Networkbuffer from this Client is Cleaned.
;*
EndSelect
2022-02-18 16:05:48 +01:00
If thread_data_size Or thread_redirect = #True
If thread_type = ""
2020-11-08 17:31:26 +01:00
thread_type = mimetype(GetExtensionPart(thread_requested))
EndIf
thread_date = http_day(DayOfWeek(Date())) +
Str(Day(Date())) +
http_month(Month(Date())) +
Str(Year(Date())) +
" " +
FormatDate("%hh:%ii:%ss GMT+1", Date())
ClearMap(Header())
If Response(#cha_R_http_head_status) <> ""
Header(#http_head_status) = Response(#cha_R_http_head_status)
ElseIf response_status <> ""
Header(#http_head_status) = response_status
Else
2024-03-13 09:23:28 +01:00
Header(#http_head_status) = status_get_header("200")
2020-11-08 17:31:26 +01:00
EndIf
2020-12-10 16:37:17 +01:00
If Response(#http_head_set_cookie) <> ""
Header(#http_head_set_cookie) = Response(#http_head_set_cookie)
EndIf
2022-02-18 16:05:48 +01:00
If thread_redirect = #True
2023-03-29 10:46:56 +02:00
Header(#http_head_redirect) = file_check(thread_requested, m_clients(Str(thread_cli_id))\host_id)
2024-09-23 05:59:11 +02:00
;TODO: Verify it's a file or a configuration ...
If Header(#http_head_redirect) = "configuration\errorfile404"
ldl::Logging("Error 404 : ["+configuration\status("404")\content+"]")
Header(#http_head_redirect) = ""
EndIf
2024-03-13 09:23:28 +01:00
Header(#http_head_status) = status_get_header("303")
2022-02-18 16:05:48 +01:00
Else
Header(#http_head_content_length) = Str(thread_data_size)
Header(#http_head_content_type) = thread_type
EndIf
2020-11-08 17:31:26 +01:00
Header(#http_head_connection) = "Keep-Alive"
Header(#http_head_keep_alive) = "timeout=15, max=1000"
2024-03-13 09:23:28 +01:00
2020-11-08 17:31:26 +01:00
thread_header = http_header_generate(Header())
2024-03-13 09:23:28 +01:00
ldl::Logging("Header:"+thread_header)
2022-02-17 13:23:42 +01:00
;large File Handling
2022-02-18 16:05:48 +01:00
If thread_redirect = #True
thread_buffer = AllocateMemory(StringByteLength(thread_header)+12)
ElseIf thread_oversized_file = #False
2022-02-17 13:23:42 +01:00
thread_buffer = AllocateMemory(thread_data_size+StringByteLength(thread_header)+12)
Else
thread_buffer = AllocateMemory(thread_temp_file_readed+StringByteLength(thread_header)+12)
EndIf
2020-11-08 17:31:26 +01:00
thread_buffer_offset = thread_buffer
thread_buffer_length = PokeS(thread_buffer_offset, thread_header,-1, #PB_UTF8|#PB_String_NoZero) : thread_buffer_offset + thread_buffer_length
2023-03-29 10:46:56 +02:00
ldl::Logging("Header Finished")
2020-11-08 17:31:26 +01:00
EndIf
2022-02-17 13:23:42 +01:00
;Copy temporary File Buffer to normal Buffer.
2022-02-18 16:05:48 +01:00
If thread_temp_cache_memory <> 0 And thread_buffer_offset <> 0 And thread_data_size <> 0 And thread_oversized_file = #False And thread_redirect = #False
2020-11-27 21:43:38 +01:00
CopyMemory(thread_temp_cache_memory, thread_buffer_offset, thread_data_size)
2023-03-29 10:46:56 +02:00
ldl::Logging("Cache Address Memory:"+Str(thread_temp_cache_memory))
2020-11-27 21:43:38 +01:00
FreeMemory(thread_temp_cache_memory)
2022-02-17 13:23:42 +01:00
thread_temp_cache_memory = 0
2022-02-18 16:05:48 +01:00
ElseIf thread_temp_cache_memory <> 0 And thread_buffer_offset <> 0 And thread_data_size <> 0 And thread_temp_file_readed <> 0 And thread_oversized_file = #True And thread_redirect = #False
2022-02-17 13:23:42 +01:00
CopyMemory(thread_temp_cache_memory, thread_buffer_offset, thread_temp_file_readed)
2024-03-13 09:23:28 +01:00
ldl::Logging("Oversized Cache Address Memory:"+Str(thread_temp_cache_memory))
2022-02-17 13:23:42 +01:00
FreeMemory(thread_temp_cache_memory)
thread_temp_cache_memory = 0
2022-02-18 16:05:48 +01:00
ElseIf thread_redirect = #True
thread_redirect = #False
2022-02-17 13:23:42 +01:00
Else
2023-03-29 10:46:56 +02:00
ldl::Logging("File Buffer Troubles.")
If thread_temp_cache_memory = 0 : ldl::Logging("thread_temp_cache_memory = 0") : EndIf
If thread_buffer_offset = 0 : ldl::Logging("thread_buffer_offset = 0") : EndIf
If thread_data_size = 0 : ldl::Logging("thread_data_size = 0") : EndIf
2020-12-04 21:03:03 +01:00
thread_alive = #False
Break 2
2020-11-27 21:43:38 +01:00
EndIf
2020-11-03 19:09:17 +01:00
; EndIf
2023-03-29 10:46:56 +02:00
ldl::Logging("HTTP File Buffer Cleaned.")
2020-11-03 19:09:17 +01:00
;Löschen des eingang Speichers.
2020-11-08 01:31:57 +01:00
If m_clients(Str(thread_cli_id))\datenbuffer()\Buffer > 0
FreeMemory(m_clients(Str(thread_cli_id))\datenbuffer()\Buffer)
EndIf
2020-11-03 19:09:17 +01:00
DeleteElement(m_clients(Str(thread_cli_id))\datenbuffer())
2020-11-30 09:52:27 +01:00
;Send the data in memory to client.
2022-02-18 16:05:48 +01:00
If thread_redirect = #True
sent_total = thread_buffer_offset - thread_buffer
ElseIf thread_oversized_file = #False
2022-02-17 13:23:42 +01:00
sent_total = thread_data_size+(thread_buffer_offset-thread_buffer)
Else
sent_total = thread_temp_file_readed+(thread_buffer_offset-thread_buffer)
EndIf
2024-03-13 09:23:28 +01:00
ldl::Logging("Memory send_total:"+Str(sent_total))
2024-09-20 11:07:37 +02:00
;Erledigt: Stoped download kill server...
2020-12-01 08:53:53 +01:00
If m_clients(Str(thread_cli_id))\client_type = #client_HTTPS
2020-11-27 21:43:38 +01:00
sent_length = sent_total
2022-02-17 13:23:42 +01:00
sent_buffer_address = thread_buffer
2020-11-27 21:43:38 +01:00
sent_total = 0
Repeat
2023-03-29 10:46:56 +02:00
sent = tls::WriteTLSSocket(thread_cli_id, sent_buffer_address , sent_length)
2020-11-27 21:43:38 +01:00
If sent <> -1
2024-03-13 09:23:28 +01:00
ldl::Logging("ClientID:" + Str(thread_cli_id) + " HTTPS Sent:"+Str(sent)+" bytes")
2020-11-27 21:43:38 +01:00
sent_length - sent
2022-02-17 13:23:42 +01:00
If thread_oversized_file = #False
sent_buffer_address + sent
Else
;Read next Block to Memory
If sent_length <= 0 And IsFile(thread_file_handle)
sent_readed = ReadData(thread_file_handle, thread_buffer, thread_temp_file_readed)
2024-03-13 09:23:28 +01:00
ldl::Logging("ClientID:" + Str(thread_cli_id) + " HTTPS Large File -> Read Next:"+Str(sent_readed)+" bytes")
2022-02-17 13:23:42 +01:00
If sent_readed < thread_temp_file_readed
CloseFile(thread_file_handle)
EndIf
sent_buffer_address = thread_buffer
sent_length = sent_readed
Else
2024-03-13 09:23:28 +01:00
ldl::Logging("ClientID:" + Str(thread_cli_id) + " HTTPS Large File -> to Send:"+Str(sent_length)+" bytes")
2022-02-17 13:23:42 +01:00
sent_buffer_address + sent
EndIf
EndIf
2020-11-27 21:43:38 +01:00
sent_total + sent
Else
2022-02-17 13:23:42 +01:00
If IsFile(thread_file_handle)
CloseFile(thread_file_handle)
EndIf
2023-03-29 10:46:56 +02:00
ldl::Logging("HTTPS Sent error:"+tls::ErrorTLSCli(thread_cli_id))
2020-12-04 21:03:03 +01:00
thread_alive = #False
Break 2
2020-11-27 21:43:38 +01:00
EndIf
Until sent_length <= 0
sent = sent_total
2020-11-27 11:52:35 +01:00
Else
2020-11-28 16:49:22 +01:00
sent_length = sent_total
sent_buffer_address = thread_buffer
sent_total = 0
Repeat
2024-09-20 11:07:37 +02:00
If lsocket::IsClientSocket(thread_cli_id)
sent = lsocket::WriteSocket(thread_cli_id, thread_buffer , sent_length)
Else
thread_alive=#False
Break 2
EndIf
2020-11-28 16:49:22 +01:00
If sent <> -1
2023-03-29 10:46:56 +02:00
ldl::Logging("HTTP Sent:"+Str(sent)+" bytes")
2020-11-28 16:49:22 +01:00
sent_length - sent
2022-02-17 13:23:42 +01:00
If thread_oversized_file = 0
sent_buffer_address + sent
Else
;Read next Block to Memory
If sent_length <= 0 And IsFile(thread_file_handle)
sent_readed = ReadData(thread_file_handle, thread_buffer, thread_temp_file_readed)
If sent_readed < thread_temp_file_readed
CloseFile(thread_file_handle)
EndIf
sent_buffer_address = thread_buffer
sent_length = sent_readed
Else
sent_buffer_address + sent
EndIf
EndIf
2020-11-28 16:49:22 +01:00
sent_total + sent
Else
2022-02-17 13:23:42 +01:00
If IsFile(thread_file_handle)
CloseFile(thread_file_handle)
EndIf
2023-03-29 10:46:56 +02:00
ldl::Logging("HTTP Sent error:"+Str(sent))
2020-12-04 21:03:03 +01:00
thread_alive = #False
Break 2
2020-11-28 16:49:22 +01:00
EndIf
Until sent_length <= 0
2020-11-30 09:52:27 +01:00
sent = sent_total
2020-11-27 11:52:35 +01:00
EndIf
2020-11-03 19:09:17 +01:00
2020-11-27 11:52:35 +01:00
If sent = thread_data_size+(thread_buffer_offset-thread_buffer)
2020-11-03 19:09:17 +01:00
;Ok
2023-03-29 10:46:56 +02:00
;ldl::Logging("Gesendet:" + PeekS(thread_buffer,thread_buffer_length, #PB_Ascii))
2020-11-30 09:52:27 +01:00
If thread_buffer > 0
FreeMemory(thread_buffer)
EndIf
2020-11-03 19:09:17 +01:00
m_clients(Str(thread_cli_id))\client_do_cli = #CLI_DO_WaitDataReceive
2020-11-30 08:15:50 +01:00
m_clients(Str(thread_cli_id))\client_do_srv = #SRV_DO_DataReceive
2020-11-03 19:09:17 +01:00
Else
;Fehler beim Senden ... Thread beenden.
2023-03-29 10:46:56 +02:00
ldl::Logging("Fehler:" + Str(Sent))
2020-11-03 19:09:17 +01:00
thread_alive = #False
EndIf
;
Wend
;}
m_clients(Str(thread_cli_id))\client_do_cli = #CLI_DO_WaitDataReceive
EndSelect
EndIf
2020-11-08 01:31:57 +01:00
;}
2020-11-03 19:09:17 +01:00
Until thread_alive = #False
2023-03-29 10:46:56 +02:00
ldl::Logging("Thread should now die...")
2020-11-03 19:09:17 +01:00
ResetList(m_clients(Str(thread_cli_id))\datenbuffer())
2023-03-29 10:46:56 +02:00
ldl::Logging("Thread kill all Initialized Memory buffers:")
2020-11-03 19:09:17 +01:00
While NextElement(m_clients(Str(thread_cli_id))\datenbuffer())
If m_clients(Str(thread_cli_id))\datenbuffer()\Initialized
2023-03-29 10:46:56 +02:00
ldl::Logging("Kill:"+Str(m_clients(Str(thread_cli_id))\datenbuffer()\Buffer)+ " ...")
2020-11-03 19:09:17 +01:00
FreeMemory(m_clients(Str(thread_cli_id))\datenbuffer()\Buffer)
DeleteElement(m_clients(Str(thread_cli_id))\datenbuffer())
2023-03-29 10:46:56 +02:00
ldl::Logging("Freed and removed")
2020-11-03 19:09:17 +01:00
Else
2023-03-29 10:46:56 +02:00
ldl::Logging("Remove element from List with uninitialized Memory ID: "+Str(m_clients(Str(thread_cli_id))\datenbuffer()\Buffer))
2020-11-03 19:09:17 +01:00
DeleteElement(m_clients(Str(thread_cli_id))\datenbuffer())
2023-03-29 10:46:56 +02:00
ldl::Logging("Removed")
2020-11-03 19:09:17 +01:00
EndIf
Wend
2020-12-01 08:53:53 +01:00
If m_clients(Str(thread_cli_id))\client_type = #client_HTTPS
2023-03-29 10:46:56 +02:00
ldl::Logging("Kill HTTPS Socket:" + Str(thread_cli_id))
;tls::CloseTLSSocket(thread_cli_id)
2020-12-01 08:53:53 +01:00
count_client(#client_HTTPS, #client_remove)
2023-03-29 10:46:56 +02:00
ldl::Logging("Killed.")
2020-11-28 16:49:22 +01:00
Else
2023-03-29 10:46:56 +02:00
ldl::Logging("Kill HTTP Socket:" + Str(thread_cli_id))
2020-11-28 16:49:22 +01:00
lsocket::CloseSocket(thread_cli_id)
2020-12-01 08:53:53 +01:00
count_client(#client_HTTP, #client_remove)
2023-03-29 10:46:56 +02:00
ldl::Logging("Killed.")
2020-11-27 21:43:38 +01:00
EndIf
2023-03-29 10:46:56 +02:00
ldl::Logging("Remove Client from map...")
2020-11-03 19:09:17 +01:00
DeleteMapElement(m_clients(), Str(thread_cli_id))
2023-03-29 10:46:56 +02:00
ldl::Logging("Removed and thread now died.")
2020-11-03 19:09:17 +01:00
EndProcedure
2023-03-29 20:06:01 +02:00
Procedure.s call_request_string(RequestString.s, Info.i = #get_handler_procedure)
2020-11-08 01:31:57 +01:00
Protected CurrPos, Count, Counter, PartString.s
2023-03-29 20:06:01 +02:00
;"SpecServer:["+Host_ID+"]"
Protected.s Host_ID = ""
Protected HF_Count, HF_Pos
If Left(RequestString,12) = "SpecServer:["
;Whe have host specific
HF_Pos = FindString(RequestString, "]",12)
Host_ID = Left(RequestString, HF_Pos)
EndIf
ldl::Logging("call_request: "+RequestString)
2020-11-08 01:31:57 +01:00
If m_request(GetExtensionPart(RequestString))\routetype = #handler_type
Select Info
Case #get_handler_procedure
2023-03-29 20:06:01 +02:00
ProcedureReturn Str(m_request(Host_ID+GetExtensionPart(RequestString))\call)
2020-11-08 01:31:57 +01:00
Case #get_handler_prototype
2023-03-29 20:06:01 +02:00
ProcedureReturn Str(m_request(Host_ID+GetExtensionPart(RequestString))\type)
Case #get_handler_library
ProcedureReturn m_request(Host_ID+GetExtensionPart(RequestString))\library
Case #get_handler_library_perm
ProcedureReturn Str(m_request(Host_ID+GetExtensionPart(RequestString))\perm)
2020-11-08 01:31:57 +01:00
Default
2023-03-29 20:06:01 +02:00
ProcedureReturn "0"
2020-11-08 01:31:57 +01:00
EndSelect
EndIf
2023-03-29 20:06:01 +02:00
ldl::Logging("call_request: "+RequestString + " Routetype :["+Str(m_request(RequestString)\routetype)+"]")
2020-11-08 01:31:57 +01:00
If m_request(RequestString)\routetype = #handler_only Or m_request(RequestString)\routetype = #handler_sub
Select Info
Case #get_handler_procedure
2023-03-29 20:06:01 +02:00
ProcedureReturn Str(m_request(RequestString)\call)
2020-11-08 01:31:57 +01:00
Case #get_handler_prototype
2023-03-29 20:06:01 +02:00
ProcedureReturn Str(m_request(RequestString)\type)
Case #get_handler_library
ProcedureReturn m_request(RequestString)\library
Case #get_handler_library_perm
ProcedureReturn Str(m_request(RequestString)\perm)
2020-11-08 01:31:57 +01:00
Default
2023-03-29 20:06:01 +02:00
ProcedureReturn "0"
2020-11-08 01:31:57 +01:00
EndSelect
ElseIf m_request(GetExtensionPart(RequestString))\routetype = #handler_sub
Select Info
Case #get_handler_procedure
2023-03-29 20:06:01 +02:00
ProcedureReturn Str(m_request(Host_ID+GetExtensionPart(RequestString))\call)
2020-11-08 01:31:57 +01:00
Case #get_handler_prototype
2023-03-29 20:06:01 +02:00
ProcedureReturn Str(m_request(Host_ID+GetExtensionPart(RequestString))\type)
Case #get_handler_library
ProcedureReturn m_request(Host_ID+GetExtensionPart(RequestString))\library
Case #get_handler_library_perm
ProcedureReturn Str(m_request(Host_ID+GetExtensionPart(RequestString))\perm)
2020-11-08 01:31:57 +01:00
Default
2023-03-29 20:06:01 +02:00
ProcedureReturn "0"
2020-11-08 01:31:57 +01:00
EndSelect
EndIf
2023-03-29 20:06:01 +02:00
ldl::Logging("call_request now on sub check: "+RequestString + " Routetype :["+Str(m_request(RequestString)\routetype)+"]")
2020-11-08 01:31:57 +01:00
;Check auf Sub
Counter = CountString(RequestString, "/")
Count = 0
CurrPos = 0
While Count < Counter
CurrPos = FindString(RequestString, "/", 2 + CurrPos )
PartString = Mid(RequestString, 1, CurrPos)
If m_request(PartString)\routetype = #handler_sub
Select Info
Case #get_handler_procedure
2023-03-29 20:06:01 +02:00
ProcedureReturn Str(m_request(PartString)\call)
2020-11-08 01:31:57 +01:00
Case #get_handler_prototype
2023-03-29 20:06:01 +02:00
ProcedureReturn Str(m_request(PartString)\type)
Case #get_handler_library
ProcedureReturn m_request(PartString)\library
Case #get_handler_library_perm
ProcedureReturn Str(m_request(PartString)\type)
2020-11-08 01:31:57 +01:00
Default
2023-03-29 20:06:01 +02:00
ProcedureReturn "0"
2020-11-08 01:31:57 +01:00
EndSelect
EndIf
Count + 1
Wend
2023-03-29 20:06:01 +02:00
ldl::Logging("call_request Not found !!!!!!!!! : "+RequestString + "Routetype :["+Str(m_request(RequestString)\routetype)+"]")
ProcedureReturn "0"
EndProcedure
Procedure call_request(RequestString.s, Info.i = #get_handler_procedure)
ProcedureReturn Val(call_request_string(RequestString.s, Info.i))
2020-11-08 01:31:57 +01:00
EndProcedure
2023-03-29 20:06:01 +02:00
Procedure.s register_client_handler(Route.s, Callback.i, AppPrototype.i = #handler_proto_get, RouteType.i = #handler_sub, Host.s = "")
If Len(Host) > 0
Host = "SpecServer:["+Host+"]"
EndIf
m_request(Host+Route)\type = AppPrototype
m_request(Host+Route)\call = Callback
m_request(Host+Route)\routetype = RouteType
m_request(Host+Route)\route = Route
m_request(Host+Route)\host = Host
ldl::Logging("Handler Registriert:"+Route+ " at Host:["+Host+"]")
EndProcedure
Procedure advanced_register_handler(Route.s, Permament.i = 0, Library.s = "", Host.s="")
If Len(Host) > 0
Host = "SpecServer:["+Host+"]"
EndIf
m_request(Host+Route)\perm = Permament
m_request(Host+Route)\library = Library
2020-11-08 01:31:57 +01:00
EndProcedure
2023-03-30 09:46:13 +02:00
Procedure.s call_function(ToCallType.i, ToCall.i, Map Header.s(), PostMapString.s ="")
Define.i Handler_Response_Adress
Define.s Handler_Response, Header_string
Header_string = MapToJSONString(Header())
Select ToCallType
Case #handler_proto_universal
Define.WebHandler_Universal ToCallProcedure_Universal = ToCall
Handler_Response_Adress = ToCallProcedure_Universal(Header_string, PostMapString )
ldl::Logging("Response Address:"+Str(Handler_Response_Adress))
Handler_Response = PeekS(Handler_Response_Adress)
Case #handler_proto_get
Define.WebHandler_Get ToCallProcedureGet = ToCall
Handler_Response_Adress = ToCallProcedureGet(Header_string)
Handler_Response = PeekS(Handler_Response_Adress)
Case #handler_proto_post
Define.WebHandler_Post ToCallProcedure_Post = ToCall
Handler_Response_Adress = ToCallProcedure_Post(Header_string, PostMapString)
Handler_Response = PeekS(Handler_Response_Adress)
Case #handler_proto_i_universal
Define.WebHandler_i_Universal ToCallProcedure_i_Universal = ToCall
Handler_Response = ToCallProcedure_i_Universal(Header_string, PostMapString )
Case #handler_proto_i_get
Define.WebHandler_i_get ToCallProcedure_i_get = ToCall
Handler_Response = ToCallProcedure_i_get(Header_string)
Case #handler_proto_i_post
Define.WebHandler_i_Universal ToCallProcedure_i_post = ToCall
Handler_Response = ToCallProcedure_i_post(Header_string, PostMapString )
EndSelect
ProcedureReturn Handler_Response
EndProcedure
2020-12-01 08:53:53 +01:00
Procedure count_client(Type.i, Countchange.i)
Select Type
Case #client_HTTP
LockMutex(count_HTTP_mutex)
count_HTTP_client + Countchange
UnlockMutex(count_HTTP_mutex)
Case #client_HTTPS
LockMutex(count_HTTPS_mutex)
count_HTTPS_client + Countchange
UnlockMutex(count_HTTPS_mutex)
EndSelect
EndProcedure
2023-03-29 10:46:56 +02:00
Procedure.s file_check(thread_requested.s, hostid.s)
2022-02-18 16:05:48 +01:00
Protected file_type.i
2023-03-29 10:46:56 +02:00
Debug thread_requested
file_type = FileSize(configuration\hosts(hostid)\basedir + thread_requested)
2022-02-18 16:05:48 +01:00
If file_type = -1
2023-03-29 10:46:56 +02:00
thread_requested = "configuration\errorfile404"
2022-02-18 16:05:48 +01:00
ElseIf file_type = -2
;Is a Directory
If Right(thread_requested,1) = "/"
2023-03-29 10:46:56 +02:00
thread_requested = thread_requested + "index.html"
2022-02-18 16:05:48 +01:00
Else
2023-03-29 10:46:56 +02:00
thread_requested = thread_requested + "/" + "index.html"
2022-02-18 16:05:48 +01:00
EndIf
EndIf
2023-03-29 10:46:56 +02:00
Debug "Final:"+thread_requested
2022-02-18 16:05:48 +01:00
ProcedureReturn thread_requested.s
EndProcedure
2020-11-03 19:09:17 +01:00
EndModule