From e1198f29ce71cb834dbb3d9569b8b22eb07cf6cf Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Thu, 19 Sep 2013 11:54:24 +0200 Subject: [PATCH] Refactoring according to new coding guidelines --- Server/BasicDatastructures.h | 1 - Server/Connection.h | 230 +++++++++++++++++++++++++---------- 2 files changed, 166 insertions(+), 65 deletions(-) diff --git a/Server/BasicDatastructures.h b/Server/BasicDatastructures.h index 633675684..6d6d17485 100644 --- a/Server/BasicDatastructures.h +++ b/Server/BasicDatastructures.h @@ -129,7 +129,6 @@ std::vector Reply::HeaderstoBuffers(){ buffers.push_back(ToBuffer(status)); for (std::size_t i = 0; i < headers.size(); ++i) { Header& h = headers[i]; -// std::cout << h.name << ": " << h.value << std::endl; buffers.push_back(boost::asio::buffer(h.name)); buffers.push_back(boost::asio::buffer(seperators)); buffers.push_back(boost::asio::buffer(h.value)); diff --git a/Server/Connection.h b/Server/Connection.h index ffd6be3dc..1f1019d9a 100644 --- a/Server/Connection.h +++ b/Server/Connection.h @@ -25,12 +25,13 @@ or see http://www.gnu.org/licenses/agpl.txt. #include "RequestHandler.h" #include "RequestParser.h" -#include #include +#include +#include #include +#include #include #include -#include #include @@ -39,88 +40,178 @@ or see http://www.gnu.org/licenses/agpl.txt. namespace http { /// Represents a single connection from a client. -class Connection : public boost::enable_shared_from_this, private boost::noncopyable { +class Connection : public boost::enable_shared_from_this, + private boost::noncopyable { public: - explicit Connection(boost::asio::io_service& io_service, RequestHandler& handler) : strand(io_service), TCPsocket(io_service), requestHandler(handler) {} + explicit Connection( + boost::asio::io_service& io_service, + RequestHandler& handler + ) : strand(io_service), TCP_socket(io_service), request_handler(handler) { } boost::asio::ip::tcp::socket& socket() { - return TCPsocket; + return TCP_socket; } /// Start the first asynchronous operation for the connection. void start() { - TCPsocket.async_read_some(boost::asio::buffer(incomingDataBuffer), strand.wrap( boost::bind(&Connection::handleRead, this->shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred))); + TCP_socket.async_read_some( + boost::asio::buffer(incoming_data_buffer), + strand.wrap( boost::bind( + &Connection::handle_read, + this->shared_from_this(), + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred) + ) + ); } private: - void handleRead(const boost::system::error_code& e, std::size_t bytes_transferred) { - if (!e) { - CompressionType compressionType(noCompression); + void handle_read( + const boost::system::error_code& e, + std::size_t bytes_transferred + ) { + if( !e ) { + CompressionType compression_type(noCompression); boost::tribool result; - boost::tie(result, boost::tuples::ignore) = requestParser.Parse( request, incomingDataBuffer.data(), incomingDataBuffer.data() + bytes_transferred, &compressionType); + boost::tie(result, boost::tuples::ignore) = request_parser.Parse( + request, + incoming_data_buffer.data(), + incoming_data_buffer.data() + bytes_transferred, + &compression_type + ); - if (result) { - // std::cout << "----" << std::endl; - // if(compressionType == gzipRFC1952) - // std::cout << "[debug] using gzip" << std::endl; - // if(compressionType == deflateRFC1951) - // std::cout << "[debug] using deflate" << std::endl; - // if(compressionType == noCompression) - // std::cout << "[debug] no compression" << std::endl; - request.endpoint = TCPsocket.remote_endpoint().address(); - requestHandler.handle_request(request, reply); + if( result ) { + request.endpoint = TCP_socket.remote_endpoint().address(); + request_handler.handle_request(request, reply); - Header compressionHeader; - std::vector compressed; - std::vector outputBuffer; - switch(compressionType) { + Header compression_header; + std::vector compressed_output; + std::vector output_buffer; + switch(compression_type) { case deflateRFC1951: - compressionHeader.name = "Content-Encoding"; - compressionHeader.value = "deflate"; - reply.headers.insert(reply.headers.begin(), compressionHeader); //push_back(compressionHeader); - compressCharArray(reply.content.c_str(), reply.content.length(), compressed, compressionType); - reply.setSize(compressed.size()); - outputBuffer = reply.HeaderstoBuffers(); - outputBuffer.push_back(boost::asio::buffer(compressed)); - boost::asio::async_write(TCPsocket, outputBuffer, strand.wrap( boost::bind(&Connection::handleWrite, this->shared_from_this(), boost::asio::placeholders::error))); + compression_header.name = "Content-Encoding"; + compression_header.value = "deflate"; + reply.headers.insert( + reply.headers.begin(), + compression_header + ); + compressCharArray( + reply.content.c_str(), + reply.content.length(), + compressed_output, + compression_type + ); + reply.setSize(compressed_output.size()); + output_buffer = reply.HeaderstoBuffers(); + output_buffer.push_back( + boost::asio::buffer(compressed_output) + ); + boost::asio::async_write( + TCP_socket, + output_buffer, + strand.wrap( + boost::bind( + &Connection::handle_write, + this->shared_from_this(), + boost::asio::placeholders::error + ) + ) + ); break; case gzipRFC1952: - compressionHeader.name = "Content-Encoding"; - compressionHeader.value = "gzip"; - reply.headers.insert(reply.headers.begin(), compressionHeader); - compressCharArray(reply.content.c_str(), reply.content.length(), compressed, compressionType); - reply.setSize(compressed.size()); - outputBuffer = reply.HeaderstoBuffers(); - outputBuffer.push_back(boost::asio::buffer(compressed)); - boost::asio::async_write(TCPsocket, outputBuffer, strand.wrap( boost::bind(&Connection::handleWrite, this->shared_from_this(), boost::asio::placeholders::error)));break; + compression_header.name = "Content-Encoding"; + compression_header.value = "gzip"; + reply.headers.insert( + reply.headers.begin(), + compression_header + ); + compressCharArray( + reply.content.c_str(), + reply.content.length(), + compressed_output, + compression_type + ); + reply.setSize(compressed_output.size()); + output_buffer = reply.HeaderstoBuffers(); + output_buffer.push_back( + boost::asio::buffer(compressed_output) + ); + boost::asio::async_write( + TCP_socket, + output_buffer, + strand.wrap( + boost::bind( + &Connection::handle_write, + this->shared_from_this(), + boost::asio::placeholders::error + ) + ) + ); + break; case noCompression: - boost::asio::async_write(TCPsocket, reply.toBuffers(), strand.wrap( boost::bind(&Connection::handleWrite, this->shared_from_this(), boost::asio::placeholders::error))); + boost::asio::async_write( + TCP_socket, + reply.toBuffers(), + strand.wrap( + boost::bind( + &Connection::handle_write, + this->shared_from_this(), + boost::asio::placeholders::error + ) + ) + ); break; } - } else if (!result) { reply = Reply::stockReply(Reply::badRequest); - boost::asio::async_write(TCPsocket, reply.toBuffers(), strand.wrap( boost::bind(&Connection::handleWrite, this->shared_from_this(), boost::asio::placeholders::error))); + boost::asio::async_write( + TCP_socket, + reply.toBuffers(), + strand.wrap( + boost::bind( + &Connection::handle_write, + this->shared_from_this(), + boost::asio::placeholders::error + ) + ) + ); } else { - TCPsocket.async_read_some(boost::asio::buffer(incomingDataBuffer), strand.wrap( boost::bind(&Connection::handleRead, this->shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred))); + TCP_socket.async_read_some( + boost::asio::buffer(incoming_data_buffer), + strand.wrap( + boost::bind( + &Connection::handle_read, + this->shared_from_this(), + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred + ) + ) + ); } } } /// Handle completion of a write operation. - void handleWrite(const boost::system::error_code& e) { + void handle_write(const boost::system::error_code& e) { if (!e) { // Initiate graceful connection closure. boost::system::error_code ignoredEC; - TCPsocket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignoredEC); + TCP_socket.shutdown( + boost::asio::ip::tcp::socket::shutdown_both, + ignoredEC + ); } - // No new asynchronous operations are started. This means that all shared_ptr - // references to the connection object will disappear and the object will be - // destroyed automatically after this handler returns. The connection class's - // destructor closes the socket. } - void compressCharArray(const void *in_data, size_t in_data_size, std::vector &buffer, CompressionType type) { + // Big thanks to deusty who explains how to use gzip compression by + // the right call to deflateInit2(): + // http://deusty.blogspot.com/2007/07/gzip-compressiondecompression.html + void compressCharArray( + const char * in_data, + size_t in_data_size, + std::vector & buffer, + CompressionType type + ) { const size_t BUFSIZE = 128 * 1024; unsigned char temp_buffer[BUFSIZE]; @@ -140,20 +231,23 @@ private: deflateInit(&strm, Z_BEST_SPEED); break; case gzipRFC1952: - /* - * Big thanks to deusty who explains how to have gzip compression turned on by the right call to deflateInit2(): - * http://deusty.blogspot.com/2007/07/gzip-compressiondecompression.html - */ - deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 9, Z_DEFAULT_STRATEGY); + deflateInit2( + &strm, + Z_DEFAULT_COMPRESSION, + Z_DEFLATED, + (15+16), + 9, + Z_DEFAULT_STRATEGY + ); break; default: - assert(false); + BOOST_ASSERT_MSG(false, "should not happen"); break; } int deflate_res = Z_OK; do { - if (strm.avail_out == 0) { + if ( 0 == strm.avail_out ) { buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE); strm.next_out = temp_buffer; strm.avail_out = BUFSIZE; @@ -162,17 +256,25 @@ private: } while (deflate_res == Z_OK); - assert(deflate_res == Z_STREAM_END); - buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE - strm.avail_out); + BOOST_ASSERT_MSG( + deflate_res == Z_STREAM_END, + "compression not properly finished" + ); + + buffer.insert( + buffer.end(), + temp_buffer, + temp_buffer + BUFSIZE - strm.avail_out + ); deflateEnd(&strm); } boost::asio::io_service::strand strand; - boost::asio::ip::tcp::socket TCPsocket; - RequestHandler& requestHandler; - boost::array incomingDataBuffer; + boost::asio::ip::tcp::socket TCP_socket; + RequestHandler& request_handler; + boost::array incoming_data_buffer; Request request; - RequestParser requestParser; + RequestParser request_parser; Reply reply; };