Skip to content

Commit 3003bda

Browse files
KaloNKvvigilante
andauthored
Fix/readline inf loop (#6)
* Fix infinite loop when the buffer ends with \r * Properly check for end of line across buffers Co-authored-by: vvigilante <[email protected]>
1 parent acb9fe8 commit 3003bda

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/HTTPConnection.cpp

+14-16
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

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ class HTTPConnection : private ConnectionContext {
131131
int _bufferProcessed;
132132
// The index on the receive_buffer that is the first one which is empty at the end.
133133
int _bufferUnusedIdx;
134+
// If \r character has been read, in this case we expect \n to terminate the line
135+
bool partialTerminationParsed = false;
134136

135137
// Socket address, length etc for the connection
136138
struct sockaddr _sockAddr;

0 commit comments

Comments
 (0)