]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/http/reply.hh
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / seastar / include / seastar / http / reply.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 // This file was modified from boost http example
23 //
24 // reply.hpp
25 // ~~~~~~~~~
26 //
27 // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
28 //
29 // Distributed under the Boost Software License, Version 1.0. (See accompanying
30 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
31 //
32 #pragma once
33
34 #include <seastar/core/sstring.hh>
35 #include <unordered_map>
36 #include <seastar/http/mime_types.hh>
37 #include <seastar/core/iostream.hh>
38 #include <seastar/util/noncopyable_function.hh>
39
40 namespace seastar {
41
42 namespace httpd {
43
44 class connection;
45 class routes;
46
47 /**
48 * A reply to be sent to a client.
49 */
50 struct reply {
51 /**
52 * The status of the reply.
53 */
54 enum class status_type {
55 continue_ = 100, //!< continue
56 ok = 200, //!< ok
57 created = 201, //!< created
58 accepted = 202, //!< accepted
59 no_content = 204, //!< no_content
60 multiple_choices = 300, //!< multiple_choices
61 moved_permanently = 301, //!< moved_permanently
62 moved_temporarily = 302, //!< moved_temporarily
63 not_modified = 304, //!< not_modified
64 bad_request = 400, //!< bad_request
65 unauthorized = 401, //!< unauthorized
66 forbidden = 403, //!< forbidden
67 not_found = 404, //!< not_found
68 length_required = 411, //!< length_required
69 payload_too_large = 413, //!< payload_too_large
70 internal_server_error = 500, //!< internal_server_error
71 not_implemented = 501, //!< not_implemented
72 bad_gateway = 502, //!< bad_gateway
73 service_unavailable = 503 //!< service_unavailable
74 } _status;
75
76 /**
77 * The headers to be included in the reply.
78 */
79 std::unordered_map<sstring, sstring> _headers;
80
81 sstring _version;
82 /**
83 * The content to be sent in the reply.
84 */
85 sstring _content;
86
87 sstring _response_line;
88 reply()
89 : _status(status_type::ok) {
90 }
91
92 reply& add_header(const sstring& h, const sstring& value) {
93 _headers[h] = value;
94 return *this;
95 }
96
97 reply& set_version(const sstring& version) {
98 _version = version;
99 return *this;
100 }
101
102 reply& set_status(status_type status, const sstring& content = "") {
103 _status = status;
104 if (content != "") {
105 _content = content;
106 }
107 return *this;
108 }
109
110 /**
111 * Set the content type mime type.
112 * Used when the mime type is known.
113 * For most cases, use the set_content_type
114 */
115 reply& set_mime_type(const sstring& mime) {
116 _headers["Content-Type"] = mime;
117 return *this;
118 }
119
120 /**
121 * Set the content type mime type according to the file extension
122 * that would have been used if it was a file: e.g. html, txt, json etc'
123 */
124 reply& set_content_type(const sstring& content_type = "html") {
125 set_mime_type(httpd::mime_types::extension_to_type(content_type));
126 return *this;
127 }
128
129 reply& done(const sstring& content_type) {
130 return set_content_type(content_type).done();
131 }
132 /**
133 * Done should be called before using the reply.
134 * It would set the response line
135 */
136 reply& done() {
137 _response_line = response_line();
138 return *this;
139 }
140 sstring response_line();
141
142 /*!
143 * \brief use an output stream to write the message body
144 *
145 * When a handler needs to use an output stream it should call this method
146 * with a function.
147 *
148 * \param content_type - is used to choose the content type of the body. Use the file extension
149 * you would have used for such a content, (i.e. "txt", "html", "json", etc')
150 * \param body_writer - a function that accept an output stream and use that stream to write the body.
151 * The function should take ownership of the stream while using it and must close the stream when it
152 * is done.
153 *
154 * Message would use chunked transfer encoding in the reply.
155 *
156 */
157
158 void write_body(const sstring& content_type, noncopyable_function<future<>(output_stream<char>&&)>&& body_writer);
159
160 /*!
161 * \brief Write a string as the reply
162 *
163 * \param content_type - is used to choose the content type of the body. Use the file extension
164 * you would have used for such a content, (i.e. "txt", "html", "json", etc')
165 * \param content - the message content.
166 * This would set the the content and content type of the message along
167 * with any additional information that is needed to send the message.
168 */
169 void write_body(const sstring& content_type, const sstring& content);
170
171 private:
172 future<> write_reply_to_connection(connection& con);
173 future<> write_reply_headers(connection& connection);
174
175 noncopyable_function<future<>(output_stream<char>&&)> _body_writer;
176 friend class routes;
177 friend class connection;
178 };
179
180 } // namespace httpd
181
182 }