lweb/server_example.pb

163 lines
5.5 KiB
Text

;****************************
;*
;* Lihaso Web Server
;*
;* (c)2015 - 2021 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
lhs_web::set_config(lhs_web::#conf_HTTP_port, "8081")
;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, "127.0.0.1")
lhs_web::set_config(lhs_web::#conf_HTTPS_Port, "8444")
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")
;*
;* includes
;*
Procedure.s Sample_Header(Map Header.s())
Define *Text
Define.s Encoded, ZumSenden, TBD, Text
NewMap Response.s()
If Header("cookie:") <> ""
Debug "A Cookie is set :" + Header("cookie:")
Else
Debug "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))
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"
If Header("cookie:") = ""
Response(lhs_web::#http_head_set_cookie) = "session=test"
EndIf
ZumSenden = lhs_web::MapToJSONString(Response())
Debug "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
Debug "TBD:"+TBD
Debug "contentData:"+ContentData
Text = "<!DOCTYPE html><html><header><title>Alle Client Headers</title></header><body>"+TBD+"<hr/><br/><h1>ConentJSON</h1><br/>"+ContentData+"</body></html>"
*Text = AllocateMemory(StringByteLength(Text))
PokeS(*Text, Text)
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"
ZumSenden = lhs_web::MapToJSONString(Response())
Debug "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)
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 = Phpresult
*Text = AllocateMemory(StringByteLength(Text))
PokeS(*Text, Text)
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"
ZumSenden = lhs_web::MapToJSONString(Response())
Debug "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)
;*
;* Start http & https Server
;*
If lhs_web::start_server()
Debug "Server gestartet" + lhs_web::get_config(lhs_web::#conf_HTTP_port)
Else
Debug "Fehlgeschlagen"
End
EndIf
counter = 0
OpenConsole("Test")
PrintN("Webserver")
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()
PrintN("Finished")
End