Bug fixes

Fixed Content-Length header being added to Http responses even if there's a pre-existing one.

Fixed header parsing sometimes adding a carriage return to the header value.

Removed useless copy/move constructors from HttpResponse, causing issues.
This commit is contained in:
Unknown 2017-07-10 21:47:34 +01:00
parent df509a42f5
commit 93ec4fa999
3 changed files with 21 additions and 26 deletions

View File

@ -17,8 +17,6 @@ namespace fr
public: public:
//Constructors //Constructors
HttpResponse(){}; HttpResponse(){};
HttpResponse(HttpResponse &&other);
HttpResponse(const HttpResponse &);
virtual ~HttpResponse(){} virtual ~HttpResponse(){}
/*! /*!

View File

@ -234,17 +234,27 @@ namespace fr
void Http::parse_header_line(const std::string &str) void Http::parse_header_line(const std::string &str)
{ {
auto colon_pos = str.find(":"); size_t colon_pos = str.find(':');
if(colon_pos != std::string::npos) if(colon_pos == std::string::npos)
{ return;
auto data_begin = str.find_first_not_of(" ", colon_pos + 1); auto data_begin = str.find_first_not_of(" ", colon_pos + 1);
if(data_begin != std::string::npos) if(data_begin == std::string::npos)
return;
size_t data_len = 0;
for(size_t a = data_begin; a < str.size(); a++)
{ {
if(str[a] >= 32 && str[a] <= 126)
data_len++;
else
break;
}
std::string header_name = str.substr(0, colon_pos); std::string header_name = str.substr(0, colon_pos);
std::string header_value = str.substr(data_begin, data_len);
std::transform(header_name.begin(), header_name.end(), header_name.begin(), ::tolower); 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)); header_data.emplace(std::move(header_name), std::move(header_value));
}
}
} }
void Http::load_mimetypes() void Http::load_mimetypes()

View File

@ -7,20 +7,6 @@
namespace fr namespace fr
{ {
HttpResponse::HttpResponse(HttpResponse &&other)
: header_ended(other.header_ended),
content_length(other.content_length)
{
}
HttpResponse::HttpResponse(const HttpResponse &other)
: header_ended(other.header_ended),
content_length(other.content_length)
{
}
bool HttpResponse::parse(const std::string &response_data) bool HttpResponse::parse(const std::string &response_data)
{ {
body += response_data; body += response_data;
@ -69,6 +55,7 @@ namespace fr
response += "connection: close_socket\r\n"; response += "connection: close_socket\r\n";
if(header_data.find("content-type") == header_data.end()) if(header_data.find("content-type") == header_data.end())
response += "content-type: text/html\r\n"; response += "content-type: text/html\r\n";
if(header_data.find("content-length") == header_data.end())
response += "content-length: " + std::to_string(body.size()) + "\r\n"; response += "content-length: " + std::to_string(body.size()) + "\r\n";
//Add in space //Add in space