SOIL C++
C++ Unified Device Interface
Resource.cpp
Go to the documentation of this file.
1#include "Resource.h"
2
3web::json::value HTTP::Resource::request_info(Request message, std::smatch match)
4{
5 web::json::value headers;
6 for (web::http::http_headers::iterator i = message.headers().begin(); i != message.headers().end(); i++)
7 {
8 headers[i->first] = web::json::value::string(i->second);
9 }
10 web::json::value body;
11 body[U("method")] = web::json::value::string(message.method());
12 body[U("headers")] = headers;
13 auto task = message.extract_string();
14 task.wait();
15 body[U("body")] = web::json::value::string(task.get());
16 web::uri uri = message.absolute_uri();
17 web::json::value url;
18 url[U("path")] = web::json::value::string(uri.path());
19 url[U("scheme")] = web::json::value::string(uri.scheme());
20 url[U("query")] = web::json::value::string(uri.query());
21 url[U("host")] = web::json::value::string(uri.host());
22 url[U("port")] = web::json::value::number(uri.port());
23
24 body[U("url")] = url;
25 body[U("remote_address")] = web::json::value(message.remote_address());
26
27 std::vector<web::json::value> matches;
28 for (size_t i = 0; i < match.size(); i++)
29 {
30 std::string m = match.str(i);
31 matches.push_back(web::json::value::string(utility::conversions::to_string_t(m)));
32 }
33
34 body[U("matches")] = web::json::value::array(matches);
35
36 return body;
37}
38
39HTTP::Response HTTP::Resource::fallback(Request message, std::smatch match)
40{
41
42 web::json::value body = request_info(message, match);
43 Response response;
44 response.set_body(body);
45 response.set_status_code(Status::NotImplemented);
46 this->apply_headers(response);
47 return response;
48}
49
51{
52 utility::string_t allowed_methods;
53 for (auto i = this->allowed_methods.begin(); i != this->allowed_methods.end(); i++)
54 {
55 if (i != this->allowed_methods.begin())
56 {
57 allowed_methods.append(U(", "));
58 }
59 allowed_methods.append(*i);
60 }
61 response.headers().add(U("Access-Control-Allow-Origin"), utility::conversions::to_string_t(allowed_origins));
62 response.headers().add(U("Access-Control-Allow-Headers"), U("content-type"));
63 response.headers().add(U("Allow"), allowed_methods);
64 response.headers().add(U("Content-Type"), utility::conversions::to_string_t(content_type));
65}
66
68{
69 allowed_origins = "*";
70 allowed_methods = { web::http::methods::GET, web::http::methods::DEL, web::http::methods::PATCH, web::http::methods::POST, web::http::methods::OPTIONS, web::http::methods::HEAD, web::http::methods::PUT };
71 content_type = "application/json";
72}
73
75{
76}
77
79{
80 Response response;
81 try
82 {
83 if (message.method() == Methods::GET)
84 {
85 response = this->handle_get(message, match);
86 }
87 else if (message.method() == Methods::POST)
88 {
89 response = this->handle_post(message, match);
90 }
91 else if (message.method() == Methods::PATCH)
92 {
93 response = this->handle_patch(message, match);
94 }
95 else if (message.method() == Methods::DEL)
96 {
97 response = this->handle_delete(message, match);
98 }
99 else if (message.method() == Methods::PUT)
100 {
101 response = this->handle_put(message, match);
102 }
103 else if (message.method() == Methods::OPTIONS)
104 {
105 response = this->handle_options(message, match);
106 }
107 else if (message.method() == Methods::HEAD)
108 {
109 response = this->handle_head(message, match);
110 }
111 }
112 catch (std::exception& exception)
113 {
114 response = handle_exception(message, exception, match);
115
116 }
117 return response;
118}
119
121{
122 return this->fallback(message, match);
123}
124
126{
127 return this->fallback(message, match);
128}
129
131{
132 return this->fallback(message, match);
133}
134
135
137{
138 return this->fallback(message, match);
139}
140
141
143{
144 return this->fallback(message, match);
145}
146
147
149{
150 return this->fallback(message, match);
151}
152
154{
155 return this->fallback(message, match);
156}
157
158HTTP::Response HTTP::Resource::handle_exception(Request message, std::exception & exception, std::smatch match)
159{
160 web::json::value body = request_info(message, match);
161 body[U("error")] = web::json::value(utility::conversions::to_string_t(exception.what()));
162 Response response;
163 response.set_body(body);
164 response.set_status_code(Status::InternalError);
165 this->apply_headers(response);
166 return response;
167}
~Resource()
Default Destructor.
Definition: Resource.cpp:74
virtual Response handle_put(Request message, std::smatch match=std::smatch())
HTTP PUT Handler.
Definition: Resource.cpp:142
virtual Response handle_post(Request message, std::smatch match=std::smatch())
HTTP POST Handler.
Definition: Resource.cpp:125
static web::json::value request_info(Request message, std::smatch match=std::smatch())
Request Info.
Definition: Resource.cpp:3
Resource()
Constructor.
Definition: Resource.cpp:67
virtual Response handle(Request message, std::smatch match=std::smatch())
HTTP Handler.
Definition: Resource.cpp:78
virtual Response handle_head(Request message, std::smatch match=std::smatch())
HTTP HEAD Handler.
Definition: Resource.cpp:153
void apply_headers(Response &response)
Apply headers.
Definition: Resource.cpp:50
virtual Response handle_exception(Request message, std::exception &exception, std::smatch match=std::smatch())
HTTP Exception handler.
Definition: Resource.cpp:158
virtual Response handle_options(Request message, std::smatch match=std::smatch())
HTTP OPTIONS Handler.
Definition: Resource.cpp:148
virtual Response handle_delete(Request message, std::smatch match=std::smatch())
HTTP DELETE Handler.
Definition: Resource.cpp:136
virtual Response handle_get(Request message, std::smatch match=std::smatch())
HTTP GET Handler.
Definition: Resource.cpp:120
virtual Response handle_patch(Request message, std::smatch match=std::smatch())
HTTP PATCH Handler.
Definition: Resource.cpp:130
web::http::http_request Request
HTTP Request.
Definition: Types.h:11
web::http::http_response Response
HTTP Response.
Definition: Types.h:18