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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright 2015 Cloudius Systems
24 #include <seastar/http/handlers.hh>
26 #include <seastar/json/json_elements.hh>
33 * A request function is a lambda expression that gets only the request
36 typedef std::function<sstring(const_req req)> request_function;
39 * A handle function is a lambda expression that gets request and reply
41 typedef std::function<sstring(const_req req, reply&)> handle_function;
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.
48 typedef std::function<json::json_return_type(const_req req)> json_request_function;
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
55 typedef std::function<
56 future<json::json_return_type>(std::unique_ptr<request> req)> future_json_function;
58 typedef std::function<
59 future<std::unique_ptr<reply>>(std::unique_ptr<request> req,
60 std::unique_ptr<reply> rep)> future_handler_function;
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
67 class function_handler : public handler_base {
70 function_handler(const handle_function & f_handle, const sstring& type)
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));
78 function_handler(const future_handler_function& f_handle, const sstring& type)
79 : _f_handle(f_handle), _type(type) {
82 function_handler(const request_function & _handle, const sstring& type)
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));
90 function_handler(const json_request_function& _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));
99 function_handler(const future_json_function& _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));
106 rep->_content += res._res;
109 return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
114 function_handler(const function_handler&) = default;
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) {
121 return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
126 future_handler_function _f_handle;