2020-11-03 19:09:17 +01:00
;****************************
;*
;* Lihaso Web Server
;*
2021-05-22 12:09:31 +02:00
;* HTTP/HTTPS example extended Server
2021-05-21 07:45:23 +02:00
;*
2021-04-29 09:41:41 +02:00
;* (c)2015 - 2021 by Linder Hard- und Software
2020-11-03 19:09:17 +01:00
;*
;*
EnableExplicit
;*
2021-05-22 12:09:31 +02:00
;* Includes:
2020-11-03 19:09:17 +01:00
;*
2021-05-21 11:24:35 +02:00
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.
2020-11-03 19:09:17 +01:00
XIncludeFile "lweb_header.pbi"
XIncludeFile "lweb.pbi"
2021-05-22 12:09:31 +02:00
;*
;* Variable declaration:
;*
2020-11-03 19:09:17 +01:00
Define counter, v_lweb_srv_stop
2021-06-06 09:49:08 +02:00
Define Accesslog.s, Errorlog.s, Cachelog.s
2021-05-22 12:09:31 +02:00
;*
;* Basic Debug Log initialization:
;*
2020-11-03 19:09:17 +01:00
2021-05-21 07:45:23 +02:00
lhs_log::App_Name = "http-s_server_example"
2021-05-22 12:09:31 +02:00
lhs_log::SetLogFile("http-s_server_example_debug.log")
2021-05-21 07:45:23 +02:00
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()
2021-05-22 12:09:31 +02:00
;*
;* Enable extended Logging:
;*
Accesslog = lhs_log_ext::Create("Accesslog")
2021-05-25 10:05:41 +02:00
Errorlog = lhs_log_ext::Create("Errorlog")
2021-06-06 09:49:08 +02:00
Cachelog = lhs_log_ext::Create("Cachelog")
2021-05-25 10:05:41 +02:00
lhs_log::Out("Errorlog UUID:"+Errorlog)
lhs_log::Out("Accesslog UUID:"+Accesslog)
2021-06-06 09:49:08 +02:00
lhs_log::Out("Cachelog UUID:"+Cachelog)
2021-05-25 10:05:41 +02:00
lhs_log_ext::SetLogFile(Accesslog, "http-s_access.log")
lhs_log_ext::SetLogFile(Errorlog, "http-s_error.log")
2021-06-06 09:49:08 +02:00
lhs_log_ext::SetLogFile(Cachelog, "http-s_cache.log")
2021-05-25 10:05:41 +02:00
lhs_log_ext::Init(Accesslog)
lhs_log_ext::Init(Errorlog)
2021-06-06 09:49:08 +02:00
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)
2021-05-25 10:05:41 +02:00
lhs_web::set_config(lhs_web::#conf_HTTP_port, "8084")
2020-11-03 19:09:17 +01:00
;lhs_web::set_config(lhs_web::#conf_binding, "127.0.0.1")
2020-11-30 10:08:21 +01:00
lhs_web::set_config(lhs_web::#conf_HTTP_binding, "0.0.0.0")
2020-12-04 21:00:49 +01:00
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, "")
2021-05-25 10:05:41 +02:00
lhs_web::set_config(lhs_web::#conf_HTTPS_Binding, "0.0.0.0")
lhs_web::set_config(lhs_web::#conf_HTTPS_Port, "8446")
2020-11-28 16:49:22 +01:00
lhs_web::set_config(lhs_web::#conf_HTTPS_Enable, "1")
2022-02-18 16:08:53 +01:00
lhs_web::set_config(lhs_web::#conf_defaultfile, "index.html")
2020-11-08 01:31:57 +01:00
lhs_web::set_config(lhs_web::#conf_basedir, "/home/renlin/testweb/")
2020-11-03 19:09:17 +01:00
lhs_web::set_config(lhs_web::#conf_error400, "integrated")
2020-11-30 10:08:21 +01:00
lhs_web::set_config(lhs_web::#conf_max_HTTP_clients, "100")
lhs_web::set_config(lhs_web::#conf_max_HTTPS_clients, "100")
2020-11-03 19:09:17 +01:00
lhs_web::set_config(lhs_web::#conf_cache_enable, "0")
2022-02-17 13:23:42 +01:00
2020-11-03 19:09:17 +01:00
;*
;* includes
;*
2020-11-08 01:31:57 +01:00
Procedure.s Sample_Header(Map Header.s())
Define *Text
2020-12-04 21:00:49 +01:00
Define.s Encoded, ZumSenden, TBD, Text
2020-11-08 01:31:57 +01:00
NewMap Response.s()
2020-12-10 16:37:17 +01:00
If Header("cookie:") <> ""
2021-05-21 07:45:23 +02:00
lhs_log::Out("A Cookie is set :" + Header("cookie:"))
2020-12-10 16:37:17 +01:00
Else
2021-05-21 07:45:23 +02:00
lhs_log::Out("No Cookie.")
2020-12-10 16:37:17 +01:00
EndIf
2020-11-08 01:31:57 +01:00
TBD="<div><div><h1>Header</h1></div><br/>"+#CRLF$
ResetMap(Header())
While NextMapElement(Header())
2020-12-10 16:37:17 +01:00
TBD = TBD+"<div>"+MapKey(Header())+" "+Header()+" </div><br/>"+#CRLF$
2020-11-08 01:31:57 +01:00
Wend
2020-12-04 21:00:49 +01:00
Text = "<!DOCTYPE html><html><header><title>Alle Client Headers</title></header><body>"+TBD+"</body></html>"
*Text = AllocateMemory(StringByteLength(Text))
PokeS(*Text, Text)
2020-11-08 01:31:57 +01:00
Encoded = Base64Encoder(*Text, MemorySize(*Text))
2021-05-21 07:45:23 +02:00
lhs_log::Out("Encoded: " + Encoded)
2020-11-08 01:31:57 +01:00
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"
2020-12-10 16:37:17 +01:00
If Header("cookie:") = ""
Response(lhs_web::#http_head_set_cookie) = "session=test"
EndIf
2020-11-27 11:58:18 +01:00
ZumSenden = lhs_web::MapToJSONString(Response())
2021-05-21 07:45:23 +02:00
lhs_log::Out("ZumSenden: " + ZumSenden)
2020-11-08 01:31:57 +01:00
ProcedureReturn ZumSenden
EndProcedure
2020-11-03 19:09:17 +01:00
2020-11-08 01:31:57 +01:00
Procedure.s Formular_Test(Map Header.s(), ContentData.s)
Define *Text
2020-12-04 21:00:49 +01:00
Define.s Encoded, ZumSenden, TBD, Text
2020-11-08 01:31:57 +01:00
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
2021-05-21 07:45:23 +02:00
lhs_log::Out("TBD:"+TBD)
lhs_log::Out("contentData:"+ContentData)
2022-02-17 15:30:51 +01:00
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)
2021-05-21 07:45:23 +02:00
lhs_log::Out("Unencoded:"+PeekS(*Text, -1 , #PB_UTF8))
2020-11-08 01:31:57 +01:00
Encoded = Base64Encoder(*Text, MemorySize(*Text))
2021-05-21 07:45:23 +02:00
lhs_log::Out("Encoded: " + Encoded)
2020-11-08 01:31:57 +01:00
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"
2020-11-27 11:58:18 +01:00
ZumSenden = lhs_web::MapToJSONString(Response())
2021-05-21 07:45:23 +02:00
lhs_log::Out("ZumSenden: " + ZumSenden)
2020-11-08 01:31:57 +01:00
ProcedureReturn ZumSenden
EndProcedure
2020-11-08 23:14:35 +01:00
Procedure.s Php_Test(Map Header.s(), ContentData.s)
Define *Text, Php
2020-12-04 21:00:49 +01:00
Define.s Execute, Phpresult, Encoded, ZumSenden, Text
2020-11-08 23:14:35 +01:00
NewMap Response.s()
Execute = lhs_web::get_config(lhs_web::#conf_basedir) + Header(lhs_web::#http_head_request)
2021-05-21 07:45:23 +02:00
lhs_log::Out("To Execute:" + Execute)
2020-11-08 23:14:35 +01:00
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
2020-12-04 21:00:49 +01:00
Text = Phpresult
2022-02-17 15:30:51 +01:00
*Text = AllocateMemory(StringByteLength(Text, #PB_UTF8))
PokeS(*Text, Text, -1, #PB_UTF8)
2021-05-21 07:45:23 +02:00
lhs_log::Out("Unencoded:"+PeekS(*Text, -1 , #PB_UTF8))
2020-11-08 23:14:35 +01:00
Encoded = Base64Encoder(*Text, MemorySize(*Text))
2021-05-21 07:45:23 +02:00
lhs_log::Out("Encoded: " + Encoded)
2020-11-08 23:14:35 +01:00
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"
2020-11-27 11:58:18 +01:00
ZumSenden = lhs_web::MapToJSONString(Response())
2021-05-21 07:45:23 +02:00
lhs_log::Out("ZumSenden: " + ZumSenden)
2020-11-08 23:14:35 +01:00
ProcedureReturn ZumSenden
EndProcedure
2020-11-08 01:31:57 +01:00
lhs_web::register_client_handler("/server/show_client_headers", @Sample_Header(),lhs_web::#handler_proto_get, lhs_web::#handler_sub)
2020-11-08 23:36:44 +01:00
lhs_web::register_client_handler("/post_test", @Formular_Test(),lhs_web::#handler_proto_post, lhs_web::#handler_sub) ;Im zusammenspiel mit einem Formular
2020-11-03 19:09:17 +01:00
2020-11-08 23:14:35 +01:00
lhs_web::register_client_handler("php", @Php_Test(),lhs_web::#handler_proto_universal, lhs_web::#handler_type)
2020-11-03 19:09:17 +01:00
;*
2020-12-01 15:39:19 +01:00
;* Start http & https Server
2020-11-03 19:09:17 +01:00
;*
If lhs_web::start_server()
2021-05-21 07:45:23 +02:00
lhs_log::Out("Server gestartet" + lhs_web::get_config(lhs_web::#conf_HTTP_port))
2020-11-03 19:09:17 +01:00
Else
2021-05-21 07:45:23 +02:00
lhs_log::Out("Fehlgeschlagen")
2020-11-03 19:09:17 +01:00
End
EndIf
counter = 0
OpenConsole("Test")
2022-02-18 16:08:53 +01:00
PrintN("Server_Sample and tests")
2020-11-30 10:08:21 +01:00
PrintN("HTTP an Port:"+lhs_web::get_config(lhs_web::#conf_HTTP_port))
2020-11-28 16:49:22 +01:00
PrintN("HTTPS an Port:"+lhs_web::get_config(lhs_web::#conf_HTTPS_Port))
2020-11-03 19:09:17 +01:00
PrintN("Press Enter to Exit")
Input()
2021-05-21 07:45:23 +02:00
lhs_log::Close()
2021-05-25 10:05:41 +02:00
lhs_log_ext::StopAllLogger()
PrintN("Finished")
2020-11-03 19:09:17 +01:00
End