|
| 1 | +// Copyright (c) 2018 PHP Desktop, see the Authors file. |
| 2 | +// All rights reserved. Licensed under BSD 3-clause license. |
| 3 | +// Project website: https://github.com/cztomczak/phpdesktop |
| 4 | + |
| 5 | +#include "mongoose.h" |
| 6 | +#include "utils.h" |
| 7 | +#include "version.h" |
| 8 | +#include "include/base/cef_logging.h" |
| 9 | + |
| 10 | +std::string g_mongoose_port = "0"; // @TODO from settings.json |
| 11 | +std::string g_mongoose_ip_address = "127.0.0.1"; // @TODO from settings.json |
| 12 | +std::string g_mongoose_url = ""; |
| 13 | + |
| 14 | +struct mg_context* g_mongoose_context = 0; |
| 15 | +extern std::string g_cgi_env_from_argv; |
| 16 | + |
| 17 | +static int mongoose_log_message(const struct mg_connection* conn, |
| 18 | + const char *message) { |
| 19 | + // Called when mongoose is about to log a message. If callback returns |
| 20 | + // non-zero, mongoose does not log anything. |
| 21 | + LOG(WARNING) << message; |
| 22 | + return 0; |
| 23 | +} |
| 24 | + |
| 25 | +static void mongoose_end_request(const struct mg_connection* conn, |
| 26 | + int reply_status_code) { |
| 27 | + // Called when mongoose has finished processing request. |
| 28 | + mg_request_info* request = mg_get_request_info( |
| 29 | + const_cast<mg_connection*>(conn)); |
| 30 | + std::string message; |
| 31 | + message.append(request->request_method); |
| 32 | + message.append(" "); |
| 33 | + std::stringstream ss; |
| 34 | + ss << reply_status_code; |
| 35 | + message.append(ss.str()); |
| 36 | + message.append(" "); |
| 37 | + message.append(request->uri); |
| 38 | + if (request->query_string) { |
| 39 | + message.append("?"); |
| 40 | + message.append(request->query_string); |
| 41 | + } |
| 42 | + LOG(INFO) << message; |
| 43 | +} |
| 44 | + |
| 45 | +bool MongooseStart() { |
| 46 | + LOG(INFO) << "Start Mongoose server"; |
| 47 | + |
| 48 | + // Temp directory |
| 49 | + std::string cgi_temp_dir = "/tmp"; |
| 50 | + // @TODO load cgi_temp_dir from settings.json |
| 51 | + char const* tmp = getenv("TMP"); |
| 52 | + if (tmp != NULL) { |
| 53 | + cgi_temp_dir.assign(tmp); |
| 54 | + } else { |
| 55 | + char const* temp = getenv("TEMP"); |
| 56 | + if (temp != NULL) { |
| 57 | + cgi_temp_dir.assign(temp); |
| 58 | + } else { |
| 59 | + char const* tmpdir = getenv("TMPDIR"); |
| 60 | + if (tmpdir != NULL) { |
| 61 | + cgi_temp_dir.assign(tmpdir); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // CGI environment variables |
| 67 | + std::string cgi_env = ""; |
| 68 | + cgi_env.append("TMP=").append(cgi_temp_dir).append(","); |
| 69 | + cgi_env.append("TEMP=").append(cgi_temp_dir).append(","); |
| 70 | + cgi_env.append("TMPDIR=").append(cgi_temp_dir).append(","); |
| 71 | + if (g_mongoose_ip_address != "*") { |
| 72 | + cgi_env.append("SERVER_NAME=").append(g_mongoose_ip_address) |
| 73 | + .append(","); |
| 74 | + } |
| 75 | + cgi_env.append("PHPDESKTOP_VERSION=").append(PHPDESKTOP_VERSION); |
| 76 | + if (g_cgi_env_from_argv.length()) { |
| 77 | + cgi_env.append(",").append(g_cgi_env_from_argv); |
| 78 | + } |
| 79 | + LOG(INFO) << "CGI environment variables set: " << cgi_env; |
| 80 | + |
| 81 | + // Document root |
| 82 | + std::string document_root = executable_dir().append("/www"); |
| 83 | + LOG(INFO) << "document_root=" << document_root; |
| 84 | + |
| 85 | + // Listening ports |
| 86 | + // @TODO from settings.json |
| 87 | + std::string listening_ports; |
| 88 | + if (g_mongoose_ip_address == "*") { |
| 89 | + listening_ports = g_mongoose_port; |
| 90 | + } else { |
| 91 | + listening_ports = g_mongoose_ip_address + ":" + g_mongoose_port; |
| 92 | + } |
| 93 | + |
| 94 | + // CGI interpreter |
| 95 | + std::string cgi_interpreter = executable_dir().append("/php-cgi"); |
| 96 | + |
| 97 | + // Mongoose options |
| 98 | + const char* options[] = { |
| 99 | + "document_root", document_root.c_str(), |
| 100 | + "listening_ports", listening_ports.c_str(), |
| 101 | + "index_files", "index.html,index.php", // @TODO from settings.json |
| 102 | + "cgi_interpreter", cgi_interpreter.c_str(), |
| 103 | + "cgi_pattern", "**.php$", // @TODO from settings.json |
| 104 | + "cgi_environment", cgi_env.c_str(), |
| 105 | + "404_handler", "/pretty-urls.php", // @TODO from settings.json |
| 106 | + "hide_files_patterns", "", // @TODO from settings.json |
| 107 | + NULL |
| 108 | + }; |
| 109 | + |
| 110 | + // Start mongoose |
| 111 | + mg_callbacks callbacks = {0}; |
| 112 | + callbacks.log_message = &mongoose_log_message; |
| 113 | + callbacks.end_request = &mongoose_end_request; |
| 114 | + g_mongoose_context = mg_start(&callbacks, NULL, options); |
| 115 | + if (g_mongoose_context == NULL) { |
| 116 | + LOG(ERROR) << "mg_start() failed"; |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + // When port was set to 0 then a random free port was assigned |
| 121 | + // by OS. |
| 122 | + int port = mg_get_listening_port(g_mongoose_context); |
| 123 | + std::stringstream port_ss; |
| 124 | + port_ss << port; |
| 125 | + g_mongoose_port = port_ss.str(); |
| 126 | + if (g_mongoose_ip_address == "*") { |
| 127 | + g_mongoose_url = "http://127.0.0.1:" + port_ss.str() + "/"; |
| 128 | + } else { |
| 129 | + g_mongoose_ip_address = g_mongoose_ip_address; |
| 130 | + g_mongoose_url = "http://" + g_mongoose_ip_address + ":" |
| 131 | + + g_mongoose_port + "/"; |
| 132 | + } |
| 133 | + LOG(INFO) << "Mongoose url: " << g_mongoose_url; |
| 134 | + |
| 135 | + return true; |
| 136 | +} |
| 137 | + |
| 138 | +void MongooseStop() { |
| 139 | + // Don't call mg_stop(), as there were issues in the past |
| 140 | + // with doing so. It is not necessary to stop mongoose server, |
| 141 | + // as this is done only when application quits and mongoose |
| 142 | + // server will automatically stop when application quits. |
| 143 | +} |
| 144 | + |
| 145 | +std::string MongooseGetPort() { |
| 146 | + return g_mongoose_port; |
| 147 | +} |
| 148 | + |
| 149 | +std::string MongooseGetIpAddress() { |
| 150 | + return g_mongoose_ip_address; |
| 151 | +} |
| 152 | + |
| 153 | +std::string MongooseGetUrl() { |
| 154 | + return g_mongoose_url; |
| 155 | +} |
0 commit comments