1 | // |
2 | // HTTPSClientSession.cpp |
3 | // |
4 | // Library: NetSSL_OpenSSL |
5 | // Package: HTTPSClient |
6 | // Module: HTTPSClientSession |
7 | // |
8 | // Copyright (c) 2006-2010, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Net/HTTPSClientSession.h" |
16 | #include "Poco/Net/SecureStreamSocket.h" |
17 | #include "Poco/Net/SecureStreamSocketImpl.h" |
18 | #include "Poco/Net/SSLManager.h" |
19 | #include "Poco/Net/SSLException.h" |
20 | #include "Poco/Net/HTTPRequest.h" |
21 | #include "Poco/Net/HTTPResponse.h" |
22 | #include "Poco/Net/NetException.h" |
23 | #include "Poco/NumberFormatter.h" |
24 | |
25 | |
26 | using Poco::NumberFormatter; |
27 | using Poco::IllegalStateException; |
28 | |
29 | |
30 | namespace Poco { |
31 | namespace Net { |
32 | |
33 | |
34 | HTTPSClientSession::HTTPSClientSession(): |
35 | HTTPClientSession(SecureStreamSocket()), |
36 | _pContext(SSLManager::instance().defaultClientContext()) |
37 | { |
38 | setPort(HTTPS_PORT); |
39 | } |
40 | |
41 | |
42 | HTTPSClientSession::HTTPSClientSession(const SecureStreamSocket& socket): |
43 | HTTPClientSession(socket), |
44 | _pContext(socket.context()) |
45 | { |
46 | setPort(HTTPS_PORT); |
47 | } |
48 | |
49 | |
50 | HTTPSClientSession::HTTPSClientSession(const SecureStreamSocket& socket, Session::Ptr pSession): |
51 | HTTPClientSession(socket), |
52 | _pContext(socket.context()), |
53 | _pSession(pSession) |
54 | { |
55 | setPort(HTTPS_PORT); |
56 | } |
57 | |
58 | |
59 | HTTPSClientSession::HTTPSClientSession(const std::string& host, Poco::UInt16 port): |
60 | HTTPClientSession(SecureStreamSocket()), |
61 | _pContext(SSLManager::instance().defaultClientContext()) |
62 | { |
63 | setHost(host); |
64 | setPort(port); |
65 | } |
66 | |
67 | |
68 | HTTPSClientSession::HTTPSClientSession(Context::Ptr pContext): |
69 | HTTPClientSession(SecureStreamSocket(pContext)), |
70 | _pContext(pContext) |
71 | { |
72 | } |
73 | |
74 | |
75 | HTTPSClientSession::HTTPSClientSession(Context::Ptr pContext, Session::Ptr pSession): |
76 | HTTPClientSession(SecureStreamSocket(pContext, pSession)), |
77 | _pContext(pContext), |
78 | _pSession(pSession) |
79 | { |
80 | } |
81 | |
82 | |
83 | HTTPSClientSession::HTTPSClientSession(const std::string& host, Poco::UInt16 port, Context::Ptr pContext): |
84 | HTTPClientSession(SecureStreamSocket(pContext)), |
85 | _pContext(pContext) |
86 | { |
87 | setHost(host); |
88 | setPort(port); |
89 | } |
90 | |
91 | |
92 | HTTPSClientSession::HTTPSClientSession(const std::string& host, Poco::UInt16 port, Context::Ptr pContext, Session::Ptr pSession): |
93 | HTTPClientSession(SecureStreamSocket(pContext, pSession)), |
94 | _pContext(pContext), |
95 | _pSession(pSession) |
96 | { |
97 | setHost(host); |
98 | setPort(port); |
99 | } |
100 | |
101 | |
102 | HTTPSClientSession::~HTTPSClientSession() |
103 | { |
104 | } |
105 | |
106 | |
107 | bool HTTPSClientSession::secure() const |
108 | { |
109 | return true; |
110 | } |
111 | |
112 | |
113 | void HTTPSClientSession::abort() |
114 | { |
115 | SecureStreamSocket sss(socket()); |
116 | sss.abort(); |
117 | } |
118 | |
119 | |
120 | X509Certificate HTTPSClientSession::serverCertificate() |
121 | { |
122 | SecureStreamSocket sss(socket()); |
123 | return sss.peerCertificate(); |
124 | } |
125 | |
126 | |
127 | std::string HTTPSClientSession::proxyRequestPrefix() const |
128 | { |
129 | return std::string(); |
130 | } |
131 | |
132 | |
133 | void HTTPSClientSession::proxyAuthenticate(HTTPRequest& request) |
134 | { |
135 | } |
136 | |
137 | |
138 | void HTTPSClientSession::connect(const SocketAddress& address) |
139 | { |
140 | if (getProxyHost().empty() || bypassProxy()) |
141 | { |
142 | connectToTargetPre(); |
143 | HTTPSession::connect(address); |
144 | connectToTargetPost(); |
145 | } |
146 | else |
147 | { |
148 | connectToProxy(); |
149 | } |
150 | } |
151 | |
152 | |
153 | void HTTPSClientSession::connect(const SocketAddress& targetAddress, const SocketAddress& sourceAddress) |
154 | { |
155 | if (getProxyHost().empty() || bypassProxy()) |
156 | { |
157 | connectToTargetPre(); |
158 | HTTPSession::connect(targetAddress, sourceAddress); |
159 | connectToTargetPost(); |
160 | } |
161 | else |
162 | { |
163 | connectToProxy(); |
164 | } |
165 | } |
166 | |
167 | |
168 | void HTTPSClientSession::connectToTargetPre() |
169 | { |
170 | SecureStreamSocket sss(socket()); |
171 | if (sss.getPeerHostName().empty()) |
172 | { |
173 | sss.setPeerHostName(getHost()); |
174 | } |
175 | if (_pContext->sessionCacheEnabled()) |
176 | { |
177 | sss.useSession(_pSession); |
178 | } |
179 | } |
180 | |
181 | |
182 | void HTTPSClientSession::connectToTargetPost() |
183 | { |
184 | SecureStreamSocket sss(socket()); |
185 | if (_pContext->sessionCacheEnabled()) |
186 | { |
187 | _pSession = sss.currentSession(); |
188 | } |
189 | } |
190 | |
191 | |
192 | void HTTPSClientSession::connectToProxy() |
193 | { |
194 | StreamSocket proxySocket(proxyConnect()); |
195 | SecureStreamSocket secureSocket = SecureStreamSocket::attach(proxySocket, getHost(), _pContext, _pSession); |
196 | attachSocket(secureSocket); |
197 | if (_pContext->sessionCacheEnabled()) |
198 | { |
199 | _pSession = secureSocket.currentSession(); |
200 | } |
201 | } |
202 | |
203 | |
204 | int HTTPSClientSession::read(char* buffer, std::streamsize length) |
205 | { |
206 | try |
207 | { |
208 | return HTTPSession::read(buffer, length); |
209 | } |
210 | catch(SSLConnectionUnexpectedlyClosedException&) |
211 | { |
212 | return 0; |
213 | } |
214 | } |
215 | |
216 | |
217 | Session::Ptr HTTPSClientSession::sslSession() |
218 | { |
219 | return _pSession; |
220 | } |
221 | |
222 | |
223 | } } // namespace Poco::Net |
224 | |