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
response_impl()38 response_impl::response_impl()
39 : strm_(nullptr),
40 generator_cb_(deferred_generator()),
41 status_code_(200),
42 state_(response_state::INITIAL),
43 pushed_(false),
44 push_promise_sent_(false) {}
45
status_code() const46 unsigned int response_impl::status_code() const { return status_code_; }
47
write_head(unsigned int status_code,header_map h)48 void response_impl::write_head(unsigned int status_code, header_map h) {
49 if (state_ != response_state::INITIAL) {
50 return;
51 }
52
53 status_code_ = status_code;
54 header_ = std::move(h);
55
56 state_ = response_state::HEADER_DONE;
57
58 if (pushed_ && !push_promise_sent_) {
59 return;
60 }
61
62 start_response();
63 }
64
end(std::string data)65 void response_impl::end(std::string data) {
66 end(string_generator(std::move(data)));
67 }
68
end(generator_cb cb)69 void response_impl::end(generator_cb cb) {
70 if (state_ == response_state::BODY_STARTED) {
71 return;
72 }
73
74 generator_cb_ = std::move(cb);
75
76 if (state_ == response_state::INITIAL) {
77 write_head(status_code_);
78 } else {
79 // generator_cb is changed, start writing in case it is deferred.
80 auto handler = strm_->handler();
81 handler->resume(*strm_);
82 }
83
84 state_ = response_state::BODY_STARTED;
85 }
86
write_trailer(header_map h)87 void response_impl::write_trailer(header_map h) {
88 auto handler = strm_->handler();
89 handler->submit_trailer(*strm_, std::move(h));
90 }
91
start_response()92 void response_impl::start_response() {
93 auto handler = strm_->handler();
94
95 auto &req = strm_->request().impl();
96
97 if (!::nghttp2::http2::expect_response_body(req.method(), status_code_)) {
98 state_ = response_state::BODY_STARTED;
99 }
100
101 if (handler->start_response(*strm_) != 0) {
102 handler->stream_error(strm_->get_stream_id(), NGHTTP2_INTERNAL_ERROR);
103 return;
104 }
105 }
106
on_close(close_cb cb)107 void response_impl::on_close(close_cb cb) { close_cb_ = std::move(cb); }
108
call_on_close(uint32_t error_code)109 void response_impl::call_on_close(uint32_t error_code) {
110 if (close_cb_) {
111 close_cb_(error_code);
112 }
113 }
114
cancel(uint32_t error_code)115 void response_impl::cancel(uint32_t error_code) {
116 auto handler = strm_->handler();
117 handler->stream_error(strm_->get_stream_id(), error_code);
118 }
119
push(boost::system::error_code & ec,std::string method,std::string raw_path_query,header_map h) const120 response *response_impl::push(boost::system::error_code &ec, std::string method,
121 std::string raw_path_query, header_map h) const {
122 auto handler = strm_->handler();
123 return handler->push_promise(ec, *strm_, std::move(method),
124 std::move(raw_path_query), std::move(h));
125 }
126
resume()127 void response_impl::resume() {
128 auto handler = strm_->handler();
129 handler->resume(*strm_);
130 }
131
io_service()132 boost::asio::io_service &response_impl::io_service() {
133 return strm_->handler()->io_service();
134 }
135
pushed(bool f)136 void response_impl::pushed(bool f) { pushed_ = f; }
137
push_promise_sent()138 void response_impl::push_promise_sent() {
139 if (push_promise_sent_) {
140 return;
141 }
142 push_promise_sent_ = true;
143 if (state_ == response_state::INITIAL) {
144 return;
145 }
146 start_response();
147 }
148
header() const149 const header_map &response_impl::header() const { return header_; }
150
stream(class stream * s)151 void response_impl::stream(class stream *s) { strm_ = s; }
152
153 generator_cb::result_type
call_read(uint8_t * data,std::size_t len,uint32_t * data_flags)154 response_impl::call_read(uint8_t *data, std::size_t len, uint32_t *data_flags) {
155 if (generator_cb_) {
156 return generator_cb_(data, len, data_flags);
157 }
158
159 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
160
161 return 0;
162 }
163
164 } // namespace server
165 } // namespace asio_http2
166 } // namespace nghttp2
167