Skip to content

Commit

Permalink
removed hardcoded size for decompressed data buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-crowhurst committed Jun 23, 2015
1 parent 419961c commit a63ae59
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions example/compression/source/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@ using namespace framework;
void deflate_method_handler( const shared_ptr< Session >& session )
{
const auto request = session->get_request( );

size_t content_length = 0;
request->get_header( "Content-Length", content_length );

session->fetch( content_length, [ request ]( const shared_ptr< Session >& session, const Bytes& body )
session->fetch( content_length, [ request ]( const shared_ptr< Session >& session, const Bytes & body )
{
Bytes result = body;

if ( request->get_header( "Content-Encoding", String::lowercase ) == "deflate" )
{
size_t length = 15;
size_t length = compressBound( body.size( ) );
unsigned char* data = new unsigned char[ length ];
const int status = uncompress( data, &length, body.data( ), body.size( ) );

if ( status not_eq MZ_OK )
{
const auto message = String::format( "Failed to deflate: %s\n", mz_error( status ) );
session->close( 400, message, { { "Content-Length", ::to_string( message.length( ) ) }, { "Content-Type", "text/plain" } } );
delete[ ] data;
return;
}

result = Bytes( data, data + length );
delete[ ] data;
}

session->close( 200, result, { { "Content-Length", ::to_string( result.size( ) ) }, { "Content-Type", "text/plain" } } );
} );
}
Expand All @@ -58,11 +58,11 @@ int main( const int, const char** )
auto resource = make_shared< Resource >( );
resource->set_path( "/api/deflate" );
resource->set_method_handler( "POST", deflate_method_handler );

auto settings = make_shared< Settings >( );
settings->set_port( 1984 );
settings->set_default_header( "Connection", "close" );

Service service;
service.publish( resource );
service.start( settings );
Expand Down

0 comments on commit a63ae59

Please sign in to comment.