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
|
//
// HTTPServerConnection.cpp
//
// Library: Net
// Package: HTTPServer
// Module: HTTPServerConnection
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/HTTPServerConnection.h"
#include "Poco/Net/HTTPServerSession.h"
#include "Poco/Net/HTTPServerRequestImpl.h"
#include "Poco/Net/HTTPServerResponseImpl.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/NetException.h"
#include "Poco/NumberFormatter.h"
#include "Poco/Timestamp.h"
#include "Poco/Delegate.h"
#include <memory>
namespace Poco {
namespace Net {
HTTPServerConnection::HTTPServerConnection(const StreamSocket& socket, HTTPServerParams::Ptr pParams, HTTPRequestHandlerFactory::Ptr pFactory):
TCPServerConnection(socket),
_pParams(pParams),
_pFactory(pFactory),
_stopped(false)
{
poco_check_ptr (pFactory);
_pFactory->serverStopped += Poco::delegate(this, &HTTPServerConnection::onServerStopped);
}
HTTPServerConnection::~HTTPServerConnection()
{
try
{
_pFactory->serverStopped -= Poco::delegate(this, &HTTPServerConnection::onServerStopped);
}
catch (...)
{
poco_unexpected();
}
}
void HTTPServerConnection::run()
{
std::string server = _pParams->getSoftwareVersion();
HTTPServerSession session(socket(), _pParams);
while (!_stopped && session.hasMoreRequests())
{
try
{
Poco::FastMutex::ScopedLock lock(_mutex);
if (!_stopped)
{
HTTPServerResponseImpl response(session);
HTTPServerRequestImpl request(response, session, _pParams);
Poco::Timestamp now;
response.setDate(now);
response.setVersion(request.getVersion());
response.setKeepAlive(_pParams->getKeepAlive() && request.getKeepAlive() && session.canKeepAlive());
if (!server.empty())
response.set("Server", server);
try
{
#ifndef POCO_ENABLE_CPP11
std::auto_ptr<HTTPRequestHandler> pHandler(_pFactory->createRequestHandler(request));
#else
std::unique_ptr<HTTPRequestHandler> pHandler(_pFactory->createRequestHandler(request));
#endif
if (pHandler.get())
{
if (request.getExpectContinue() && response.getStatus() == HTTPResponse::HTTP_OK)
response.sendContinue();
pHandler->handleRequest(request, response);
session.setKeepAlive(_pParams->getKeepAlive() && response.getKeepAlive() && session.canKeepAlive());
}
else sendErrorResponse(session, HTTPResponse::HTTP_NOT_IMPLEMENTED);
}
catch (Poco::Exception&)
{
if (!response.sent())
{
try
{
sendErrorResponse(session, HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
}
catch (...)
{
}
}
throw;
}
}
}
catch (NoMessageException&)
{
break;
}
catch (MessageException&)
{
sendErrorResponse(session, HTTPResponse::HTTP_BAD_REQUEST);
}
catch (Poco::Exception&)
{
if (session.networkException())
{
session.networkException()->rethrow();
}
else throw;
}
}
}
void HTTPServerConnection::sendErrorResponse(HTTPServerSession& session, HTTPResponse::HTTPStatus status)
{
HTTPServerResponseImpl response(session);
response.setVersion(HTTPMessage::HTTP_1_1);
response.setStatusAndReason(status);
response.setKeepAlive(false);
response.send();
session.setKeepAlive(false);
}
void HTTPServerConnection::onServerStopped(const bool& abortCurrent)
{
_stopped = true;
if (abortCurrent)
{
try
{
// Note: On Windows, select() will not return if one of its socket is being
// shut down. Therefore we have to call close(), which works better.
// On other platforms, we do the more graceful thing.
#if defined(_WIN32)
socket().close();
#else
socket().shutdown();
#endif
}
catch (...)
{
}
}
else
{
Poco::FastMutex::ScopedLock lock(_mutex);
try
{
#if defined(_WIN32)
socket().close();
#else
socket().shutdown();
#endif
}
catch (...)
{
}
}
}
} } // namespace Poco::Net
|