Browse Source

Use host from uri

pull/2/head
tomleb 5 years ago
parent
commit
2ac17670d0
  1. 2
      src/WebResource.cpp
  2. 4
      src/main.cpp
  3. 8
      src/networking/HTTPRequest.cpp
  4. 3
      src/networking/HTTPRequest.h

2
src/WebResource.cpp

@ -76,7 +76,7 @@ WebResource getLocalWebResource(std::string fileName) { @@ -76,7 +76,7 @@ WebResource getLocalWebResource(std::string fileName) {
WebResource getOnlineWebResource(std::string url) {
std::shared_ptr<URI> uri = parseUri(url);
HTTPRequest request (uri, getHostFromURL(url), getDocumentFromURL(url));
HTTPRequest request (uri, getDocumentFromURL(url));
WebResource returnRes;
request.sendRequest([&](HTTPResponse const& response){

4
src/main.cpp

@ -84,7 +84,7 @@ void navTo(std::string url) { @@ -84,7 +84,7 @@ void navTo(std::string url) {
std::shared_ptr<Node> rootNode = std::make_shared<Node>(NodeType::ROOT);
window->setDOM(rootNode);
std::shared_ptr<URI> uri = parseUri(url);
const std::shared_ptr<HTTPRequest> request = std::make_shared<HTTPRequest>(uri, getHostFromURL(url), getDocumentFromURL(url));
const std::shared_ptr<HTTPRequest> request = std::make_shared<HTTPRequest>(uri, getDocumentFromURL(url));
currentURL=url;
request->sendRequest(handleRequest);
}
@ -116,7 +116,7 @@ void handleRequest(const HTTPResponse &response) { @@ -116,7 +116,7 @@ void handleRequest(const HTTPResponse &response) {
}
std::cout << "Redirect To: " << location << std::endl;
std::shared_ptr<URI> uri = parseUri(location);
const std::unique_ptr<HTTPRequest> request = std::make_unique<HTTPRequest>(uri, getHostFromURL(location), getDocumentFromURL(location));
const std::unique_ptr<HTTPRequest> request = std::make_unique<HTTPRequest>(uri, getDocumentFromURL(location));
request->sendRequest(handleRequest);
return;
}

8
src/networking/HTTPRequest.cpp

@ -22,7 +22,7 @@ WSADATA wsaData; @@ -22,7 +22,7 @@ WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
HTTPRequest::HTTPRequest(const std::shared_ptr<URI> u, const std::string &hostName, const std::string &doc) {
HTTPRequest::HTTPRequest(const std::shared_ptr<URI> u, const std::string &doc) {
#ifdef WIN32
if (iResult != 0) {
std::cout << "WSAStartup failed: " << iResult << std::endl;
@ -33,20 +33,20 @@ HTTPRequest::HTTPRequest(const std::shared_ptr<URI> u, const std::string &hostNa @@ -33,20 +33,20 @@ HTTPRequest::HTTPRequest(const std::shared_ptr<URI> u, const std::string &hostNa
document = doc;
version = Version::HTTP10;
method = Method::GET;
host = hostName;
userAgent = "NetRunner";
}
bool HTTPRequest::sendRequest(std::function<void(const HTTPResponse&)> responseCallback) const {
struct addrinfo hints;
struct addrinfo *serverInfo = nullptr;
std::string host = uri->authority.host;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
const int res = getaddrinfo(host.c_str(), "80", &hints, &serverInfo);
const int res = getaddrinfo(uri->authority.host.c_str(), std::to_string(uri->authority.port).c_str(), &hints, &serverInfo);
if (res != 0) {
std::cout << "Could not lookup " << host << ": " << res << std::endl;
std::cout << "Could not lookup " << uri->authority.host << ": " << res << std::endl;
freeaddrinfo(serverInfo);
return false;
}

3
src/networking/HTTPRequest.h

@ -18,7 +18,7 @@ enum class Method { @@ -18,7 +18,7 @@ enum class Method {
class HTTPRequest {
public:
HTTPRequest(const std::shared_ptr<URI> u, const std::string &hostName, const std::string &doc);
HTTPRequest(const std::shared_ptr<URI> u, const std::string &doc);
bool sendRequest(std::function<void(const HTTPResponse&)> responseCallback) const;
const std::string versionToString(const Version version) const;
const std::string methodToString(const Method method) const;
@ -26,7 +26,6 @@ private: @@ -26,7 +26,6 @@ private:
std::string document;
Version version;
Method method;
std::string host;
std::string userAgent;
std::shared_ptr<URI> uri;
};

Loading…
Cancel
Save