diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2013-03-21 21:49:06 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2013-03-21 21:49:06 (GMT) |
| commit | 81a34222d1797f112a65c30f9ef7db07562ffdf1 (patch) | |
| tree | 4a7500b25f58cf13a4744ea75ae855c0495d5613 /src/client/requestbroker/APIRequest.cpp | |
| parent | b4564f212a17539604e9fa63d649e157f8888eb1 (diff) | |
| download | powder-81a34222d1797f112a65c30f9ef7db07562ffdf1.zip powder-81a34222d1797f112a65c30f9ef7db07562ffdf1.tar.gz | |
POST requests for APIRequest, allow saving user details from within the game, Asynchronous HTTP POST
Diffstat (limited to 'src/client/requestbroker/APIRequest.cpp')
| -rw-r--r-- | src/client/requestbroker/APIRequest.cpp | 65 |
1 files changed, 62 insertions, 3 deletions
diff --git a/src/client/requestbroker/APIRequest.cpp b/src/client/requestbroker/APIRequest.cpp index 71d683f..811096d 100644 --- a/src/client/requestbroker/APIRequest.cpp +++ b/src/client/requestbroker/APIRequest.cpp @@ -1,6 +1,10 @@ #include <iostream> #include <typeinfo> #include <cstdlib> +#include <cstring> +#include "Config.h" +#include "Format.h" +#include "client/Client.h" #include "APIRequest.h" #include "client/HTTP.h" #include "APIResultParser.h" @@ -8,6 +12,17 @@ APIRequest::APIRequest(std::string url, APIResultParser * parser, ListenerHandle listener): RequestBroker::Request(API, listener) { + Post = false; + HTTPContext = NULL; + Parser = parser; + URL = url; +} + +APIRequest::APIRequest(std::string url, std::map<std::string, std::string> postData, APIResultParser * parser, ListenerHandle listener): + RequestBroker::Request(API, listener) +{ + Post = true; + PostData = postData; HTTPContext = NULL; Parser = parser; URL = url; @@ -26,17 +41,18 @@ RequestBroker::ProcessResponse APIRequest::Process(RequestBroker & rb) if (status == 200 && data) { void * resultObject = Parser->ProcessResponse((unsigned char *)data, data_size); - free(data); 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" << status << std::endl; + std::cout << typeid(*this).name() << " Request for " << URL << " could not be parsed: " << data << std::endl; + free(data); return RequestBroker::Failed; } } @@ -55,7 +71,50 @@ RequestBroker::ProcessResponse APIRequest::Process(RequestBroker & rb) else { std::cout << typeid(*this).name() << " New Request for " << URL << std::endl; - HTTPContext = http_async_req_start(NULL, (char *)URL.c_str(), NULL, 0, 0); + 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[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); + } + 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); + } //RequestTime = time(NULL); } return RequestBroker::OK; |
