diff --git a/include/Http.h b/include/Http.h index c3e447e..efdb544 100644 --- a/include/Http.h +++ b/include/Http.h @@ -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) diff --git a/include/HttpRequest.h b/include/HttpRequest.h index ddc00b7..2abf161 100644 --- a/include/HttpRequest.h +++ b/include/HttpRequest.h @@ -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; }; } diff --git a/include/HttpResponse.h b/include/HttpResponse.h index 573261c..b8b8785 100644 --- a/include/HttpResponse.h +++ b/include/HttpResponse.h @@ -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; }; } diff --git a/src/HttpRequest.cpp b/src/HttpRequest.cpp index 212dc6a..21fd60b 100644 --- a/src/HttpRequest.cpp +++ b/src/HttpRequest.cpp @@ -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; } } \ No newline at end of file diff --git a/src/HttpResponse.cpp b/src/HttpResponse.cpp index cc3b8bf..b10b45a 100644 --- a/src/HttpResponse.cpp +++ b/src/HttpResponse.cpp @@ -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"; diff --git a/src/HttpSocket.cpp b/src/HttpSocket.cpp index 8031067..8a23993 100644 --- a/src/HttpSocket.cpp +++ b/src/HttpSocket.cpp @@ -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()); } } \ No newline at end of file diff --git a/src/TcpSocket.cpp b/src/TcpSocket.cpp index b2fac14..736393e 100644 --- a/src/TcpSocket.cpp +++ b/src/TcpSocket.cpp @@ -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;