From ca07e477fe9a01df4666e2669b2fb6d96f434eff Mon Sep 17 00:00:00 2001 From: Thulinma Date: Fri, 23 Aug 2013 00:04:38 +0200 Subject: [PATCH] HTTP library usability improvements. --- lib/http_parser.cpp | 62 ++++++++++++++++++++++++++++++++------------- lib/http_parser.h | 3 ++- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/lib/http_parser.cpp b/lib/http_parser.cpp index 268b01c3..35747a89 100644 --- a/lib/http_parser.cpp +++ b/lib/http_parser.cpp @@ -114,6 +114,35 @@ void HTTP::Parser::SendResponse(std::string code, std::string message, Socket::C } } conn.SendNow("\r\n", 2); + conn.SendNow(body); +} + +/// Creates and sends a valid HTTP 1.0 or 1.1 response, based on the given request. +/// The headers must be set before this call is made. +/// This call sets up chunked transfer encoding if the request was protocol HTTP/1.1, otherwise uses a zero-content-length HTTP/1.0 response. +/// \param code The HTTP response code. Usually you want 200. +/// \param message The HTTP response message. Usually you want "OK". +/// \param request The HTTP request to respond to. +/// \param conn The connection to send over. +void HTTP::Parser::StartResponse(std::string code, std::string message, HTTP::Parser & request, Socket::Connection & conn){ + protocol = request.protocol; + body = ""; + if (protocol == "HTTP/1.1"){ + SetHeader("Transfer-Encoding", "chunked"); + }else{ + SetBody(""); + } + SendResponse(code, message, conn); +} + +/// Creates and sends a valid HTTP 1.0 or 1.1 response, based on the given request. +/// The headers must be set before this call is made. +/// This call sets up chunked transfer encoding if the request was protocol HTTP/1.1, otherwise uses a zero-content-length HTTP/1.0 response. +/// This call simply calls StartResponse("200", "OK", request, conn) +/// \param request The HTTP request to respond to. +/// \param conn The connection to send over. +void HTTP::Parser::StartResponse(HTTP::Parser & request, Socket::Connection & conn){ + StartResponse("200", "OK", request, conn); } /// After receiving a header with this object, this function call will: @@ -483,27 +512,17 @@ void HTTP::Parser::parseVars(std::string data){ } } -/// Converts a string to chunked format if protocol is HTTP/1.1 - does nothing otherwise. -/// \param bodypart The data to convert - will be converted in-place. -void HTTP::Parser::Chunkify(std::string & bodypart){ - if (protocol == "HTTP/1.1"){ - char len[10]; - int sizelen = snprintf(len, 10, "%x\r\n", (unsigned int)bodypart.size()); - //prepend the chunk size and \r\n - bodypart.insert(0, len, sizelen); - //append \r\n - bodypart.append("\r\n", 2); - } -} - -/// Converts a string to chunked format if protocol is HTTP/1.1 - does nothing otherwise. -/// \param bodypart The data to convert - will be converted in-place. +/// Sends a string in chunked format if protocol is HTTP/1.1, sends as-is otherwise. +/// \param bodypart The data to send. +/// \param conn The connection to use for sending. void HTTP::Parser::Chunkify(std::string & bodypart, Socket::Connection & conn){ Chunkify(bodypart.c_str(), bodypart.size(), conn); } -/// Converts a string to chunked format if protocol is HTTP/1.1 - does nothing otherwise. -/// \param bodypart The data to convert - will be converted in-place. +/// Sends a string in chunked format if protocol is HTTP/1.1, sends as-is otherwise. +/// \param data The data to send. +/// \param size The size of the data to send. +/// \param conn The connection to use for sending. void HTTP::Parser::Chunkify(const char * data, unsigned int size, Socket::Connection & conn){ if (protocol == "HTTP/1.1"){ char len[10]; @@ -515,9 +534,16 @@ void HTTP::Parser::Chunkify(const char * data, unsigned int size, Socket::Connec //append \r\n conn.SendNow("\r\n", 2); if ( !size){ - //append \r\n again! + //append \r\n again if this was the end of the file (required by chunked transfer encoding according to spec) conn.SendNow("\r\n", 2); } + }else{ + //just send the chunk itself + conn.SendNow(data, size); + //close the connection if this was the end of the file + if ( !size){ + conn.close(); + } } } diff --git a/lib/http_parser.h b/lib/http_parser.h index bb6ff8a5..35fdfee8 100644 --- a/lib/http_parser.h +++ b/lib/http_parser.h @@ -28,7 +28,8 @@ namespace HTTP { std::string & BuildResponse(std::string code, std::string message); void SendRequest(Socket::Connection & conn); void SendResponse(std::string code, std::string message, Socket::Connection & conn); - void Chunkify(std::string & bodypart); + void StartResponse(std::string code, std::string message, Parser & request, Socket::Connection & conn); + void StartResponse(Parser & request, Socket::Connection & conn); void Chunkify(std::string & bodypart, Socket::Connection & conn); void Chunkify(const char * data, unsigned int size, Socket::Connection & conn); void Proxy(Socket::Connection & from, Socket::Connection & to);