Browse Source

Add support for reading local files at startup

pull/2/head
Nubben 5 years ago
parent
commit
2f126bc42e
  1. 24
      src/WebResource.cpp
  2. 3
      src/WebResource.h
  3. 17
      src/main.cpp

24
src/WebResource.cpp

@ -15,6 +15,10 @@ std::map<std::string, ResourceType> strToRT = { @@ -15,6 +15,10 @@ std::map<std::string, ResourceType> strToRT = {
{"js", ResourceType::JS}
};
bool isOnlineResource(std::string const& resourceName) {
return resourceName.find("http:") != std::string::npos;
}
}
WebResource::WebResource() {
@ -28,7 +32,10 @@ WebResource::WebResource(ResourceType rtype, std::string const& rraw) { @@ -28,7 +32,10 @@ WebResource::WebResource(ResourceType rtype, std::string const& rraw) {
}
WebResource getWebResource(std::string resourceName) {
return WebResource(ResourceType::INVALID, "Not implemented");
if (isOnlineResource(resourceName)) {
return getOnlineWebResource(resourceName);
}
return getLocalWebResource(resourceName);
}
WebResource getLocalWebResource(std::string fileName) {
@ -64,5 +71,18 @@ WebResource getLocalWebResource(std::string fileName) { @@ -64,5 +71,18 @@ WebResource getLocalWebResource(std::string fileName) {
}
WebResource getOnlineWebResource(std::string url) {
return WebResource(ResourceType::INVALID, "Not implemented");
HTTPRequest request (getHostFromURL(url), getDocumentFromURL(url));
WebResource returnRes;
request.sendRequest([&](HTTPResponse const& response){
if (response.statusCode != 200) {
returnRes.resourceType = ResourceType::INVALID;
returnRes.raw = "Unsupported status code";
}
// TODO: Set resourceType based on Content-Type field.
returnRes.resourceType = ResourceType::HTML;
returnRes.raw = std::move(response.body);
});
return returnRes;
}

3
src/WebResource.h

@ -10,8 +10,7 @@ enum class ResourceType { @@ -10,8 +10,7 @@ enum class ResourceType {
JS
};
class WebResource {
public:
struct WebResource {
WebResource();
WebResource(ResourceType rtype, std::string const& rraw);

17
src/main.cpp

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
#include <iostream>
#include <memory>
#include "StringUtils.h"
#include "WebResource.h"
void handleRequest(const HTTPResponse &response);
@ -128,8 +129,20 @@ int main(int argc, char *argv[]) { @@ -128,8 +129,20 @@ int main(int argc, char *argv[]) {
return 1;
}
currentURL=argv[1];
const std::unique_ptr<HTTPRequest> request = std::make_unique<HTTPRequest>(getHostFromURL(currentURL), getDocumentFromURL(currentURL));
request->sendRequest(handleRequest);
WebResource res = getWebResource(currentURL);
if (res.resourceType == ResourceType::INVALID) {
std::cout << "Invalid resource type" << std::endl;
return 1;
}
const 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