Skip to content

Commit

Permalink
fixed parsing large post requests
Browse files Browse the repository at this point in the history
  • Loading branch information
loentar committed Jun 13, 2017
1 parent df9f607 commit 011162a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
8 changes: 6 additions & 2 deletions core/server/src/ClientHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ bool ClientHandler::readyRead(ClientContext* clientContext)
Status res = tryParseHeaders(clientContext, pool, findOffset);
switch (res) {
case Status::Again:
// if HTTP header is larger than default read size
if (received == static_cast<int64_t>(sizeToRead))
continue;
return true;
case Status::Close:
return false;
Expand Down Expand Up @@ -558,8 +561,8 @@ Status ClientHandler::tryNextRequest(ClientContext* clientContext)
clientContext->currentRequestOffset = clientContext->nextRequestOffset;
NGREST_ASSERT(clientContext->poolRead->getChunkCount() == 1, "Inconsistent mempool");
MemPool::Chunk* chunk = clientContext->poolRead->getChunks();
uint64_t remaining = chunk->size - clientContext->currentRequestOffset;
if (!remaining) {
if (clientContext->currentRequestOffset == INVALID_VALUE
|| chunk->size == clientContext->currentRequestOffset) {
// no data remaining - reset all
clientContext->currentRequestOffset = 0;
clientContext->nextRequestOffset = 0;
Expand All @@ -570,6 +573,7 @@ Status ClientHandler::tryNextRequest(ClientContext* clientContext)
// some data remaining in the read buffer
// there is a part of the next request or even the next full request

uint64_t remaining = chunk->size - clientContext->currentRequestOffset;
Status status = tryParseHeaders(clientContext, clientContext->poolRead, clientContext->currentRequestOffset);
switch (status) {
case Status::Again:
Expand Down
13 changes: 8 additions & 5 deletions core/utils/src/MemPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ MemPool::Chunk* MemPool::flatten(bool terminate)

char* pos = chunks->buffer + oldFirstChunkSize;

Chunk* curr = chunks + 1;
for (int i = 1; i < chunksCount; ++i, pos += curr->size, ++curr) {
for (Chunk* curr = (chunks + 1); curr != (currChunk + 1); pos += curr->size, ++curr) {
memcpy(pos, curr->buffer, curr->size);
::free(curr->buffer);
}
Expand Down Expand Up @@ -146,8 +145,13 @@ void MemPool::reserve(uint64_t size)

void MemPool::newChunk(uint64_t size)
{
if ((chunkIndex + 1) < chunksCount) {
Chunk* chunk = chunks + chunkIndex + 1;
if ((chunkIndex + 1) <= chunksCount) {
Chunk* chunk = chunks + chunkIndex;
if (chunk->size != 0) {
++chunk;
++chunkIndex;
} // else try to resize existing empty chunk

if (chunk->bufferSize < size) {
char* newBuffer = reinterpret_cast<char*>(realloc(chunk->buffer, size));
if (!newBuffer)
Expand All @@ -158,7 +162,6 @@ void MemPool::newChunk(uint64_t size)
}
chunk->size = 0;
currChunk = chunk;
++chunkIndex;
return;
}

Expand Down

0 comments on commit 011162a

Please sign in to comment.