Replace http_* calls with Download calls in non-RequestBroker stuff
This commit is contained in:
parent
9e110cba73
commit
8b5cf394e0
@ -44,6 +44,7 @@
|
|||||||
#include "client/UserInfo.h"
|
#include "client/UserInfo.h"
|
||||||
#include "gui/preview/Comment.h"
|
#include "gui/preview/Comment.h"
|
||||||
#include "ClientListener.h"
|
#include "ClientListener.h"
|
||||||
|
#include "client/Download.h"
|
||||||
#include "requestbroker/RequestBroker.h"
|
#include "requestbroker/RequestBroker.h"
|
||||||
#include "requestbroker/WebRequest.h"
|
#include "requestbroker/WebRequest.h"
|
||||||
#include "requestbroker/APIRequest.h"
|
#include "requestbroker/APIRequest.h"
|
||||||
@ -63,8 +64,8 @@ extern "C"
|
|||||||
|
|
||||||
Client::Client():
|
Client::Client():
|
||||||
messageOfTheDay("Fetching the message of the day..."),
|
messageOfTheDay("Fetching the message of the day..."),
|
||||||
versionCheckRequest(NULL),
|
versionCheckRequest(nullptr),
|
||||||
alternateVersionCheckRequest(NULL),
|
alternateVersionCheckRequest(nullptr),
|
||||||
usingAltUpdateServer(false),
|
usingAltUpdateServer(false),
|
||||||
updateAvailable(false),
|
updateAvailable(false),
|
||||||
authUser(0, "")
|
authUser(0, "")
|
||||||
@ -134,28 +135,23 @@ void Client::Initialise(ByteString proxyString)
|
|||||||
stampsLib.close();
|
stampsLib.close();
|
||||||
|
|
||||||
//Begin version check
|
//Begin version check
|
||||||
versionCheckRequest = http_async_req_start(NULL, "http://" SERVER "/Startup.json", NULL, 0, 0);
|
versionCheckRequest = new Download("http://" SERVER "/Startup.json");
|
||||||
|
|
||||||
if (authUser.UserID)
|
if (authUser.UserID)
|
||||||
{
|
{
|
||||||
ByteString idTempString = ByteString::Build(authUser.UserID);
|
versionCheckRequest->AuthHeaders(ByteString::Build(authUser.UserID), authUser.SessionID);
|
||||||
char *id = new char[idTempString.length() + 1];
|
|
||||||
std::strcpy (id, idTempString.c_str());
|
|
||||||
char *session = new char[authUser.SessionID.length() + 1];
|
|
||||||
std::strcpy (session, authUser.SessionID.c_str());
|
|
||||||
http_auth_headers(versionCheckRequest, id, NULL, session);
|
|
||||||
delete[] id;
|
|
||||||
delete[] session;
|
|
||||||
}
|
}
|
||||||
|
versionCheckRequest->Start();
|
||||||
|
|
||||||
#ifdef UPDATESERVER
|
#ifdef UPDATESERVER
|
||||||
// use an alternate update server
|
// use an alternate update server
|
||||||
alternateVersionCheckRequest = http_async_req_start(NULL, "http://" UPDATESERVER "/Startup.json", NULL, 0, 0);
|
alternateVersionCheckRequest = new Download("http://" UPDATESERVER "/Startup.json");
|
||||||
usingAltUpdateServer = true;
|
usingAltUpdateServer = true;
|
||||||
if (authUser.UserID)
|
if (authUser.UserID)
|
||||||
{
|
{
|
||||||
http_auth_headers(alternateVersionCheckRequest, authUser.Username.c_str(), NULL, NULL);
|
alternateVersionCheckRequest->AuthHeaders(authUser.Username, "");
|
||||||
}
|
}
|
||||||
|
alternateVersionCheckRequest->Start();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -664,11 +660,11 @@ std::vector<std::pair<String, ByteString> > Client::GetServerNotifications()
|
|||||||
return serverNotifications;
|
return serverNotifications;
|
||||||
}
|
}
|
||||||
|
|
||||||
RequestStatus Client::ParseServerReturn(char *result, int status, bool json)
|
RequestStatus Client::ParseServerReturn(ByteString &result, int status, bool json)
|
||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
// no server response, return "Malformed Response"
|
// no server response, return "Malformed Response"
|
||||||
if (status == 200 && !result)
|
if (status == 200 && !result.size())
|
||||||
{
|
{
|
||||||
status = 603;
|
status = 603;
|
||||||
}
|
}
|
||||||
@ -676,13 +672,13 @@ RequestStatus Client::ParseServerReturn(char *result, int status, bool json)
|
|||||||
return RequestOkay;
|
return RequestOkay;
|
||||||
if (status != 200)
|
if (status != 200)
|
||||||
{
|
{
|
||||||
lastError = String::Build("HTTP Error ", status, ": ", ByteString(http_ret_text(status)).FromUtf8());
|
lastError = String::Build("HTTP Error ", status, ": ", ByteString(Download::StatusText(status)).FromUtf8());
|
||||||
return RequestFailure;
|
return RequestFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json)
|
if (json)
|
||||||
{
|
{
|
||||||
std::istringstream datastream(result);
|
std::istringstream datastream(result.c_str());
|
||||||
Json::Value root;
|
Json::Value root;
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -703,10 +699,10 @@ RequestStatus Client::ParseServerReturn(char *result, int status, bool json)
|
|||||||
catch (std::exception &e)
|
catch (std::exception &e)
|
||||||
{
|
{
|
||||||
// sometimes the server returns a 200 with the text "Error: 401"
|
// sometimes the server returns a 200 with the text "Error: 401"
|
||||||
if (!strncmp((const char *)result, "Error: ", 7))
|
if (!strncmp(result.c_str(), "Error: ", 7))
|
||||||
{
|
{
|
||||||
status = atoi(result+7);
|
status = ByteString(result.begin() + 7, result.end()).ToNumber<int>();
|
||||||
lastError = String::Build("HTTP Error ", status, ": ", ByteString(http_ret_text(status)).FromUtf8());
|
lastError = String::Build("HTTP Error ", status, ": ", ByteString(Download::StatusText(status)).FromUtf8());
|
||||||
return RequestFailure;
|
return RequestFailure;
|
||||||
}
|
}
|
||||||
lastError = "Could not read response: " + ByteString(e.what()).FromUtf8();
|
lastError = "Could not read response: " + ByteString(e.what()).FromUtf8();
|
||||||
@ -715,9 +711,9 @@ RequestStatus Client::ParseServerReturn(char *result, int status, bool json)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (strncmp((const char *)result, "OK", 2))
|
if (strncmp(result.c_str(), "OK", 2))
|
||||||
{
|
{
|
||||||
lastError = ByteString(result).FromUtf8();
|
lastError = result.FromUtf8();
|
||||||
return RequestFailure;
|
return RequestFailure;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -731,31 +727,31 @@ void Client::Tick()
|
|||||||
if (versionCheckRequest)
|
if (versionCheckRequest)
|
||||||
{
|
{
|
||||||
if (CheckUpdate(versionCheckRequest, true))
|
if (CheckUpdate(versionCheckRequest, true))
|
||||||
versionCheckRequest = NULL;
|
versionCheckRequest = nullptr;
|
||||||
}
|
}
|
||||||
if (alternateVersionCheckRequest)
|
if (alternateVersionCheckRequest)
|
||||||
{
|
{
|
||||||
if (CheckUpdate(alternateVersionCheckRequest, false))
|
if (CheckUpdate(alternateVersionCheckRequest, false))
|
||||||
alternateVersionCheckRequest = NULL;
|
alternateVersionCheckRequest = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Client::CheckUpdate(void *updateRequest, bool checkSession)
|
bool Client::CheckUpdate(Download *updateRequest, bool checkSession)
|
||||||
{
|
{
|
||||||
//Check status on version check request
|
//Check status on version check request
|
||||||
if (http_async_req_status(updateRequest))
|
if (updateRequest->CheckDone())
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
int dataLength;
|
int dataLength;
|
||||||
char * data = http_async_req_stop(updateRequest, &status, &dataLength);
|
ByteString data = updateRequest->Finish(&dataLength, &status);
|
||||||
|
|
||||||
if (status != 200)
|
if (status != 200)
|
||||||
{
|
{
|
||||||
free(data);
|
//free(data);
|
||||||
}
|
}
|
||||||
else if(data)
|
else if(data.size())
|
||||||
{
|
{
|
||||||
std::istringstream dataStream(data);
|
std::istringstream dataStream(data.c_str());
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -844,8 +840,6 @@ bool Client::CheckUpdate(void *updateRequest, bool checkSession)
|
|||||||
{
|
{
|
||||||
//Do nothing
|
//Do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
free(data);
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -967,7 +961,7 @@ RequestStatus Client::UploadSave(SaveInfo & save)
|
|||||||
unsigned int gameDataLength;
|
unsigned int gameDataLength;
|
||||||
char * gameData = NULL;
|
char * gameData = NULL;
|
||||||
int dataStatus;
|
int dataStatus;
|
||||||
char * data;
|
ByteString data;
|
||||||
int dataLength = 0;
|
int dataLength = 0;
|
||||||
ByteString userID = ByteString::Build(authUser.UserID);
|
ByteString userID = ByteString::Build(authUser.UserID);
|
||||||
if (authUser.UserID)
|
if (authUser.UserID)
|
||||||
@ -995,24 +989,12 @@ RequestStatus Client::UploadSave(SaveInfo & save)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char *saveName = new char[save.GetName().length() + 1];
|
data = Download::SimpleAuth("http://" SERVER "/Save.api", &dataLength, &dataStatus, userID, authUser.SessionID, {
|
||||||
std::strcpy (saveName, save.GetName().ToUtf8().c_str());
|
{ "Name", save.GetName().ToUtf8() },
|
||||||
char *saveDescription = new char[save.GetDescription().length() + 1];
|
{ "Description", save.GetDescription().ToUtf8() },
|
||||||
std::strcpy (saveDescription, save.GetDescription().ToUtf8().c_str());
|
{ "Data:save.bin", ByteString(gameData, gameData + gameDataLength) },
|
||||||
char *userid = new char[userID.size() + 1];
|
{ "Publish", save.GetPublished() ? "Public" : "Private" },
|
||||||
std::strcpy (userid, userID.c_str());
|
});
|
||||||
char *session = new char[authUser.SessionID.length() + 1];
|
|
||||||
std::strcpy (session, authUser.SessionID.c_str());
|
|
||||||
|
|
||||||
const char *const postNames[] = { "Name", "Description", "Data:save.bin", "Publish", NULL };
|
|
||||||
const char *const postDatas[] = { saveName, saveDescription, gameData, save.GetPublished()?"Public":"Private" };
|
|
||||||
size_t postLengths[] = { save.GetName().length(), save.GetDescription().length(), gameDataLength, (size_t)(save.GetPublished()?6:7) };
|
|
||||||
data = http_multipart_post("http://" SERVER "/Save.api", postNames, postDatas, postLengths, userid, NULL, session, &dataStatus, &dataLength);
|
|
||||||
|
|
||||||
delete[] saveDescription;
|
|
||||||
delete[] saveName;
|
|
||||||
delete[] userid;
|
|
||||||
delete[] session;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1023,7 +1005,7 @@ RequestStatus Client::UploadSave(SaveInfo & save)
|
|||||||
RequestStatus ret = ParseServerReturn(data, dataStatus, false);
|
RequestStatus ret = ParseServerReturn(data, dataStatus, false);
|
||||||
if (ret == RequestOkay)
|
if (ret == RequestOkay)
|
||||||
{
|
{
|
||||||
int saveID = ByteString(data+3).ToNumber<int>();
|
int saveID = ByteString(data.begin() + 3, data.end()).ToNumber<int>();
|
||||||
if (!saveID)
|
if (!saveID)
|
||||||
{
|
{
|
||||||
lastError = "Server did not return Save ID";
|
lastError = "Server did not return Save ID";
|
||||||
@ -1032,7 +1014,6 @@ RequestStatus Client::UploadSave(SaveInfo & save)
|
|||||||
else
|
else
|
||||||
save.SetID(saveID);
|
save.SetID(saveID);
|
||||||
}
|
}
|
||||||
free(data);
|
|
||||||
delete[] gameData;
|
delete[] gameData;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -1198,30 +1179,17 @@ RequestStatus Client::ExecVote(int saveID, int direction)
|
|||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
int dataStatus;
|
int dataStatus;
|
||||||
char * data;
|
ByteString data;
|
||||||
int dataLength = 0;
|
int dataLength = 0;
|
||||||
|
|
||||||
if (authUser.UserID)
|
if (authUser.UserID)
|
||||||
{
|
{
|
||||||
char * directionText = (char*)(direction==1?"Up":"Down");
|
|
||||||
ByteString saveIDText = ByteString::Build(saveID);
|
ByteString saveIDText = ByteString::Build(saveID);
|
||||||
ByteString userIDText = ByteString::Build(authUser.UserID);
|
ByteString userIDText = ByteString::Build(authUser.UserID);
|
||||||
|
data = Download::SimpleAuth("http://" SERVER "/Vote.api", &dataLength, &dataStatus, userIDText, authUser.SessionID, {
|
||||||
char *id = new char[saveIDText.length() + 1];
|
{ "ID", saveIDText },
|
||||||
std::strcpy(id, saveIDText.c_str());
|
{ "Action", direction == 1 ? "Up" : "Down" },
|
||||||
char *userid = new char[userIDText.length() + 1];
|
});
|
||||||
std::strcpy(userid, userIDText.c_str());
|
|
||||||
char *session = new char[authUser.SessionID.length() + 1];
|
|
||||||
std::strcpy(session, authUser.SessionID.c_str());
|
|
||||||
|
|
||||||
const char *const postNames[] = { "ID", "Action", NULL };
|
|
||||||
const char *const postDatas[] = { id, directionText };
|
|
||||||
size_t postLengths[] = { saveIDText.length(), strlen(directionText) };
|
|
||||||
data = http_multipart_post("http://" SERVER "/Vote.api", postNames, postDatas, postLengths, userid, NULL, session, &dataStatus, &dataLength);
|
|
||||||
|
|
||||||
delete[] id;
|
|
||||||
delete[] userid;
|
|
||||||
delete[] session;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1236,7 +1204,7 @@ unsigned char * Client::GetSaveData(int saveID, int saveDate, int & dataLength)
|
|||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
int dataStatus;
|
int dataStatus;
|
||||||
char *data;
|
ByteString data;
|
||||||
dataLength = 0;
|
dataLength = 0;
|
||||||
ByteString urlStr;
|
ByteString urlStr;
|
||||||
if (saveDate)
|
if (saveDate)
|
||||||
@ -1244,16 +1212,16 @@ unsigned char * Client::GetSaveData(int saveID, int saveDate, int & dataLength)
|
|||||||
else
|
else
|
||||||
urlStr = ByteString::Build("http://", STATICSERVER, "/", saveID, ".cps");
|
urlStr = ByteString::Build("http://", STATICSERVER, "/", saveID, ".cps");
|
||||||
|
|
||||||
char *url = new char[urlStr.size() + 1];
|
data = Download::Simple(urlStr, &dataLength, &dataStatus);
|
||||||
std::strcpy(url, urlStr.c_str());
|
|
||||||
data = http_simple_get(url, &dataStatus, &dataLength);
|
|
||||||
delete[] url;
|
|
||||||
|
|
||||||
// will always return failure
|
// will always return failure
|
||||||
ParseServerReturn(data, dataStatus, false);
|
ParseServerReturn(data, dataStatus, false);
|
||||||
if (data && dataStatus == 200)
|
if (data.size() && dataStatus == 200)
|
||||||
return (unsigned char *)data;
|
{
|
||||||
free(data);
|
unsigned char *data_out = (unsigned char *)malloc(dataLength);
|
||||||
|
std::copy(data_out, data_out + dataLength, data.begin());
|
||||||
|
return data_out;
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1367,21 +1335,21 @@ LoginStatus Client::Login(ByteString username, ByteString password, User & user)
|
|||||||
md5_ascii(totalHash, (const unsigned char *)(total.c_str()), total.size());
|
md5_ascii(totalHash, (const unsigned char *)(total.c_str()), total.size());
|
||||||
totalHash[32] = 0;
|
totalHash[32] = 0;
|
||||||
|
|
||||||
char * data;
|
ByteString data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
const char *const postNames[] = { "Username", "Hash", NULL };
|
data = Download::Simple("http://" SERVER "/Login.json", &dataLength, &dataStatus, {
|
||||||
const char *const postDatas[] = { username.c_str(), totalHash };
|
{ "Username", username },
|
||||||
size_t postLengths[] = { username.length(), 32 };
|
{ "Hash", totalHash },
|
||||||
data = http_multipart_post("http://" SERVER "/Login.json", postNames, postDatas, postLengths, NULL, NULL, NULL, &dataStatus, &dataLength);
|
});
|
||||||
|
|
||||||
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
||||||
if (ret == RequestOkay)
|
if (ret == RequestOkay)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::istringstream dataStream(data);
|
std::istringstream dataStream(data.c_str());
|
||||||
Json::Value objDocument;
|
Json::Value objDocument;
|
||||||
dataStream >> objDocument;
|
dataStream >> objDocument;
|
||||||
free(data);
|
|
||||||
|
|
||||||
int userIDTemp = objDocument["UserID"].asInt();
|
int userIDTemp = objDocument["UserID"].asInt();
|
||||||
ByteString sessionIDTemp = objDocument["SessionID"].asString();
|
ByteString sessionIDTemp = objDocument["SessionID"].asString();
|
||||||
@ -1417,20 +1385,19 @@ LoginStatus Client::Login(ByteString username, ByteString password, User & user)
|
|||||||
return LoginError;
|
return LoginError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(data);
|
|
||||||
return LoginError;
|
return LoginError;
|
||||||
}
|
}
|
||||||
|
|
||||||
RequestStatus Client::DeleteSave(int saveID)
|
RequestStatus Client::DeleteSave(int saveID)
|
||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
char * data = NULL;
|
ByteString data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
ByteString url = ByteString::Build("http://", SERVER, "/Browse/Delete.json?ID=", saveID, "&Mode=Delete&Key=", authUser.SessionKey);
|
ByteString url = ByteString::Build("http://", SERVER, "/Browse/Delete.json?ID=", saveID, "&Mode=Delete&Key=", authUser.SessionKey);
|
||||||
if(authUser.UserID)
|
if(authUser.UserID)
|
||||||
{
|
{
|
||||||
ByteString userID = ByteString::Build(authUser.UserID);
|
ByteString userID = ByteString::Build(authUser.UserID);
|
||||||
data = http_auth_get(url.c_str(), userID.c_str(), NULL, authUser.SessionID.c_str(), &dataStatus, &dataLength);
|
data = Download::SimpleAuth(url, &dataLength, &dataStatus, userID, authUser.SessionID);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1438,25 +1405,21 @@ RequestStatus Client::DeleteSave(int saveID)
|
|||||||
return RequestFailure;
|
return RequestFailure;
|
||||||
}
|
}
|
||||||
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
||||||
free(data);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
RequestStatus Client::AddComment(int saveID, String comment)
|
RequestStatus Client::AddComment(int saveID, String comment)
|
||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
char * data = NULL;
|
ByteString data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
ByteString url = ByteString::Build("http://", SERVER, "/Browse/Comments.json?ID=", saveID);
|
ByteString url = ByteString::Build("http://", SERVER, "/Browse/Comments.json?ID=", saveID);
|
||||||
if(authUser.UserID)
|
if(authUser.UserID)
|
||||||
{
|
{
|
||||||
ByteString userID = ByteString::Build(authUser.UserID);
|
ByteString userID = ByteString::Build(authUser.UserID);
|
||||||
|
data = Download::SimpleAuth(url, &dataLength, &dataStatus, userID, authUser.SessionID, {
|
||||||
const char *const postNames[] = { "Comment", NULL };
|
{ "Comment", comment.ToUtf8() },
|
||||||
ByteString commentBytes = comment.ToUtf8();
|
});
|
||||||
const char *const postDatas[] = { commentBytes.c_str() };
|
|
||||||
size_t postLengths[] = { commentBytes.size() };
|
|
||||||
data = http_multipart_post(url.c_str(), postNames, postDatas, postLengths, userID.c_str(), NULL, authUser.SessionID.c_str(), &dataStatus, &dataLength);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1464,7 +1427,6 @@ RequestStatus Client::AddComment(int saveID, String comment)
|
|||||||
return RequestFailure;
|
return RequestFailure;
|
||||||
}
|
}
|
||||||
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
||||||
free(data);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1472,7 +1434,7 @@ RequestStatus Client::FavouriteSave(int saveID, bool favourite)
|
|||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
ByteStringBuilder urlStream;
|
ByteStringBuilder urlStream;
|
||||||
char * data = NULL;
|
ByteString data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
urlStream << "http://" << SERVER << "/Browse/Favourite.json?ID=" << saveID << "&Key=" << authUser.SessionKey;
|
urlStream << "http://" << SERVER << "/Browse/Favourite.json?ID=" << saveID << "&Key=" << authUser.SessionKey;
|
||||||
if(!favourite)
|
if(!favourite)
|
||||||
@ -1480,7 +1442,7 @@ RequestStatus Client::FavouriteSave(int saveID, bool favourite)
|
|||||||
if(authUser.UserID)
|
if(authUser.UserID)
|
||||||
{
|
{
|
||||||
ByteString userID = ByteString::Build(authUser.UserID);
|
ByteString userID = ByteString::Build(authUser.UserID);
|
||||||
data = http_auth_get(urlStream.Build().c_str(), userID.c_str(), NULL, authUser.SessionID.c_str(), &dataStatus, &dataLength);
|
data = Download::SimpleAuth(urlStream.Build(), &dataLength, &dataStatus, userID, authUser.SessionID);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1488,25 +1450,21 @@ RequestStatus Client::FavouriteSave(int saveID, bool favourite)
|
|||||||
return RequestFailure;
|
return RequestFailure;
|
||||||
}
|
}
|
||||||
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
||||||
free(data);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
RequestStatus Client::ReportSave(int saveID, String message)
|
RequestStatus Client::ReportSave(int saveID, String message)
|
||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
char * data = NULL;
|
ByteString data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
ByteString url = ByteString::Build("http://", SERVER, "/Browse/Report.json?ID=", saveID, "&Key=", authUser.SessionKey);
|
ByteString url = ByteString::Build("http://", SERVER, "/Browse/Report.json?ID=", saveID, "&Key=", authUser.SessionKey);
|
||||||
if(authUser.UserID)
|
if(authUser.UserID)
|
||||||
{
|
{
|
||||||
ByteString userID = ByteString::Build(authUser.UserID);
|
ByteString userID = ByteString::Build(authUser.UserID);
|
||||||
|
data = Download::SimpleAuth(url, &dataLength, &dataStatus, userID, authUser.SessionID, {
|
||||||
const char *const postNames[] = { "Reason", NULL };
|
{ "Reason", message.ToUtf8() },
|
||||||
ByteString messageBytes = message.ToUtf8();
|
});
|
||||||
const char *const postDatas[] = { messageBytes.c_str() };
|
|
||||||
size_t postLengths[] = { messageBytes.size() };
|
|
||||||
data = http_multipart_post(url.c_str(), postNames, postDatas, postLengths, userID.c_str(), NULL, authUser.SessionID.c_str(), &dataStatus, &dataLength);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1514,21 +1472,19 @@ RequestStatus Client::ReportSave(int saveID, String message)
|
|||||||
return RequestFailure;
|
return RequestFailure;
|
||||||
}
|
}
|
||||||
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
||||||
free(data);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
RequestStatus Client::UnpublishSave(int saveID)
|
RequestStatus Client::UnpublishSave(int saveID)
|
||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
char * data = NULL;
|
ByteString data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
ByteString url = ByteString::Build("http://", SERVER, "/Browse/Delete.json?ID=", saveID, "&Mode=Unpublish&Key=", authUser.SessionKey);
|
ByteString url = ByteString::Build("http://", SERVER, "/Browse/Delete.json?ID=", saveID, "&Mode=Unpublish&Key=", authUser.SessionKey);
|
||||||
if(authUser.UserID)
|
if(authUser.UserID)
|
||||||
{
|
{
|
||||||
ByteString userID = ByteString::Build(authUser.UserID);
|
ByteString userID = ByteString::Build(authUser.UserID);
|
||||||
|
data = Download::SimpleAuth(url, &dataLength, &dataStatus, userID, authUser.SessionID);
|
||||||
data = http_auth_get(url.c_str(), userID.c_str(), NULL, authUser.SessionID.c_str(), &dataStatus, &dataLength);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1536,31 +1492,28 @@ RequestStatus Client::UnpublishSave(int saveID)
|
|||||||
return RequestFailure;
|
return RequestFailure;
|
||||||
}
|
}
|
||||||
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
||||||
free(data);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
RequestStatus Client::PublishSave(int saveID)
|
RequestStatus Client::PublishSave(int saveID)
|
||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
char *data;
|
ByteString data;
|
||||||
int dataStatus;
|
int dataStatus;
|
||||||
ByteString url = ByteString::Build("http://", SERVER, "/Browse/View.json?ID=", saveID, "&Key=", authUser.SessionKey);
|
ByteString url = ByteString::Build("http://", SERVER, "/Browse/View.json?ID=", saveID, "&Key=", authUser.SessionKey);
|
||||||
if (authUser.UserID)
|
if (authUser.UserID)
|
||||||
{
|
{
|
||||||
ByteString userID = ByteString::Build(authUser.UserID);
|
ByteString userID = ByteString::Build(authUser.UserID);
|
||||||
|
data = Download::SimpleAuth(url, nullptr, &dataStatus, userID, authUser.SessionID, {
|
||||||
const char *const postNames[] = { "ActionPublish", NULL };
|
{ "ActionPublish", "bagels" },
|
||||||
const char *const postDatas[] = { "" };
|
});
|
||||||
size_t postLengths[] = { 1 };
|
}
|
||||||
data = http_multipart_post(url.c_str(), postNames, postDatas, postLengths, userID.c_str(), NULL, authUser.SessionID.c_str(), &dataStatus, NULL); }
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lastError = "Not authenticated";
|
lastError = "Not authenticated";
|
||||||
return RequestFailure;
|
return RequestFailure;
|
||||||
}
|
}
|
||||||
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
RequestStatus ret = ParseServerReturn(data, dataStatus, true);
|
||||||
free(data);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1573,23 +1526,23 @@ SaveInfo * Client::GetSave(int saveID, int saveDate)
|
|||||||
{
|
{
|
||||||
urlStream << "&Date=" << saveDate;
|
urlStream << "&Date=" << saveDate;
|
||||||
}
|
}
|
||||||
char * data;
|
ByteString data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
if(authUser.UserID)
|
if(authUser.UserID)
|
||||||
{
|
{
|
||||||
ByteString userID = ByteString::Build(authUser.UserID);
|
ByteString userID = ByteString::Build(authUser.UserID);
|
||||||
|
|
||||||
data = http_auth_get(urlStream.Build().c_str(), userID.c_str(), NULL, authUser.SessionID.c_str(), &dataStatus, &dataLength);
|
data = Download::SimpleAuth(urlStream.Build(), &dataLength, &dataStatus, userID, authUser.SessionID);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
data = http_simple_get(urlStream.Build().c_str(), &dataStatus, &dataLength);
|
data = Download::Simple(urlStream.Build(), &dataLength, &dataStatus);
|
||||||
}
|
}
|
||||||
if(dataStatus == 200 && data)
|
if(dataStatus == 200 && data.size())
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::istringstream dataStream(data);
|
std::istringstream dataStream(data.c_str());
|
||||||
Json::Value objDocument;
|
Json::Value objDocument;
|
||||||
dataStream >> objDocument;
|
dataStream >> objDocument;
|
||||||
|
|
||||||
@ -1620,20 +1573,17 @@ SaveInfo * Client::GetSave(int saveID, int saveDate)
|
|||||||
tempSave->Favourite = tempFavourite;
|
tempSave->Favourite = tempFavourite;
|
||||||
tempSave->Views = tempViews;
|
tempSave->Views = tempViews;
|
||||||
tempSave->Version = tempVersion;
|
tempSave->Version = tempVersion;
|
||||||
free(data);
|
|
||||||
return tempSave;
|
return tempSave;
|
||||||
}
|
}
|
||||||
catch (std::exception & e)
|
catch (std::exception & e)
|
||||||
{
|
{
|
||||||
lastError = "Could not read response: " + ByteString(e.what()).FromUtf8();
|
lastError = "Could not read response: " + ByteString(e.what()).FromUtf8();
|
||||||
free(data);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
free(data);
|
lastError = ByteString(Download::StatusText(dataStatus)).FromUtf8();
|
||||||
lastError = ByteString(http_ret_text(dataStatus)).FromUtf8();
|
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -1748,7 +1698,7 @@ std::vector<std::pair<ByteString, int> > * Client::GetTags(int start, int count,
|
|||||||
resultCount = 0;
|
resultCount = 0;
|
||||||
std::vector<std::pair<ByteString, int> > * tagArray = new std::vector<std::pair<ByteString, int> >();
|
std::vector<std::pair<ByteString, int> > * tagArray = new std::vector<std::pair<ByteString, int> >();
|
||||||
ByteStringBuilder urlStream;
|
ByteStringBuilder urlStream;
|
||||||
char * data;
|
ByteString data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
urlStream << "http://" << SERVER << "/Browse/Tags.json?Start=" << start << "&Count=" << count;
|
urlStream << "http://" << SERVER << "/Browse/Tags.json?Start=" << start << "&Count=" << count;
|
||||||
if(query.length())
|
if(query.length())
|
||||||
@ -1758,12 +1708,12 @@ std::vector<std::pair<ByteString, int> > * Client::GetTags(int start, int count,
|
|||||||
urlStream << format::URLEncode(query.ToUtf8());
|
urlStream << format::URLEncode(query.ToUtf8());
|
||||||
}
|
}
|
||||||
|
|
||||||
data = http_simple_get(urlStream.Build().c_str(), &dataStatus, &dataLength);
|
data = Download::Simple(urlStream.Build(), &dataLength, &dataStatus);
|
||||||
if(dataStatus == 200 && data)
|
if(dataStatus == 200 && data.size())
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::istringstream dataStream(data);
|
std::istringstream dataStream(data.c_str());
|
||||||
Json::Value objDocument;
|
Json::Value objDocument;
|
||||||
dataStream >> objDocument;
|
dataStream >> objDocument;
|
||||||
|
|
||||||
@ -1783,9 +1733,8 @@ std::vector<std::pair<ByteString, int> > * Client::GetTags(int start, int count,
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lastError = ByteString(http_ret_text(dataStatus)).FromUtf8();
|
lastError = ByteString(Download::StatusText(dataStatus)).FromUtf8();
|
||||||
}
|
}
|
||||||
free(data);
|
|
||||||
return tagArray;
|
return tagArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1795,7 +1744,7 @@ std::vector<SaveInfo*> * Client::SearchSaves(int start, int count, String query,
|
|||||||
resultCount = 0;
|
resultCount = 0;
|
||||||
std::vector<SaveInfo*> * saveArray = new std::vector<SaveInfo*>();
|
std::vector<SaveInfo*> * saveArray = new std::vector<SaveInfo*>();
|
||||||
ByteStringBuilder urlStream;
|
ByteStringBuilder urlStream;
|
||||||
char * data;
|
ByteString data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
urlStream << "http://" << SERVER << "/Browse.json?Start=" << start << "&Count=" << count;
|
urlStream << "http://" << SERVER << "/Browse.json?Start=" << start << "&Count=" << count;
|
||||||
if(query.length() || sort.length())
|
if(query.length() || sort.length())
|
||||||
@ -1817,18 +1766,18 @@ std::vector<SaveInfo*> * Client::SearchSaves(int start, int count, String query,
|
|||||||
if(authUser.UserID)
|
if(authUser.UserID)
|
||||||
{
|
{
|
||||||
ByteString userID = ByteString::Build(authUser.UserID);
|
ByteString userID = ByteString::Build(authUser.UserID);
|
||||||
data = http_auth_get(urlStream.Build().c_str(), userID.c_str(), NULL, authUser.SessionID.c_str(), &dataStatus, &dataLength);
|
data = Download::SimpleAuth(urlStream.Build(), &dataLength, &dataStatus, userID, authUser.SessionID);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
data = http_simple_get(urlStream.Build().c_str(), &dataStatus, &dataLength);
|
data = Download::Simple(urlStream.Build(), &dataLength, &dataStatus);
|
||||||
}
|
}
|
||||||
ParseServerReturn(data, dataStatus, true);
|
ParseServerReturn(data, dataStatus, true);
|
||||||
if (dataStatus == 200 && data)
|
if (dataStatus == 200 && data.size())
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::istringstream dataStream(data);
|
std::istringstream dataStream(data.c_str());
|
||||||
Json::Value objDocument;
|
Json::Value objDocument;
|
||||||
dataStream >> objDocument;
|
dataStream >> objDocument;
|
||||||
|
|
||||||
@ -1856,7 +1805,6 @@ std::vector<SaveInfo*> * Client::SearchSaves(int start, int count, String query,
|
|||||||
lastError = "Could not read response: " + ByteString(e.what()).FromUtf8();
|
lastError = "Could not read response: " + ByteString(e.what()).FromUtf8();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(data);
|
|
||||||
return saveArray;
|
return saveArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1864,13 +1812,13 @@ std::list<ByteString> * Client::RemoveTag(int saveID, ByteString tag)
|
|||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
std::list<ByteString> * tags = NULL;
|
std::list<ByteString> * tags = NULL;
|
||||||
char * data = NULL;
|
ByteString data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
ByteString url = ByteString::Build("http://", SERVER, "/Browse/EditTag.json?Op=delete&ID=", saveID, "&Tag=", tag, "&Key=", authUser.SessionKey);
|
ByteString url = ByteString::Build("http://", SERVER, "/Browse/EditTag.json?Op=delete&ID=", saveID, "&Tag=", tag, "&Key=", authUser.SessionKey);
|
||||||
if(authUser.UserID)
|
if(authUser.UserID)
|
||||||
{
|
{
|
||||||
ByteString userID = ByteString::Build(authUser.UserID);
|
ByteString userID = ByteString::Build(authUser.UserID);
|
||||||
data = http_auth_get(url.c_str(), userID.c_str(), NULL, authUser.SessionID.c_str(), &dataStatus, &dataLength);
|
data = Download::SimpleAuth(url, &dataLength, &dataStatus, userID, authUser.SessionID);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1882,7 +1830,7 @@ std::list<ByteString> * Client::RemoveTag(int saveID, ByteString tag)
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::istringstream dataStream(data);
|
std::istringstream dataStream(data.c_str());
|
||||||
Json::Value responseObject;
|
Json::Value responseObject;
|
||||||
dataStream >> responseObject;
|
dataStream >> responseObject;
|
||||||
|
|
||||||
@ -1896,7 +1844,6 @@ std::list<ByteString> * Client::RemoveTag(int saveID, ByteString tag)
|
|||||||
lastError = "Could not read response: " + ByteString(e.what()).FromUtf8();
|
lastError = "Could not read response: " + ByteString(e.what()).FromUtf8();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(data);
|
|
||||||
return tags;
|
return tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1904,13 +1851,13 @@ std::list<ByteString> * Client::AddTag(int saveID, ByteString tag)
|
|||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
std::list<ByteString> * tags = NULL;
|
std::list<ByteString> * tags = NULL;
|
||||||
char * data = NULL;
|
ByteString data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
ByteString url = ByteString::Build("http://", SERVER, "/Browse/EditTag.json?Op=add&ID=", saveID, "&Tag=", tag, "&Key=", authUser.SessionKey);
|
ByteString url = ByteString::Build("http://", SERVER, "/Browse/EditTag.json?Op=add&ID=", saveID, "&Tag=", tag, "&Key=", authUser.SessionKey);
|
||||||
if(authUser.UserID)
|
if(authUser.UserID)
|
||||||
{
|
{
|
||||||
ByteString userID = ByteString::Build(authUser.UserID);
|
ByteString userID = ByteString::Build(authUser.UserID);
|
||||||
data = http_auth_get(url.c_str(), userID.c_str(), NULL, authUser.SessionID.c_str(), &dataStatus, &dataLength);
|
data = Download::SimpleAuth(url, &dataLength, &dataStatus, userID, authUser.SessionID);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1922,7 +1869,7 @@ std::list<ByteString> * Client::AddTag(int saveID, ByteString tag)
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::istringstream dataStream(data);
|
std::istringstream dataStream(data.c_str());
|
||||||
Json::Value responseObject;
|
Json::Value responseObject;
|
||||||
dataStream >> responseObject;
|
dataStream >> responseObject;
|
||||||
|
|
||||||
@ -1936,7 +1883,6 @@ std::list<ByteString> * Client::AddTag(int saveID, ByteString tag)
|
|||||||
lastError = "Could not read response: " + ByteString(e.what()).FromUtf8();
|
lastError = "Could not read response: " + ByteString(e.what()).FromUtf8();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(data);
|
|
||||||
return tags;
|
return tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,13 +48,14 @@ public:
|
|||||||
|
|
||||||
class RequestListener;
|
class RequestListener;
|
||||||
class ClientListener;
|
class ClientListener;
|
||||||
|
class Download;
|
||||||
class Client: public Singleton<Client> {
|
class Client: public Singleton<Client> {
|
||||||
private:
|
private:
|
||||||
String messageOfTheDay;
|
String messageOfTheDay;
|
||||||
std::vector<std::pair<String, ByteString> > serverNotifications;
|
std::vector<std::pair<String, ByteString> > serverNotifications;
|
||||||
|
|
||||||
void * versionCheckRequest;
|
Download *versionCheckRequest;
|
||||||
void * alternateVersionCheckRequest;
|
Download *alternateVersionCheckRequest;
|
||||||
bool usingAltUpdateServer;
|
bool usingAltUpdateServer;
|
||||||
bool updateAvailable;
|
bool updateAvailable;
|
||||||
UpdateInfo updateInfo;
|
UpdateInfo updateInfo;
|
||||||
@ -172,9 +173,9 @@ public:
|
|||||||
String GetLastError() {
|
String GetLastError() {
|
||||||
return lastError;
|
return lastError;
|
||||||
}
|
}
|
||||||
RequestStatus ParseServerReturn(char *result, int status, bool json);
|
RequestStatus ParseServerReturn(ByteString &result, int status, bool json);
|
||||||
void Tick();
|
void Tick();
|
||||||
bool CheckUpdate(void *updateRequest, bool checkSession);
|
bool CheckUpdate(Download *updateRequest, bool checkSession);
|
||||||
void Shutdown();
|
void Shutdown();
|
||||||
|
|
||||||
// preferences functions
|
// preferences functions
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include "Download.h"
|
#include "Download.h"
|
||||||
#include "DownloadManager.h"
|
#include "DownloadManager.h"
|
||||||
#include "HTTP.h"
|
#include "HTTP.h"
|
||||||
|
#include "Platform.h"
|
||||||
|
|
||||||
Download::Download(ByteString uri_, bool keepAlive):
|
Download::Download(ByteString uri_, bool keepAlive):
|
||||||
http(NULL),
|
http(NULL),
|
||||||
@ -84,10 +85,10 @@ bool Download::Reuse(ByteString newuri)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// finish the download (if called before the download is done, this will block)
|
// finish the download (if called before the download is done, this will block)
|
||||||
char* Download::Finish(int *length, int *status)
|
ByteString Download::Finish(int *length, int *status)
|
||||||
{
|
{
|
||||||
if (CheckCanceled())
|
if (CheckCanceled())
|
||||||
return NULL; // shouldn't happen but just in case
|
return ""; // shouldn't happen but just in case
|
||||||
while (!CheckDone()); // block
|
while (!CheckDone()); // block
|
||||||
DownloadManager::Ref().Lock();
|
DownloadManager::Ref().Lock();
|
||||||
downloadStarted = false;
|
downloadStarted = false;
|
||||||
@ -95,7 +96,12 @@ char* Download::Finish(int *length, int *status)
|
|||||||
*length = downloadSize;
|
*length = downloadSize;
|
||||||
if (status)
|
if (status)
|
||||||
*status = downloadStatus;
|
*status = downloadStatus;
|
||||||
char *ret = downloadData;
|
ByteString ret;
|
||||||
|
if (downloadData)
|
||||||
|
{
|
||||||
|
ret = ByteString(downloadData, downloadData + downloadSize);
|
||||||
|
free(downloadData);
|
||||||
|
}
|
||||||
downloadData = NULL;
|
downloadData = NULL;
|
||||||
if (!keepAlive)
|
if (!keepAlive)
|
||||||
downloadCanceled = true;
|
downloadCanceled = true;
|
||||||
@ -149,3 +155,34 @@ void Download::Cancel()
|
|||||||
downloadCanceled = true;
|
downloadCanceled = true;
|
||||||
DownloadManager::Ref().Unlock();
|
DownloadManager::Ref().Unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ByteString Download::Simple(ByteString uri, int *length, int *status, std::map<ByteString, ByteString> post_data)
|
||||||
|
{
|
||||||
|
Download *request = new Download(uri);
|
||||||
|
request->AddPostData(post_data);
|
||||||
|
request->Start();
|
||||||
|
while(!request->CheckDone())
|
||||||
|
{
|
||||||
|
Platform::Millisleep(1);
|
||||||
|
}
|
||||||
|
return request->Finish(length, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteString Download::SimpleAuth(ByteString uri, int *length, int *status, ByteString ID, ByteString session, std::map<ByteString, ByteString> post_data)
|
||||||
|
{
|
||||||
|
Download *request = new Download(uri);
|
||||||
|
request->AddPostData(post_data);
|
||||||
|
request->AuthHeaders(ID, session);
|
||||||
|
request->Start();
|
||||||
|
while(!request->CheckDone())
|
||||||
|
{
|
||||||
|
Platform::Millisleep(1);
|
||||||
|
}
|
||||||
|
return request->Finish(length, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *Download::StatusText(int code)
|
||||||
|
{
|
||||||
|
return http_ret_text(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ public:
|
|||||||
void AuthHeaders(ByteString ID, ByteString session);
|
void AuthHeaders(ByteString ID, ByteString session);
|
||||||
void Start();
|
void Start();
|
||||||
bool Reuse(ByteString newuri);
|
bool Reuse(ByteString newuri);
|
||||||
char* Finish(int *length, int *status);
|
ByteString Finish(int *length, int *status);
|
||||||
void Cancel();
|
void Cancel();
|
||||||
|
|
||||||
void CheckProgress(int *total, int *done);
|
void CheckProgress(int *total, int *done);
|
||||||
@ -42,6 +42,11 @@ public:
|
|||||||
bool CheckStarted();
|
bool CheckStarted();
|
||||||
|
|
||||||
friend class DownloadManager;
|
friend class DownloadManager;
|
||||||
|
|
||||||
|
static ByteString Simple(ByteString uri, int *length, int *status, std::map<ByteString, ByteString> post_data = {});
|
||||||
|
static ByteString SimpleAuth(ByteString uri, int *length, int *status, ByteString ID, ByteString session, std::map<ByteString, ByteString> post_data = {});
|
||||||
|
|
||||||
|
static const char *StatusText(int code);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -38,7 +38,8 @@ RequestBroker::ProcessResponse APIRequest::Process(RequestBroker & rb)
|
|||||||
int status, data_size;
|
int status, data_size;
|
||||||
data = http_async_req_stop(HTTPContext, &status, &data_size);
|
data = http_async_req_stop(HTTPContext, &status, &data_size);
|
||||||
|
|
||||||
Client::Ref().ParseServerReturn(data, status, true);
|
ByteString nice_data(data);
|
||||||
|
Client::Ref().ParseServerReturn(nice_data, status, true);
|
||||||
if (status == 200 && data)
|
if (status == 200 && data)
|
||||||
{
|
{
|
||||||
void * resultObject = Parser->ProcessResponse((unsigned char *)data, data_size);
|
void * resultObject = Parser->ProcessResponse((unsigned char *)data, data_size);
|
||||||
|
@ -37,7 +37,8 @@ RequestBroker::ProcessResponse WebRequest::Process(RequestBroker & rb)
|
|||||||
int status, data_size;
|
int status, data_size;
|
||||||
data = http_async_req_stop(HTTPContext, &status, &data_size);
|
data = http_async_req_stop(HTTPContext, &status, &data_size);
|
||||||
|
|
||||||
Client::Ref().ParseServerReturn(NULL, status, true);
|
ByteString nothing;
|
||||||
|
Client::Ref().ParseServerReturn(nothing, status, true);
|
||||||
if (status == 200 && data)
|
if (status == 200 && data)
|
||||||
{
|
{
|
||||||
void * resultObject = new std::vector<unsigned char>(data, data+data_size);
|
void * resultObject = new std::vector<unsigned char>(data, data+data_size);
|
||||||
|
@ -193,13 +193,13 @@ void PreviewModel::ClearComments()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PreviewModel::ParseSaveInfo(char * saveInfoResponse)
|
bool PreviewModel::ParseSaveInfo(ByteString &saveInfoResponse)
|
||||||
{
|
{
|
||||||
delete saveInfo;
|
delete saveInfo;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::istringstream dataStream(saveInfoResponse);
|
std::istringstream dataStream(saveInfoResponse.c_str());
|
||||||
Json::Value objDocument;
|
Json::Value objDocument;
|
||||||
dataStream >> objDocument;
|
dataStream >> objDocument;
|
||||||
|
|
||||||
@ -251,13 +251,13 @@ bool PreviewModel::ParseSaveInfo(char * saveInfoResponse)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PreviewModel::ParseComments(char *commentsResponse)
|
bool PreviewModel::ParseComments(ByteString &commentsResponse)
|
||||||
{
|
{
|
||||||
ClearComments();
|
ClearComments();
|
||||||
saveComments = new std::vector<SaveComment*>();
|
saveComments = new std::vector<SaveComment*>();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::istringstream dataStream(commentsResponse);
|
std::istringstream dataStream(commentsResponse.c_str());
|
||||||
Json::Value commentsArray;
|
Json::Value commentsArray;
|
||||||
dataStream >> commentsArray;
|
dataStream >> commentsArray;
|
||||||
|
|
||||||
@ -284,13 +284,14 @@ void PreviewModel::Update()
|
|||||||
if (saveDataDownload && saveDataDownload->CheckDone())
|
if (saveDataDownload && saveDataDownload->CheckDone())
|
||||||
{
|
{
|
||||||
int status, length;
|
int status, length;
|
||||||
char *ret = saveDataDownload->Finish(&length, &status);
|
ByteString ret = saveDataDownload->Finish(&length, &status);
|
||||||
|
|
||||||
Client::Ref().ParseServerReturn(NULL, status, true);
|
ByteString nothing;
|
||||||
if (status == 200 && ret)
|
Client::Ref().ParseServerReturn(nothing, status, true);
|
||||||
|
if (status == 200 && ret.size())
|
||||||
{
|
{
|
||||||
delete saveData;
|
delete saveData;
|
||||||
saveData = new std::vector<unsigned char>(ret, ret+length);
|
saveData = new std::vector<unsigned char>(ret.begin(), ret.end());
|
||||||
if (saveInfo && saveData)
|
if (saveInfo && saveData)
|
||||||
OnSaveReady();
|
OnSaveReady();
|
||||||
}
|
}
|
||||||
@ -302,16 +303,16 @@ void PreviewModel::Update()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
saveDataDownload = NULL;
|
saveDataDownload = NULL;
|
||||||
free(ret);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (saveInfoDownload && saveInfoDownload->CheckDone())
|
if (saveInfoDownload && saveInfoDownload->CheckDone())
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
char *ret = saveInfoDownload->Finish(NULL, &status);
|
ByteString ret = saveInfoDownload->Finish(NULL, &status);
|
||||||
|
|
||||||
Client::Ref().ParseServerReturn(NULL, status, true);
|
ByteString nothing;
|
||||||
if (status == 200 && ret)
|
Client::Ref().ParseServerReturn(nothing, status, true);
|
||||||
|
if (status == 200 && ret.size())
|
||||||
{
|
{
|
||||||
if (ParseSaveInfo(ret))
|
if (ParseSaveInfo(ret))
|
||||||
{
|
{
|
||||||
@ -330,17 +331,17 @@ void PreviewModel::Update()
|
|||||||
observers[i]->SaveLoadingError(Client::Ref().GetLastError());
|
observers[i]->SaveLoadingError(Client::Ref().GetLastError());
|
||||||
}
|
}
|
||||||
saveInfoDownload = NULL;
|
saveInfoDownload = NULL;
|
||||||
free(ret);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (commentsDownload && commentsDownload->CheckDone())
|
if (commentsDownload && commentsDownload->CheckDone())
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
char *ret = commentsDownload->Finish(NULL, &status);
|
ByteString ret = commentsDownload->Finish(NULL, &status);
|
||||||
ClearComments();
|
ClearComments();
|
||||||
|
|
||||||
Client::Ref().ParseServerReturn(NULL, status, true);
|
ByteString nothing;
|
||||||
if (status == 200 && ret)
|
Client::Ref().ParseServerReturn(nothing, status, true);
|
||||||
|
if (status == 200 && ret.size())
|
||||||
ParseComments(ret);
|
ParseComments(ret);
|
||||||
|
|
||||||
commentsLoaded = true;
|
commentsLoaded = true;
|
||||||
@ -348,7 +349,6 @@ void PreviewModel::Update()
|
|||||||
notifyCommentsPageChanged();
|
notifyCommentsPageChanged();
|
||||||
|
|
||||||
commentsDownload = NULL;
|
commentsDownload = NULL;
|
||||||
free(ret);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,8 +57,8 @@ public:
|
|||||||
void Update();
|
void Update();
|
||||||
void ClearComments();
|
void ClearComments();
|
||||||
void OnSaveReady();
|
void OnSaveReady();
|
||||||
bool ParseSaveInfo(char * saveInfoResponse);
|
bool ParseSaveInfo(ByteString &saveInfoResponse);
|
||||||
bool ParseComments(char * commentsResponse);
|
bool ParseComments(ByteString &commentsResponse);
|
||||||
virtual ~PreviewModel();
|
virtual ~PreviewModel();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -3,9 +3,9 @@
|
|||||||
#include "gui/interface/Engine.h"
|
#include "gui/interface/Engine.h"
|
||||||
#include "UpdateActivity.h"
|
#include "UpdateActivity.h"
|
||||||
#include "tasks/Task.h"
|
#include "tasks/Task.h"
|
||||||
#include "client/HTTP.h"
|
|
||||||
#include "client/Client.h"
|
#include "client/Client.h"
|
||||||
#include "Update.h"
|
#include "Update.h"
|
||||||
|
#include "client/Download.h"
|
||||||
#include "Platform.h"
|
#include "Platform.h"
|
||||||
|
|
||||||
|
|
||||||
@ -26,27 +26,27 @@ private:
|
|||||||
virtual bool doWork()
|
virtual bool doWork()
|
||||||
{
|
{
|
||||||
String error;
|
String error;
|
||||||
void * request = http_async_req_start(NULL, (char*)updateName.c_str(), NULL, 0, 0);
|
Download *request = new Download(updateName);
|
||||||
|
request->Start();
|
||||||
notifyStatus("Downloading update");
|
notifyStatus("Downloading update");
|
||||||
notifyProgress(-1);
|
notifyProgress(-1);
|
||||||
while(!http_async_req_status(request))
|
while(!request->CheckDone())
|
||||||
{
|
{
|
||||||
int total, done;
|
int total, done;
|
||||||
http_async_get_length(request, &total, &done);
|
request->CheckProgress(&total, &done);
|
||||||
notifyProgress((float(done)/float(total))*100.0f);
|
notifyProgress((float(done)/float(total))*100.0f);
|
||||||
|
Platform::Millisleep(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
char * data;
|
|
||||||
int dataLength, status;
|
int dataLength, status;
|
||||||
data = http_async_req_stop(request, &status, &dataLength);
|
ByteString data = request->Finish(&dataLength, &status);
|
||||||
if (status!=200)
|
if (status!=200)
|
||||||
{
|
{
|
||||||
free(data);
|
|
||||||
error = String::Build("Server responded with Status ", status);
|
error = String::Build("Server responded with Status ", status);
|
||||||
notifyError("Could not download update: " + error);
|
notifyError("Could not download update: " + error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!data)
|
if (data.size())
|
||||||
{
|
{
|
||||||
error = "Server responded with nothing";
|
error = "Server responded with nothing";
|
||||||
notifyError("Server did not return any data");
|
notifyError("Server did not return any data");
|
||||||
@ -83,7 +83,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
int dstate;
|
int dstate;
|
||||||
dstate = BZ2_bzBuffToBuffDecompress((char *)res, (unsigned *)&uncompressedLength, (char *)(data+8), dataLength-8, 0, 0);
|
dstate = BZ2_bzBuffToBuffDecompress((char *)res, (unsigned *)&uncompressedLength, &data[8], dataLength-8, 0, 0);
|
||||||
if (dstate)
|
if (dstate)
|
||||||
{
|
{
|
||||||
error = String::Build("Unable to decompress update: ", dstate);
|
error = String::Build("Unable to decompress update: ", dstate);
|
||||||
@ -91,8 +91,6 @@ private:
|
|||||||
goto corrupt;
|
goto corrupt;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(data);
|
|
||||||
|
|
||||||
notifyStatus("Applying update");
|
notifyStatus("Applying update");
|
||||||
notifyProgress(-1);
|
notifyProgress(-1);
|
||||||
|
|
||||||
@ -110,7 +108,6 @@ private:
|
|||||||
|
|
||||||
corrupt:
|
corrupt:
|
||||||
notifyError("Downloaded update is corrupted\n" + error);
|
notifyError("Downloaded update is corrupted\n" + error);
|
||||||
free(data);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
|
|
||||||
#include "client/HTTP.h"
|
#include "client/Download.h"
|
||||||
#include "Format.h"
|
#include "Format.h"
|
||||||
#include "LuaScriptInterface.h"
|
#include "LuaScriptInterface.h"
|
||||||
#include "LuaScriptHelper.h"
|
#include "LuaScriptHelper.h"
|
||||||
@ -1355,21 +1355,18 @@ int luatpt_getscript(lua_State* l)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
int ret, len;
|
int ret, len;
|
||||||
char *scriptData = http_simple_get(url.c_str(), &ret, &len);
|
ByteString scriptData = Download::Simple(url, &len, &ret);
|
||||||
if (len <= 0 || !filename)
|
if (len <= 0 || !filename)
|
||||||
{
|
{
|
||||||
free(scriptData);
|
|
||||||
return luaL_error(l, "Server did not return data");
|
return luaL_error(l, "Server did not return data");
|
||||||
}
|
}
|
||||||
if (ret != 200)
|
if (ret != 200)
|
||||||
{
|
{
|
||||||
free(scriptData);
|
return luaL_error(l, Download::StatusText(ret));
|
||||||
return luaL_error(l, http_ret_text(ret));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!strcmp(scriptData, "Invalid script ID\r\n"))
|
if (!strcmp(scriptData.c_str(), "Invalid script ID\r\n"))
|
||||||
{
|
{
|
||||||
free(scriptData);
|
|
||||||
return luaL_error(l, "Invalid Script ID");
|
return luaL_error(l, "Invalid Script ID");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1384,7 +1381,6 @@ int luatpt_getscript(lua_State* l)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
free(scriptData);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1394,11 +1390,10 @@ int luatpt_getscript(lua_State* l)
|
|||||||
}
|
}
|
||||||
if (!outputfile)
|
if (!outputfile)
|
||||||
{
|
{
|
||||||
free(scriptData);
|
|
||||||
return luaL_error(l, "Unable to write to file");
|
return luaL_error(l, "Unable to write to file");
|
||||||
}
|
}
|
||||||
|
|
||||||
fputs(scriptData, outputfile);
|
fputs(scriptData.c_str(), outputfile);
|
||||||
fclose(outputfile);
|
fclose(outputfile);
|
||||||
outputfile = NULL;
|
outputfile = NULL;
|
||||||
if (runScript)
|
if (runScript)
|
||||||
|
Reference in New Issue
Block a user