From 16448d1877b783e78bec5a0d77506cf263037f09 Mon Sep 17 00:00:00 2001 From: cztomczak Date: Sun, 3 Feb 2019 20:36:37 +0100 Subject: [PATCH] Support more options: listen_on, www_directory. index_files, (#221) cgi_interpreter, cgi_extensions, 404_handler, hide_files. --- build.sh | 4 +- src/client_handler.cpp | 2 +- src/main.cpp | 7 +- src/mongoose_server.cpp | 196 +++++++++++++++++++++++++++++----------- src/settings.json | 1 - src/www/temp-dir.php | 29 ------ 6 files changed, 149 insertions(+), 90 deletions(-) delete mode 100644 src/www/temp-dir.php diff --git a/build.sh b/build.sh index d77df72..d57bd11 100755 --- a/build.sh +++ b/build.sh @@ -10,8 +10,8 @@ if [[ -f build/bin/phpdesktop ]]; then rm build/bin/phpdesktop fi -if [[ -f debug.log ]]; then - rm debug.log +if [[ -f build/bin/debug.log ]]; then + rm build/bin/debug.log fi count=`ls -1 build/bin/phpdesktop/*.log 2>/dev/null | wc -l` diff --git a/src/client_handler.cpp b/src/client_handler.cpp index 025cad1..7d6d09c 100644 --- a/src/client_handler.cpp +++ b/src/client_handler.cpp @@ -152,7 +152,7 @@ void ClientHandler::OnAfterCreated(CefRefPtr browser) { browser_list_.push_back(browser); if (browser->IsPopup()) { // Set window title for popup, same as main window title. - // Icon for popup is still missing (todo). + // Icon for popup is still missing. const char* title = (*get_app_settings())["main_window"]["title"]; XStoreName(cef_get_xdisplay(), browser->GetHost()->GetWindowHandle(), title); diff --git a/src/main.cpp b/src/main.cpp index fbbf078..c75e9dd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,8 +48,6 @@ void app_terminate_signal(int signatl) { } int main(int argc, char **argv) { - LOG(INFO) << "Executable directory: " << get_executable_dir(); - // Passing ENV variables to PHP using the --cgi-environment // command line arg passed to app. if (argv) { @@ -107,6 +105,8 @@ int main(int argc, char **argv) { return exit_code; } + LOG(INFO) << "Executable directory: " << get_executable_dir(); + // If reading settings.json fails exit app immediately json_value* app_settings = get_app_settings(); if (get_app_settings_error().length()) { @@ -158,9 +158,6 @@ int main(int argc, char **argv) { } } - // Single instance application - // @TODO - // Start Mongoose server mongoose_start(); diff --git a/src/mongoose_server.cpp b/src/mongoose_server.cpp index c9abdb0..a5a3ed5 100644 --- a/src/mongoose_server.cpp +++ b/src/mongoose_server.cpp @@ -5,11 +5,19 @@ #include "mongoose.h" #include "utils.h" #include "version.h" +#include "settings.h" + +#include +#include + #include "include/base/cef_logging.h" -std::string g_mongoose_port = "0"; +// If port was set to 0, then real port will be known +// only after the webserver was started. + +std::string g_mongoose_port = ""; int g_mongoose_port_int = 0; -std::string g_mongoose_ip_address = "127.0.0.1"; // @TODO from settings.json +std::string g_mongoose_ip_address = ""; std::string g_mongoose_url = ""; struct mg_context* g_mongoose_context = 0; @@ -46,68 +54,154 @@ static void mongoose_end_request(const struct mg_connection* conn, bool mongoose_start() { LOG(INFO) << "Start Mongoose server"; + json_value* app_settings = get_app_settings(); - // Temp directory - std::string cgi_temp_dir = "/tmp"; - // @TODO load cgi_temp_dir from settings.json - char const* tmp = getenv("TMP"); - if (tmp != NULL) { - cgi_temp_dir.assign(tmp); + // ip_address, port_str + std::string ip_address((*app_settings)["web_server"]["listen_on"][0]); + long port_int = (*app_settings)["web_server"]["listen_on"][1]; + if (ip_address.empty()) { + ip_address = "127.0.0.1"; + } + g_mongoose_ip_address = ip_address; + std::stringstream port_ss1; + port_ss1 << port_int; + std::string port_str = port_ss1.str(); + if (port_str.empty()) { + port_str = "0"; + } + + // listening_ports (from ip_address and port_str) + std::string listening_ports; + if (ip_address == "*") { + listening_ports = port_str; } else { - char const* temp = getenv("TEMP"); - if (temp != NULL) { - cgi_temp_dir.assign(temp); - } else { - char const* tmpdir = getenv("TMPDIR"); - if (tmpdir != NULL) { - cgi_temp_dir.assign(tmpdir); + listening_ports = ip_address + ":" + port_str; + } + LOG(INFO) << "listening_ports=" << listening_ports; + + // www_directory + std::string www_directory((*app_settings)["web_server"]["www_directory"]); + www_directory = get_full_path(www_directory); + LOG(INFO) << "www_directory=" << www_directory; + + // index_files + const json_value index_files_array = + (*app_settings)["web_server"]["index_files"]; + std::string index_files; + for (int i = 0; i < 32; i++) { + const char* file = index_files_array[i]; + if (strlen(file)) { + if (index_files.length()) { + index_files.append(","); } + index_files.append(file); } } + if (index_files.empty()) { + index_files = "index.html,index.php"; + } + LOG(INFO) << "index_files=" << index_files; - // CGI environment variables + // cgi_interpreter + std::string cgi_interpreter( + (*app_settings)["web_server"]["cgi_interpreter"]); + cgi_interpreter = get_full_path(cgi_interpreter); + LOG(INFO) << "cgi_interpreter=" << cgi_interpreter; + + // cgi_pattern (from cgi_extensions) + const json_value cgi_extensions = + (*app_settings)["web_server"]["cgi_extensions"]; + std::string cgi_pattern; + for (int i = 0; i < 32; i++) { + const char* extension = cgi_extensions[i]; + if (strlen(extension)) { + if (cgi_pattern.length()) { + cgi_pattern.append("|"); + } + cgi_pattern.append("**.").append(extension).append("$"); + } + } + if (cgi_pattern.empty()) { + cgi_pattern = "**.php$"; + } + LOG(INFO) << "cgi_pattern=" << cgi_pattern; + + // cgi_temp_dir - doesn't work + // std::string cgi_temp_dir((*app_settings)["web_server"]["cgi_temp_dir"]); + // if (cgi_temp_dir.empty()) { + // char const* tmp = getenv("TMP"); + // if (tmp != NULL) { + // cgi_temp_dir.assign(tmp); + // } else { + // char const* temp = getenv("TEMP"); + // if (temp != NULL) { + // cgi_temp_dir.assign(temp); + // } else { + // char const* tmpdir = getenv("TMPDIR"); + // if (tmpdir != NULL) { + // cgi_temp_dir.assign(tmpdir); + // } else { + // cgi_temp_dir.assign("/tmp"); + // } + // } + // } + // } else { + // cgi_temp_dir = get_full_path(cgi_temp_dir); + // } + // DIR* temp_dir = opendir(cgi_temp_dir.c_str()); + // if (temp_dir) { + // closedir(temp_dir); + // LOG(INFO) << "cgi_temp_dir=" << cgi_temp_dir; + // } else if (ENOENT == errno) { + // LOG(ERROR) << "Temp directory does not exist: cgi_temp_dir=" + // << cgi_temp_dir; + // cgi_temp_dir = "/tmp"; + // } + + // 404_handler + std::string _404_handler((*app_settings)["web_server"]["404_handler"]); + + // Hide files patterns. + const json_value hide_files = (*app_settings)["web_server"]["hide_files"]; + std::string hide_files_patterns = ""; + for (int i = 0; i < 100; i++) { + const char* pattern = hide_files[i]; + if (strlen(pattern)) { + if (hide_files_patterns.length()) { + hide_files_patterns.append("|"); + } + hide_files_patterns.append("**/").append(pattern).append("$"); + } + } + LOG(INFO) << "hide_files_patterns=" << hide_files_patterns; + + // cgi_environment (same as cgi_env) std::string cgi_env = ""; - cgi_env.append("TMP=").append(cgi_temp_dir).append(","); - cgi_env.append("TEMP=").append(cgi_temp_dir).append(","); - cgi_env.append("TMPDIR=").append(cgi_temp_dir).append(","); - if (g_mongoose_ip_address != "*") { - cgi_env.append("SERVER_NAME=").append(g_mongoose_ip_address) + cgi_env.append("TMP=").append("/tmp").append(","); + cgi_env.append("TEMP=").append("/tmp").append(","); + cgi_env.append("TMPDIR=").append("/tmp").append(","); + if (ip_address != "*") { + cgi_env.append("SERVER_NAME=").append(ip_address) .append(","); - cgi_env.append("SERVER_ADDR=").append(g_mongoose_ip_address) + cgi_env.append("SERVER_ADDR=").append(ip_address) .append(","); } cgi_env.append("PHPDESKTOP_VERSION=").append(PHPDESKTOP_VERSION); if (g_cgi_env_from_argv.length()) { cgi_env.append(",").append(g_cgi_env_from_argv); } - LOG(INFO) << "CGI environment variables set: " << cgi_env; - - // Document root - std::string document_root = get_executable_dir().append("/www"); - LOG(INFO) << "document_root=" << document_root; - - // Listening ports - // @TODO from settings.json - std::string listening_ports; - if (g_mongoose_ip_address == "*") { - listening_ports = g_mongoose_port; - } else { - listening_ports = g_mongoose_ip_address + ":" + g_mongoose_port; - } - - // CGI interpreter - std::string cgi_interpreter = get_executable_dir().append("/php-cgi"); + LOG(INFO) << "cgi_environment=" << cgi_env; // Mongoose options const char* options[] = { - "document_root", document_root.c_str(), + "document_root", www_directory.c_str(), "listening_ports", listening_ports.c_str(), - "index_files", "index.html,index.php", // @TODO from settings.json + "index_files", index_files.c_str(), "cgi_interpreter", cgi_interpreter.c_str(), - "cgi_pattern", "**.php$", // @TODO from settings.json + "cgi_pattern", cgi_pattern.c_str(), "cgi_environment", cgi_env.c_str(), - "404_handler", "/pretty-urls.php", // @TODO from settings.json - "hide_files_patterns", "", // @TODO from settings.json + "404_handler", _404_handler.c_str(), + "hide_files_patterns", hide_files_patterns.c_str(), NULL }; @@ -125,15 +219,13 @@ bool mongoose_start() { // by OS. int port = mg_get_listening_port(g_mongoose_context); g_mongoose_port_int = port; - std::stringstream port_ss; - port_ss << port; - g_mongoose_port = port_ss.str(); - if (g_mongoose_ip_address == "*") { - g_mongoose_url = "http://127.0.0.1:" + port_ss.str() + "/"; + std::stringstream port_ss2; + port_ss2 << port; + g_mongoose_port = port_ss2.str(); + if (ip_address == "*") { + g_mongoose_url = "http://127.0.0.1:" + port_ss2.str() + "/"; } else { - g_mongoose_ip_address = g_mongoose_ip_address; - g_mongoose_url = "http://" + g_mongoose_ip_address + ":" - + g_mongoose_port + "/"; + g_mongoose_url = "http://" + ip_address + ":" + port_ss2.str() + "/"; } LOG(INFO) << "Mongoose url: " << g_mongoose_url; diff --git a/src/settings.json b/src/settings.json index 3bc5a5d..0117bf5 100644 --- a/src/settings.json +++ b/src/settings.json @@ -14,7 +14,6 @@ "index_files": ["index.html", "index.php"], "cgi_interpreter": "php-cgi", "cgi_extensions": ["php"], - "cgi_temp_dir": "", "404_handler": "/pretty-urls.php", "hide_files": [] }, diff --git a/src/www/temp-dir.php b/src/www/temp-dir.php deleted file mode 100644 index 9bf2b6f..0000000 --- a/src/www/temp-dir.php +++ /dev/null @@ -1,29 +0,0 @@ - - - -Go back to index -| ">Refresh - - -

TMP directory

- -

-A few calls to putenv() overwrite the default directory, -call sys_get_temp_dir() to see it if was set successfully. -

- -

You can also set the temp directory using the "cgi_temp_dir" -option in the settings.json file.

- -

sys_get_temp_dir()

- -
-
-