Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / asio_server_response_impl.cc
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2015 Tatsuhiro Tsujikawa
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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.
24  */
25 #include "asio_server_response_impl.h"
26
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"
31
32 #include "http2.h"
33
34 namespace nghttp2 {
35 namespace asio_http2 {
36 namespace server {
37
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) {}
42
43 unsigned int response_impl::status_code() const { return status_code_; }
44
45 void response_impl::write_head(unsigned int status_code, header_map h) {
46   if (state_ != response_state::INITIAL) {
47     return;
48   }
49
50   status_code_ = status_code;
51   header_ = std::move(h);
52
53   state_ = response_state::HEADER_DONE;
54
55   if (pushed_ && !push_promise_sent_) {
56     return;
57   }
58
59   start_response();
60 }
61
62 void response_impl::end(std::string data) {
63   end(string_generator(std::move(data)));
64 }
65
66 void response_impl::end(generator_cb cb) {
67   if (state_ == response_state::BODY_STARTED) {
68     return;
69   }
70
71   generator_cb_ = std::move(cb);
72
73   if (state_ == response_state::INITIAL) {
74     write_head(status_code_);
75   } else {
76     // generator_cb is changed, start writing in case it is deferred.
77     auto handler = strm_->handler();
78     handler->resume(*strm_);
79   }
80
81   state_ = response_state::BODY_STARTED;
82 }
83
84 void response_impl::write_trailer(header_map h) {
85   auto handler = strm_->handler();
86   handler->submit_trailer(*strm_, std::move(h));
87 }
88
89 void response_impl::start_response() {
90   auto handler = strm_->handler();
91
92   auto &req = strm_->request().impl();
93
94   if (!::nghttp2::http2::expect_response_body(req.method(), status_code_)) {
95     state_ = response_state::BODY_STARTED;
96   }
97
98   if (handler->start_response(*strm_) != 0) {
99     handler->stream_error(strm_->get_stream_id(), NGHTTP2_INTERNAL_ERROR);
100     return;
101   }
102 }
103
104 void response_impl::on_close(close_cb cb) { close_cb_ = std::move(cb); }
105
106 void response_impl::call_on_close(uint32_t error_code) {
107   if (close_cb_) {
108     close_cb_(error_code);
109   }
110 }
111
112 void response_impl::cancel(uint32_t error_code) {
113   auto handler = strm_->handler();
114   handler->stream_error(strm_->get_stream_id(), error_code);
115 }
116
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));
122 }
123
124 void response_impl::resume() {
125   auto handler = strm_->handler();
126   handler->resume(*strm_);
127 }
128
129 boost::asio::io_service &response_impl::io_service() {
130   return strm_->handler()->io_service();
131 }
132
133 void response_impl::pushed(bool f) { pushed_ = f; }
134
135 void response_impl::push_promise_sent() {
136   if (push_promise_sent_) {
137     return;
138   }
139   push_promise_sent_ = true;
140   if (state_ == response_state::INITIAL) {
141     return;
142   }
143   start_response();
144 }
145
146 const header_map &response_impl::header() const { return header_; }
147
148 void response_impl::stream(class stream *s) { strm_ = s; }
149
150 generator_cb::result_type
151 response_impl::call_read(uint8_t *data, std::size_t len, uint32_t *data_flags) {
152   if (generator_cb_) {
153     return generator_cb_(data, len, data_flags);
154   }
155
156   *data_flags |= NGHTTP2_DATA_FLAG_EOF;
157
158   return 0;
159 }
160
161 } // namespace server
162 } // namespace asio_http2
163 } // namespace nghttp2