-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathConnectionInternal.cpp
More file actions
267 lines (229 loc) · 8.55 KB
/
Copy pathConnectionInternal.cpp
File metadata and controls
267 lines (229 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright 2017 Sony Corporation
*/
#include "Poco/DateTimeFormat.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/Mutex.h"
#include "Poco/String.h"
#include "Poco/URI.h"
#include "Poco/Net/HTTPMessage.h"
#include "easyhttpcpp/common/CoreLogger.h"
#include "easyhttpcpp/common/StringUtil.h"
#include "easyhttpcpp/HttpConstants.h"
#include "easyhttpcpp/HttpException.h"
#include "ConnectionInternal.h"
#include "ConnectionPoolInternal.h"
#include "ConnectionStatusListener.h"
using easyhttpcpp::common::StringUtil;
namespace easyhttpcpp {
static const std::string Tag = "ConnectionInternal";
ConnectionInternal::ConnectionInternal(PocoHttpClientSessionPtr pPocoHttpClientSession, const std::string& url,
EasyHttpContext::Ptr pContext) : m_connectionStatus(Inuse), m_cancelled(false),
m_pPocoHttpClientSession(pPocoHttpClientSession), m_hostPort(0), m_timeoutSec(0),
m_pConnectionStatusListener(NULL)
{
EASYHTTPCPP_LOG_D(Tag, "create this=[%p] url=[%s]", this, url.c_str());
try {
Poco::URI uri(url);
m_scheme = uri.getScheme();
m_hostName = uri.getHost();
m_hostPort = uri.getPort();
} catch (const Poco::Exception& e) {
EASYHTTPCPP_LOG_D(Tag, "ConnectionInternal: invalid url[%s] message=%s", url.c_str(), e.message().c_str());
throw HttpExecutionException(StringUtil::format(
"url is not valid. [%s] message=[%s]", url.c_str(), e.message().c_str()), e);
}
m_pProxy = pContext->getProxy();
m_rootCaDirectory = pContext->getRootCaDirectory();
m_rootCaFile = pContext->getRootCaFile();
m_timeoutSec = pContext->getTimeoutSec();
}
ConnectionInternal::~ConnectionInternal()
{
}
const std::string& ConnectionInternal::getProtocol() const
{
return Poco::Net::HTTPMessage::HTTP_1_1;
}
ConnectionInternal::ConnectionStatus ConnectionInternal::getStatus()
{
Poco::FastMutex::ScopedLock lock(m_instanceMutex);
return m_connectionStatus;
}
bool ConnectionInternal::setInuseIfReusable(const std::string& url, EasyHttpContext::Ptr pContext)
{
Poco::FastMutex::ScopedLock lock(m_instanceMutex);
if (m_connectionStatus != Idle) {
return false;
}
// check scheme, host name m host port
try {
Poco::URI uri(url);
if (Poco::icompare(uri.getScheme(), m_scheme) != 0) {
EASYHTTPCPP_LOG_V(Tag, "setInuseIfReusable: scheme is different. request=[%s] target=[%s]",
uri.getScheme().c_str(), m_scheme.c_str());
return false;
}
if (uri.getHost() != m_hostName) {
EASYHTTPCPP_LOG_V(Tag, "setInuseIfReusable: host name is different. request=[%s] target=[%s]",
uri.getHost().c_str(), m_hostName.c_str());
return false;
}
if (uri.getPort() != m_hostPort) {
EASYHTTPCPP_LOG_V(Tag, "setInuseIfReusable: port is different. request=[%u] target=[%u]",
uri.getPort(), m_hostPort);
return false;
}
} catch (const Poco::Exception& e) {
EASYHTTPCPP_LOG_D(Tag, "setInuseIfReusable: invalid url[%s] message=%s", url.c_str(), e.message().c_str());
return false;
}
// check proxy
Proxy::Ptr pProxy = pContext->getProxy();
if (pProxy) {
if (!m_pProxy) {
EASYHTTPCPP_LOG_V(Tag, "setInuseIfReusable: no proxy. request=[%s]%u]",
pProxy->getHost().c_str(), pProxy->getPort());
return false;
}
if (*pProxy != *m_pProxy) {
EASYHTTPCPP_LOG_V(Tag, "setInuseIfReusable: proxy name is different. request=[%s:%u] target=[%s:%u]",
pProxy->getHost().c_str(), pProxy->getPort(), m_pProxy->getHost().c_str(), m_pProxy->getPort());
return false;
}
} else {
if (m_pProxy) {
EASYHTTPCPP_LOG_V(Tag, "setInuseIfReusable: proxy exist. target=[%s:%u]",
m_pProxy->getHost().c_str(), m_pProxy->getPort());
return false;
}
}
// check rootCa
if (Poco::icompare(m_scheme, HttpConstants::Schemes::Https) == 0) {
if (pContext->getRootCaDirectory() != m_rootCaDirectory) {
EASYHTTPCPP_LOG_V(Tag, "setInuseIfReusable: rooCaDirectory is different. request=[%s] target=[%s]",
pContext->getRootCaDirectory().c_str(), m_rootCaDirectory.c_str());
return false;
}
if (pContext->getRootCaFile() != m_rootCaFile) {
EASYHTTPCPP_LOG_V(Tag, "setInuseIfReusable: rooCaFile is different. request=[%s] target=[%s]",
pContext->getRootCaFile().c_str(), m_rootCaFile.c_str());
return false;
}
}
// check timeout sec.
if (pContext->getTimeoutSec() != m_timeoutSec) {
EASYHTTPCPP_LOG_V(Tag, "setInuseIfReusable: timeoutSec is different. request=[%u] target=[%u]",
pContext->getTimeoutSec(), m_timeoutSec);
return false;
}
EASYHTTPCPP_LOG_D(Tag, "setInuseIfReusable: reuse Connection and change status to Inuse.");
m_connectionStatus = Inuse;
return true;
}
bool ConnectionInternal::cancel()
{
Poco::FastMutex::ScopedLock lock(m_instanceMutex);
if (m_cancelled) {
EASYHTTPCPP_LOG_D(Tag, "cancel: already cancelled");
return true;
}
bool ret = true;
m_cancelled = true;
EASYHTTPCPP_LOG_D(Tag, "cancel: cancelled");
if (m_pPocoHttpClientSession) {
try {
#ifdef WIN32
// Windows can not close socket with shutdown; We also need to call socket::close, so we call
// HttpClientSession::abort.
EASYHTTPCPP_LOG_D(Tag, "cancel: call HTTPClientSession::abort.");
m_pPocoHttpClientSession->abort();
#else
// Since communication can be interrupted by calling Socket::shutdown, no need to call
// Socket::close on Linux.
EASYHTTPCPP_LOG_D(Tag, "cancel: call HTTPClientSession::socket::shutdown.");
m_pPocoHttpClientSession->socket().shutdown();
#endif
} catch (const Poco::Exception& e) {
EASYHTTPCPP_LOG_D(Tag, "cancel: HTTPClientSession::abort throws PocoException [%s]", e.message().c_str());
ret = false;
} catch (const std::exception& e) {
EASYHTTPCPP_LOG_D(Tag, "cancel: HTTPClientSession::abort throws std::exception [%s]", e.what());
ret = false;
}
}
// When releaseConnection, create KeepAliveTimeoutTask and clear reference of ConnectionInternal from HttpEngine.
// Therefore, there is no KeepAliveTimeoutTask when cancel is called from HttpEngine.
// It is not necessary to call m_pKeepAliveTimeoutTask->cancel().
return ret;
}
bool ConnectionInternal::isCancelled()
{
Poco::FastMutex::ScopedLock lock(m_instanceMutex);
return m_cancelled;
}
PocoHttpClientSessionPtr ConnectionInternal::getPocoHttpClientSession() const
{
return m_pPocoHttpClientSession;
}
void ConnectionInternal::setConnectionStatusListener(ConnectionStatusListener* pListener)
{
Poco::FastMutex::ScopedLock lock(m_connectionStatusListenerMutex);
m_pConnectionStatusListener = pListener;
}
bool ConnectionInternal::onConnectionReleased()
{
{
Poco::FastMutex::ScopedLock lock(m_connectionStatusListenerMutex);
if (m_pConnectionStatusListener) {
bool listenerInvalidated = false;
m_pConnectionStatusListener->onIdle(this, listenerInvalidated);
if (listenerInvalidated) {
m_pConnectionStatusListener = NULL;
}
}
}
{
Poco::FastMutex::ScopedLock lock(m_instanceMutex);
EASYHTTPCPP_LOG_D(Tag, "releaseConnection: change status to Idle");
if (m_connectionStatus == Idle) {
EASYHTTPCPP_LOG_D(Tag, "releaseConnection: status is already Idle.");
return false;
}
m_connectionStatus = Idle;
}
return true;
}
const std::string& ConnectionInternal::getScheme() const
{
return m_scheme;
}
const std::string& ConnectionInternal::getHostName() const
{
return m_hostName;
}
unsigned short ConnectionInternal::getHostPort() const
{
return m_hostPort;
}
Proxy::Ptr ConnectionInternal::getProxy() const
{
return m_pProxy;
}
const std::string& ConnectionInternal::getRootCaDirectory() const
{
return m_rootCaDirectory;
}
const std::string& ConnectionInternal::getRootCaFile() const
{
return m_rootCaFile;
}
unsigned int ConnectionInternal::getTimeoutSec() const
{
return m_timeoutSec;
}
ConnectionStatusListener* ConnectionInternal::getConnectionStatusListener()
{
return m_pConnectionStatusListener;
}
} /* namespace easyhttpcpp */