Skip to content

Commit 58ac9cc

Browse files
committed
Increased allowed length for vhosts, properly check return values on server start to detect a failure on start
1 parent ab92b89 commit 58ac9cc

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/HTTPServer.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,27 @@ HTTPServer::HTTPServer(std::vector<std::string> vhost_aliases, int port, std::st
3131
listenSocket = INVALID_SOCKET;
3232
listenPort = port;
3333

34-
printf("Primary port: %i, disk path: %s\n", port, diskpath.c_str());
34+
std::cout << "Primary port: " << port << ", disk path: " << diskpath.c_str() << std::endl;
3535

3636
// Create a resource host serving the base path ./htdocs on disk
3737
ResourceHost* resHost = new ResourceHost(diskpath);
3838
hostList.push_back(resHost);
3939

4040
// Always serve up localhost/127.0.0.1 (which is why we only added one ResourceHost to hostList above)
41-
char tmpstr[32];
41+
char tmpstr[128];
4242
sprintf(tmpstr, "localhost:%i", listenPort);
4343
vhosts.insert(std::pair<std::string, ResourceHost*>(std::string(tmpstr).c_str(), resHost));
4444
sprintf(tmpstr, "127.0.0.1:%i", listenPort);
4545
vhosts.insert(std::pair<std::string, ResourceHost*>(std::string(tmpstr).c_str(), resHost));
4646

4747
// Setup the resource host serving htdocs to provide for the vhost aliases
4848
for (std::string vh : vhost_aliases) {
49-
printf("vhost: %s\n", vh.c_str());
49+
if ((vh.length() - 6) >= 128) {
50+
std::cout << "vhost " << vh << " too long, skipping!" << std::endl;
51+
continue;
52+
}
53+
54+
std::cout << "vhost: " << vh << std::endl;
5055
sprintf(tmpstr, "%s:%i", vh.c_str(), listenPort);
5156
vhosts.insert(std::pair<std::string, ResourceHost*>(std::string(tmpstr).c_str(), resHost));
5257
}

src/main.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int main (int argc, const char * argv[])
4646
int epos;
4747
cfile.open("server.config");
4848
if (!cfile.is_open()) {
49-
printf("Unable to open server.config file in working directory\n");
49+
std::cout << "Unable to open server.config file in working directory" << std::endl;
5050
return -1;
5151
}
5252
while (getline(cfile, line)) {
@@ -66,7 +66,7 @@ int main (int argc, const char * argv[])
6666
auto it_port = config.find("port");
6767
auto it_path = config.find("diskpath");
6868
if (it_vhost == config.end() || it_port == config.end() || it_path == config.end()) {
69-
printf("vhost, port, and diskpath must be supplied in the config, at a minimum\n");
69+
std::cout << "vhost, port, and diskpath must be supplied in the config, at a minimum" << std::endl;
7070
return -1;
7171
}
7272

@@ -93,7 +93,11 @@ int main (int argc, const char * argv[])
9393

9494
// Instance and start the server
9595
svr = new HTTPServer(vhosts, atoi(config["port"].c_str()), config["diskpath"]);
96-
svr->start();
96+
if (!svr->start()) {
97+
svr->stop();
98+
delete svr;
99+
return -1;
100+
}
97101

98102
// Run main event loop
99103
svr->process();

0 commit comments

Comments
 (0)