Initial Websocket and Bugfix for prototype support

This commit is contained in:
René Linder 2023-03-31 10:40:32 +02:00
parent 194245d3b6
commit 8574b29021

270
lweb.pbi
View file

@ -677,141 +677,173 @@ Module lhs_web
;{
ldl::Logging(#http_method_get)
If Header(#http_head_request) = "/"
thread_requested = default_file
Else
thread_requested = Header(#http_head_request)
EndIf
;*
;* Detect websocket upgrade request
;*
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
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
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
Else
ldl::Logging("Library could not be opened")
EndIf
EndIf
EndIf
Else
ToCall = 0
EndIf
If ToCall = 0
If Right(thread_requested,1) = "/"
If Header(#http_head_request) = "/"
thread_requested = default_file
Else
thread_requested = thread_requested
EndIf
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)
thread_requested = Header(#http_head_request)
EndIf
ldl::Logging("Main Client Response :"+Handler_Response)
MyThreadJSON = ParseJSON(#PB_Any, Handler_Response)
If MyThreadJSON
ClearMap(Response())
ExtractJSONMap(JSONValue(MyThreadJSON), Response())
FreeJSON(MyThreadJSON)
Else
;WTF ???
ldl::Logging("Fehler Absturz")
Break 2 ; Thread abschiessen
EndIf
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")
;}
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
; 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
; ;}
Else
;{ 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")
Else
ldl::Logging("Library could not be opened")
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
EndIf
Else
thread_redirect = #True
ToCall = 0
EndIf
If ToCall = 0
If Right(thread_requested,1) = "/"
thread_requested = default_file
Else
thread_requested = thread_requested
EndIf
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)
Else
;WTF ???
ldl::Logging("Fehler Absturz")
Break 2 ; Thread abschiessen
EndIf
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
; ;}
Else
;{ 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
;}
EndIf
EndIf
EndIf
;}
Case #http_method_post
@ -859,7 +891,7 @@ Module lhs_web
EndIf
ldl::Logging("Requested:"+thread_requested)
ToCallType = call_request(Host_call+thread_requested, #get_handler_prototype)
If ToCallType = #handler_proto_universal Or ToCallType = #handler_proto_post
If ToCallType = #handler_proto_universal Or ToCallType = #handler_proto_post Or ToCallType = #handler_proto_i_universal Or ToCallType = #handler_proto_i_post
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