/src/trafficserver/include/proxy/http/HttpSessionAccept.h
Line | Count | Source |
1 | | /** @file |
2 | | |
3 | | A brief file description |
4 | | |
5 | | @section license License |
6 | | |
7 | | Licensed to the Apache Software Foundation (ASF) under one |
8 | | or more contributor license agreements. See the NOTICE file |
9 | | distributed with this work for additional information |
10 | | regarding copyright ownership. The ASF licenses this file |
11 | | to you under the Apache License, Version 2.0 (the |
12 | | "License"); you may not use this file except in compliance |
13 | | with the License. You may obtain a copy of the License at |
14 | | |
15 | | http://www.apache.org/licenses/LICENSE-2.0 |
16 | | |
17 | | Unless required by applicable law or agreed to in writing, software |
18 | | distributed under the License is distributed on an "AS IS" BASIS, |
19 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | See the License for the specific language governing permissions and |
21 | | limitations under the License. |
22 | | */ |
23 | | |
24 | | #pragma once |
25 | | |
26 | | #include "tscore/ink_platform.h" |
27 | | #include "records/RecHttp.h" |
28 | | #include "iocore/eventsystem/EventSystem.h" |
29 | | #include "proxy/http/HttpConfig.h" |
30 | | #include "proxy/hdrs/HTTP.h" |
31 | | #include "iocore/net/Net.h" |
32 | | #include "iocore/net/SessionAccept.h" |
33 | | |
34 | | #include <memory> |
35 | | #include <utility> |
36 | | |
37 | | namespace detail |
38 | | { |
39 | | /** Options for @c HttpSessionAccept. |
40 | | |
41 | | @internal This is done as a separate class for two reasons. |
42 | | |
43 | | The first is that in current usage many instances are created |
44 | | with the same options so (for the client) this is easier and |
45 | | more efficient than passing options directly to the @c |
46 | | HttpSessionAccept or calling setters. |
47 | | |
48 | | The second is that @c HttpSessionAccept is not provided with any thread |
49 | | safety because it is intended as an immutable object. Putting |
50 | | the setters here and not there makes that clearer. |
51 | | |
52 | | We don't do this directly as nested class because we want to |
53 | | inherit the data members rather than duplicate the declarations |
54 | | and initializations. |
55 | | */ |
56 | | class HttpSessionAcceptOptions |
57 | | { |
58 | | private: |
59 | | using self = HttpSessionAcceptOptions; ///< Self reference type. |
60 | | public: |
61 | | HttpSessionAcceptOptions(); |
62 | | |
63 | | // Connection type (HttpProxyPort::TransportType) |
64 | | int transport_type = 0; |
65 | | /// Set the transport type. |
66 | | self &setTransportType(int); |
67 | | /// Local address to bind for outbound connections. |
68 | | ts::IPAddrPair outbound; |
69 | | /// Set the outbound IP address to @a ip. |
70 | | self &setOutboundIp(IpAddr &ip); |
71 | | /// Set the outbound IP address to @a ip. |
72 | | self &setOutboundIp(IpEndpoint *ip); |
73 | | /// Local port for outbound connection. |
74 | | uint16_t outbound_port = 0; |
75 | | /// Set outbound port. |
76 | | self &setOutboundPort(uint16_t); |
77 | | /// Outbound transparent. |
78 | | bool f_outbound_transparent = false; |
79 | | /// Set outbound transparency. |
80 | | self &setOutboundTransparent(bool); |
81 | | /// Transparent pass-through. |
82 | | bool f_transparent_passthrough = false; |
83 | | /// Set transparent passthrough. |
84 | | self &setTransparentPassthrough(bool); |
85 | | /// Host address resolution preference order. |
86 | | HostResPreferenceOrder host_res_preference; |
87 | | /// Set the host query preference. |
88 | | self &setHostResPreference(HostResPreferenceOrder const &); |
89 | | /// Acceptable session protocols. |
90 | | SessionProtocolSet session_protocol_preference; |
91 | | /// Set the session protocol preference. |
92 | | self &setSessionProtocolPreference(SessionProtocolSet const &); |
93 | | }; |
94 | | |
95 | | inline HttpSessionAcceptOptions::HttpSessionAcceptOptions() |
96 | 0 | { |
97 | 0 | host_res_preference = host_res_default_preference_order; |
98 | 0 | } |
99 | | |
100 | | inline HttpSessionAcceptOptions & |
101 | | HttpSessionAcceptOptions::setTransportType(int type) |
102 | 0 | { |
103 | 0 | transport_type = type; |
104 | 0 | return *this; |
105 | 0 | } |
106 | | |
107 | | inline HttpSessionAcceptOptions & |
108 | | HttpSessionAcceptOptions::setOutboundIp(IpAddr &ip) |
109 | 0 | { |
110 | 0 | if (ip.isIp4()) |
111 | 0 | outbound = swoc::IP4Addr(ip._addr._ip4); |
112 | 0 | else if (ip.isIp6()) |
113 | 0 | outbound = swoc::IP6Addr(ip._addr._ip6); |
114 | 0 | return *this; |
115 | 0 | } |
116 | | |
117 | | inline HttpSessionAcceptOptions & |
118 | | HttpSessionAcceptOptions::setOutboundIp(IpEndpoint *ip) |
119 | 0 | { |
120 | 0 | outbound = swoc::IPAddr(&(ip->sa)); |
121 | 0 | return *this; |
122 | 0 | } |
123 | | |
124 | | inline HttpSessionAcceptOptions & |
125 | | HttpSessionAcceptOptions::setOutboundPort(uint16_t port) |
126 | 0 | { |
127 | 0 | outbound_port = port; |
128 | 0 | return *this; |
129 | 0 | } |
130 | | |
131 | | inline HttpSessionAcceptOptions & |
132 | | HttpSessionAcceptOptions::setOutboundTransparent(bool flag) |
133 | 0 | { |
134 | 0 | f_outbound_transparent = flag; |
135 | 0 | return *this; |
136 | 0 | } |
137 | | |
138 | | inline HttpSessionAcceptOptions & |
139 | | HttpSessionAcceptOptions::setTransparentPassthrough(bool flag) |
140 | 0 | { |
141 | 0 | f_transparent_passthrough = flag; |
142 | 0 | return *this; |
143 | 0 | } |
144 | | |
145 | | inline HttpSessionAcceptOptions & |
146 | | HttpSessionAcceptOptions::setHostResPreference(HostResPreferenceOrder const &order) |
147 | 0 | { |
148 | 0 | host_res_preference = order; |
149 | 0 | return *this; |
150 | 0 | } |
151 | | |
152 | | inline HttpSessionAcceptOptions & |
153 | | HttpSessionAcceptOptions::setSessionProtocolPreference(SessionProtocolSet const &sp_set) |
154 | 0 | { |
155 | 0 | session_protocol_preference = sp_set; |
156 | 0 | return *this; |
157 | 0 | } |
158 | | } // namespace detail |
159 | | |
160 | | /** Common state for protocol-specific HTTP acceptors. |
161 | | * |
162 | | * A proxy port can dispatch connections to several HTTP protocol acceptors. |
163 | | * This base keeps one shared, immutable set of connection properties for all |
164 | | * of those acceptors and provides access to the complete proxy port. Sessions |
165 | | * retain a pointer to their acceptor so the properties remain available |
166 | | * without protocol-specific copies. |
167 | | * |
168 | | * The continuation mutex is @c NULL because the acceptors do not mutate their |
169 | | * shared state after construction. |
170 | | */ |
171 | | class HttpSessionAcceptBase : public SessionAccept |
172 | | { |
173 | | public: |
174 | | using Options = detail::HttpSessionAcceptOptions; |
175 | | using OptionsHandle = std::shared_ptr<Options const>; |
176 | | |
177 | | HttpSessionAcceptBase(OptionsHandle options, HttpProxyPort *proxy_port = nullptr) |
178 | 0 | : SessionAccept(nullptr), _options(std::move(options)) |
179 | 0 | { |
180 | 0 | ink_release_assert(_options != nullptr); |
181 | 0 | proxyPort = proxy_port; |
182 | 0 | } |
183 | | |
184 | 0 | ~HttpSessionAcceptBase() override = default; |
185 | | |
186 | | Options const & |
187 | | options() const |
188 | 0 | { |
189 | 0 | return *_options; |
190 | 0 | } |
191 | | |
192 | | private: |
193 | | OptionsHandle _options; |
194 | | }; |
195 | | |
196 | | class HttpSessionAccept : public HttpSessionAcceptBase |
197 | | { |
198 | | private: |
199 | | using self = HttpSessionAccept; ///< Self reference type. |
200 | | public: |
201 | | /** Construction options. |
202 | | Provide an easier to remember typedef for clients. |
203 | | */ |
204 | | using Options = HttpSessionAcceptBase::Options; |
205 | | using OptionsHandle = HttpSessionAcceptBase::OptionsHandle; |
206 | | |
207 | | /** Default constructor. |
208 | | @internal We don't use a static default options object because of |
209 | | initialization order issues. It is important to pick up data that is read |
210 | | from the config file and a static is initialized long before that point. |
211 | | */ |
212 | 0 | HttpSessionAccept(Options const &opt = Options()) : HttpSessionAccept(std::make_shared<Options>(opt)) {} |
213 | | |
214 | | HttpSessionAccept(OptionsHandle options, HttpProxyPort *proxy_port = nullptr) |
215 | 0 | : HttpSessionAcceptBase(std::move(options), proxy_port) |
216 | 0 | { |
217 | 0 | SET_HANDLER(&HttpSessionAccept::mainEvent); |
218 | 0 | } |
219 | | |
220 | 0 | ~HttpSessionAccept() override { return; } |
221 | | bool accept(NetVConnection *, MIOBuffer *, IOBufferReader *) override; |
222 | | int mainEvent(int event, void *netvc) override; |
223 | | |
224 | | // noncopyable |
225 | | HttpSessionAccept(const HttpSessionAccept &) = delete; |
226 | | HttpSessionAccept &operator=(const HttpSessionAccept &) = delete; |
227 | | }; |