Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite loop when the buffer ends with \r #123 #2

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/HTTPConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,26 +289,24 @@ void HTTPConnection::raiseError(uint16_t code, std::string reason) {
void HTTPConnection::readLine(int lengthLimit) {
while(_bufferProcessed < _bufferUnusedIdx) {
char newChar = _receiveBuffer[_bufferProcessed];

if ( newChar == '\r') {
// Look ahead for \n (if not possible, wait for next round
if (_bufferProcessed+1 < _bufferUnusedIdx) {
if (_receiveBuffer[_bufferProcessed+1] == '\n') {
_bufferProcessed += 2;
_parserLine.parsingFinished = true;
return;
} else {
// Line has not been terminated by \r\n
HTTPS_LOGW("Line without \\r\\n (got only \\r). FID=%d", _socket);
raiseError(400, "Bad Request");
return;
}
_bufferProcessed++;
if ( partialTerminationParsed ){
partialTerminationParsed = false;
if (newChar == '\n') {
_parserLine.parsingFinished = true;
} else {
// Line has not been terminated by \r\n
HTTPS_LOGW("Line without \\r\\n (got only \\r). FID=%d", _socket);
raiseError(400, "Bad Request");
}
return;
}
if ( newChar == '\r') {
partialTerminationParsed = true;
} else {
_parserLine.text += newChar;
_bufferProcessed += 1;
}

// Check that the max request string size is not exceeded
if (_parserLine.text.length() > lengthLimit) {
HTTPS_LOGW("Header length exceeded. FID=%d", _socket);
Expand Down
2 changes: 2 additions & 0 deletions src/HTTPConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class HTTPConnection : private ConnectionContext {
int _bufferProcessed;
// The index on the receive_buffer that is the first one which is empty at the end.
int _bufferUnusedIdx;
// If \r character has been read, in this case we expect \n to terminate the line
bool partialTerminationParsed = false;

// Socket address, length etc for the connection
struct sockaddr _sockAddr;
Expand Down