forked from etr/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.cpp
More file actions
166 lines (123 loc) · 3.93 KB
/
service.cpp
File metadata and controls
166 lines (123 loc) · 3.93 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <httpserver.hpp>
#include <iostream>
#include <unistd.h>
#include <cstdio>
using namespace httpserver;
class service_resource: public http_resource<service_resource> {
public:
service_resource();
~service_resource();
void render_GET(const http_request &req, http_response** res);
void render_PUT(const http_request &req, http_response** res);
void render_POST(const http_request &req, http_response** res);
void render(const http_request &req, http_response** res);
void render_HEAD(const http_request &req, http_response** res);
void render_OPTIONS(const http_request &req, http_response** res);
void render_CONNECT(const http_request &req, http_response** res);
void render_DELETE(const http_request &req, http_response** res);
private:
};
service_resource::service_resource()
{}
service_resource::~service_resource()
{}
void
service_resource::render_GET(const http_request &req, http_response** res)
{
std::cout << "service_resource::render_GET()" << std::endl;
*res = new http_response(http_response_builder("GET response", 200).string_response());
}
void
service_resource::render_PUT(const http_request &req, http_response** res)
{
std::cout << "service_resource::render_PUT()" << std::endl;
*res = new http_response(http_response_builder("PUT response", 200).string_response());
}
void
service_resource::render_POST(const http_request &req, http_response** res)
{
std::cout << "service_resource::render_POST()" << std::endl;
*res = new http_response(http_response_builder("POST response", 200).string_response());
}
void
service_resource::render(const http_request &req, http_response** res)
{
std::cout << "service_resource::render()" << std::endl;
*res = new http_response(http_response_builder("generic response", 200).string_response());
}
void
service_resource::render_HEAD(const http_request &req, http_response** res)
{
std::cout << "service_resource::render_HEAD()" << std::endl;
*res = new http_response(http_response_builder("HEAD response", 200).string_response());
}
void
service_resource::render_OPTIONS(const http_request &req, http_response** res)
{
std::cout << "service_resource::render_OPTIONS()" << std::endl;
*res = new http_response(http_response_builder("OPTIONS response", 200).string_response());
}
void
service_resource::render_CONNECT(const http_request &req, http_response** res)
{
std::cout << "service_resource::render_CONNECT()" << std::endl;
*res = new http_response(http_response_builder("CONNECT response", 200).string_response());
}
void
service_resource::render_DELETE(const http_request &req, http_response** res)
{
std::cout << "service_resource::render_DELETE()" << std::endl;
*res = new http_response(http_response_builder("DELETE response", 200).string_response());
}
int main(int argc, char **argv)
{
uint16_t port=8090;
int c;
const char *key="key.pem";
const char *cert="cert.pem";
bool secure=false;
while ((c = getopt(argc,argv,"p:k:c:s")) != EOF) {
switch (c) {
case 'p':
port=strtoul(optarg,NULL,10);
break;
case 'k':
key = optarg;
break;
case 'c':
cert=optarg;
break;
case 's':
secure=true;
break;
default:
break;
}
}
std::cout << "Using port " << port << std::endl;
if (secure) {
std::cout << "Key: " << key << " Certificate: " << cert
<< std::endl;
}
//
// Use builder to define webserver configuration options
//
create_webserver cw = create_webserver(port).max_threads(5);
if (secure) {
cw.use_ssl().https_mem_key(key).https_mem_cert(cert);
}
//
// Create webserver using the configured options
//
webserver ws = cw;
//
// Create and register service resource available at /service
//
service_resource res;
ws.register_resource("/service",&res,true);
//
// Start and block the webserver
//
ws.start(true);
return 0;
}