From df509a42f5b6e5f8625b52b4a5c68296292ae7c0 Mon Sep 17 00:00:00 2001 From: Fred Nicolson Date: Fri, 7 Jul 2017 11:05:19 +0100 Subject: [PATCH] Socket and HTTP Response bug fixes fr::TcpSocket checked if the recv response was greater than 0, which created issues as 0 is a valid result if no data was received and no error occurred. fr::HttpResponse::construct() now adds in a content-length header. fr::HttpResponse::parse trims the body if it's larger than content length. fr::Http::set_uri() now prepends a '/' if one's not provided. Fixed fr::Http::parse_header from cutting off the end of headers. --- include/frnetlib/HttpRequest.h | 2 +- src/Http.cpp | 12 ++++++++++-- src/HttpRequest.cpp | 4 ++-- src/HttpResponse.cpp | 5 ++++- src/TcpSocket.cpp | 4 ++-- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/frnetlib/HttpRequest.h b/include/frnetlib/HttpRequest.h index 0b37f33..d2d1463 100644 --- a/include/frnetlib/HttpRequest.h +++ b/include/frnetlib/HttpRequest.h @@ -41,7 +41,7 @@ namespace fr * * @param header_end_pos The position in 'body' of the end of the header */ - bool parse_header(int32_t header_end_pos); + bool parse_header(int64_t header_end_pos); /*! * Parses the POST data from the body diff --git a/src/Http.cpp b/src/Http.cpp index 45508bd..513f410 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -119,7 +119,15 @@ namespace fr void Http::set_uri(const std::string &str) { - uri = str; + //Don't do anything if there's no URI provided + if(str.empty()) + return; + + //Ensure that URI begins with a / + if(str.front() == '/') + uri = str; + else + uri = "/" + str; } std::string Http::request_type_to_string(RequestType type) const @@ -234,7 +242,7 @@ namespace fr { std::string header_name = str.substr(0, colon_pos); std::transform(header_name.begin(), header_name.end(), header_name.begin(), ::tolower); - header_data.emplace(std::move(header_name), str.substr(data_begin, str.size() - (data_begin + 1))); + header_data.emplace(std::move(header_name), str.substr(data_begin, str.size() - data_begin)); } } } diff --git a/src/HttpRequest.cpp b/src/HttpRequest.cpp index 3b1e0ce..d4a4148 100644 --- a/src/HttpRequest.cpp +++ b/src/HttpRequest.cpp @@ -53,13 +53,13 @@ namespace fr return true; } - bool HttpRequest::parse_header(int32_t header_end_pos) + bool HttpRequest::parse_header(int64_t header_end_pos) { try { //Split the header into lines size_t line = 0; - std::vector header_lines = split_string(body.substr(0, header_end_pos)); + std::vector header_lines = split_string(body.substr(0, (unsigned long)header_end_pos)); if(header_lines.empty()) return false; diff --git a/src/HttpResponse.cpp b/src/HttpResponse.cpp index 33cbd2d..3ec55b8 100644 --- a/src/HttpResponse.cpp +++ b/src/HttpResponse.cpp @@ -46,6 +46,8 @@ namespace fr body += response_data.substr(header_end + 4, response_data.size() - header_end - 4); } + if(body.size() > content_length) + body.resize(content_length); return body.size() < content_length; } @@ -67,12 +69,13 @@ namespace fr response += "connection: close_socket\r\n"; if(header_data.find("content-type") == header_data.end()) response += "content-type: text/html\r\n"; + response += "content-length: " + std::to_string(body.size()) + "\r\n"; //Add in space response += "\r\n"; //Add in the body - response += body + "\r\n"; + response += body; return response; } diff --git a/src/TcpSocket.cpp b/src/TcpSocket.cpp index d319f82..cf48457 100644 --- a/src/TcpSocket.cpp +++ b/src/TcpSocket.cpp @@ -53,9 +53,9 @@ namespace fr received = 0; //Read RECV_CHUNK_SIZE bytes into the recv buffer - int32_t status = ::recv(socket_descriptor, (char*)data, buffer_size, 0); + int64_t status = ::recv(socket_descriptor, (char*)data, buffer_size, 0); - if(status > 0) + if(status >= 0) { received += status; }