Newer
Older
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <cstring>
#include <sstream>
#include <chrono>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include "httpd.hpp"
#include "http_service.hpp"
HttpRouteHandling handle_echo_path(const HttpRequest &req, HttpResponse &res)
{
std::string echo_text = req.regexMatches().at(1);
std::string resp = "Echo: " + echo_text;
res.send((uint8_t*) resp.c_str(), resp.size());
return HttpRouteHandling::End;
}
// Calculate the first N prime numbers where N is extracted from the request uri
HttpRouteHandling handle_prime(const HttpRequest &req, HttpResponse &res)
{
std::string result;
int number_of_primes_found = 0;
int max = std::stoi(req.regexMatches().at(1));
int num = 2;
while(number_of_primes_found < max)
{
bool is_prime = true;
for (int i = 2; i <= num/2; i++)
{
if (num % i == 0)
{
is_prime = false;
break;
}
}
if (is_prime)
{
result += std::to_string(num) + " ";
number_of_primes_found++;
}
num++;
}
res.send((uint8_t*) result.c_str(), result.size());
return HttpRouteHandling::End;
}
int main(int argc, char **argv)
{
// Create the webserver on port 8081. By default listening on 0.0.0.0
HttpServer srv(8081);
// Log Middleware example
srv.addRoute(HttpRoute(
"",
[](const HttpRequest &req, HttpResponse &res) {
std::string log = "Request: " + req.ip() + " => " + req.uri() + "\n";
log += " Agent: " + req.headers().getHeader(HttpHeader::UserAgent).getValue() + "\n";
std::cout << log;
return HttpRouteHandling::Continue;
},
HttpRoute::MatchType::MatchAny
));
// Example index route with hardcoded html response
srv.addRoute(HttpRoute(
"/(index.html)?", // match "/" or "/index.html"
[](const HttpRequest &req, HttpResponse &res) {
char body[] = R"EOT(
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example HTML Document</title>
</head>
<body>
<h1><a href="/example.html">example.html</a></h1>
<h1><a href="/cached/example.html">cached example.html</a></h1>
<h1><a href="/prime/100">first 100 primes</a></h1>
<h1><a href="/echo/Hello">echo hello</a></h1>
</body>
</html>
)EOT";
res.sendAll((uint8_t*)body, sizeof(body));
return HttpRouteHandling::End;
}
));
// Static file example
srv.addRoute(serveFile(
"/example.html",
"./static/example.html",
"text/html; charset=utf-8"
));
// Cached static file example. The file is loaded into memory at launch
srv.addRoute(serveFileCached(
"/cached/example.html",
"./static/example.html",
"text/html; charset=utf-8"
));
// Example for a handler function that uses regex to extract a path segment
srv.addRoute(HttpRoute(
"/prime/(\\d+)",
&handle_prime
));
// Example for a handler function that uses regex to extract a path segment
srv.addRoute(HttpRoute(
"/echo/([a-zA-Z0-9_\\-.]+)/?",
&handle_echo_path
));
try
{
// Start serving the content
srv.serveForever();
}
catch(const HttpException& e)
{
std::cerr << e.what() << '\n';
}
return 0;
}