lweb/lhttpd.pb

158 lines
6 KiB
Text

;****************************
;*
;* Lihaso Web Server (lhttpd)
;*
;* (c)2020 by Linder Hard- und Software
;*
;* V0.5
;* - Simple webserver for HTTP & HTTPS
;*
EnableExplicit
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.
OpenConsole("lhttpd")
Define Parameter.s, Configfile.s, Checkruntime.s, Exit_Web.i
Define Accesslog.s, Errorlog.s
Configfile = "/etc/lhttpd/lhttpd.cfg"
Checkruntime = "/tmp/lhttpd.run"
Repeat
Parameter = ProgramParameter()
If Parameter = "-h" Or Parameter = "--help"
PrintN("lhttpd Web Server")
PrintN("(c)2021 by Linder Hard- und Software")
PrintN("")
PrintN("Start parameters:")
PrintN("-c=/etc/lhttpd or --config=/etc/lhttpd")
PrintN(" Configuration file.")
PrintN("--run=/tmp/lhttpd.run")
PrintN(" Check periodical for the file")
ElseIf Left(Parameter,3) = "-c=" Or Left(Parameter,9) = "--config="
Configfile = Right(Parameter, Len(Parameter) - FindString(Parameter, "=") + 1)
If ReadFile(0, Configfile)
CloseFile(0)
Else
PrintN("Failed unable to open config file")
End
EndIf
ElseIf Left(Parameter,6) = "--run="
Checkruntime = Right(Parameter, Len(Parameter)-6)
If ReadFile(0, Checkruntime)
CloseFile(0)
Else
PrintN("Failed to read check file:["+Checkruntime+"]")
End
EndIf
ElseIf Parameter = ""
;No Parameters
Else
PrintN("unknown parameter")
PrintN("-h for parameters")
End
EndIf
Until Parameter = ""
;*
;* Variable Declaration.
;*
XIncludeFile "lweb_header.pbi"
XIncludeFile "lweb.pbi"
If OpenPreferences(Configfile, #PB_Preference_GroupSeparator)
If PreferenceGroup("lhttpd")
lhs_web::set_config(lhs_web::#conf_HTTP_port, ReadPreferenceString("HTTP_Port","8081"))
lhs_web::set_config(lhs_web::#conf_HTTP_binding, ReadPreferenceString("HTTP_Binding","0.0.0.0"))
lhs_web::set_config(lhs_web::#conf_HTTPS_CA, ReadPreferenceString("HTTPS_CA","/home/renlin/dev/libressl/sample/fullchain.pem"))
lhs_web::set_config(lhs_web::#conf_HTTPS_Cert, ReadPreferenceString("HTTPS_Cert","/home/renlin/dev/libressl/sample/cert.pem"))
lhs_web::set_config(lhs_web::#conf_HTTPS_Key, ReadPreferenceString("HTTPS_Key","/home/renlin/dev/libressl/sample/privkey.pem"))
lhs_web::set_config(lhs_web::#conf_HTTPS_Key_Pass, ReadPreferenceString("HTTPS_Key_Pass",""))
lhs_web::set_config(lhs_web::#conf_HTTPS_Port, ReadPreferenceString("HTTPS_Port","8444"))
lhs_web::set_config(lhs_web::#conf_HTTPS_Binding, ReadPreferenceString("HTTPS_Binding","127.0.0.1"))
lhs_web::set_config(lhs_web::#conf_HTTPS_Enable, ReadPreferenceString("HTTPS_enable","0"))
lhs_web::set_config(lhs_web::#conf_defaultfile, ReadPreferenceString("Defaultfile","/index.html"))
lhs_web::set_config(lhs_web::#conf_basedir, ReadPreferenceString("Basedirectory","/home/renlin/testweb/"))
lhs_web::set_config(lhs_web::#conf_error400, ReadPreferenceString("Error400_Handling","integrated"))
lhs_web::set_config(lhs_web::#conf_max_HTTP_clients, ReadPreferenceString("Max_HTTP_Clients","10"))
lhs_web::set_config(lhs_web::#conf_max_HTTPS_clients, ReadPreferenceString("Max_HTTPS_Clients","100"))
lhs_web::set_config(lhs_web::#conf_cache_enable, ReadPreferenceString("Filememorycache","0"))
Checkruntime = ReadPreferenceString("Checkrun","/tmp/lhttpd.run")
lhs_web::set_config(lhs_web::#conf_debug_logfile, ReadPreferenceString("Debug_logfile","/var/log/lhttpd-debug.log"))
lhs_web::set_config(lhs_web::#conf_debug_disable, ReadPreferenceString("Debug_disable","true"))
lhs_web::set_config(lhs_web::#conf_access_logfile, ReadPreferenceString("Access_logfile","/var/log/lhttpd-access.log"))
lhs_web::set_config(lhs_web::#conf_error_logfile, ReadPreferenceString("Error_logfile","/var/log/lhttpd-error.log"))
ClosePreferences()
Else
PrintN("No correct config file -> End")
End
EndIf
Else
PrintN("Could not open config file.")
End
EndIf
If CreateFile(0, Checkruntime)
CloseFile(0)
PrintN("PID File created")
Else
PrintN("Error: PID could not be created.")
End
EndIf
lhs_log::App_Name = "lhttpd Web Server Debug"
lhs_log::SetLogFile(lhs_web::get_config(lhs_web::#conf_debug_logfile))
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()
;*
;* Enable extended Logging:
;*
Accesslog = lhs_log_ext::Create("Accesslog")
Errorlog = lhs_log_ext::Create("Errorlog")
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Errorlog UUID:"+Errorlog)
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Accesslog UUID:"+Accesslog)
lhs_web::set_config(lhs_web::#conf_Access_logUUID, Accesslog)
lhs_web::set_config(lhs_web::#conf_Error_logUUID, Errorlog)
lhs_log_ext::SetLogFile(Accesslog, lhs_web::get_config(lhs_web::#conf_access_logfile))
lhs_log_ext::SetLogFile(Errorlog, lhs_web::get_config(lhs_web::#conf_error_logfile))
lhs_log_ext::Init(Accesslog)
lhs_log_ext::Init(Errorlog)
;*
;* includes
;*
If lhs_web::start_server()
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Server started:")
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"HTTP Port:"+ lhs_web::get_config(lhs_web::#conf_HTTP_port))
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"HTTPS Port:"+ lhs_web::get_config(lhs_web::#conf_HTTPS_port))
Else
lhs_log_ext::OutL(lhs_web::get_config(lhs_web::#conf_Debug_logUUID), lhs_log_ext::#Debugging,"Serverstart failed.")
End
EndIf
PrintN("lhttpd Webserver")
PrintN("HTTP on Port:"+lhs_web::get_config(lhs_web::#conf_HTTP_port))
PrintN("HTTPS on Port:"+lhs_web::get_config(lhs_web::#conf_HTTPS_Port))
Exit_Web = 0
Repeat
Delay(100)
If ReadFile(0, Checkruntime)
CloseFile(0)
Else
PrintN("lhttpd stopping...")
Exit_Web = 1
EndIf
Until Exit_Web = 1
PrintN("lhttpd stoped.")
End