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
26using Poco::NumberFormatter;
27using Poco::IllegalStateException;
28
29
30namespace Poco {
31namespace Net {
32
33
34HTTPSClientSession::HTTPSClientSession():
35 HTTPClientSession(SecureStreamSocket()),
36 _pContext(SSLManager::instance().defaultClientContext())
37{
38 setPort(HTTPS_PORT);
39}
40
41
42HTTPSClientSession::HTTPSClientSession(const SecureStreamSocket& socket):
43 HTTPClientSession(socket),
44 _pContext(socket.context())
45{
46 setPort(HTTPS_PORT);
47}
48
49
50HTTPSClientSession::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
59HTTPSClientSession::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
68HTTPSClientSession::HTTPSClientSession(Context::Ptr pContext):
69 HTTPClientSession(SecureStreamSocket(pContext)),
70 _pContext(pContext)
71{
72}
73
74
75HTTPSClientSession::HTTPSClientSession(Context::Ptr pContext, Session::Ptr pSession):
76 HTTPClientSession(SecureStreamSocket(pContext, pSession)),
77 _pContext(pContext),
78 _pSession(pSession)
79{
80}
81
82
83HTTPSClientSession::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
92HTTPSClientSession::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
102HTTPSClientSession::~HTTPSClientSession()
103{
104}
105
106
107bool HTTPSClientSession::secure() const
108{
109 return true;
110}
111
112
113void HTTPSClientSession::abort()
114{
115 SecureStreamSocket sss(socket());
116 sss.abort();
117}
118
119
120X509Certificate HTTPSClientSession::serverCertificate()
121{
122 SecureStreamSocket sss(socket());
123 return sss.peerCertificate();
124}
125
126
127std::string HTTPSClientSession::proxyRequestPrefix() const
128{
129 return std::string();
130}
131
132
133void HTTPSClientSession::proxyAuthenticate(HTTPRequest& request)
134{
135}
136
137
138void HTTPSClientSession::connect(const SocketAddress& address)
139{
140 if (getProxyHost().empty() || bypassProxy())
141 {
142 SecureStreamSocket sss(socket());
143 if (sss.getPeerHostName().empty())
144 {
145 sss.setPeerHostName(getHost());
146 }
147 if (_pContext->sessionCacheEnabled())
148 {
149 sss.useSession(_pSession);
150 }
151 HTTPSession::connect(address);
152 if (_pContext->sessionCacheEnabled())
153 {
154 _pSession = sss.currentSession();
155 }
156 }
157 else
158 {
159 StreamSocket proxySocket(proxyConnect());
160 SecureStreamSocket secureSocket = SecureStreamSocket::attach(proxySocket, getHost(), _pContext, _pSession);
161 attachSocket(secureSocket);
162 if (_pContext->sessionCacheEnabled())
163 {
164 _pSession = secureSocket.currentSession();
165 }
166 }
167}
168
169
170int HTTPSClientSession::read(char* buffer, std::streamsize length)
171{
172 try
173 {
174 return HTTPSession::read(buffer, length);
175 }
176 catch(SSLConnectionUnexpectedlyClosedException&)
177 {
178 return 0;
179 }
180}
181
182
183Session::Ptr HTTPSClientSession::sslSession()
184{
185 return _pSession;
186}
187
188
189} } // namespace Poco::Net
190