Browse Source

detect context-type, plain/text support

pull/2/head
Odilitime 5 years ago
parent
commit
b2f1b7373a
  1. 16
      src/WebResource.cpp
  2. 3
      src/WebResource.h
  3. 37
      src/main.cpp

16
src/WebResource.cpp

@ -16,6 +16,8 @@ namespace { @@ -16,6 +16,8 @@ namespace {
// only used for local files atm
std::map<std::string, ResourceType> strToRT = {
{"html", ResourceType::HTML},
{"txt", ResourceType::TXT},
{"text", ResourceType::TXT},
{"css", ResourceType::CSS},
{"js", ResourceType::JS}
};
@ -114,7 +116,19 @@ WebResource getOnlineWebResource(URL const& url) { @@ -114,7 +116,19 @@ WebResource getOnlineWebResource(URL const& url) {
returnRes.raw = "Unsupported status code";
} else {
// TODO: Set resourceType based on Content-Type field.
returnRes.resourceType = ResourceType::HTML;
std::string contentType = "";
if (response.properties.find("Content-type") != response.properties.end()) {
contentType = response.properties.at("Content-type");
}
if (response.properties.find("Content-Type") != response.properties.end()) {
contentType = response.properties.at("Content-Type");
}
std::cout << "Content-type: " << contentType << std::endl;
if (contentType == "text/plain") {
returnRes.resourceType = ResourceType::TXT;
} else {
returnRes.resourceType = ResourceType::HTML;
}
returnRes.raw = std::move(response.body);
}
});

3
src/WebResource.h

@ -8,7 +8,8 @@ enum class ResourceType { @@ -8,7 +8,8 @@ enum class ResourceType {
INVALID,
HTML,
CSS,
JS
JS,
TXT
};
struct WebResource {

37
src/main.cpp

@ -23,14 +23,35 @@ bool setWindowContent(URL const& url) { @@ -23,14 +23,35 @@ bool setWindowContent(URL const& url) {
}
// parse HTML
HTMLParser parser;
const std::clock_t begin = clock();
std::shared_ptr<Node> rootNode = parser.parse(res.raw);
const std::clock_t end = clock();
logDebug() << "main::setWindowContent - Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
// send NodeTree to window
window->setDOM(rootNode);
if (res.resourceType == ResourceType::HTML) {
HTMLParser parser;
const std::clock_t begin = clock();
std::shared_ptr<Node> rootNode = parser.parse(res.raw);
const std::clock_t end = clock();
logDebug() << "main::setWindowContent - Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
// send NodeTree to window
window->setDOM(rootNode);
} else if (res.resourceType == ResourceType::TXT) {
std::cout << "Rendering text document" << std::endl;
std::shared_ptr<Node> rootNode = std::make_shared<Node>(NodeType::ROOT);
std::shared_ptr<TagNode> tagNode = std::make_shared<TagNode>();
std::shared_ptr<TextNode> textNode = std::make_shared<TextNode>();
textNode->text = res.raw;
tagNode->tag="p";
// bind text to tag
textNode->parent = tagNode;
tagNode->children.push_back(textNode);
// bind tag to root
tagNode->parent = rootNode;
rootNode->children.push_back(tagNode);
// send NodeTree to window
window->setDOM(rootNode);
} else {
std::cout << "setWindowContent() - I don't know how to render non-html files" << std::endl;
}
return true;
}

Loading…
Cancel
Save