Skip to content

Commit 443ce89

Browse files
authored
Merge pull request #2 from vvigilante/fix/readline_inf_loop
Fix/readline inf loop
2 parents 894603a + 85a05b5 commit 443ce89

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/HTTPConnection.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -289,26 +289,24 @@ void HTTPConnection::raiseError(uint16_t code, std::string reason) {
289289
void HTTPConnection::readLine(int lengthLimit) {
290290
while(_bufferProcessed < _bufferUnusedIdx) {
291291
char newChar = _receiveBuffer[_bufferProcessed];
292-
293-
if ( newChar == '\r') {
294-
// Look ahead for \n (if not possible, wait for next round
295-
if (_bufferProcessed+1 < _bufferUnusedIdx) {
296-
if (_receiveBuffer[_bufferProcessed+1] == '\n') {
297-
_bufferProcessed += 2;
298-
_parserLine.parsingFinished = true;
299-
return;
300-
} else {
301-
// Line has not been terminated by \r\n
302-
HTTPS_LOGW("Line without \\r\\n (got only \\r). FID=%d", _socket);
303-
raiseError(400, "Bad Request");
304-
return;
305-
}
292+
_bufferProcessed++;
293+
if ( partialTerminationParsed ){
294+
partialTerminationParsed = false;
295+
if (newChar == '\n') {
296+
_parserLine.parsingFinished = true;
297+
} else {
298+
// Line has not been terminated by \r\n
299+
HTTPS_LOGW("Line without \\r\\n (got only \\r). FID=%d", _socket);
300+
raiseError(400, "Bad Request");
306301
}
302+
return;
303+
}
304+
if ( newChar == '\r') {
305+
partialTerminationParsed = true;
307306
} else {
308307
_parserLine.text += newChar;
309-
_bufferProcessed += 1;
310308
}
311-
309+
312310
// Check that the max request string size is not exceeded
313311
if (_parserLine.text.length() > lengthLimit) {
314312
HTTPS_LOGW("Header length exceeded. FID=%d", _socket);

src/HTTPConnection.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ class HTTPConnection : private ConnectionContext {
135135
int _bufferProcessed;
136136
// The index on the receive_buffer that is the first one which is empty at the end.
137137
int _bufferUnusedIdx;
138+
// If \r character has been read, in this case we expect \n to terminate the line
139+
bool partialTerminationParsed = false;
138140

139141
// Socket address, length etc for the connection
140142
struct sockaddr _sockAddr;

0 commit comments

Comments
 (0)