]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/http/function_handlers.hh
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / include / seastar / http / 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
28 namespace seastar {
29
30 namespace httpd {
31
32 /**
33 * A request function is a lambda expression that gets only the request
34 * as its parameter
35 */
36 typedef std::function<sstring(const_req req)> request_function;
37
38 /**
39 * A handle function is a lambda expression that gets request and reply
40 */
41 typedef std::function<sstring(const_req req, reply&)> handle_function;
42
43 /**
44 * A json request function is a lambda expression that gets only the request
45 * as its parameter and return a json response.
46 * Using the json response is done implicitly.
47 */
48 typedef std::function<json::json_return_type(const_req req)> json_request_function;
49
50 /**
51 * A future_json_function is a function that returns a future json reponse.
52 * Similiar to the json_request_function, using the json reponse is done
53 * implicitly.
54 */
55 typedef std::function<
56 future<json::json_return_type>(std::unique_ptr<request> req)> future_json_function;
57
58 typedef std::function<
59 future<std::unique_ptr<reply>>(std::unique_ptr<request> req,
60 std::unique_ptr<reply> rep)> future_handler_function;
61 /**
62 * The function handler get a lambda expression in the constructor.
63 * it will call that expression to get the result
64 * This is suited for very simple handlers
65 *
66 */
67 class function_handler : public handler_base {
68 public:
69
70 function_handler(const handle_function & f_handle, const sstring& type)
71 : _f_handle(
72 [f_handle](std::unique_ptr<request> req, std::unique_ptr<reply> rep) {
73 rep->_content += f_handle(*req.get(), *rep.get());
74 return make_ready_future<std::unique_ptr<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<request> req, std::unique_ptr<reply> rep) {
85 rep->_content += _handle(*req.get());
86 return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
87 }), _type(type) {
88 }
89
90 function_handler(const json_request_function& _handle)
91 : _f_handle(
92 [_handle](std::unique_ptr<request> req, std::unique_ptr<reply> rep) {
93 json::json_return_type res = _handle(*req.get());
94 rep->_content += res._res;
95 return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
96 }), _type("json") {
97 }
98
99 function_handler(const future_json_function& _handle)
100 : _f_handle(
101 [_handle](std::unique_ptr<request> req, std::unique_ptr<reply> rep) {
102 return _handle(std::move(req)).then([rep = std::move(rep)](json::json_return_type&& res) mutable {
103 if (res._body_writer) {
104 rep->write_body("json", std::move(res._body_writer));
105 } else {
106 rep->_content += res._res;
107
108 }
109 return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
110 });
111 }), _type("json") {
112 }
113
114 function_handler(const function_handler&) = default;
115
116 future<std::unique_ptr<reply>> handle(const sstring& path,
117 std::unique_ptr<request> req, std::unique_ptr<reply> rep) override {
118 return _f_handle(std::move(req), std::move(rep)).then(
119 [this](std::unique_ptr<reply> rep) {
120 rep->done(_type);
121 return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
122 });
123 }
124
125 protected:
126 future_handler_function _f_handle;
127 sstring _type;
128 };
129
130 }
131
132 }