Fixed 'host' HTTP header not being sent

This commit is contained in:
Fred Nicolson 2016-12-11 15:14:23 +00:00
parent 98897dec02
commit 3b890972d1
7 changed files with 19 additions and 11 deletions

View File

@ -43,9 +43,10 @@ namespace fr
/*!
* Constructs a HTTP request/response to send.
*
* @param host The host that we're connected to.
* @return The HTTP request
*/
virtual std::string construct() const=0;
virtual std::string construct(const std::string &host) const=0;
/*!
* Gets the request type (post, get etc)

View File

@ -31,7 +31,7 @@ namespace fr
*
* @return The constructed HTTP request.
*/
std::string construct() const override;
std::string construct(const std::string &host) const override;
};
}

View File

@ -31,7 +31,7 @@ namespace fr
*
* @return The constructed HTTP response.
*/
std::string construct() const override;
std::string construct(const std::string &host) const override;
};
}

View File

@ -105,27 +105,31 @@ namespace fr
return;
}
std::string HttpRequest::construct() const
std::string HttpRequest::construct(const std::string &host) const
{
//Add HTTP header
std::string request = request_type_to_string(request_type) + " " + uri + " HTTP/1.1\r\n";
std::string request = request_type_to_string(request_type) + " " + uri + " HTTP/1.1\n";
//Add the headers to the request
for(const auto &header : headers)
{
std::string data = header.first + ": " + header.second + "\r\n";
std::string data = header.first + ": " + header.second + "\n";
request += data;
}
//Add in required headers if they're missing
if(headers.find("Connection") == headers.end())
request += "Connection: keep-alive\r\n";
request += "Connection: keep-alive\n";
if(headers.find("Host") == headers.end())
request += "Host: " + host + "\n";
//Add in space
request += "\r\n";
request += "\n";
//Add in the body
request += body + "\r\n";
request += body + "\n";
std::cout << "constructed: " << std::endl << request << std::endl;
return request;
}
}

View File

@ -9,6 +9,7 @@ namespace fr
{
void HttpResponse::parse(const std::string &response_data)
{
std::cout << "Parsing: " << response_data << std::endl;
//Clear old headers/data
clear();
@ -56,7 +57,7 @@ namespace fr
return;
}
std::string HttpResponse::construct() const
std::string HttpResponse::construct(const std::string &host) const
{
//Add HTTP header
std::string response = "HTTP/1.1 " + std::to_string(status) + " \r\n";

View File

@ -28,7 +28,7 @@ namespace fr
Socket::Status HttpSocket::send(const Http &request)
{
std::string data = request.construct();
std::string data = request.construct(remote_address);
return send_raw(&data[0], data.size());
}
}

View File

@ -156,6 +156,8 @@ namespace fr
Socket::Status TcpSocket::connect(const std::string &address, const std::string &port)
{
remote_address = address + ":" + port;
addrinfo *info;
addrinfo hints;