Browse Source

postWebResource()

master
Odilitime 5 years ago
parent
commit
7363852cac
  1. 235
      src/WebResource.cpp
  2. 3
      src/WebResource.h

235
src/WebResource.cpp

@ -18,8 +18,8 @@ namespace { @@ -18,8 +18,8 @@ namespace {
{"html", ResourceType::HTML},
{"txt", ResourceType::TXT},
{"text", ResourceType::TXT},
{"css", ResourceType::CSS},
{"js", ResourceType::JS}
{"css", ResourceType::CSS}, //TXT?
{"js", ResourceType::JS} //TXT?
};
bool isOnlineResource(URL const& url) {
@ -89,81 +89,80 @@ WebResource getOnlineWebResource(URL const& url) { @@ -89,81 +89,80 @@ WebResource getOnlineWebResource(URL const& url) {
std::string redirectLocation = "";
if (uri->scheme == "http")
{
HTTPRequest request(uri);
request.sendRequest([&](HTTPResponse const& response){
logDebug() << "getOnlineWebResource request.sendRequest" << std::endl;
if (response.statusCode == 301 || response.statusCode == 302 || response.statusCode == 303) {
std::string location;
if (response.properties.find("Location")==response.properties.end()) {
if (response.properties.find("location")==response.properties.end()) {
logDebug() << "getOnlineWebResource - got " << response.statusCode << " without a location" << std::endl;
for(auto const &row : response.properties) {
logDebug() << "getOnlineWebResource - " << row.first << "=" << response.properties.at(row.first) << std::endl;
if (uri->scheme == "http") {
HTTPRequest request(uri);
request.sendRequest([&](HTTPResponse const& response){
logDebug() << "getOnlineWebResource request.sendRequest got " << response.statusCode << std::endl;
if (response.statusCode == 301 || response.statusCode == 302 || response.statusCode == 303) {
std::string location;
if (response.properties.find("Location")==response.properties.end()) {
if (response.properties.find("location")==response.properties.end()) {
logDebug() << "getOnlineWebResource - got " << response.statusCode << " without a location" << std::endl;
for(auto const &row : response.properties) {
logDebug() << "getOnlineWebResource - " << row.first << "=" << response.properties.at(row.first) << std::endl;
}
redirectLocation = "_";
} else {
location = response.properties.at("location");
}
redirectLocation = "_";
} else {
location = response.properties.at("location");
location = response.properties.at("Location");
}
logDebug() << "Redirect To: " << location << std::endl;
redirectLocation = location;
} else if (response.statusCode != 200) {
returnRes.resourceType = ResourceType::INVALID;
returnRes.raw = "Unsupported status code";
} else {
location = response.properties.at("Location");
}
logDebug() << "Redirect To: " << location << std::endl;
redirectLocation = location;
} else if (response.statusCode != 200) {
returnRes.resourceType = ResourceType::INVALID;
returnRes.raw = "Unsupported status code";
} else {
// TODO: Set resourceType based on Content-Type field.
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;
// TODO: Set resourceType based on Content-Type field.
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 if (contentType == "application/json") {
returnRes.resourceType = ResourceType::TXT;
} else {
returnRes.resourceType = ResourceType::HTML;
}
returnRes.raw = std::move(response.body);
}
returnRes.raw = std::move(response.body);
}
});
}
else
{
HTTPSRequest secure(uri);
secure.sendRequest([&](HTTPResponse const& response){
logDebug() << "getOnlineWebResource secure.sendRequest" << std::endl;
if (response.statusCode == 301 || response.statusCode == 302 || response.statusCode == 303) {
std::string location;
if (response.properties.find("Location")==response.properties.end()) {
if (response.properties.find("location")==response.properties.end()) {
logDebug() << "getOnlineWebResource - got " << response.statusCode << " without a location" << std::endl;
for(auto const &row : response.properties) {
logDebug() << "getOnlineWebResource - " << row.first << "=" << response.properties.at(row.first) << std::endl;
}, nullptr);
} else {
HTTPSRequest secure(uri);
secure.sendRequest([&](HTTPResponse const& response){
logDebug() << "getOnlineWebResource secure.sendRequest" << std::endl;
if (response.statusCode == 301 || response.statusCode == 302 || response.statusCode == 303) {
std::string location;
if (response.properties.find("Location")==response.properties.end()) {
if (response.properties.find("location")==response.properties.end()) {
logDebug() << "getOnlineWebResource - got " << response.statusCode << " without a location" << std::endl;
for(auto const &row : response.properties) {
logDebug() << "getOnlineWebResource - " << row.first << "=" << response.properties.at(row.first) << std::endl;
}
redirectLocation = "_";
} else {
location = response.properties.at("location");
}
redirectLocation = "_";
} else {
location = response.properties.at("location");
location = response.properties.at("Location");
}
logDebug() << "Redirect To: " << location << std::endl;
redirectLocation = location;
} else if (response.statusCode != 200) {
returnRes.resourceType = ResourceType::INVALID;
returnRes.raw = "Unsupported status code";
} else {
location = response.properties.at("Location");
// TODO: Set resourceType based on Content-Type field.
returnRes.resourceType = ResourceType::HTML;
returnRes.raw = std::move(response.body);
}
logDebug() << "Redirect To: " << location << std::endl;
redirectLocation = location;
} else if (response.statusCode != 200) {
returnRes.resourceType = ResourceType::INVALID;
returnRes.raw = "Unsupported status code";
} else {
// TODO: Set resourceType based on Content-Type field.
returnRes.resourceType = ResourceType::HTML;
returnRes.raw = std::move(response.body);
}
});
}, nullptr);
}
if (redirectLocation.size() > 0) {
@ -176,3 +175,105 @@ WebResource getOnlineWebResource(URL const& url) { @@ -176,3 +175,105 @@ WebResource getOnlineWebResource(URL const& url) {
return returnRes;
}
WebResource postWebResource(URL const& url, std::unique_ptr<std::map<std::string, std::string>> formData) {
std::shared_ptr<URL> uri=std::make_shared<URL>(url);
std::unique_ptr<std::string> ptrPostBody = nullptr;
if (formData) {
std::string postBody = "";
// iterator over formData
for (auto &entry : *formData) {
postBody += entry.first + "=" + entry.second + "&";
}
// strip last & off
postBody = postBody.substr(0, postBody.size() - 1);
std::cout << "postBody is " << postBody << std::endl;
// add to string
ptrPostBody = std::make_unique<std::string>(postBody);
}
WebResource returnRes;
std::string redirectLocation = "";
if (uri->scheme == "https") {
HTTPSRequest secure(uri);
if (formData) {
secure.method = Method::POST;
}
//secure.version = Version::HTTP11;
secure.sendRequest([&](HTTPResponse const& response){
logDebug() << "WebResource.postWebResource secure.sendRequest" << std::endl;
if (response.statusCode == 301 || response.statusCode == 302 || response.statusCode == 303) {
std::string location;
if (response.properties.find("Location")==response.properties.end()) {
if (response.properties.find("location")==response.properties.end()) {
logDebug() << "WebResource.postWebResource - got " << response.statusCode << " without a location" << std::endl;
for(auto const &row : response.properties) {
logDebug() << "WebResource.postWebResource - " << row.first << "=" << response.properties.at(row.first) << std::endl;
}
redirectLocation = "_";
} else {
location = response.properties.at("location");
}
} else {
location = response.properties.at("Location");
}
logDebug() << "Redirect To: " << location << std::endl;
redirectLocation = location;
} else if (response.statusCode != 200) {
returnRes.resourceType = ResourceType::INVALID;
returnRes.raw = "Unsupported status code";
} else {
// TODO: Set resourceType based on Content-Type field.
returnRes.resourceType = ResourceType::HTML;
returnRes.raw = std::move(response.body);
}
}, std::move(ptrPostBody));
} else {
HTTPRequest request(uri);
if (formData) {
request.method = Method::POST;
}
request.sendRequest([&](HTTPResponse const& response){
logDebug() << "getOnlineWebResource request.sendRequest" << std::endl;
if (response.statusCode == 301 || response.statusCode == 302 || response.statusCode == 303) {
std::string location;
if (response.properties.find("Location")==response.properties.end()) {
if (response.properties.find("location")==response.properties.end()) {
logDebug() << "getOnlineWebResource - got " << response.statusCode << " without a location" << std::endl;
for(auto const &row : response.properties) {
logDebug() << "getOnlineWebResource - " << row.first << "=" << response.properties.at(row.first) << std::endl;
}
redirectLocation = "_";
} else {
location = response.properties.at("location");
}
} else {
location = response.properties.at("Location");
}
logDebug() << "Redirect To: " << location << std::endl;
redirectLocation = location;
} else if (response.statusCode != 200) {
returnRes.resourceType = ResourceType::INVALID;
returnRes.raw = "Unsupported status code";
} else {
// TODO: Set resourceType based on Content-Type field.
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);
}
}, std::move(ptrPostBody));
}
return returnRes;
}

3
src/WebResource.h

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
#define WEBRESOURCE_H
#include <string>
#include <map>
#include "URL.h"
enum class ResourceType {
@ -24,6 +25,8 @@ struct WebResource { @@ -24,6 +25,8 @@ struct WebResource {
// to either getLocalWebResource or getOnlineWebResource.
WebResource getWebResource(URL const& url);
WebResource postWebResource(URL const& url, std::unique_ptr<std::map<std::string, std::string>> formData);
// Loads a resource from the local file storage.
WebResource getLocalWebResource(URL const& url);

Loading…
Cancel
Save