Browse Source

Use path from uri instead of document

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

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, getDocumentFromURL(url));
HTTPRequest request (uri);
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, getDocumentFromURL(url));
const std::shared_ptr<HTTPRequest> request = std::make_shared<HTTPRequest>(uri);
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, getDocumentFromURL(location));
const std::unique_ptr<HTTPRequest> request = std::make_unique<HTTPRequest>(uri);
request->sendRequest(handleRequest);
return;
}

6
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 &doc) {
HTTPRequest::HTTPRequest(const std::shared_ptr<URI> u) {
#ifdef WIN32
if (iResult != 0) {
std::cout << "WSAStartup failed: " << iResult << std::endl;
@ -30,7 +30,6 @@ HTTPRequest::HTTPRequest(const std::shared_ptr<URI> u, const std::string &doc) { @@ -30,7 +30,6 @@ HTTPRequest::HTTPRequest(const std::shared_ptr<URI> u, const std::string &doc) {
}
#endif
uri = u;
document = doc;
version = Version::HTTP10;
method = Method::GET;
userAgent = "NetRunner";
@ -40,13 +39,14 @@ bool HTTPRequest::sendRequest(std::function<void(const HTTPResponse&)> responseC @@ -40,13 +39,14 @@ bool HTTPRequest::sendRequest(std::function<void(const HTTPResponse&)> responseC
struct addrinfo hints;
struct addrinfo *serverInfo = nullptr;
std::string host = uri->authority.host;
std::string document = uri->path;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
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 " << uri->authority.host << ": " << res << std::endl;
std::cout << "Could not lookup " << host << ": " << res << std::endl;
freeaddrinfo(serverInfo);
return false;
}

3
src/networking/HTTPRequest.h

@ -18,12 +18,11 @@ enum class Method { @@ -18,12 +18,11 @@ enum class Method {
class HTTPRequest {
public:
HTTPRequest(const std::shared_ptr<URI> u, const std::string &doc);
HTTPRequest(const std::shared_ptr<URI> u);
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;
private:
std::string document;
Version version;
Method method;
std::string userAgent;

2
src/networking/URI.cpp

@ -71,7 +71,7 @@ std::unique_ptr<URI> parseUri(std::string raw) { @@ -71,7 +71,7 @@ std::unique_ptr<URI> parseUri(std::string raw) {
last = cursor + 1;
state = AUTHORITY;
} else {
// TODO Handle this, URI can have only one slash
// TODO Handle this, URI may have only one slash
}
} else if (state == AUTHORITY) {
/* At this point, this could either be the semi colon for

Loading…
Cancel
Save