Skip to content

Commit 16448d1

Browse files
committed
Support more options: listen_on, www_directory. index_files, (#221)
cgi_interpreter, cgi_extensions, 404_handler, hide_files.
1 parent f30b4a0 commit 16448d1

File tree

6 files changed

+149
-90
lines changed

6 files changed

+149
-90
lines changed

build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ if [[ -f build/bin/phpdesktop ]]; then
1010
rm build/bin/phpdesktop
1111
fi
1212

13-
if [[ -f debug.log ]]; then
14-
rm debug.log
13+
if [[ -f build/bin/debug.log ]]; then
14+
rm build/bin/debug.log
1515
fi
1616

1717
count=`ls -1 build/bin/phpdesktop/*.log 2>/dev/null | wc -l`

src/client_handler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
152152
browser_list_.push_back(browser);
153153
if (browser->IsPopup()) {
154154
// Set window title for popup, same as main window title.
155-
// Icon for popup is still missing (todo).
155+
// Icon for popup is still missing.
156156
const char* title = (*get_app_settings())["main_window"]["title"];
157157
XStoreName(cef_get_xdisplay(), browser->GetHost()->GetWindowHandle(),
158158
title);

src/main.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ void app_terminate_signal(int signatl) {
4848
}
4949

5050
int main(int argc, char **argv) {
51-
LOG(INFO) << "Executable directory: " << get_executable_dir();
52-
5351
// Passing ENV variables to PHP using the --cgi-environment
5452
// command line arg passed to app.
5553
if (argv) {
@@ -107,6 +105,8 @@ int main(int argc, char **argv) {
107105
return exit_code;
108106
}
109107

108+
LOG(INFO) << "Executable directory: " << get_executable_dir();
109+
110110
// If reading settings.json fails exit app immediately
111111
json_value* app_settings = get_app_settings();
112112
if (get_app_settings_error().length()) {
@@ -158,9 +158,6 @@ int main(int argc, char **argv) {
158158
}
159159
}
160160

161-
// Single instance application
162-
// @TODO
163-
164161
// Start Mongoose server
165162
mongoose_start();
166163

src/mongoose_server.cpp

Lines changed: 144 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55
#include "mongoose.h"
66
#include "utils.h"
77
#include "version.h"
8+
#include "settings.h"
9+
10+
#include <dirent.h>
11+
#include <errno.h>
12+
813
#include "include/base/cef_logging.h"
914

10-
std::string g_mongoose_port = "0";
15+
// If port was set to 0, then real port will be known
16+
// only after the webserver was started.
17+
18+
std::string g_mongoose_port = "";
1119
int g_mongoose_port_int = 0;
12-
std::string g_mongoose_ip_address = "127.0.0.1"; // @TODO from settings.json
20+
std::string g_mongoose_ip_address = "";
1321
std::string g_mongoose_url = "";
1422

1523
struct mg_context* g_mongoose_context = 0;
@@ -46,68 +54,154 @@ static void mongoose_end_request(const struct mg_connection* conn,
4654

4755
bool mongoose_start() {
4856
LOG(INFO) << "Start Mongoose server";
57+
json_value* app_settings = get_app_settings();
4958

50-
// Temp directory
51-
std::string cgi_temp_dir = "/tmp";
52-
// @TODO load cgi_temp_dir from settings.json
53-
char const* tmp = getenv("TMP");
54-
if (tmp != NULL) {
55-
cgi_temp_dir.assign(tmp);
59+
// ip_address, port_str
60+
std::string ip_address((*app_settings)["web_server"]["listen_on"][0]);
61+
long port_int = (*app_settings)["web_server"]["listen_on"][1];
62+
if (ip_address.empty()) {
63+
ip_address = "127.0.0.1";
64+
}
65+
g_mongoose_ip_address = ip_address;
66+
std::stringstream port_ss1;
67+
port_ss1 << port_int;
68+
std::string port_str = port_ss1.str();
69+
if (port_str.empty()) {
70+
port_str = "0";
71+
}
72+
73+
// listening_ports (from ip_address and port_str)
74+
std::string listening_ports;
75+
if (ip_address == "*") {
76+
listening_ports = port_str;
5677
} else {
57-
char const* temp = getenv("TEMP");
58-
if (temp != NULL) {
59-
cgi_temp_dir.assign(temp);
60-
} else {
61-
char const* tmpdir = getenv("TMPDIR");
62-
if (tmpdir != NULL) {
63-
cgi_temp_dir.assign(tmpdir);
78+
listening_ports = ip_address + ":" + port_str;
79+
}
80+
LOG(INFO) << "listening_ports=" << listening_ports;
81+
82+
// www_directory
83+
std::string www_directory((*app_settings)["web_server"]["www_directory"]);
84+
www_directory = get_full_path(www_directory);
85+
LOG(INFO) << "www_directory=" << www_directory;
86+
87+
// index_files
88+
const json_value index_files_array =
89+
(*app_settings)["web_server"]["index_files"];
90+
std::string index_files;
91+
for (int i = 0; i < 32; i++) {
92+
const char* file = index_files_array[i];
93+
if (strlen(file)) {
94+
if (index_files.length()) {
95+
index_files.append(",");
6496
}
97+
index_files.append(file);
6598
}
6699
}
100+
if (index_files.empty()) {
101+
index_files = "index.html,index.php";
102+
}
103+
LOG(INFO) << "index_files=" << index_files;
67104

68-
// CGI environment variables
105+
// cgi_interpreter
106+
std::string cgi_interpreter(
107+
(*app_settings)["web_server"]["cgi_interpreter"]);
108+
cgi_interpreter = get_full_path(cgi_interpreter);
109+
LOG(INFO) << "cgi_interpreter=" << cgi_interpreter;
110+
111+
// cgi_pattern (from cgi_extensions)
112+
const json_value cgi_extensions =
113+
(*app_settings)["web_server"]["cgi_extensions"];
114+
std::string cgi_pattern;
115+
for (int i = 0; i < 32; i++) {
116+
const char* extension = cgi_extensions[i];
117+
if (strlen(extension)) {
118+
if (cgi_pattern.length()) {
119+
cgi_pattern.append("|");
120+
}
121+
cgi_pattern.append("**.").append(extension).append("$");
122+
}
123+
}
124+
if (cgi_pattern.empty()) {
125+
cgi_pattern = "**.php$";
126+
}
127+
LOG(INFO) << "cgi_pattern=" << cgi_pattern;
128+
129+
// cgi_temp_dir - doesn't work
130+
// std::string cgi_temp_dir((*app_settings)["web_server"]["cgi_temp_dir"]);
131+
// if (cgi_temp_dir.empty()) {
132+
// char const* tmp = getenv("TMP");
133+
// if (tmp != NULL) {
134+
// cgi_temp_dir.assign(tmp);
135+
// } else {
136+
// char const* temp = getenv("TEMP");
137+
// if (temp != NULL) {
138+
// cgi_temp_dir.assign(temp);
139+
// } else {
140+
// char const* tmpdir = getenv("TMPDIR");
141+
// if (tmpdir != NULL) {
142+
// cgi_temp_dir.assign(tmpdir);
143+
// } else {
144+
// cgi_temp_dir.assign("/tmp");
145+
// }
146+
// }
147+
// }
148+
// } else {
149+
// cgi_temp_dir = get_full_path(cgi_temp_dir);
150+
// }
151+
// DIR* temp_dir = opendir(cgi_temp_dir.c_str());
152+
// if (temp_dir) {
153+
// closedir(temp_dir);
154+
// LOG(INFO) << "cgi_temp_dir=" << cgi_temp_dir;
155+
// } else if (ENOENT == errno) {
156+
// LOG(ERROR) << "Temp directory does not exist: cgi_temp_dir="
157+
// << cgi_temp_dir;
158+
// cgi_temp_dir = "/tmp";
159+
// }
160+
161+
// 404_handler
162+
std::string _404_handler((*app_settings)["web_server"]["404_handler"]);
163+
164+
// Hide files patterns.
165+
const json_value hide_files = (*app_settings)["web_server"]["hide_files"];
166+
std::string hide_files_patterns = "";
167+
for (int i = 0; i < 100; i++) {
168+
const char* pattern = hide_files[i];
169+
if (strlen(pattern)) {
170+
if (hide_files_patterns.length()) {
171+
hide_files_patterns.append("|");
172+
}
173+
hide_files_patterns.append("**/").append(pattern).append("$");
174+
}
175+
}
176+
LOG(INFO) << "hide_files_patterns=" << hide_files_patterns;
177+
178+
// cgi_environment (same as cgi_env)
69179
std::string cgi_env = "";
70-
cgi_env.append("TMP=").append(cgi_temp_dir).append(",");
71-
cgi_env.append("TEMP=").append(cgi_temp_dir).append(",");
72-
cgi_env.append("TMPDIR=").append(cgi_temp_dir).append(",");
73-
if (g_mongoose_ip_address != "*") {
74-
cgi_env.append("SERVER_NAME=").append(g_mongoose_ip_address)
180+
cgi_env.append("TMP=").append("/tmp").append(",");
181+
cgi_env.append("TEMP=").append("/tmp").append(",");
182+
cgi_env.append("TMPDIR=").append("/tmp").append(",");
183+
if (ip_address != "*") {
184+
cgi_env.append("SERVER_NAME=").append(ip_address)
75185
.append(",");
76-
cgi_env.append("SERVER_ADDR=").append(g_mongoose_ip_address)
186+
cgi_env.append("SERVER_ADDR=").append(ip_address)
77187
.append(",");
78188
}
79189
cgi_env.append("PHPDESKTOP_VERSION=").append(PHPDESKTOP_VERSION);
80190
if (g_cgi_env_from_argv.length()) {
81191
cgi_env.append(",").append(g_cgi_env_from_argv);
82192
}
83-
LOG(INFO) << "CGI environment variables set: " << cgi_env;
84-
85-
// Document root
86-
std::string document_root = get_executable_dir().append("/www");
87-
LOG(INFO) << "document_root=" << document_root;
88-
89-
// Listening ports
90-
// @TODO from settings.json
91-
std::string listening_ports;
92-
if (g_mongoose_ip_address == "*") {
93-
listening_ports = g_mongoose_port;
94-
} else {
95-
listening_ports = g_mongoose_ip_address + ":" + g_mongoose_port;
96-
}
97-
98-
// CGI interpreter
99-
std::string cgi_interpreter = get_executable_dir().append("/php-cgi");
193+
LOG(INFO) << "cgi_environment=" << cgi_env;
100194

101195
// Mongoose options
102196
const char* options[] = {
103-
"document_root", document_root.c_str(),
197+
"document_root", www_directory.c_str(),
104198
"listening_ports", listening_ports.c_str(),
105-
"index_files", "index.html,index.php", // @TODO from settings.json
199+
"index_files", index_files.c_str(),
106200
"cgi_interpreter", cgi_interpreter.c_str(),
107-
"cgi_pattern", "**.php$", // @TODO from settings.json
201+
"cgi_pattern", cgi_pattern.c_str(),
108202
"cgi_environment", cgi_env.c_str(),
109-
"404_handler", "/pretty-urls.php", // @TODO from settings.json
110-
"hide_files_patterns", "", // @TODO from settings.json
203+
"404_handler", _404_handler.c_str(),
204+
"hide_files_patterns", hide_files_patterns.c_str(),
111205
NULL
112206
};
113207

@@ -125,15 +219,13 @@ bool mongoose_start() {
125219
// by OS.
126220
int port = mg_get_listening_port(g_mongoose_context);
127221
g_mongoose_port_int = port;
128-
std::stringstream port_ss;
129-
port_ss << port;
130-
g_mongoose_port = port_ss.str();
131-
if (g_mongoose_ip_address == "*") {
132-
g_mongoose_url = "http://127.0.0.1:" + port_ss.str() + "/";
222+
std::stringstream port_ss2;
223+
port_ss2 << port;
224+
g_mongoose_port = port_ss2.str();
225+
if (ip_address == "*") {
226+
g_mongoose_url = "http://127.0.0.1:" + port_ss2.str() + "/";
133227
} else {
134-
g_mongoose_ip_address = g_mongoose_ip_address;
135-
g_mongoose_url = "http://" + g_mongoose_ip_address + ":"
136-
+ g_mongoose_port + "/";
228+
g_mongoose_url = "http://" + ip_address + ":" + port_ss2.str() + "/";
137229
}
138230
LOG(INFO) << "Mongoose url: " << g_mongoose_url;
139231

src/settings.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"index_files": ["index.html", "index.php"],
1515
"cgi_interpreter": "php-cgi",
1616
"cgi_extensions": ["php"],
17-
"cgi_temp_dir": "",
1817
"404_handler": "/pretty-urls.php",
1918
"hide_files": []
2019
},

src/www/temp-dir.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)