Skip to content
Permalink
Browse files

Replace http_* calls with Download calls in non-RequestBroker stuff

  • Loading branch information...
LBPHacker authored and jacob1 committed Mar 5, 2019
1 parent 9e110cb commit 8b5cf394e05a8912603d3f80eeca71b34794c9ba

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -48,13 +48,14 @@ class UpdateInfo

class RequestListener;
class ClientListener;
class Download;
class Client: public Singleton<Client> {
private:
String messageOfTheDay;
std::vector<std::pair<String, ByteString> > serverNotifications;

void * versionCheckRequest;
void * alternateVersionCheckRequest;
Download *versionCheckRequest;
Download *alternateVersionCheckRequest;
bool usingAltUpdateServer;
bool updateAvailable;
UpdateInfo updateInfo;
@@ -172,9 +173,9 @@ class Client: public Singleton<Client> {
String GetLastError() {
return lastError;
}
RequestStatus ParseServerReturn(char *result, int status, bool json);
RequestStatus ParseServerReturn(ByteString &result, int status, bool json);
void Tick();
bool CheckUpdate(void *updateRequest, bool checkSession);
bool CheckUpdate(Download *updateRequest, bool checkSession);
void Shutdown();

// preferences functions
@@ -2,6 +2,7 @@
#include "Download.h"
#include "DownloadManager.h"
#include "HTTP.h"
#include "Platform.h"

Download::Download(ByteString uri_, bool keepAlive):
http(NULL),
@@ -84,18 +85,23 @@ bool Download::Reuse(ByteString newuri)
}

// 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())
return NULL; // shouldn't happen but just in case
return ""; // shouldn't happen but just in case
while (!CheckDone()); // block
DownloadManager::Ref().Lock();
downloadStarted = false;
if (length)
*length = downloadSize;
if (status)
*status = downloadStatus;
char *ret = downloadData;
ByteString ret;
if (downloadData)
{
ret = ByteString(downloadData, downloadData + downloadSize);
free(downloadData);
}
downloadData = NULL;
if (!keepAlive)
downloadCanceled = true;
@@ -149,3 +155,34 @@ void Download::Cancel()
downloadCanceled = true;
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 @@ class Download
void AuthHeaders(ByteString ID, ByteString session);
void Start();
bool Reuse(ByteString newuri);
char* Finish(int *length, int *status);
ByteString Finish(int *length, int *status);
void Cancel();

void CheckProgress(int *total, int *done);
@@ -42,6 +42,11 @@ class Download
bool CheckStarted();

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
@@ -38,7 +38,8 @@ RequestBroker::ProcessResponse APIRequest::Process(RequestBroker & rb)
int 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)
{
void * resultObject = Parser->ProcessResponse((unsigned char *)data, data_size);
@@ -37,7 +37,8 @@ RequestBroker::ProcessResponse WebRequest::Process(RequestBroker & rb)
int 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)
{
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;

try
{
std::istringstream dataStream(saveInfoResponse);
std::istringstream dataStream(saveInfoResponse.c_str());
Json::Value objDocument;
dataStream >> objDocument;

@@ -251,13 +251,13 @@ bool PreviewModel::ParseSaveInfo(char * saveInfoResponse)
}
}

bool PreviewModel::ParseComments(char *commentsResponse)
bool PreviewModel::ParseComments(ByteString &commentsResponse)
{
ClearComments();
saveComments = new std::vector<SaveComment*>();
try
{
std::istringstream dataStream(commentsResponse);
std::istringstream dataStream(commentsResponse.c_str());
Json::Value commentsArray;
dataStream >> commentsArray;

@@ -284,13 +284,14 @@ void PreviewModel::Update()
if (saveDataDownload && saveDataDownload->CheckDone())
{
int status, length;
char *ret = saveDataDownload->Finish(&length, &status);
ByteString ret = saveDataDownload->Finish(&length, &status);

Client::Ref().ParseServerReturn(NULL, status, true);
if (status == 200 && ret)
ByteString nothing;
Client::Ref().ParseServerReturn(nothing, status, true);
if (status == 200 && ret.size())
{
delete saveData;
saveData = new std::vector<unsigned char>(ret, ret+length);
saveData = new std::vector<unsigned char>(ret.begin(), ret.end());
if (saveInfo && saveData)
OnSaveReady();
}
@@ -302,16 +303,16 @@ void PreviewModel::Update()
}
}
saveDataDownload = NULL;
free(ret);
}

