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_client_request_impl.h"
26 
27 #include "asio_client_stream.h"
28 #include "asio_client_session_impl.h"
29 #include "template.h"
30 
31 namespace nghttp2 {
32 namespace asio_http2 {
33 namespace client {
34 
request_impl()35 request_impl::request_impl() : strm_(nullptr), header_buffer_size_(0) {}
36 
write_trailer(header_map h)37 void request_impl::write_trailer(header_map h) {
38   auto sess = strm_->session();
39   sess->write_trailer(*strm_, std::move(h));
40 }
41 
cancel(uint32_t error_code)42 void request_impl::cancel(uint32_t error_code) {
43   auto sess = strm_->session();
44   sess->cancel(*strm_, error_code);
45 }
46 
on_response(response_cb cb)47 void request_impl::on_response(response_cb cb) { response_cb_ = std::move(cb); }
48 
call_on_response(response & res)49 void request_impl::call_on_response(response &res) {
50   if (response_cb_) {
51     response_cb_(res);
52   }
53 }
54 
on_push(request_cb cb)55 void request_impl::on_push(request_cb cb) { push_request_cb_ = std::move(cb); }
56 
call_on_push(request & push_req)57 void request_impl::call_on_push(request &push_req) {
58   if (push_request_cb_) {
59     push_request_cb_(push_req);
60   }
61 };
62 
on_close(close_cb cb)63 void request_impl::on_close(close_cb cb) { close_cb_ = std::move(cb); }
64 
call_on_close(uint32_t error_code)65 void request_impl::call_on_close(uint32_t error_code) {
66   if (close_cb_) {
67     close_cb_(error_code);
68   }
69 }
70 
on_read(generator_cb cb)71 void request_impl::on_read(generator_cb cb) { generator_cb_ = std::move(cb); }
72 
call_on_read(uint8_t * buf,std::size_t len,uint32_t * data_flags)73 generator_cb::result_type request_impl::call_on_read(uint8_t *buf,
74                                                      std::size_t len,
75                                                      uint32_t *data_flags) {
76   if (generator_cb_) {
77     return generator_cb_(buf, len, data_flags);
78   }
79 
80   *data_flags |= NGHTTP2_DATA_FLAG_EOF;
81 
82   return 0;
83 }
84 
resume()85 void request_impl::resume() {
86   auto sess = strm_->session();
87   sess->resume(*strm_);
88 }
89 
header(header_map h)90 void request_impl::header(header_map h) { header_ = std::move(h); }
91 
header()92 header_map &request_impl::header() { return header_; }
93 
header() const94 const header_map &request_impl::header() const { return header_; }
95 
stream(class stream * strm)96 void request_impl::stream(class stream *strm) { strm_ = strm; }
97 
uri(uri_ref uri)98 void request_impl::uri(uri_ref uri) { uri_ = std::move(uri); }
99 
uri() const100 const uri_ref &request_impl::uri() const { return uri_; }
101 
uri()102 uri_ref &request_impl::uri() { return uri_; }
103 
method(std::string s)104 void request_impl::method(std::string s) { method_ = std::move(s); }
105 
method() const106 const std::string &request_impl::method() const { return method_; }
107 
header_buffer_size() const108 size_t request_impl::header_buffer_size() const { return header_buffer_size_; }
109 
update_header_buffer_size(size_t len)110 void request_impl::update_header_buffer_size(size_t len) {
111   header_buffer_size_ += len;
112 }
113 
114 } // namespace client
115 } // namespace asio_http2
116 } // namespace nghttp2
117