5 web::json::value headers;
6 for (web::http::http_headers::iterator i = message.headers().begin(); i != message.headers().end(); i++)
8 headers[i->first] = web::json::value::string(i->second);
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();
15 body[U(
"body")] = web::json::value::string(task.get());
16 web::uri uri = message.absolute_uri();
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());
25 body[U(
"remote_address")] = web::json::value(message.remote_address());
27 std::vector<web::json::value> matches;
28 for (
size_t i = 0; i < match.size(); i++)
30 std::string m = match.str(i);
31 matches.push_back(web::json::value::string(utility::conversions::to_string_t(m)));
34 body[U(
"matches")] = web::json::value::array(matches);
42 web::json::value body = request_info(message, match);
44 response.set_body(body);
45 response.set_status_code(Status::NotImplemented);
46 this->apply_headers(response);
52 utility::string_t allowed_methods;
53 for (
auto i = this->allowed_methods.begin(); i != this->allowed_methods.end(); i++)
55 if (i != this->allowed_methods.begin())
57 allowed_methods.append(U(
", "));
59 allowed_methods.append(*i);
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));
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";
83 if (message.method() == Methods::GET)
85 response = this->handle_get(message, match);
87 else if (message.method() == Methods::POST)
89 response = this->handle_post(message, match);
91 else if (message.method() == Methods::PATCH)
93 response = this->handle_patch(message, match);
95 else if (message.method() == Methods::DEL)
97 response = this->handle_delete(message, match);
99 else if (message.method() == Methods::PUT)
101 response = this->handle_put(message, match);
103 else if (message.method() == Methods::OPTIONS)
105 response = this->handle_options(message, match);
107 else if (message.method() == Methods::HEAD)
109 response = this->handle_head(message, match);
112 catch (std::exception& exception)
114 response = handle_exception(message, exception, match);
122 return this->fallback(message, match);
127 return this->fallback(message, match);
132 return this->fallback(message, match);
138 return this->fallback(message, match);
144 return this->fallback(message, match);
150 return this->fallback(message, match);
155 return this->fallback(message, match);
160 web::json::value body = request_info(message, match);
161 body[U(
"error")] = web::json::value(utility::conversions::to_string_t(exception.what()));
163 response.set_body(body);
164 response.set_status_code(Status::InternalError);
165 this->apply_headers(response);
~Resource()
Default Destructor.
virtual Response handle_put(Request message, std::smatch match=std::smatch())
HTTP PUT Handler.
virtual Response handle_post(Request message, std::smatch match=std::smatch())
HTTP POST Handler.
static web::json::value request_info(Request message, std::smatch match=std::smatch())
Request Info.
virtual Response handle(Request message, std::smatch match=std::smatch())
HTTP Handler.
virtual Response handle_head(Request message, std::smatch match=std::smatch())
HTTP HEAD Handler.
void apply_headers(Response &response)
Apply headers.
virtual Response handle_exception(Request message, std::exception &exception, std::smatch match=std::smatch())
HTTP Exception handler.
virtual Response handle_options(Request message, std::smatch match=std::smatch())
HTTP OPTIONS Handler.
virtual Response handle_delete(Request message, std::smatch match=std::smatch())
HTTP DELETE Handler.
virtual Response handle_get(Request message, std::smatch match=std::smatch())
HTTP GET Handler.
virtual Response handle_patch(Request message, std::smatch match=std::smatch())
HTTP PATCH Handler.
web::http::http_request Request
HTTP Request.
web::http::http_response Response
HTTP Response.