if (saveInfoDownload && saveInfoDownload->CheckDone())
{
int status;
char *ret = saveInfoDownload->Finish(NULL, &status);
ByteString ret = saveInfoDownload->Finish(NULL, &status);

Client::Ref().ParseServerReturn(NULL, status, true);
if (status == 200 && ret)
ByteString nothing;
Client::Ref().ParseServerReturn(nothing, status, true);
if (status == 200 && ret.size())
{
if (ParseSaveInfo(ret))
{
@@ -330,25 +331,24 @@ void PreviewModel::Update()
observers[i]->SaveLoadingError(Client::Ref().GetLastError());
}
saveInfoDownload = NULL;
free(ret);
}

if (commentsDownload && commentsDownload->CheckDone())
{
int status;
char *ret = commentsDownload->Finish(NULL, &status);
ByteString ret = commentsDownload->Finish(NULL, &status);
ClearComments();

Client::Ref().ParseServerReturn(NULL, status, true);
if (status == 200 && ret)
ByteString nothing;
Client::Ref().ParseServerReturn(nothing, status, true);
if (status == 200 && ret.size())
ParseComments(ret);

commentsLoaded = true;
notifySaveCommentsChanged();
notifyCommentsPageChanged();

commentsDownload = NULL;
free(ret);
}
}

@@ -57,8 +57,8 @@ class PreviewModel {
void Update();
void ClearComments();
void OnSaveReady();
bool ParseSaveInfo(char * saveInfoResponse);
bool ParseComments(char * commentsResponse);
bool ParseSaveInfo(ByteString &saveInfoResponse);
bool ParseComments(ByteString &commentsResponse);
virtual ~PreviewModel();
};

@@ -3,9 +3,9 @@
#include "gui/interface/Engine.h"
#include "UpdateActivity.h"
#include "tasks/Task.h"
#include "client/HTTP.h"
#include "client/Client.h"
#include "Update.h"
#include "client/Download.h"
#include "Platform.h"


@@ -26,27 +26,27 @@ class UpdateDownloadTask : public Task
virtual bool doWork()
{
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");
notifyProgress(-1);
while(!http_async_req_status(request))
while(!request->CheckDone())
{
int total, done;
http_async_get_length(request, &total, &done);
request->CheckProgress(&total, &done);
notifyProgress((float(done)/float(total))*100.0f);
Platform::Millisleep(1);
}

char * data;
int dataLength, status;
data = http_async_req_stop(request, &status, &dataLength);
ByteString data = request->Finish(&dataLength, &status);
if (status!=200)
{
free(data);
error = String::Build("Server responded with Status ", status);
notifyError("Could not download update: " + error);
return false;
}
if (!data)
if (data.size())
{
error = "Server responded with nothing";
notifyError("Server did not return any data");
@@ -83,16 +83,14 @@ class UpdateDownloadTask : public Task
}

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)
{
error = String::Build("Unable to decompress update: ", dstate);
free(res);
goto corrupt;
}

free(data);

notifyStatus("Applying update");
notifyProgress(-1);

@@ -110,7 +108,6 @@ class UpdateDownloadTask : public Task

corrupt:
notifyError("Downloaded update is corrupted\n" + error);
free(data);
return false;
}
};
@@ -4,7 +4,7 @@
#include <algorithm>
#include <locale>

#include "client/HTTP.h"
#include "client/Download.h"
#include "Format.h"
#include "LuaScriptInterface.h"
#include "LuaScriptHelper.h"
@@ -1355,21 +1355,18 @@ int luatpt_getscript(lua_State* l)
return 0;

int ret, len;
char *scriptData = http_simple_get(url.c_str(), &ret, &len);
ByteString scriptData = Download::Simple(url, &len, &ret);
if (len <= 0 || !filename)
{
free(scriptData);
return luaL_error(l, "Server did not return data");
}
if (ret != 200)
{
free(scriptData);
return luaL_error(l, http_ret_text(ret));
return luaL_error(l, Download::StatusText(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");
}

@@ -1384,7 +1381,6 @@ int luatpt_getscript(lua_State* l)
}
else
{
free(scriptData);
return 0;
}
}
@@ -1394,11 +1390,10 @@ int luatpt_getscript(lua_State* l)
}
if (!outputfile)
{
free(scriptData);
return luaL_error(l, "Unable to write to file");
}

fputs(scriptData, outputfile);
fputs(scriptData.c_str(), outputfile);
fclose(outputfile);
outputfile = NULL;
if (runScript)

0 comments on commit 8b5cf39

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.