Browse Source

move post into get (removed postWebResource), refactored callbacks into one function

master
Odilitime 4 years ago
parent
commit
0f96ee13e4
2 changed files with 52 additions and 170 deletions
  1. 48
    167
      src/WebResource.cpp
  2. 4
    3
      src/WebResource.h

+ 48
- 167
src/WebResource.cpp View File

@@ -41,7 +41,7 @@ WebResource::WebResource(ResourceType rtype, std::string const& rraw) {
WebResource getWebResource(URL const& url) {
if (isOnlineResource(url)) {
//std::cout << "WebReousrce::getWebResource - isOnline" << std::endl;
return getOnlineWebResource(url);
return getOnlineWebResource(url, nullptr);
}
//std::cout << "WebReousrce::getWebResource - isOffline" << std::endl;
return getLocalWebResource(url);
@@ -79,104 +79,7 @@ WebResource getLocalWebResource(URL const& url) {
"Could not open file " + url.path);
}

WebResource getOnlineWebResource(URL const& url) {
std::shared_ptr<URL> uri=std::make_shared<URL>(url);

//window->currentURL=url;
//request->sendRequest(handleRequest);
WebResource returnRes;

std::string redirectLocation = "";

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");
}
} 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 if (contentType == "application/json") {
returnRes.resourceType = ResourceType::TXT;
} else {
returnRes.resourceType = ResourceType::HTML;
}
returnRes.raw = std::move(response.body);
}
}, 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");
}
} 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);
}
}, nullptr);
}

if (redirectLocation.size() > 0) {
if (redirectLocation == "_") {
return WebResource(ResourceType::INVALID,
"Got a 301 without a location");
}
return getOnlineWebResource(URL(redirectLocation));
}

return returnRes;
}

WebResource postWebResource(URL const& url, std::unique_ptr<std::map<std::string, std::string>> formData) {
WebResource getOnlineWebResource(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;
@@ -195,85 +98,63 @@ WebResource postWebResource(URL const& url, std::unique_ptr<std::map<std::string
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");

std::function<void(const HTTPResponse&)> responseCallback = [&](HTTPResponse const& response){
logDebug() << "getOnlineWebResource responseCallback 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 responseCallback - got " << response.statusCode << " without a location" << std::endl;
for(auto const &row : response.properties) {
logDebug() << "getOnlineWebResource responseCallback - " << row.first << "=" << response.properties.at(row.first) << std::endl;
}
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 {
// TODO: Set resourceType based on Content-Type field.
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 if (contentType == "application/json") {
returnRes.resourceType = ResourceType::TXT;
} else {
returnRes.resourceType = ResourceType::HTML;
returnRes.raw = std::move(response.body);
}
}, std::move(ptrPostBody));
returnRes.raw = std::move(response.body);
}
};
if (uri->scheme == "https") {
HTTPSRequest secure(uri);
if (formData) {
secure.method = Method::POST;
}
//secure.version = Version::HTTP11;
secure.sendRequest(responseCallback, 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));
request.sendRequest(responseCallback, std::move(ptrPostBody));
}
return returnRes;
}

+ 4
- 3
src/WebResource.h View File

@@ -4,6 +4,7 @@
#include <string>
#include <map>
#include "URL.h"
#include "networking/HTTPResponse.h"

enum class ResourceType {
INVALID,
@@ -19,18 +20,18 @@ struct WebResource {

ResourceType resourceType;
std::string raw;
static std::function<void(const HTTPResponse&)> responseCallback;
};


// Depending on the resourceName specified, this function will forward the call
// 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);

// Loads a resource from an internet address.
WebResource getOnlineWebResource(URL const& url);
WebResource getOnlineWebResource(URL const& url, std::unique_ptr<std::map<std::string, std::string>> formData);

#endif

Loading…
Cancel
Save