New server_example

This commit is contained in:
René Linder 2023-03-27 17:38:13 +02:00
parent ae8cc74d27
commit 2827ee8a78
2 changed files with 124 additions and 152 deletions

View file

@ -14,7 +14,6 @@ EnableExplicit
;* Includes:
;*
XIncludeFile "lhs_lib/SYS/lhs_log.pbi" ;Currently for Debugging
XIncludeFile "lhs_lib/SYS/lhs_log_ext.pbi" ;User for Access Log, Error Access Log, Error Log and maybe more.
XIncludeFile "lweb_header.pbi"
XIncludeFile "lweb.pbi"
@ -24,173 +23,39 @@ XIncludeFile "lweb.pbi"
;*
Define counter, v_lweb_srv_stop
Define Accesslog.s, Errorlog.s, Cachelog.s
;*
;* Basic Debug Log initialization:
;*
lhs_log::App_Name = "http-s_server_example"
lhs_log::SetLogFile("http-s_server_example_debug.log")
lhs_log::SetMaxSize(32)
lhs_log::SetLogDateFormat("%yyyy.%mm.%dd %hh:%ii:%ss")
lhs_log::SetLogFileDateFormat("%yyyy_%mm_%dd_%hh_%ii_%ss")
lhs_log::Init()
Global Accesslog.s, Errorlog.s, Cachelog.s, DebugLog.s
;*
;* Enable extended Logging:
;*
Accesslog = lhs_log_ext::Create("Accesslog")
Errorlog = lhs_log_ext::Create("Errorlog")
Cachelog = lhs_log_ext::Create("Cachelog")
lhs_log::Out("Errorlog UUID:"+Errorlog)
lhs_log::Out("Accesslog UUID:"+Accesslog)
lhs_log::Out("Cachelog UUID:"+Cachelog)
lhs_log_ext::SetLogFile(Accesslog, "http-s_access.log")
lhs_log_ext::SetLogFile(Errorlog, "http-s_error.log")
lhs_log_ext::SetLogFile(Cachelog, "http-s_cache.log")
lhs_log_ext::Init(Accesslog)
lhs_log_ext::Init(Errorlog)
lhs_log_ext::Init(Cachelog)
lhs_web::set_config(lhs_web::#conf_Access_logUUID, Accesslog)
lhs_web::set_config(lhs_web::#conf_Error_logUUID, ErrorLog)
lhs_web::set_config(lhs_web::#conf_Cache_logUUID, Cachelog)
lhs_web::set_config(lhs_web::#conf_HTTP_port, "8084")
;lhs_web::set_config(lhs_web::#conf_binding, "127.0.0.1")
lhs_web::set_config(lhs_web::#conf_HTTP_binding, "0.0.0.0")
lhs_web::set_config(lhs_web::#conf_HTTPS_CA, "fullchain.pem")
lhs_web::set_config(lhs_web::#conf_HTTPS_Cert, "cert.pem")
lhs_web::set_config(lhs_web::#conf_HTTPS_Key, "privkey.pem")
lhs_web::set_config(lhs_web::#conf_HTTPS_Key_Pass, "")
lhs_web::set_config(lhs_web::#conf_HTTPS_Binding, "0.0.0.0")
lhs_web::set_config(lhs_web::#conf_HTTPS_Port, "8446")
lhs_web::set_config(lhs_web::#conf_HTTPS_Enable, "1")
lhs_web::set_config(lhs_web::#conf_defaultfile, "index.html")
lhs_web::set_config(lhs_web::#conf_basedir, "/home/renlin/testweb/")
lhs_web::set_config(lhs_web::#conf_error400, "integrated")
lhs_web::set_config(lhs_web::#conf_max_HTTP_clients, "100")
lhs_web::set_config(lhs_web::#conf_max_HTTPS_clients, "100")
lhs_web::set_config(lhs_web::#conf_cache_enable, "0")
DebugLog = lhs_log_ext::Create("DebugLog")
lhs_log_ext::SetLogFile(DebugLog, "http-s_debug.log")
lhs_log_ext::Init(DebugLog)
lhs_web::set_config(lhs_web::#conf_Debug_logUUID, DebugLog)
;*
;* includes
;*
Procedure.s Sample_Header(Map Header.s())
Define *Text
Define.s Encoded, ZumSenden, TBD, Text
NewMap Response.s()
If Header("cookie:") <> ""
lhs_log::Out("A Cookie is set :" + Header("cookie:"))
Else
lhs_log::Out("No Cookie.")
EndIf
TBD="<div><div><h1>Header</h1></div><br/>"+#CRLF$
ResetMap(Header())
While NextMapElement(Header())
TBD = TBD+"<div>"+MapKey(Header())+" "+Header()+" </div><br/>"+#CRLF$
Wend
Text = "<!DOCTYPE html><html><header><title>Alle Client Headers</title></header><body>"+TBD+"</body></html>"
*Text = AllocateMemory(StringByteLength(Text))
PokeS(*Text, Text)
Encoded = Base64Encoder(*Text, MemorySize(*Text))
lhs_log::Out("Encoded: " + Encoded)
Response(lhs_web::#cha_R_ResponseContentType) = lhs_web::#response_string
Response(lhs_web::#cha_R_StringBase64) = Encoded
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
Response(lhs_web::#cha_R_http_head_status) = "200 OK"
If Header("cookie:") = ""
Response(lhs_web::#http_head_set_cookie) = "session=test"
EndIf
ZumSenden = lhs_web::MapToJSONString(Response())
lhs_log::Out("ZumSenden: " + ZumSenden)
ProcedureReturn ZumSenden
EndProcedure
Procedure.s Formular_Test(Map Header.s(), ContentData.s)
Define *Text
Define.s Encoded, ZumSenden, TBD, Text
NewMap Response.s()
TBD="<div><div><h1>FormularPost</h1></div><br/>"+#CRLF$
ResetMap(Header())
While NextMapElement(Header())
TBD = TBD+"<div>"+MapKey(Header())+" : "+Header()+" </div><br/>"+#CRLF$
Wend
lhs_log::Out("TBD:"+TBD)
lhs_log::Out("contentData:"+ContentData)
ContentData = URLDecoder(ContentData, #PB_UTF8)
Text = ~"<html lang=\"de\">" +
"<head>" +
~"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>" +
"<title>Alle Client Headers</title></header><body>"+TBD+"<hr/><br/><h1>ConentJSON</h1><br/>"+ContentData+"</body></html>"
*Text = AllocateMemory(StringByteLength(Text, #PB_UTF8))
PokeS(*Text, Text, -1, #PB_UTF8)
lhs_log::Out("Unencoded:"+PeekS(*Text, -1 , #PB_UTF8))
Encoded = Base64Encoder(*Text, MemorySize(*Text))
lhs_log::Out("Encoded: " + Encoded)
Response(lhs_web::#cha_R_ResponseContentType) = lhs_web::#response_string
Response(lhs_web::#cha_R_StringBase64) = Encoded
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
Response(lhs_web::#cha_R_http_head_status) = "200 OK"
ZumSenden = lhs_web::MapToJSONString(Response())
lhs_log::Out("ZumSenden: " + ZumSenden)
ProcedureReturn ZumSenden
EndProcedure
Procedure.s Php_Test(Map Header.s(), ContentData.s)
Define *Text, Php
Define.s Execute, Phpresult, Encoded, ZumSenden, Text
NewMap Response.s()
Execute = lhs_web::get_config(lhs_web::#conf_basedir) + Header(lhs_web::#http_head_request)
lhs_log::Out("To Execute:" + Execute)
Php = RunProgram("php", Execute, "", #PB_Program_Open | #PB_Program_Read)
Phpresult = ""
If Php
While ProgramRunning(Php)
If AvailableProgramOutput(Php)
Phpresult + ReadProgramString(Php) + #CRLF$
EndIf
Wend
CloseProgram(Php) ; Schließt die Verbindung zum Programm
EndIf
Text = Phpresult
*Text = AllocateMemory(StringByteLength(Text, #PB_UTF8))
PokeS(*Text, Text, -1, #PB_UTF8)
lhs_log::Out("Unencoded:"+PeekS(*Text, -1 , #PB_UTF8))
Encoded = Base64Encoder(*Text, MemorySize(*Text))
lhs_log::Out("Encoded: " + Encoded)
Response(lhs_web::#cha_R_ResponseContentType) = lhs_web::#response_string
Response(lhs_web::#cha_R_StringBase64) = Encoded
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
Response(lhs_web::#cha_R_http_head_status) = "200 OK"
ZumSenden = lhs_web::MapToJSONString(Response())
lhs_log::Out("ZumSenden: " + ZumSenden)
ProcedureReturn ZumSenden
EndProcedure
lhs_web::register_client_handler("/server/show_client_headers", @Sample_Header(),lhs_web::#handler_proto_get, lhs_web::#handler_sub)
lhs_web::register_client_handler("/post_test", @Formular_Test(),lhs_web::#handler_proto_post, lhs_web::#handler_sub) ;Im zusammenspiel mit einem Formular
lhs_web::register_client_handler("php", @Php_Test(),lhs_web::#handler_proto_universal, lhs_web::#handler_type)
XIncludeFile "server_example_function.pbi"
;*
;* Start http & https Server
;*
If lhs_web::start_server()
lhs_log::Out("Server gestartet" + lhs_web::get_config(lhs_web::#conf_HTTP_port))
If lhs_web::server_initial("cfg/server_config.xml")
lhs_log_ext::OutL(DebugLog, lhs_log_ext::#Info, "config succesfully loaded.")
Else
lhs_log::Out("Fehlgeschlagen")
lhs_log_ext::OutL(DebugLog, lhs_log_ext::#Error, "config load failed.")
End
EndIf
;If lhs_web::start_server("Default")
; lhs_log_ext::OutL(DebugLog, lhs_log_ext::#Debugging,"Server gestartet" + lhs_web::get_config(lhs_web::#conf_HTTP_port))
;Else
; lhs_log_ext::OutL(DebugLog, lhs_log_ext::#Debugging,"Fehlgeschlagen")
; End
;EndIf
counter = 0
OpenConsole("Test")
PrintN("Server_Sample and tests")
@ -198,8 +63,8 @@ PrintN("HTTP an Port:"+lhs_web::get_config(lhs_web::#conf_HTTP_port))
PrintN("HTTPS an Port:"+lhs_web::get_config(lhs_web::#conf_HTTPS_Port))
PrintN("Press Enter to Exit")
Input()
lhs_log::Close()
lhs_log_ext::StopAllLogger()
lhs_web::server_stop()
PrintN("Finished")
End

107
server_example_function.pbi Normal file
View file

@ -0,0 +1,107 @@
Procedure.s Sample_Header(Map Header.s())
Define *Text
Define.s Encoded, ZumSenden, TBD, Text
NewMap Response.s()
If Header("cookie:") <> ""
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"A Cookie is set :" + Header("cookie:"))
Else
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"No Cookie.")
EndIf
TBD="<div><div><h1>Header</h1></div><br/>"+#CRLF$
ResetMap(Header())
While NextMapElement(Header())
TBD = TBD+"<div>"+MapKey(Header())+" "+Header()+" </div><br/>"+#CRLF$
Wend
Text = "<!DOCTYPE html><html><header><title>Alle Client Headers</title></header><body>"+TBD+"</body></html>"
*Text = AllocateMemory(StringByteLength(Text))
PokeS(*Text, Text)
Encoded = Base64Encoder(*Text, MemorySize(*Text))
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Encoded: " + Encoded)
Response(lhs_web::#cha_R_ResponseContentType) = lhs_web::#response_string
Response(lhs_web::#cha_R_StringBase64) = Encoded
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
Response(lhs_web::#cha_R_http_head_status) = lhs_web::status_get_header("200")
If Header("cookie:") = ""
Response(lhs_web::#http_head_set_cookie) = "session=this will be after second call here"
EndIf
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Status Header get: " + lhs_web::status_get_header("200"))
ZumSenden = lhs_web::MapToJSONString(Response())
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"ZumSenden: " + ZumSenden)
ProcedureReturn ZumSenden
EndProcedure
Procedure.s Formular_Test(Map Header.s(), ContentData.s)
Define *Text
Define.s Encoded, ZumSenden, TBD, Text
NewMap Response.s()
TBD="<div><div><h1>FormularPost</h1></div><br/>"+#CRLF$
ResetMap(Header())
While NextMapElement(Header())
TBD = TBD+"<div>"+MapKey(Header())+" : "+Header()+" </div><br/>"+#CRLF$
Wend
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"TBD:"+TBD)
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"contentData:"+ContentData)
ContentData = URLDecoder(ContentData, #PB_UTF8)
Text = ~"<html lang=\"de\">" +
"<head>" +
~"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>" +
"<title>Alle Client Headers</title></header><body>"+TBD+"<hr/><br/><h1>ConentJSON</h1><br/>"+ContentData+"</body></html>"
*Text = AllocateMemory(StringByteLength(Text, #PB_UTF8))
PokeS(*Text, Text, -1, #PB_UTF8)
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Unencoded:"+PeekS(*Text, -1 , #PB_UTF8))
Encoded = Base64Encoder(*Text, MemorySize(*Text))
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Encoded: " + Encoded)
Response(lhs_web::#cha_R_ResponseContentType) = lhs_web::#response_string
Response(lhs_web::#cha_R_StringBase64) = Encoded
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
Response(lhs_web::#cha_R_http_head_status) = lhs_web::status_get_header("200")
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Status Header get: " + lhs_web::status_get_header("200"))
ZumSenden = lhs_web::MapToJSONString(Response())
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"ZumSenden: " + ZumSenden)
ProcedureReturn ZumSenden
EndProcedure
Procedure.s Php_Test(Map Header.s(), ContentData.s)
Define *Text, Php
Define.s Execute, Phpresult, Encoded, ZumSenden, Text
NewMap Response.s()
Execute = lhs_web::get_config(lhs_web::#conf_basedir) + Header(lhs_web::#http_head_request)
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"To Execute:" + Execute)
Php = RunProgram("php", Execute, "", #PB_Program_Open | #PB_Program_Read)
Phpresult = ""
If Php
While ProgramRunning(Php)
If AvailableProgramOutput(Php)
Phpresult + ReadProgramString(Php) + #CRLF$
EndIf
Wend
CloseProgram(Php) ; Schließt die Verbindung zum Programm
EndIf
Text = Phpresult
*Text = AllocateMemory(StringByteLength(Text, #PB_UTF8))
PokeS(*Text, Text, -1, #PB_UTF8)
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Unencoded:"+PeekS(*Text, -1 , #PB_UTF8))
Encoded = Base64Encoder(*Text, MemorySize(*Text))
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Encoded: " + Encoded)
Response(lhs_web::#cha_R_ResponseContentType) = lhs_web::#response_string
Response(lhs_web::#cha_R_StringBase64) = Encoded
Response(lhs_web::#cha_R_ResponseType) = lhs_web::mimetype("html")
Response(lhs_web::#cha_R_http_head_status) = lhs_web::status_get_header("200")
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Status Header get: " + lhs_web::status_get_header("200"))
ZumSenden = lhs_web::MapToJSONString(Response())
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"ZumSenden: " + ZumSenden)
ProcedureReturn ZumSenden
EndProcedure
lhs_web::register_client_handler("/server/show_client_headers", @Sample_Header(),lhs_web::#handler_proto_get, lhs_web::#handler_sub)
lhs_web::register_client_handler("/post_test", @Formular_Test(),lhs_web::#handler_proto_post, lhs_web::#handler_sub) ;Im zusammenspiel mit einem Formular
lhs_web::register_client_handler("php", @Php_Test(),lhs_web::#handler_proto_universal, lhs_web::#handler_type)