Skip to content

Commit 4b00009

Browse files
committed
47.5 RC. Issue #165. Option for Mongoose web server to listen on all IP addresses available.
In settings.json set: "listen_on": ["*", 54007] - this will make web server to listen on 127.0.0.1 and on 192.168.0.x and also on a public internet ip address. During testing I only confirmed that it listened on local ip addresses (127.xxx and 192.xxx), I couldn't make it work for the internet ip address - not sure if this is an issue in Mongoose web server, or or my router's port forwarding wasn't configured properly. Note also that when listen_on is set to "*" then the SERVER_NAME environment variable will still be set to 127.0.0.1 - this may cause issues in PHP scripts that depend on that env variable. To fix it add this code at the top of all your PHP scripts: <?php $_SERVER["SERVER_NAME"] = $_SERVER["HTTP_HOST"]; ?> This will set SERVER_NAME to an ip address that the web server is being accessed from. For example when accessing from 127.0.0.1 it will be set to that. When accessing from 192.168.0.2 then it will be 192.xxx in that case.
1 parent fa2ef7b commit 4b00009

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

phpdesktop-chrome47/resource.rc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ IDR_MAINWINDOW ICON "icon.ico"
4747
//
4848

4949
VS_VERSION_INFO VERSIONINFO
50-
FILEVERSION 47,4,0,0
51-
PRODUCTVERSION 47,4,0,0
50+
FILEVERSION 47,5,0,0
51+
PRODUCTVERSION 47,5,0,0
5252
FILEFLAGSMASK 0x3fL
5353
#ifdef _DEBUG
5454
FILEFLAGS 0x1L
@@ -65,12 +65,12 @@ BEGIN
6565
BEGIN
6666
VALUE "CompanyName", "PHP Desktop"
6767
VALUE "FileDescription", "PHP Desktop Chrome"
68-
VALUE "FileVersion", "47.4.0.0"
68+
VALUE "FileVersion", "47.5.0.0"
6969
VALUE "InternalName", "phpdesktop"
7070
VALUE "LegalCopyright", "(c) Czarek Tomczak 2012"
7171
VALUE "OriginalFilename", "phpdesktop-chrome.exe"
7272
VALUE "ProductName", "PHP Desktop Chrome"
73-
VALUE "ProductVersion", "47.4.0.0"
73+
VALUE "ProductVersion", "47.5.0.0"
7474
END
7575
END
7676
BLOCK "VarFileInfo"

phpdesktop-chrome47/web_server.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,14 @@ bool StartWebServer() {
146146
cgiEnvironment.append("TMP=").append(cgi_temp_dir).append(",");
147147
cgiEnvironment.append("TEMP=").append(cgi_temp_dir).append(",");
148148
cgiEnvironment.append("TMPDIR=").append(cgi_temp_dir).append(",");
149-
// Mongoose sets SERVER_NAME to "mydomain.com"
150-
cgiEnvironment.append("SERVER_NAME=").append(ipAddress).append(",");
149+
// Mongoose sets SERVER_NAME to "127.0.0.1" by default.
150+
// In case of "*" it should be set to what HTTP_HOST is being set.
151+
// A local or public address depending from what ip address it is
152+
// being accessed from. Currently just setting it to 127.0.0.1
153+
// but this may cause troubles in PHP scripts, so mention this in docs.
154+
if (ipAddress != "*") {
155+
cgiEnvironment.append("SERVER_NAME=").append(ipAddress).append(",");
156+
}
151157
// Let users identify whether web app runs in a normal browser
152158
// or a phpdesktop browser.
153159
cgiEnvironment.append("PHPDESKTOP_VERSION=").append(GetPhpDesktopVersion());
@@ -158,7 +164,12 @@ bool StartWebServer() {
158164
LOG_INFO << "CGI environment variables set: " << cgiEnvironment;
159165

160166
// Mongoose web server.
161-
std::string listening_ports = ipAddress + ":" + port;
167+
std::string listening_ports;
168+
if (ipAddress == "*") {
169+
listening_ports = port;
170+
} else {
171+
listening_ports = ipAddress + ":" + port;
172+
}
162173
const char* options[] = {
163174
"document_root", wwwDirectory.c_str(),
164175
"listening_ports", listening_ports.c_str(),
@@ -184,8 +195,13 @@ bool StartWebServer() {
184195

185196
// When port was set to 0 then a random free port was assigned.
186197
g_webServerPort = mg_get_listening_port(g_mongooseContext);
187-
g_webServerIpAddress = ipAddress;
188-
g_webServerUrl = "http://" + ipAddress + ":" + IntToString(g_webServerPort) + "/";
198+
if (ipAddress == "*") {
199+
g_webServerIpAddress = "127.0.0.1";
200+
g_webServerUrl = "http://127.0.0.1:" + IntToString(g_webServerPort) + "/";
201+
} else {
202+
g_webServerIpAddress = ipAddress;
203+
g_webServerUrl = "http://" + ipAddress + ":" + IntToString(g_webServerPort) + "/";
204+
}
189205
LOG_INFO << "Web server url: " << g_webServerUrl;
190206

191207
return true;

0 commit comments

Comments
 (0)