Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.cpp
More file actions
Latest commit
130 lines (107 loc) · 2.91 KB
/
Copy pathHttpServer.cpp
File metadata and controls
130 lines (107 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//SYS
#include<getopt.h>
//STD
#include<memory>
#include<string>
#include<fstream>
#include<iostream>
//LIBEVENT
#include<evhttp.h>
//CONST
constchar* M404 = "<html><body>This page doesn't exist (404)</body></html>";
//DEFAULT OPTS
std::string ip = "127.0.0.1";
std::string path = "";
shortint port = 8081;
voidPrintUsage()
{
usingnamespacestd;
cerr << "MHTTP: Minimal HTTP Server" << endl;
cerr << "\t-i,--ip \thost ip" << endl;
cerr << "\t-p,--port\thost port" << endl;
cerr << "\t-u,--path\tpath for the uri (without last '/')" << endl;
cerr << "\t-h,--help\thelp" << endl;
}
voidHttpReq(evhttp_request* req, void*)
{
auto OutBuf = evhttp_request_get_output_buffer(req);
if (!OutBuf) return;
std::string uri = path + evhttp_request_get_uri(req);
if (*(uri.end()-1) == '/')
uri = uri + "index.html";
elseif (uri.rfind(".") == std::string::npos)
uri = uri + ".html";
//erase first '/'
if (path.empty())
uri.erase(0,1);
std::string page;
int code;
//get page reading requested file
std::ifstream infile(uri);
if (infile.is_open()) {
page.assign(
(std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>()
);
code = HTTP_OK;
} else {
page = M404;
code = HTTP_NOTFOUND;
}
evbuffer_add_printf(OutBuf, "%s", page.c_str());
evhttp_send_reply(req, code, "", OutBuf);
}
intmain(int argc, char* argv[])
{
//OPTS
int c;
while (true) {
staticstructoption long_options[] = {
{"ip", required_argument, 0, 'i'},
{"port", required_argument, 0, 'p'},
{"path", required_argument, 0, 'u'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long(argc, argv, "i:p:u:h", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case'i':
ip = optarg;
break;
case'p':
port = atoi(optarg);
break;
case'u':
path = optarg;
break;
case'h':
PrintUsage();
exit(1);
default:
PrintUsage();
exit(1);
}
}
//MHTTP
if (!event_init()) {
std::cerr << "Error trying to initialize the event API" << std::endl;
return -1;
}
std::unique_ptr<evhttp, decltype(&evhttp_free)> EventHttp(
evhttp_start(ip.c_str(), port),
&evhttp_free
);
if (!EventHttp) {
std::cerr << "Error starting HTTP server" << std::endl;
return -1;
}
evhttp_set_gencb(EventHttp.get(), HttpReq, nullptr);
if (event_dispatch() == -1) {
std::cerr << "HTTP server loop failed" << std::endl;
return -1;
}
return0;
}