Skip to content

Commit d615972

Browse files
committed
Added support for multiple vhost aliases, comma separated in the config
1 parent 19099fe commit d615972

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

src/HTTPServer.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,34 @@
2222
* Server Constructor
2323
* Initialize state and server variables
2424
*
25-
* @param vhost Name of the primary Host the HTTP server will respond to
25+
* @param vhost_aliases List of hostnames the HTTP server will respond to
2626
* @param port Port the vhost listens on
2727
* @param diskpath Path to the folder the vhost serves up
2828
*/
29-
HTTPServer::HTTPServer(std::string vhost, int port, std::string diskpath) {
29+
HTTPServer::HTTPServer(std::vector<std::string> vhost_aliases, int port, std::string diskpath) {
3030
canRun = false;
3131
listenSocket = INVALID_SOCKET;
3232
listenPort = port;
3333

34-
// TODO: Eventually we should allow the config to specify multiple vhosts with their own diskpaths
35-
printf("Primary vhost: %s, port: %i, disk path: %s\n", vhost.c_str(), port, diskpath.c_str());
34+
printf("Primary port: %i, disk path: %s\n", port, diskpath.c_str());
3635

3736
// Create a resource host serving the base path ./htdocs on disk
3837
ResourceHost* resHost = new ResourceHost(diskpath);
3938
hostList.push_back(resHost);
4039

41-
// Setup the resource host serving htdocs to provide for the following vhosts
42-
// Use the primary vhost to also serve up localhost/127.0.0.1 (which is why we only added one ResourceHost to hostList above)
40+
// Always serve up localhost/127.0.0.1 (which is why we only added one ResourceHost to hostList above)
4341
char tmpstr[32];
4442
sprintf(tmpstr, "localhost:%i", listenPort);
4543
vhosts.insert(std::pair<std::string, ResourceHost*>(std::string(tmpstr).c_str(), resHost));
4644
sprintf(tmpstr, "127.0.0.1:%i", listenPort);
4745
vhosts.insert(std::pair<std::string, ResourceHost*>(std::string(tmpstr).c_str(), resHost));
48-
sprintf(tmpstr, "%s:%i", vhost.c_str(), listenPort);
49-
vhosts.insert(std::pair<std::string, ResourceHost*>(std::string(tmpstr).c_str(), resHost));
46+
47+
// Setup the resource host serving htdocs to provide for the vhost aliases
48+
for (std::string vh : vhost_aliases) {
49+
printf("vhost: %s\n", vh.c_str());
50+
sprintf(tmpstr, "%s:%i", vh.c_str(), listenPort);
51+
vhosts.insert(std::pair<std::string, ResourceHost*>(std::string(tmpstr).c_str(), resHost));
52+
}
5053
}
5154

5255
/**

src/HTTPServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class HTTPServer {
8686
bool canRun;
8787

8888
public:
89-
HTTPServer(std::string vhost, int port, std::string diskpath);
89+
HTTPServer(std::vector<std::string> vhost_aliases, int port, std::string diskpath);
9090
~HTTPServer();
9191

9292
bool start();

src/main.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ int main (int argc, const char * argv[])
7070
return -1;
7171
}
7272

73+
// Break vhost into a comma separated list (if there are multiple vhost aliases)
74+
std::vector<std::string> vhosts;
75+
std::string vhost_alias_str = config["vhost"];
76+
std::string delimiter = ",";
77+
std::string token;
78+
size_t pos = vhost_alias_str.find(delimiter);
79+
do {
80+
pos = vhost_alias_str.find(delimiter);
81+
token = vhost_alias_str.substr(0, pos);
82+
vhosts.push_back(token);
83+
vhost_alias_str.erase(0, pos+delimiter.length());
84+
} while (pos != std::string::npos);
85+
7386
// Ignore SIGPIPE "Broken pipe" signals when socket connections are broken.
7487
signal(SIGPIPE, handleSigPipe);
7588

@@ -79,7 +92,7 @@ int main (int argc, const char * argv[])
7992
signal(SIGTERM, &handleTermSig);
8093

8194
// Instance and start the server
82-
svr = new HTTPServer(config["vhost"], atoi(config["port"].c_str()), config["diskpath"]);
95+
svr = new HTTPServer(vhosts, atoi(config["port"].c_str()), config["diskpath"]);
8396
svr->start();
8497

8598
// Run main event loop

0 commit comments

Comments
 (0)