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 "nghttp2_config.h"
26
27 #include <nghttp2/asio_http2_client.h>
28
29 #include "asio_client_session_tcp_impl.h"
30 #include "asio_client_session_tls_impl.h"
31 #include "asio_common.h"
32 #include "template.h"
33
34 namespace nghttp2 {
35 namespace asio_http2 {
36 namespace client {
37
38 using boost::asio::ip::tcp;
39
session(boost::asio::io_service & io_service,const std::string & host,const std::string & service)40 session::session(boost::asio::io_service &io_service, const std::string &host,
41 const std::string &service)
42 : impl_(std::make_shared<session_tcp_impl>(
43 io_service, host, service, boost::posix_time::seconds(60))) {
44 impl_->start_resolve(host, service);
45 }
46
session(boost::asio::io_service & io_service,const boost::asio::ip::tcp::endpoint & local_endpoint,const std::string & host,const std::string & service)47 session::session(boost::asio::io_service &io_service,
48 const boost::asio::ip::tcp::endpoint &local_endpoint,
49 const std::string &host, const std::string &service)
50 : impl_(std::make_shared<session_tcp_impl>(
51 io_service, local_endpoint, host, service,
52 boost::posix_time::seconds(60))) {
53 impl_->start_resolve(host, service);
54 }
55
session(boost::asio::io_service & io_service,const std::string & host,const std::string & service,const boost::posix_time::time_duration & connect_timeout)56 session::session(boost::asio::io_service &io_service, const std::string &host,
57 const std::string &service,
58 const boost::posix_time::time_duration &connect_timeout)
59 : impl_(std::make_shared<session_tcp_impl>(io_service, host, service,
60 connect_timeout)) {
61 impl_->start_resolve(host, service);
62 }
63
session(boost::asio::io_service & io_service,const boost::asio::ip::tcp::endpoint & local_endpoint,const std::string & host,const std::string & service,const boost::posix_time::time_duration & connect_timeout)64 session::session(boost::asio::io_service &io_service,
65 const boost::asio::ip::tcp::endpoint &local_endpoint,
66 const std::string &host, const std::string &service,
67 const boost::posix_time::time_duration &connect_timeout)
68 : impl_(std::make_shared<session_tcp_impl>(io_service, local_endpoint, host,
69 service, connect_timeout)) {
70 impl_->start_resolve(host, service);
71 }
72
session(boost::asio::io_service & io_service,boost::asio::ssl::context & tls_ctx,const std::string & host,const std::string & service)73 session::session(boost::asio::io_service &io_service,
74 boost::asio::ssl::context &tls_ctx, const std::string &host,
75 const std::string &service)
76 : impl_(std::make_shared<session_tls_impl>(
77 io_service, tls_ctx, host, service, boost::posix_time::seconds(60))) {
78 impl_->start_resolve(host, service);
79 }
80
session(boost::asio::io_service & io_service,boost::asio::ssl::context & tls_ctx,const std::string & host,const std::string & service,const boost::posix_time::time_duration & connect_timeout)81 session::session(boost::asio::io_service &io_service,
82 boost::asio::ssl::context &tls_ctx, const std::string &host,
83 const std::string &service,
84 const boost::posix_time::time_duration &connect_timeout)
85 : impl_(std::make_shared<session_tls_impl>(io_service, tls_ctx, host,
86 service, connect_timeout)) {
87 impl_->start_resolve(host, service);
88 }
89
~session()90 session::~session() {}
91
session(session && other)92 session::session(session &&other) noexcept : impl_(std::move(other.impl_)) {}
93
operator =(session && other)94 session &session::operator=(session &&other) noexcept {
95 if (this == &other) {
96 return *this;
97 }
98
99 impl_ = std::move(other.impl_);
100 return *this;
101 }
102
on_connect(connect_cb cb) const103 void session::on_connect(connect_cb cb) const {
104 impl_->on_connect(std::move(cb));
105 }
106
on_error(error_cb cb) const107 void session::on_error(error_cb cb) const { impl_->on_error(std::move(cb)); }
108
shutdown() const109 void session::shutdown() const { impl_->shutdown(); }
110
io_service() const111 boost::asio::io_service &session::io_service() const {
112 return impl_->io_service();
113 }
114
submit(boost::system::error_code & ec,const std::string & method,const std::string & uri,header_map h,priority_spec prio) const115 const request *session::submit(boost::system::error_code &ec,
116 const std::string &method,
117 const std::string &uri, header_map h,
118 priority_spec prio) const {
119 return impl_->submit(ec, method, uri, generator_cb(), std::move(h),
120 std::move(prio));
121 }
122
submit(boost::system::error_code & ec,const std::string & method,const std::string & uri,std::string data,header_map h,priority_spec prio) const123 const request *session::submit(boost::system::error_code &ec,
124 const std::string &method,
125 const std::string &uri, std::string data,
126 header_map h, priority_spec prio) const {
127 return impl_->submit(ec, method, uri, string_generator(std::move(data)),
128 std::move(h), std::move(prio));
129 }
130
submit(boost::system::error_code & ec,const std::string & method,const std::string & uri,generator_cb cb,header_map h,priority_spec prio) const131 const request *session::submit(boost::system::error_code &ec,
132 const std::string &method,
133 const std::string &uri, generator_cb cb,
134 header_map h, priority_spec prio) const {
135 return impl_->submit(ec, method, uri, std::move(cb), std::move(h),
136 std::move(prio));
137 }
138
read_timeout(const boost::posix_time::time_duration & t)139 void session::read_timeout(const boost::posix_time::time_duration &t) {
140 impl_->read_timeout(t);
141 }
142
priority_spec(const int32_t stream_id,const int32_t weight,const bool exclusive)143 priority_spec::priority_spec(const int32_t stream_id, const int32_t weight,
144 const bool exclusive)
145 : valid_(true) {
146 nghttp2_priority_spec_init(&spec_, stream_id, weight, exclusive);
147 }
148
get() const149 const nghttp2_priority_spec *priority_spec::get() const {
150 if (!valid_) {
151 return nullptr;
152 }
153
154 return &spec_;
155 }
156
valid() const157 bool priority_spec::valid() const { return valid_; }
158
159 } // namespace client
160 } // namespace asio_http2
161 } // namespace nghttp2
162