forked from RamseyK/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·110 lines (92 loc) · 2.91 KB
/
main.cpp
File metadata and controls
executable file
·110 lines (92 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
httpserver
main.cpp
Copyright 2011-2012 Ramsey Kant
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>
#include <signal.h>
#include "HTTPServer.h"
#include "ResourceHost.h"
static HTTPServer* svr;
// Ignore signals with this function
void handleSigPipe(int snum) {
return;
}
// Termination signal handler (Ctrl-C)
void handleTermSig(int snum) {
svr->canRun = false;
}
int main (int argc, const char * argv[])
{
// Parse config file
std::map<std::string, std::string> config;
std::fstream cfile;
std::string line, key, val;
int epos;
cfile.open("server.config");
if (!cfile.is_open()) {
std::cout << "Unable to open server.config file in working directory" << std::endl;
return -1;
}
while (getline(cfile, line)) {
// Skip lines beginning with a #
if (line.rfind("#", 0) == 0)
continue;
epos = line.find("=");
key = line.substr(0, epos);
val = line.substr(epos+1, line.length());
config.insert(std::pair<std::string, std::string> (key, val));
}
cfile.close();
// Validate at least vhost, port, and diskpath are present
auto it_vhost = config.find("vhost");
auto it_port = config.find("port");
auto it_path = config.find("diskpath");
if (it_vhost == config.end() || it_port == config.end() || it_path == config.end()) {
std::cout << "vhost, port, and diskpath must be supplied in the config, at a minimum" << std::endl;
return -1;
}
// Break vhost into a comma separated list (if there are multiple vhost aliases)
std::vector<std::string> vhosts;
std::string vhost_alias_str = config["vhost"];
std::string delimiter = ",";
std::string token;
size_t pos = vhost_alias_str.find(delimiter);
do {
pos = vhost_alias_str.find(delimiter);
token = vhost_alias_str.substr(0, pos);
vhosts.push_back(token);
vhost_alias_str.erase(0, pos+delimiter.length());
} while (pos != std::string::npos);
// Ignore SIGPIPE "Broken pipe" signals when socket connections are broken.
signal(SIGPIPE, handleSigPipe);
// Register termination signals
signal(SIGABRT, &handleTermSig);
signal(SIGINT, &handleTermSig);
signal(SIGTERM, &handleTermSig);
// Instance and start the server
svr = new HTTPServer(vhosts, atoi(config["port"].c_str()), config["diskpath"]);
if (!svr->start()) {
svr->stop();
delete svr;
return -1;
}
// Run main event loop
svr->process();
// Stop the server
svr->stop();
delete svr;
return 0;
}