This repository has been archived on 2025-03-20. You can view files and clone it, but cannot push or open issues or pull requests.
The-Powder-Toy/src/client/requestbroker/WebRequest.cpp
jacksonmj 236ff08da9 Fix some problems noted in http://www.viva64.com/en/b/0298/
I've left some of the less important items, like SearchView.cpp "'then' statement is equivalent to the 'else' statement", and RequestBroker::Request::~Request, because I don't feel like spending a few days entirely rewriting those files at the moment (which is what I'd end up doing if I started fixing minor problems and refactoring...)

GameSave::readOPS - not changed. At some point we may have to move to a larger type for element IDs (probably two or four bytes), but PT_NUM isn't likely to be raised to the maximum value of that type immediately, so this check will be needed then. There should be an elements[partsData[i]].Enabled check in there too, but it might be a bit difficult - I'm not sure how to access a Simulation object from GameSave::readOPS...

Notes on changes:

Graphics::textsize, Element_FRZW::Element_FRZW - typos

Button::Draw - the extra case was originally used to invert the icon (draw it in black instead of in white) when the button was clicked. However, the icon colour is now automatically set depending on the background colour. (Note similar conditions "if(Enabled) { if(isButtonDown || (isTogglable && toggle)) " near the start of the function - same logic but in a different place, setting icon colour indirectly).

Simulation::transform_save - unused redundant function, everything uses GameSave::Transform which does much the same thing.

PreviewView::NotifySaveChanged - should be height==YRES/2, it's checking whether the preview image is the correct size, and resizing it if it isn't.

Element_FWRK::update - no idea why that line was there, even though it was my commit that originally added it...
2014-12-25 17:09:35 +00:00

145 lines
3.7 KiB
C++

#include <iostream>
#include <vector>
#include <typeinfo>
#include <cstdlib>
#include <cstring>
#include "Config.h"
#include "Format.h"
#include "client/Client.h"
#include "WebRequest.h"
#include "client/HTTP.h"
#include "APIResultParser.h"
WebRequest::WebRequest(std::string url, ListenerHandle listener, int identifier):
RequestBroker::Request(API, listener, identifier)
{
Post = false;
HTTPContext = NULL;
URL = url;
}
WebRequest::WebRequest(std::string url, std::map<std::string, std::string> postData, ListenerHandle listener, int identifier):
RequestBroker::Request(API, listener, identifier)
{
Post = true;
PostData = postData;
HTTPContext = NULL;
URL = url;
}
RequestBroker::ProcessResponse WebRequest::Process(RequestBroker & rb)
{
if(HTTPContext)
{
if(http_async_req_status(HTTPContext))
{
char * data;
int status, data_size;
data = http_async_req_stop(HTTPContext, &status, &data_size);
if (status == 200 && data)
{
void * resultObject = new std::vector<unsigned char>(data, data+data_size);
if(resultObject)
{
this->ResultObject = resultObject;
rb.requestComplete(this);
free(data);
return RequestBroker::Finished;
}
else
{
std::cout << typeid(*this).name() << " Request for " << URL << " could not be parsed: " << data << std::endl;
free(data);
return RequestBroker::Failed;
}
}
else
{
//#ifdef DEBUG
std::cout << typeid(*this).name() << " Request for " << URL << " failed with status " << status << std::endl;
//#endif
if(data)
free(data);
return RequestBroker::Failed;
}
}
}
else
{
std::cout << typeid(*this).name() << " New Request for " << URL << std::endl;
if(Post)
{
char ** postNames = new char*[PostData.size() + 1];
char ** postData = new char*[PostData.size()];
int * postLength = new int[PostData.size()];
int i = 0;
std::map<std::string, std::string>::iterator iter = PostData.begin();
while(iter != PostData.end())
{
std::string name = iter->first;
std::string data = iter->second;
char * cName = new char[name.length() + 1];
char * cData = new char[data.length() + 1];
std::strcpy(cName, name.c_str());
std::strcpy(cData, data.c_str());
postNames[i] = cName;
postData[i] = cData;
postLength[i] = data.length();
i++;
iter++;
}
postNames[i] = NULL;
if(Client::Ref().GetAuthUser().ID)
{
std::cout << typeid(*this).name() << " Authenticated " << std::endl;
User user = Client::Ref().GetAuthUser();
char userName[12];
char *userSession = new char[user.SessionID.length() + 1];
std::strcpy(userName, format::NumberToString<int>(user.ID).c_str());
std::strcpy(userSession, user.SessionID.c_str());
HTTPContext = http_multipart_post_async((char*)URL.c_str(), postNames, postData, postLength, userName, NULL, userSession);
delete[] userSession;
}
else
{
HTTPContext = http_multipart_post_async((char*)URL.c_str(), postNames, postData, postLength, NULL, NULL, NULL);
}
}
else
{
HTTPContext = http_async_req_start(NULL, (char *)URL.c_str(), NULL, 0, 0);
if(Client::Ref().GetAuthUser().ID)
{
User user = Client::Ref().GetAuthUser();
char userName[12];
char *userSession = new char[user.SessionID.length() + 1];
std::strcpy(userName, format::NumberToString<int>(user.ID).c_str());
std::strcpy(userSession, user.SessionID.c_str());
http_auth_headers(HTTPContext, userName, NULL, userSession);
delete[] userSession;
}
}
}
return RequestBroker::OK;
}
WebRequest::~WebRequest()
{
}
void WebRequest::Cleanup()
{
Request::Cleanup();
if(ResultObject)
{
delete (std::vector<unsigned char>*)ResultObject;
ResultObject = NULL;
}
}