diff --git a/src/WebResource.cpp b/src/WebResource.cpp index a6619ff5abf24d5271534429bc6b25fdf67ad17b..81c33d701aa9202c16d4e4bebd16b07351b6c6d4 100644 --- a/src/WebResource.cpp +++ b/src/WebResource.cpp @@ -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) { 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); } }); diff --git a/src/WebResource.h b/src/WebResource.h index 759fe314c697918f4723f3d318872d16dcc3b3cb..64b8cd0207272dd7890539341082e828036c2505 100644 --- a/src/WebResource.h +++ b/src/WebResource.h @@ -8,7 +8,8 @@ enum class ResourceType { INVALID, HTML, CSS, - JS + JS, + TXT }; struct WebResource { diff --git a/src/main.cpp b/src/main.cpp index 6052c079659b585116d07fe62da029e9e8b218a6..daee3e74d8dedc7b7281548b3db7ba1ae4159b5d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; }