From 41f600cd11d4219200d660b2c85c639a6e78a324 Mon Sep 17 00:00:00 2001 From: Fred Nicolson Date: Wed, 13 Nov 2019 17:02:23 +0000 Subject: [PATCH] HTTP construction and parsing bug fixes Don't trim received HTTP body if content length isn't specified. Don't add duplicate \r\n after POST string data, if no POST data is specified. --- src/HttpRequest.cpp | 5 ++++- src/HttpResponse.cpp | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/HttpRequest.cpp b/src/HttpRequest.cpp index 6cb96cc..7f83832 100644 --- a/src/HttpRequest.cpp +++ b/src/HttpRequest.cpp @@ -137,7 +137,10 @@ namespace fr if(++iter != get_data.end()) post_string += "&"; } - post_string += "\r\n"; + if(!post_string.empty()) + { + post_string += "\r\n"; + } //Add in required headers if they're missing if(header_data.find("Connection") == header_data.end()) diff --git a/src/HttpResponse.cpp b/src/HttpResponse.cpp index 9eccc36..d78509b 100644 --- a/src/HttpResponse.cpp +++ b/src/HttpResponse.cpp @@ -51,7 +51,7 @@ namespace fr return fr::Socket::Status::HttpBodyTooBig; //Cut off any data if it exceeds content length, todo: potentially an issue, could cut the next request off - if(body.size() > content_length) + if(content_length > 0 && body.size() > content_length) body.resize(content_length); else if(body.size() < content_length) return fr::Socket::Status::NotEnoughData; @@ -78,7 +78,7 @@ namespace fr response += "connection: keep-alive\r\n"; if(header_data.find("content-type") == header_data.end()) response += "content-type: text/html\r\n"; - if(header_data.find("content-length") == header_data.end()) + if(header_data.find("content-length") == header_data.end() && !body.empty()) response += "content-length: " + std::to_string(body.size()) + "\r\n"; //Add in space