Browse Source

Clean up main

pull/2/head
Nubben 5 years ago
parent
commit
be87f065ba
  1. 5
      src/URL.cpp
  2. 24
      src/WebResource.cpp
  3. 7
      src/WebResource.h
  4. 146
      src/main.cpp

5
src/URL.cpp

@ -47,6 +47,11 @@ URL URL::merge(URL const& url) const { @@ -47,6 +47,11 @@ URL URL::merge(URL const& url) const {
}
returnURL.document += url.document;
}
auto hashPos = returnURL.document.find("#");
if (hashPos != std::string::npos) {
returnURL.document = returnURL.document.substr(0, hashPos);
}
return returnURL;
}

24
src/WebResource.cpp

@ -17,8 +17,8 @@ namespace { @@ -17,8 +17,8 @@ namespace {
{"js", ResourceType::JS}
};
bool isOnlineResource(std::string const& resourceName) {
return resourceName.find("://") != std::string::npos && resourceName.find("file://") == std::string::npos;
bool isOnlineResource(URL const& url) {
return url.protocol != "file";
}
}
@ -33,17 +33,17 @@ WebResource::WebResource(ResourceType rtype, std::string const& rraw) { @@ -33,17 +33,17 @@ WebResource::WebResource(ResourceType rtype, std::string const& rraw) {
raw = rraw;
}
WebResource getWebResource(std::string resourceName) {
if (isOnlineResource(resourceName)) {
WebResource getWebResource(URL const& url) {
if (isOnlineResource(url)) {
//std::cout << "WebReousrce::getWebResource - isOnline" << std::endl;
return getOnlineWebResource(resourceName);
return getOnlineWebResource(url);
}
//std::cout << "WebReousrce::getWebResource - isOffline" << std::endl;
return getLocalWebResource(resourceName);
return getLocalWebResource(url);
}
WebResource getLocalWebResource(std::string fileName) {
std::string fileExtension = getFilenameExtension(fileName);
WebResource getLocalWebResource(URL const& url) {
std::string fileExtension = getFilenameExtension(url.document);
if (fileExtension.length() == 0) {
return WebResource(ResourceType::INVALID,
"Could not find any file extension");
@ -60,7 +60,7 @@ WebResource getLocalWebResource(std::string fileName) { @@ -60,7 +60,7 @@ WebResource getLocalWebResource(std::string fileName) {
"Local file with extension " + fileExtension + " is not supported. Did you forget a http://?");
}
std::ifstream in(fileName, std::ios::in | std::ios::binary);
std::ifstream in(url.document, std::ios::in | std::ios::binary);
if (in) {
// There exists more efficient ways of doing this, but it works for the
// time being.
@ -71,11 +71,11 @@ WebResource getLocalWebResource(std::string fileName) { @@ -71,11 +71,11 @@ WebResource getLocalWebResource(std::string fileName) {
}
return WebResource(ResourceType::INVALID,
"Could not open file " + fileName);
"Could not open file " + url.document);
}
WebResource getOnlineWebResource(std::string url) {
HTTPRequest request (getHostFromURL(url), getDocumentFromURL(url));
WebResource getOnlineWebResource(URL const& url) {
HTTPRequest request (url.host, url.document);
WebResource returnRes;
request.sendRequest([&](HTTPResponse const& response){

7
src/WebResource.h

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
#define WEBRESOURCE_H
#include <string>
#include "URL.h"
enum class ResourceType {
INVALID,
@ -20,12 +21,12 @@ struct WebResource { @@ -20,12 +21,12 @@ struct WebResource {
// Depending on the resourceName specified, this function will forward the call
// to either getLocalWebResource or getOnlineWebResource.
WebResource getWebResource(std::string resourceName);
WebResource getWebResource(URL const& url);
// Loads a resource from the local file storage.
WebResource getLocalWebResource(std::string fileName);
WebResource getLocalWebResource(URL const& url);
// Loads a resource from an internet address.
WebResource getOnlineWebResource(std::string url);
WebResource getOnlineWebResource(URL const& url);
#endif

146
src/main.cpp

@ -10,118 +10,35 @@ @@ -10,118 +10,35 @@
#include "StringUtils.h"
#include "WebResource.h"
#include "CommandLineParams.h"
void handleRequest(const HTTPResponse &response);
#include "URL.h"
const std::unique_ptr<Window> window = std::make_unique<Window>();
std::string currentURL="";
URL currentURL;
void navTo(std::string url) {
std::cout << "go to: " << url << std::endl;
//
// All of this needs to be redone correctly
// this is just temporary hack code, so I can get to specific pages for debugging
//
const std::size_t dSlashPos=url.find("//");
if (dSlashPos!=std::string::npos) {
// remote
// check for relative
if (dSlashPos==0) {
// we only support http atm
url="http:"+url;
}
} else {
// https://github.com/unshiftio/url-parse/blob/master/index.js#L111
//auto const lastSlashPos=url.find_last_of('/');
// could collapse ./|/. to . and /./ to /
if (url=="." || url=="./") {
auto const lastSlashPos=getDocumentFromURL(currentURL).find_last_of('/');
if (lastSlashPos!=std::string::npos) {
url="http://"+getHostFromURL(currentURL)+getDocumentFromURL(currentURL).substr(0, lastSlashPos)+'/';
}
} else {
if (url[0]=='/') {
// absolute URL
url="http://"+getHostFromURL(currentURL)+url;
} else {
// relative URL
const std::string path=getDocumentFromURL(currentURL);
std::cout << "old path: " << path << std::endl;
if (url.length()>1 && url[0]=='.' && url[1]=='.') {
// are we at / ?
if (path=="/") {
// if so, remove ../
if (url.length()>2) {
url=url.substr(3);
} else {
url=url.substr(2);
}
url=path+url;
} else {
// .. but we're not in /
// make sure not a filename
auto const inDir=path.find_last_of("/");
if (inDir!=std::string::npos) {
// now take off a directory
auto const inDir2=path.substr(0, inDir).find_last_of("/");
url=path.substr(0, inDir2+1)+url.substr(2);
} else {
url=path+url;
}
}
} else {
url=path+url;
}
url="http://"+getHostFromURL(currentURL)+url;
}
}
}
// strip # off
std::size_t hashPos=url.find("#");
if (hashPos!=std::string::npos) {
url=url.substr(0, hashPos);
bool setWindowContent(URL const& url);
bool setWindowContent(URL const& url) {
WebResource res = getWebResource(url);
std::cout << "RESTYPE: " << (int)res.resourceType << std::endl;
if (res.resourceType == ResourceType::INVALID) {
std::cout << "Invalid resource type: " << res.raw << std::endl;
return false;
}
std::cout << "go to: " << url << std::endl;
std::shared_ptr<Node> rootNode = std::make_shared<Node>(NodeType::ROOT);
HTMLParser parser;
const std::clock_t begin = clock();
std::shared_ptr<Node> rootNode = parser.parse(res.raw);
const std::clock_t end = clock();
std::cout << "Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
window->setDOM(rootNode);
const std::unique_ptr<HTTPRequest> request = std::make_unique<HTTPRequest>(getHostFromURL(url), getDocumentFromURL(url));
currentURL=url;
request->sendRequest(handleRequest);
return true;
}
void handleRequest(const HTTPResponse &response) {
std::cout << "main:::handleRequest - statusCode: " << response.statusCode << std::endl;
if (response.statusCode == 200) {
const std::unique_ptr<HTMLParser> parser = std::make_unique<HTMLParser>();
const std::clock_t begin = clock();
std::shared_ptr<Node> rootNode = parser->parse(response.body);
const std::clock_t end = clock();
std::cout << "Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
window->setDOM(rootNode);
}
else if (response.statusCode == 301) {
std::string location;
if (response.properties.find("Location")==response.properties.end()) {
if (response.properties.find("location")==response.properties.end()) {
std::cout << "::handleRequest - got 301 without a location" << std::endl;
for(auto const &row : response.properties) {
std::cout << "::handleRequest - " << row.first << "=" << response.properties.at(row.first) << std::endl;
}
return;
} else {
location = response.properties.at("location");
}
} else {
location = response.properties.at("Location");
}
std::cout << "Redirect To: " << location << std::endl;
const std::unique_ptr<HTTPRequest> request = std::make_unique<HTTPRequest>(getHostFromURL(location), getDocumentFromURL(location));
request->sendRequest(handleRequest);
return;
}
else {
std::cout << "Unknown Status Code: " << response.statusCode << std::endl;
}
void navTo(std::string url) {
std::cout << "go to: " << url << std::endl;
currentURL = currentURL.merge(URL(url));
std::cout << "go to: " << currentURL << std::endl;
setWindowContent(currentURL);
}
int main(int argc, char *argv[]) {
@ -131,22 +48,17 @@ int main(int argc, char *argv[]) { @@ -131,22 +48,17 @@ int main(int argc, char *argv[]) {
}
initCLParams(argc, argv);
std::cout << "/g/ntr - NetRunner build " << __DATE__ << std::endl;
currentURL=argv[1];
std::string url = getCLParamByIndex(1);
if (url[0] == '/') {
url = "file://" + url;
}
currentURL=URL(url);
std::cout << "loading [" << currentURL << "]" << std::endl;
WebResource res = getWebResource(currentURL);
if (res.resourceType == ResourceType::INVALID) {
std::cout << "Invalid resource type: " << res.raw << std::endl;
if (!setWindowContent(currentURL)) {
return 1;
}
HTMLParser parser;
const std::clock_t begin = clock();
std::shared_ptr<Node> rootNode = parser.parse(res.raw);
const std::clock_t end = clock();
std::cout << "Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
window->setDOM(rootNode);
window->init();
if (!window->window) {
return 1;

Loading…
Cancel
Save