Skip to content

Commit

Permalink
Replace http_* calls with Download calls in non-RequestBroker stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
LBPHacker authored and jacob1 committed Mar 8, 2019
1 parent 9e110cb commit 8b5cf39
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 200 deletions.
244 changes: 95 additions & 149 deletions src/client/Client.cpp

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/client/Client.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
43 changes: 40 additions & 3 deletions src/client/Download.cpp
Expand Up @@ -2,6 +2,7 @@
#include "Download.h"
#include "DownloadManager.h"
#include "HTTP.h"
#include "Platform.h"

Download::Download(ByteString uri_, bool keepAlive):
http(NULL),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

7 changes: 6 additions & 1 deletion src/client/Download.h
Expand Up @@ -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);
Expand All @@ -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
3 changes: 2 additions & 1 deletion src/client/requestbroker/APIRequest.cpp
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/client/requestbroker/WebRequest.cpp
Expand Up @@ -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);
Expand Down
34 changes: 17 additions & 17 deletions src/gui/preview/PreviewModel.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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();
}
Expand All @@ -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))
{
Expand All @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/gui/preview/PreviewModel.h
Expand Up @@ -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();
};

Expand Down
21 changes: 9 additions & 12 deletions src/gui/update/UpdateActivity.cpp
Expand Up @@ -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"


Expand All @@ -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");
Expand Down Expand Up @@ -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);

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

corrupt:
notifyError("Downloaded update is corrupted\n" + error);
free(data);
return false;
}
};
Expand Down
15 changes: 5 additions & 10 deletions src/lua/LegacyLuaAPI.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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");
}

Expand All @@ -1384,7 +1381,6 @@ int luatpt_getscript(lua_State* l)
}
else
{
free(scriptData);
return 0;
}
}
Expand All @@ -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)
Expand Down

0 comments on commit 8b5cf39

Please sign in to comment.