Skip to content

Commit

Permalink
47.5 RC. Issue #165. Option for Mongoose web server to listen on all …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
cztomczak committed Feb 23, 2016
1 parent fa2ef7b commit 4b00009
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
8 changes: 4 additions & 4 deletions phpdesktop-chrome47/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ IDR_MAINWINDOW ICON "icon.ico"
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 47,4,0,0
PRODUCTVERSION 47,4,0,0
FILEVERSION 47,5,0,0
PRODUCTVERSION 47,5,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -65,12 +65,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "PHP Desktop"
VALUE "FileDescription", "PHP Desktop Chrome"
VALUE "FileVersion", "47.4.0.0"
VALUE "FileVersion", "47.5.0.0"
VALUE "InternalName", "phpdesktop"
VALUE "LegalCopyright", "(c) Czarek Tomczak 2012"
VALUE "OriginalFilename", "phpdesktop-chrome.exe"
VALUE "ProductName", "PHP Desktop Chrome"
VALUE "ProductVersion", "47.4.0.0"
VALUE "ProductVersion", "47.5.0.0"
END
END
BLOCK "VarFileInfo"
Expand Down
26 changes: 21 additions & 5 deletions phpdesktop-chrome47/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,14 @@ bool StartWebServer() {
cgiEnvironment.append("TMP=").append(cgi_temp_dir).append(",");
cgiEnvironment.append("TEMP=").append(cgi_temp_dir).append(",");
cgiEnvironment.append("TMPDIR=").append(cgi_temp_dir).append(",");
// Mongoose sets SERVER_NAME to "mydomain.com"
cgiEnvironment.append("SERVER_NAME=").append(ipAddress).append(",");
// Mongoose sets SERVER_NAME to "127.0.0.1" by default.
// In case of "*" it should be set to what HTTP_HOST is being set.
// A local or public address depending from what ip address it is
// being accessed from. Currently just setting it to 127.0.0.1
// but this may cause troubles in PHP scripts, so mention this in docs.
if (ipAddress != "*") {
cgiEnvironment.append("SERVER_NAME=").append(ipAddress).append(",");
}
// Let users identify whether web app runs in a normal browser
// or a phpdesktop browser.
cgiEnvironment.append("PHPDESKTOP_VERSION=").append(GetPhpDesktopVersion());
Expand All @@ -158,7 +164,12 @@ bool StartWebServer() {
LOG_INFO << "CGI environment variables set: " << cgiEnvironment;

// Mongoose web server.
std::string listening_ports = ipAddress + ":" + port;
std::string listening_ports;
if (ipAddress == "*") {
listening_ports = port;
} else {
listening_ports = ipAddress + ":" + port;
}
const char* options[] = {
"document_root", wwwDirectory.c_str(),
"listening_ports", listening_ports.c_str(),
Expand All @@ -184,8 +195,13 @@ bool StartWebServer() {

// When port was set to 0 then a random free port was assigned.
g_webServerPort = mg_get_listening_port(g_mongooseContext);
g_webServerIpAddress = ipAddress;
g_webServerUrl = "http://" + ipAddress + ":" + IntToString(g_webServerPort) + "/";
if (ipAddress == "*") {
g_webServerIpAddress = "127.0.0.1";
g_webServerUrl = "http://127.0.0.1:" + IntToString(g_webServerPort) + "/";
} else {
g_webServerIpAddress = ipAddress;
g_webServerUrl = "http://" + ipAddress + ":" + IntToString(g_webServerPort) + "/";
}
LOG_INFO << "Web server url: " << g_webServerUrl;

return true;
Expand Down

0 comments on commit 4b00009

Please sign in to comment.