2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2015 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "asio_server_response_impl.h"
27 #include "asio_server_stream.h"
28 #include "asio_server_request_impl.h"
29 #include "asio_server_http2_handler.h"
30 #include "asio_common.h"
35 namespace asio_http2 {
38 response_impl::response_impl()
39 : strm_(nullptr), generator_cb_(deferred_generator()), status_code_(200),
40 state_(response_state::INITIAL), pushed_(false),
41 push_promise_sent_(false) {}
43 unsigned int response_impl::status_code() const { return status_code_; }
45 void response_impl::write_head(unsigned int status_code, header_map h) {
46 if (state_ != response_state::INITIAL) {
50 status_code_ = status_code;
51 header_ = std::move(h);
53 state_ = response_state::HEADER_DONE;
55 if (pushed_ && !push_promise_sent_) {
62 void response_impl::end(std::string data) {
63 end(string_generator(std::move(data)));
66 void response_impl::end(generator_cb cb) {
67 if (state_ == response_state::BODY_STARTED) {
71 generator_cb_ = std::move(cb);
73 if (state_ == response_state::INITIAL) {
74 write_head(status_code_);
76 // generator_cb is changed, start writing in case it is deferred.
77 auto handler = strm_->handler();
78 handler->resume(*strm_);
81 state_ = response_state::BODY_STARTED;
84 void response_impl::write_trailer(header_map h) {
85 auto handler = strm_->handler();
86 handler->submit_trailer(*strm_, std::move(h));
89 void response_impl::start_response() {
90 auto handler = strm_->handler();
92 auto &req = strm_->request().impl();
94 if (!::nghttp2::http2::expect_response_body(req.method(), status_code_)) {
95 state_ = response_state::BODY_STARTED;
98 if (handler->start_response(*strm_) != 0) {
99 handler->stream_error(strm_->get_stream_id(), NGHTTP2_INTERNAL_ERROR);
104 void response_impl::on_close(close_cb cb) { close_cb_ = std::move(cb); }
106 void response_impl::call_on_close(uint32_t error_code) {
108 close_cb_(error_code);
112 void response_impl::cancel(uint32_t error_code) {
113 auto handler = strm_->handler();
114 handler->stream_error(strm_->get_stream_id(), error_code);
117 response *response_impl::push(boost::system::error_code &ec, std::string method,
118 std::string raw_path_query, header_map h) const {
119 auto handler = strm_->handler();
120 return handler->push_promise(ec, *strm_, std::move(method),
121 std::move(raw_path_query), std::move(h));
124 void response_impl::resume() {
125 auto handler = strm_->handler();
126 handler->resume(*strm_);
129 boost::asio::io_service &response_impl::io_service() {
130 return strm_->handler()->io_service();
133 void response_impl::pushed(bool f) { pushed_ = f; }
135 void response_impl::push_promise_sent() {
136 if (push_promise_sent_) {
139 push_promise_sent_ = true;
140 if (state_ == response_state::INITIAL) {
146 const header_map &response_impl::header() const { return header_; }
148 void response_impl::stream(class stream *s) { strm_ = s; }
150 generator_cb::result_type
151 response_impl::call_read(uint8_t *data, std::size_t len, uint32_t *data_flags) {
153 return generator_cb_(data, len, data_flags);
156 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
161 } // namespace server
162 } // namespace asio_http2
163 } // namespace nghttp2