2020-11-03 19:09:17 +01:00
;****************************
;*
;* Lihaso Web Server
;*
;* (c)2015 by Linder Hard- und Software
;*
;* V0.1
;* - Initial Web Server
;* - Modul interface ?
;*
EnableExplicit
;*
;* Deklaration Variabeln
;*
XIncludeFile "lweb_header.pbi"
XIncludeFile "lweb.pbi"
Define counter, v_lweb_srv_stop
2020-11-27 11:52:35 +01:00
lhs_web::set_config(lhs_web::#conf_port, "8081")
2020-11-03 19:09:17 +01:00
;lhs_web::set_config(lhs_web::#conf_binding, "127.0.0.1")
lhs_web::set_config(lhs_web::#conf_binding, "0.0.0.0")
2020-11-27 11:52:35 +01:00
lhs_web::set_config(lhs_web::#conf_TLS_CA, "/home/renlin/dev/libressl/sample/test_ca.pem")
lhs_web::set_config(lhs_web::#conf_TLS_Cert, "/home/renlin/dev/libressl/sample/test_server_cert.pem")
lhs_web::set_config(lhs_web::#conf_TLS_Key, "/home/renlin/dev/libressl/sample/test_server_key.pem")
lhs_web::set_config(lhs_web::#conf_TLS_Key_Pass, "test-server-pass")
lhs_web::set_config(lhs_web::#conf_TLS_Binding, "127.0.0.1")
lhs_web::set_config(lhs_web::#conf_TLS_Port, "8444")
lhs_web::set_config(lhs_web::#conf_TLS_Enable, "1")
2020-11-08 01:31:57 +01:00
lhs_web::set_config(lhs_web::#conf_defaultfile, "/index.html")
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")
lhs_web::set_config(lhs_web::#conf_max_clients, "100")
lhs_web::set_config(lhs_web::#conf_cache_enable, "0")
;*
;* includes
;*
2020-11-08 01:31:57 +01:00
Procedure.s Sample_Header(Map Header.s())
Define *Text
Define.s Encoded, ZumSenden, TBD
NewMap Response.s()
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 = UTF8("<!DOCTYPE html><html><header><title>Alle Client Headers</title></header><body>"+TBD+"</body></html>")
Encoded = Base64Encoder(*Text, MemorySize(*Text))
Debug "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"
2020-11-27 11:58:18 +01:00
ZumSenden = lhs_web::MapToJSONString(Response())
2020-11-08 01:31:57 +01:00
Debug "ZumSenden: " + ZumSenden
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
Define.s Encoded, ZumSenden, TBD
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
Debug "TBD:"+TBD
Debug "contentData:"+ContentData
*Text = UTF8("<!DOCTYPE html><html><header><title>Alle Client Headers</title></header><body>"+TBD+"<hr/><br/><h1>ConentJSON</h1><br/>"+ContentData+"</body></html>")
Debug "Unencoded:"+PeekS(*Text, -1 , #PB_UTF8)
Encoded = Base64Encoder(*Text, MemorySize(*Text))
Debug "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"
2020-11-27 11:58:18 +01:00
ZumSenden = lhs_web::MapToJSONString(Response())
2020-11-08 01:31:57 +01:00
Debug "ZumSenden: " + ZumSenden
ProcedureReturn ZumSenden
EndProcedure
2020-11-08 23:14:35 +01:00
Procedure.s Php_Test(Map Header.s(), ContentData.s)
Define *Text, Php
Define.s Execute, Phpresult, Encoded, ZumSenden
NewMap Response.s()
Execute = lhs_web::get_config(lhs_web::#conf_basedir) + Header(lhs_web::#http_head_request)
Debug "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 = UTF8(Phpresult)
Debug "Unencoded:"+PeekS(*Text, -1 , #PB_UTF8)
Encoded = Base64Encoder(*Text, MemorySize(*Text))
Debug "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"
2020-11-27 11:58:18 +01:00
ZumSenden = lhs_web::MapToJSONString(Response())
2020-11-08 23:14:35 +01:00
Debug "ZumSenden: " + ZumSenden
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
;*
;* Initialisierung Netzwerk
;*
If InitNetwork()
;Alles IO
;PrintN("Network initalised")
Debug "Network initalised"
Else
Debug "Network initalisation failen... stop"
End
EndIf
If lhs_web::start_server()
2020-11-08 01:31:57 +01:00
Debug "Server gestartet" + lhs_web::get_config(lhs_web::#conf_port)
2020-11-03 19:09:17 +01:00
Else
Debug "Fehlgeschlagen"
End
EndIf
counter = 0
OpenConsole("Test")
2020-11-27 11:52:35 +01:00
PrintN("Webserver")
PrintN("HTTP an Port:"+lhs_web::get_config(lhs_web::#conf_port))
PrintN("HTTPS an Port:"+lhs_web::get_config(lhs_web::#conf_TLS_Port))
2020-11-03 19:09:17 +01:00
PrintN("Press Enter to Exit")
Input()
End