Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
function_handlers.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright 2015 Cloudius Systems
20 */
21
22#pragma once
23
24#include <seastar/http/handlers.hh>
25#include <functional>
26#include <seastar/json/json_elements.hh>
27
28namespace seastar {
29
30namespace httpd {
31
36typedef std::function<sstring(const_req req)> request_function;
37
41typedef std::function<sstring(const_req req, http::reply&)> handle_function;
42
48typedef std::function<json::json_return_type(const_req req)> json_request_function;
49
55typedef std::function<
56 future<json::json_return_type>(std::unique_ptr<http::request> req)> future_json_function;
57
58typedef std::function<
59 future<std::unique_ptr<http::reply>>(std::unique_ptr<http::request> req,
60 std::unique_ptr<http::reply> rep)> future_handler_function;
68public:
69
70 function_handler(const handle_function & f_handle, const sstring& type)
71 : _f_handle(
72 [f_handle](std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) {
73 rep->_content += f_handle(*req.get(), *rep.get());
74 return make_ready_future<std::unique_ptr<http::reply>>(std::move(rep));
75 }), _type(type) {
76 }
77
78 function_handler(const future_handler_function& f_handle, const sstring& type)
79 : _f_handle(f_handle), _type(type) {
80 }
81
82 function_handler(const request_function & _handle, const sstring& type)
83 : _f_handle(
84 [_handle](std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) {
85 return append_result(std::move(rep), _handle(*req.get()));
86 }), _type(type) {
87 }
88
89 function_handler(const json_request_function& _handle)
90 : _f_handle(
91 [_handle](std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) {
92 return append_result(std::move(rep), _handle(*req.get()));
93 }), _type("json") {
94 }
95
96 function_handler(const future_json_function& _handle)
97 : _f_handle(
98 [_handle](std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) {
99 return _handle(std::move(req)).then([rep = std::move(rep)](json::json_return_type&& res) mutable {
100 return append_result(std::move(rep), std::move(res));
101 });
102 }), _type("json") {
103 }
104
105 function_handler(const function_handler&) = default;
106
108 std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) override {
109 return _f_handle(std::move(req), std::move(rep)).then(
110 [this](std::unique_ptr<http::reply> rep) {
111 rep->done(_type);
112 return make_ready_future<std::unique_ptr<http::reply>>(std::move(rep));
113 });
114 }
115
116private:
117 // send the json payload of result to reply, return the reply pointer
118 static future<std::unique_ptr<http::reply>> append_result(
119 std::unique_ptr<http::reply>&& reply,
120 json::json_return_type&& result) {
121 if (result._body_writer) {
122 reply->write_body("json", std::move(result._body_writer));
123 } else {
124 reply->_content += result._res;
125 }
126 return make_ready_future<std::unique_ptr<http::reply>>(std::move(reply));
127 }
128
129protected:
130 future_handler_function _f_handle;
131 sstring _type;
132};
133
134}
135
136}
A representation of a possibly not-yet-computed value.
Definition: future.hh:1197
Definition: function_handlers.hh:67
future< std::unique_ptr< http::reply > > handle(const sstring &path, std::unique_ptr< http::request > req, std::unique_ptr< http::reply > rep) override
Definition: function_handlers.hh:107
Definition: handlers.hh:42
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: reply.hh:61
void write_body(const sstring &content_type, noncopyable_function< future<>(output_stream< char > &&)> &&body_writer)
use an output stream to write the message body
sstring _content
Definition: reply.hh:155
Definition: json_elements.hh:316