Index: ChangeLog =================================================================== --- ChangeLog (revision 36640) +++ ChangeLog (working copy) @@ -1,3 +1,6 @@ +Fri Nov 6 22:54:38 CET 2015 + Fixing the buffer shrinkage issue, this time with test. -CG + Tue Nov 3 23:24:52 CET 2015 Undoing change from Sun Oct 25 15:29:23 CET 2015 as the original code was counter-intuitive but Index: src/include/microhttpd.h =================================================================== --- src/include/microhttpd.h (revision 36640) +++ src/include/microhttpd.h (working copy) @@ -130,7 +130,7 @@ * Current version of the library. * 0x01093001 = 1.9.30-1. */ -#define MHD_VERSION 0x00094600 +#define MHD_VERSION 0x00094601 /** * MHD-internal return code for "YES". Index: src/microhttpd/connection.c =================================================================== --- src/microhttpd/connection.c (revision 36640) +++ src/microhttpd/connection.c (working copy) @@ -2559,14 +2559,15 @@ /* can try to keep-alive */ connection->version = NULL; connection->state = MHD_CONNECTION_INIT; - /* read_buffer_size is correct here, as we want to - preserve the entire read buffer allocation, even - if in terms of the data we only care to preserve - up to "read_buffer_offset" */ + /* Reset the read buffer to the starting size, + preserving the bytes we have already read. */ connection->read_buffer = MHD_pool_reset (connection->pool, connection->read_buffer, - connection->read_buffer_size); + connection->read_buffer_offset, + connection->daemon->pool_size / 2); + connection->read_buffer_size + = connection->daemon->pool_size / 2; } connection->client_aware = MHD_NO; connection->client_context = NULL; Index: src/microhttpd/memorypool.c =================================================================== --- src/microhttpd/memorypool.c (revision 36640) +++ src/microhttpd/memorypool.c (working copy) @@ -219,7 +219,8 @@ if ((pool->end < old_size) || (pool->end < asize)) return NULL; /* unsatisfiable or bogus request */ - if ((pool->pos >= old_size) && (&pool->memory[pool->pos - old_size] == old)) + if ( (pool->pos >= old_size) && + (&pool->memory[pool->pos - old_size] == old) ) { /* was the previous allocation - optimize! */ if (pool->pos + asize - old_size <= pool->end) @@ -251,32 +252,40 @@ /** * Clear all entries from the memory pool except - * for @a keep of the given @a size. + * for @a keep of the given @a size. The pointer + * returned should be a buffer of @a new_size where + * the first @a copy_bytes are from @a keep. * * @param pool memory pool to use for the operation * @param keep pointer to the entry to keep (maybe NULL) - * @param size how many bytes need to be kept at this address + * @param copy_bytes how many bytes need to be kept at this address + * @param new_size how many bytes should the allocation we return have? + * (should be larger or equal to @a copy_bytes) * @return addr new address of @a keep (if it had to change) */ void * MHD_pool_reset (struct MemoryPool *pool, void *keep, - size_t size) + size_t copy_bytes, + size_t new_size) { if (NULL != keep) { if (keep != pool->memory) { - memmove (pool->memory, keep, size); + memmove (pool->memory, + keep, + copy_bytes); keep = pool->memory; } } pool->end = pool->size; - memset (&pool->memory[size], + /* technically not needed, but safer to zero out */ + memset (&pool->memory[copy_bytes], 0, - pool->size - size); + pool->size - copy_bytes); if (NULL != keep) - pool->pos = ROUND_TO_ALIGN(size); + pool->pos = ROUND_TO_ALIGN (new_size); return keep; } Index: src/microhttpd/memorypool.h =================================================================== --- src/microhttpd/memorypool.h (revision 36640) +++ src/microhttpd/memorypool.h (working copy) @@ -99,16 +99,21 @@ /** * Clear all entries from the memory pool except - * for "keep" of the given "size". + * for @a keep of the given @a copy_bytes. The pointer + * returned should be a buffer of @a new_size where + * the first @a copy_bytes are from @a keep. * * @param pool memory pool to use for the operation * @param keep pointer to the entry to keep (maybe NULL) - * @param size how many bytes need to be kept at this address - * @return addr new address of "keep" (if it had to change) + * @param copy_bytes how many bytes need to be kept at this address + * @param new_size how many bytes should the allocation we return have? + * (should be larger or equal to @a copy_bytes) + * @return addr new address of @a keep (if it had to change) */ void * MHD_pool_reset (struct MemoryPool *pool, void *keep, - size_t size); + size_t copy_bytes, + size_t new_size); #endif