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 rep->_content += _handle(*req.get());
86 return make_ready_future<std::unique_ptr<http::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<http::request> req, std::unique_ptr<http::reply> rep) {
93 json::json_return_type res = _handle(*req.get());
94 rep->_content += res._res;
95 return make_ready_future<std::unique_ptr<http::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<http::request> req, std::unique_ptr<http::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<http::reply>>(std::move(rep));
110 });
111 }), _type("json") {
112 }
113
114 function_handler(const function_handler&) = default;
115
117 std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) override {
118 return _f_handle(std::move(req), std::move(rep)).then(
119 [this](std::unique_ptr<http::reply> rep) {
120 rep->done(_type);
121 return make_ready_future<std::unique_ptr<http::reply>>(std::move(rep));
122 });
123 }
124
125protected:
126 future_handler_function _f_handle;
127 sstring _type;
128};
129
130}
131
132}
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
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:116
Definition: handlers.hh:42
future< T > make_ready_future(A &&... value) noexcept
Creates a future in an available, value state.
Definition: future.hh:1943
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: json_elements.hh:299