Skip to content

Commit a63ac30

Browse files
author
duzhongwei
committed
if not complete http parse, will pass EOF to http parser when connection closed; if http parser not consume received data, will close connection.
1 parent b4cacee commit a63ac30

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

include/brynet/net/http/HttpParser.hpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class HTTPParser
2222
{
2323
mLastWasValue = true;
2424

25+
mIsUpgrade = false;
2526
mIsWebSocket = false;
2627
mIsKeepAlive = false;
2728
mISCompleted = false;
@@ -44,6 +45,11 @@ class HTTPParser
4445

4546
virtual ~HTTPParser() = default;
4647

48+
bool isUpgrade() const
49+
{
50+
return mIsUpgrade;
51+
}
52+
4753
bool isWebSocket() const
4854
{
4955
return mIsWebSocket;
@@ -54,6 +60,12 @@ class HTTPParser
5460
return mIsKeepAlive;
5561
}
5662

63+
64+
bool isCompleted() const
65+
{
66+
return mISCompleted;
67+
}
68+
5769
int method() const
5870
{
5971
// mMethod's value defined in http_method, such as HTTP_GET、HTTP_POST.
@@ -137,6 +149,8 @@ class HTTPParser
137149
void clearParse()
138150
{
139151
mMethod = -1;
152+
mIsUpgrade = false;
153+
mIsWebSocket = false;
140154
mISCompleted = false;
141155
mLastWasValue = true;
142156
mUrl.clear();
@@ -154,7 +168,8 @@ class HTTPParser
154168
const size_t nparsed = http_parser_execute(&mParser, &mSettings, buffer, len);
155169
if (mISCompleted)
156170
{
157-
mIsWebSocket = mParser.upgrade;
171+
mIsUpgrade = mParser.upgrade;
172+
mIsWebSocket = mIsUpgrade && hasEntry("Upgrade", "websocket");
158173
mIsKeepAlive = hasEntry("Connection", "Keep-Alive");
159174
mMethod = mParser.method;
160175
http_parser_init(&mParser, mParserType);
@@ -163,11 +178,6 @@ class HTTPParser
163178
return nparsed;
164179
}
165180

166-
bool isCompleted() const
167-
{
168-
return mISCompleted;
169-
}
170-
171181
private:
172182
static int sChunkHeader(http_parser* hp)
173183
{
@@ -288,7 +298,8 @@ class HTTPParser
288298
http_parser_settings mSettings;
289299

290300
int mMethod = -1;
291-
bool mIsWebSocket;
301+
bool mIsUpgrade = false;
302+
bool mIsWebSocket = false;
292303
bool mIsKeepAlive;
293304
bool mISCompleted;
294305

include/brynet/net/http/HttpService.hpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,22 @@ class HttpService
182182
{
183183
/*TODO::keep alive and timeout close */
184184
auto& session = httpSession->getSession();
185+
auto httpParser = std::make_shared<HTTPParser>(HTTP_BOTH);
186+
187+
session->setDisConnectCallback([httpSession, httpParser](const TcpConnection::Ptr&) {
188+
if (!httpParser->isCompleted())
189+
{
190+
// try pass EOF to http parser
191+
HttpService::ProcessHttp(nullptr, 0, httpParser, httpSession);
192+
}
185193

186-
session->setDisConnectCallback([httpSession](const TcpConnection::Ptr&) {
187194
const auto& tmp = httpSession->getCloseCallback();
188195
if (tmp != nullptr)
189196
{
190197
tmp(httpSession);
191198
}
192199
});
193200

194-
auto httpParser = std::make_shared<HTTPParser>(HTTP_BOTH);
195201
session->setDataCallback([httpSession, httpParser](
196202
brynet::base::BasePacketReader& reader) {
197203
size_t retLen = 0;
@@ -203,12 +209,22 @@ class HttpService
203209
httpParser,
204210
httpSession);
205211
}
212+
else if (httpParser->isUpgrade())
213+
{
214+
// TODO::not support other upgrade protocol
215+
}
206216
else
207217
{
208218
retLen = HttpService::ProcessHttp(reader.begin(),
209219
reader.size(),
210220
httpParser,
211221
httpSession);
222+
// if http_parser_execute not consume all data that indicate cause error in parser.
223+
// so we need close connection.
224+
if (retLen != reader.size())
225+
{
226+
httpSession->postClose();
227+
}
212228
}
213229

214230
reader.addPos(retLen);

0 commit comments

Comments
 (0)