First Dynamic Handler release

This commit is contained in:
René Linder 2023-03-29 20:06:01 +02:00
parent 22b40bd09e
commit 0b540f0fa7
2 changed files with 216 additions and 41 deletions

249
lweb.pbi
View file

@ -135,6 +135,10 @@ Module lhs_web
call.i
type.i
routetype.i
host.s
route.s
library.s
perm.i
EndStructure
Structure s_server_runs
@ -148,6 +152,8 @@ Module lhs_web
Enumeration cli_handler_infos 1
#get_handler_procedure ;Funktion die Aufgerufen werden muss
#get_handler_prototype ;Welcher Prototype
#get_handler_library ;Library to open
#get_handler_library_perm ;Library permanent in memory?
EndEnumeration
Global.i count_HTTP_client
@ -175,9 +181,9 @@ Module lhs_web
;* Handler Prototypen
;*
Prototype.s WebHandler_Get(Map handler_Map.s())
Prototype.s WebHandler_Post(Map handler_Map.s(), ContentString.s)
Prototype.s WebHandler_Universal(Map handler_Map.s(), ContentString.s)
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)
;********************************
@ -198,8 +204,9 @@ Module lhs_web
Declare server_HTTPS(network_server_id.i)
Declare client(network_client_id.i)
Declare.s file_check(thread_requested.s, hostid.s)
Declare.s call_request_string(RequestString.s, Info.i=#get_handler_procedure)
Declare call_request(RequestString.s, Info.i=#get_handler_procedure)
Declare advanced_register_handler(RequesterString.s, Permament.i = 0, Library.s = "", Host.s = "")
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)
Declare count_client(Type.i, Countchange.i)
@ -212,6 +219,7 @@ Module lhs_web
IncludeFile "lhs_lib/SYS/lhs_uuid.pbi" ;It is needed as local function.
XIncludeFile "inc/lweb_server_cfg.pbi"
;XIncludeFile "inc/lweb_dynamic.pbi"
Procedure server_reload(UUID.s)
@ -262,6 +270,12 @@ Module lhs_web
Protected counter.i, id.i
Global NewMap m_clients.s_clients(configuration\global_max_cli_threads) ;configuration\http\max_clients+configuration\https\max_clients
Protected tlsresponse.i
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
;Initialize all Servers in configuration\hosts()
ResetMap(configuration\hosts())
While NextMapElement(configuration\hosts())
@ -300,6 +314,85 @@ Module lhs_web
ldl::Register(lhs_log_ext::@Out(), configuration\hosts()\log\ErrorlogUUID)
ldl::Logging("Accesslog UUID:"+configuration\hosts()\log\AccesslogUUID)
ldl::Logging("Errorlog UUID:"+configuration\hosts()\log\ErrorlogUUID)
;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
EndSelect
ldl::Logging("react:"+current_register_react+" file:"+configuration\hosts()\dynamichandler()\file+" Host ID:"+MapKey(configuration\hosts()))
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()))
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
Else
ldl::Logging("HTTP Socket ["+configuration\hosts()\http\port+"] could Not be opened.")
End
@ -413,7 +506,7 @@ Module lhs_web
Protected MyThreadJSON, ToCall, ToCallType
Protected thread_temp_cache.s, thread_temp_cache_memory, temp_receivelength, thread_temp_decode_memory
Protected thread_reasign, thread_oversized_file.b, thread_data_to_read.i, thread_data_readed.i
Protected thread_data_size, thread_file_handle
Protected thread_data_size, thread_file_handle, Handle_Response_Address
Protected.s thread_requested, thread_type, thread_date, thread_header, thread_work, JSONStringToMap, Handler_Response, response_status, PostMapString
Protected thread_buffer, thread_buffer_offset, thread_buffer_length, buffer_sent
Protected sent_length, sent_buffer_address, sent_total
@ -423,6 +516,10 @@ Module lhs_web
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
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()
Define NewMap Response.s()
Define NewMap Post.s()
@ -567,9 +664,20 @@ Module lhs_web
EndIf
ldl::Logging("Requested:"+thread_requested)
ToCallType = call_request(thread_requested, #get_handler_prototype)
ToCallType = call_request(Host_call + thread_requested, #get_handler_prototype)
If ToCallType = #handler_proto_universal Or ToCallType = #handler_proto_get
ToCall = call_request(thread_requested)
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
Else
ldl::Logging("Library could not be opened")
EndIf
EndIf
EndIf
Else
ToCall = 0
EndIf
@ -583,17 +691,21 @@ Module lhs_web
If ToCall > 0 ;Dann ist eine Funktion hinterlegt und zulässig aufgerufen zu werden.
;{ Dynamischer WebHandler
Header_string = MapToJSONString(Header())
Select ToCallType
Case #handler_proto_universal
Define.WebHandler_Universal ToCallProcedure = ToCall
Handler_Response = ToCallProcedure(Header(), "")
Handler_Response_Adress = ToCallProcedure(Header_string, "")
Handler_Response = PeekS(Handler_Response_Adress)
Case #handler_proto_get
Define.WebHandler_Get ToCallProcedureGet = ToCall
Handler_Response = ToCallProcedureGet(Header())
Handler_Response_Adress = ToCallProcedureGet(Header_string)
Handler_Response = PeekS(Handler_Response_Adress)
EndSelect
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
@ -735,9 +847,21 @@ Module lhs_web
thread_requested = Header(#http_head_request)
EndIf
ldl::Logging("Requested:"+thread_requested)
ToCallType = call_request(thread_requested, #get_handler_prototype)
ToCallType = call_request(Host_call+thread_requested, #get_handler_prototype)
If ToCallType = #handler_proto_universal Or ToCallType = #handler_proto_post
ToCall = call_request(thread_requested)
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
Else
ToCall = 0
EndIf
@ -745,16 +869,21 @@ Module lhs_web
If ToCall > 0 ;A dynamic webhandler is available, call.
;{ Dynamic webhandler.
Header_string = MapToJSONString(Header())
Select ToCallType
Case #handler_proto_universal
Define.WebHandler_Universal ToCallProcedure_Universal = ToCall
Handler_Response = ToCallProcedure_Universal(Header(), PostMapString )
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_post
Define.WebHandler_Post ToCallProcedure_Post = ToCall
Handler_Response = ToCallProcedure_Post(Header(), PostMapString)
Handler_Response_Adress = ToCallProcedure_Post(Header_string, PostMapString)
Handler_Response = PeekS(Handler_Response_Adress)
EndSelect
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
@ -1089,38 +1218,61 @@ Module lhs_web
EndProcedure
Procedure call_request(RequestString.s, Info.i = #get_handler_procedure)
Procedure.s call_request_string(RequestString.s, Info.i = #get_handler_procedure)
Protected CurrPos, Count, Counter, PartString.s
;"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)
If m_request(GetExtensionPart(RequestString))\routetype = #handler_type
Select Info
Case #get_handler_procedure
ProcedureReturn m_request(GetExtensionPart(RequestString))\call
ProcedureReturn Str(m_request(Host_ID+GetExtensionPart(RequestString))\call)
Case #get_handler_prototype
ProcedureReturn m_request(GetExtensionPart(RequestString))\type
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)
Default
ProcedureReturn 0
ProcedureReturn "0"
EndSelect
EndIf
ldl::Logging("call_request: "+RequestString + " Routetype :["+Str(m_request(RequestString)\routetype)+"]")
If m_request(RequestString)\routetype = #handler_only Or m_request(RequestString)\routetype = #handler_sub
Select Info
Case #get_handler_procedure
ProcedureReturn m_request(RequestString)\call
ProcedureReturn Str(m_request(RequestString)\call)
Case #get_handler_prototype
ProcedureReturn m_request(RequestString)\type
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)
Default
ProcedureReturn 0
ProcedureReturn "0"
EndSelect
ElseIf m_request(GetExtensionPart(RequestString))\routetype = #handler_sub
Select Info
Case #get_handler_procedure
ProcedureReturn m_request(GetExtensionPart(RequestString))\call
ProcedureReturn Str(m_request(Host_ID+GetExtensionPart(RequestString))\call)
Case #get_handler_prototype
ProcedureReturn m_request(GetExtensionPart(RequestString))\type
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)
Default
ProcedureReturn 0
ProcedureReturn "0"
EndSelect
EndIf
ldl::Logging("call_request now on sub check: "+RequestString + " Routetype :["+Str(m_request(RequestString)\routetype)+"]")
;Check auf Sub
Counter = CountString(RequestString, "/")
@ -1132,17 +1284,26 @@ Module lhs_web
If m_request(PartString)\routetype = #handler_sub
Select Info
Case #get_handler_procedure
ProcedureReturn m_request(PartString)\call
ProcedureReturn Str(m_request(PartString)\call)
Case #get_handler_prototype
ProcedureReturn m_request(PartString)\type
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)
Default
ProcedureReturn 0
ProcedureReturn "0"
EndSelect
EndIf
Count + 1
Wend
ldl::Logging("call_request Not found !!!!!!!!! : "+RequestString + "Routetype :["+Str(m_request(RequestString)\routetype)+"]")
ProcedureReturn 0
ProcedureReturn "0"
EndProcedure
Procedure call_request(RequestString.s, Info.i = #get_handler_procedure)
ProcedureReturn Val(call_request_string(RequestString.s, Info.i))
EndProcedure
Procedure.s Work_Post_ToJSON_x_www_form_urlencoded(ContentLength.i, MemorSize.i, Memory.i)
@ -1197,11 +1358,25 @@ Module lhs_web
ProcedureReturn "NA"
EndProcedure
Procedure.s register_client_handler(Route.s, Callback.i, AppPrototype.i = #handler_proto_get, RouteType.i = #handler_sub)
m_request(Route)\type = AppPrototype
m_request(Route)\call = Callback
m_request(Route)\routetype = RouteType
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID),lhs_log_ext::#Debugging,"Handler Registriert:"+Route)
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
EndProcedure
Procedure count_client(Type.i, Countchange.i)

View file

@ -113,7 +113,7 @@ DeclareModule lhs_web
Declare server_reload(UUID.s)
Declare server_config(UUID.s, XMLStructure.s)
Declare server_stop(UUID.s = "all")
Declare.s register_client_handler(Route.s, Callback.i, AppPrototype.i = #handler_proto_get, RouteType.i = #handler_sub)
Declare.s register_client_handler(Route.s, Callback.i, AppPrototype.i = #handler_proto_get, RouteType.i = #handler_sub, Host.s = "")
XIncludeFile "inc/lweb_IP_header.pbi"
XIncludeFile "inc/lweb_helper_header.pbi"