Skip to content

Commit

Permalink
Add https, progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
bucanero committed Dec 15, 2021
1 parent 3d2848a commit 4d843e7
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 96 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CONTENT_ID := IV0000-APOL00004_00-APOLLO0000000PS4

# Libraries linked into the ELF.
LIBS := -lc -lkernel -lc++ -lSceAudioOut -lSceUserService -lSceVideoOut -lSceGnmDriver -lSceSysmodule -lSceFreeType \
-lScePad -lSceSystemService -lSceSaveData -lSceCommonDialog -lSceMsgDialog \
-lScePad -lSceSystemService -lSceSaveData -lSceCommonDialog -lSceMsgDialog -lSceNet -lSceSsl -lSceHttp \
-lSDL2 -lapollo -ldbglogger -lpolarssl -lz -lzip

# Additional compile flags.
Expand Down
5 changes: 3 additions & 2 deletions include/saves.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ enum cmd_code_enum
#define SAVE_FLAG_TROPHY 128
#define SAVE_FLAG_ONLINE 256
#define SAVE_FLAG_PS4 512
#define SAVE_FLAG_HDD 1024

enum save_type_enum
{
Expand Down Expand Up @@ -242,8 +243,8 @@ int extract_zip(const char* zip_file, const char* dest_path);
int zip_directory(const char* basedir, const char* inputdir, const char* output_zipfile);

int show_dialog(int dialog_type, const char * format, ...);
void init_progress_bar(const char* progress_bar_title, const char* msg);
void update_progress_bar(uint64_t* progress, const uint64_t total_size, const char* msg);
void init_progress_bar(const char* msg);
void update_progress_bar(uint64_t progress, const uint64_t total_size, const char* msg);
void end_progress_bar(void);
#define show_message(...) show_dialog(0, __VA_ARGS__)

Expand Down
51 changes: 27 additions & 24 deletions source/dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

void drawDialogBackground();

static float bar1_countparts;

static inline void _orbisCommonDialogSetMagicNumber(uint32_t* magic, const OrbisCommonDialogBaseParam* param)
{
Expand Down Expand Up @@ -67,38 +66,42 @@ int show_dialog(int tdialog, const char * format, ...)
return (result.buttonId == ORBIS_MSG_DIALOG_BUTTON_ID_YES);
}

/*
void init_progress_bar(const char* progress_bar_title, const char* msg)
void init_progress_bar(const char* msg)
{
bar1_countparts = 0.0f;
OrbisMsgDialogParam param;
OrbisMsgDialogProgressBarParam userBarParam;
OrbisMsgDialogResult result;

msgDialogOpen2(MSG_DIALOG_BKG_INVISIBLE | MSG_DIALOG_SINGLE_PROGRESSBAR | MSG_DIALOG_MUTE_ON, progress_bar_title, NULL, NULL, NULL);
msgDialogProgressBarSetMsg(MSG_PROGRESSBAR_INDEX0, msg);
msgDialogProgressBarReset(MSG_PROGRESSBAR_INDEX0);
sceMsgDialogInitialize();
orbisMsgDialogParamInitialize(&param);
param.mode = ORBIS_MSG_DIALOG_MODE_PROGRESS_BAR;

memset(&userBarParam, 0, sizeof(userBarParam));
userBarParam.msg = msg;
userBarParam.barType = ORBIS_MSG_DIALOG_PROGRESSBAR_TYPE_PERCENTAGE;
param.progBarParam = &userBarParam;

if (sceMsgDialogOpen(&param) < 0)
return;

drawDialogBackground();
}

void end_progress_bar(void)
{
msgDialogAbort();
sceMsgDialogClose();
sceMsgDialogTerminate();
}

void update_progress_bar(uint64_t* progress, const uint64_t total_size, const char* msg)
void update_progress_bar(uint64_t progress, const uint64_t total_size, const char* msg)
{
if(*progress > 0) {
bar1_countparts += (100.0f * ((double) *progress)) / ((double) total_size);
*progress = 0;
}
if(bar1_countparts >= 1.0f) {
msgDialogProgressBarSetMsg(MSG_PROGRESSBAR_INDEX0, msg);
msgDialogProgressBarInc(MSG_PROGRESSBAR_INDEX0, (u32) bar1_countparts);
bar1_countparts -= (float) ((u32) bar1_countparts);
}
drawDialogBackground();
}
float bar_value = (100.0f * ((double) progress)) / ((double) total_size);

*/
if (sceMsgDialogUpdateStatus() == ORBIS_COMMON_DIALOG_STATUS_RUNNING)
{
sceMsgDialogProgressBarSetMsg(0, msg);
sceMsgDialogProgressBarSetValue(0, (uint32_t) bar_value);
}

drawDialogBackground();
}
230 changes: 230 additions & 0 deletions source/http.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#include <orbis/Http.h>
#include <orbis/Ssl.h>
#include <orbis/Net.h>
#include <orbis/Sysmodule.h>

#include "common.h"
#include "saves.h"

#define HTTP_SUCCESS 1
#define HTTP_FAILED 0
#define HTTP_USER_AGENT "Mozilla/5.0 (PLAYSTATION 4; 1.00)"

#define NET_POOLSIZE (4 * 1024)


static int libnetMemId = 0, libhttpCtxId = 0, libsslCtxId = 0;

static int skipSslCallback(int libsslId, unsigned int verifyErr, void * const sslCert[], int certNum, void *userArg)
{
LOG("sslCtx=%x (%X)", libsslId, verifyErr);
return HTTP_SUCCESS;
}

int http_init()
{
int ret;

if(sceSysmoduleLoadModuleInternal(ORBIS_SYSMODULE_INTERNAL_NET) < 0)
return HTTP_FAILED;

if(sceSysmoduleLoadModuleInternal(ORBIS_SYSMODULE_INTERNAL_HTTP) < 0)
return HTTP_FAILED;

if(sceSysmoduleLoadModuleInternal(ORBIS_SYSMODULE_INTERNAL_SSL) < 0)
return HTTP_FAILED;

if (libnetMemId == 0 || libhttpCtxId == 0)
{
LOG("sceNet init");
ret = sceNetInit();
ret = sceNetPoolCreate("netPool", NET_POOLSIZE, 0);
if (ret < 0) {
LOG("sceNetPoolCreate() error: 0x%08X\n", ret);
return HTTP_FAILED;
}
libnetMemId = ret;

LOG("sceSsl init");
ret = sceSslInit(SSL_POOLSIZE);
if (ret < 0) {
LOG("sceSslInit() error: 0x%08X\n", ret);
return HTTP_FAILED;
}
libsslCtxId = ret;

LOG("sceHttp init");
ret = sceHttpInit(libnetMemId, libsslCtxId, LIBHTTP_POOLSIZE);
if (ret < 0) {
LOG("sceHttpInit() error: 0x%08X\n", ret);
return HTTP_FAILED;
}
libhttpCtxId = ret;
}

return HTTP_SUCCESS;
}

int http_download(const char* url, const char* filename, const char* local_dst, int show_progress)
{
int ret, tpl = 0, conn = 0, req = 0;
int http_res = HTTP_FAILED;
int contentLengthType;
uint64_t contentLength;
int32_t statusCode;
char full_url[1024];

snprintf(full_url, sizeof(full_url), "%s%s", url, filename);

tpl = sceHttpCreateTemplate(libhttpCtxId, HTTP_USER_AGENT, ORBIS_HTTP_VERSION_1_1, 1);
if (tpl < 0) {
LOG("sceHttpCreateConnectionWithURL() error: 0x%08X\n", tpl);
return HTTP_FAILED;
}

ret = sceHttpsSetSslCallback(tpl, skipSslCallback, NULL);
if (ret < 0) {
LOG("sceHttpsSetSslCallback() error: 0x%08X\n", ret);
}

conn = sceHttpCreateConnectionWithURL(tpl, full_url, 1);
if (conn < 0) {
LOG("sceHttpCreateConnectionWithURL() error: 0x%08X\n", conn);
goto close_http;
}

req = sceHttpCreateRequestWithURL(conn, ORBIS_METHOD_GET, full_url, 0);
if (req < 0) {
LOG("sceHttpCreateRequestWithURL() error: 0x%08X\n", req);
goto close_http;
}

LOG("Sending Request to '%s'\n", full_url);
ret = sceHttpSendRequest(req, NULL, 0);
if (ret < 0) {
LOG("sceHttpCreateRequestWithURL (%X)", ret);
goto close_http;
}

ret = sceHttpGetStatusCode(req, &statusCode);
if (ret < 0) {
LOG("sceHttpGetStatusCode (%X)", ret);
goto close_http;
}

ret = sceHttpGetResponseContentLength(req, &contentLengthType, &contentLength);
if (ret < 0) {
LOG("sceHttpGetContentLength() error: 0x%08X\n", ret);
//goto close_http;
}
else if (contentLengthType == ORBIS_HTTP_CONTENTLEN_EXIST) {
LOG("Content-Length = %lu\n", contentLength);
}

if (statusCode != 200)
{
if (statusCode == 404)
LOG("404 Download Not Found");

else if (statusCode == 408)
LOG("Request Timed Out - Check your Connection");

goto close_http;
}

uint8_t dl_buf[8 * 1024];
uint64_t total_read = 0;
FILE* fd = fopen(local_dst, "wb");

if (!fd) {
LOG("fopen Error: File path '%s'", local_dst);
goto close_http;
}

if (show_progress)
init_progress_bar("Downloading...");

while (1) {
int read = sceHttpReadData(req, dl_buf, sizeof(dl_buf));
if (read < 0)
break;

if (read == 0)
{
http_res = HTTP_SUCCESS;
break;
}

ret = fwrite(dl_buf, 1, read, fd);
if (ret < 0 || ret != read)
break;

total_read += read;

if (show_progress)
update_progress_bar(total_read, contentLength, "Downloading...");

LOG("Downloaded %d/%d\n", total_read, contentLength);
}

fclose(fd);

if (show_progress)
end_progress_bar();

close_http:
if (req > 0) {
ret = sceHttpDeleteRequest(req);
if (ret < 0) {
LOG("sceHttpDeleteRequest() error: 0x%08X\n", ret);
}
}

if (conn > 0) {
ret = sceHttpDeleteConnection(conn);
if (ret < 0) {
LOG("sceHttpDeleteConnection() error: 0x%08X\n", ret);
}
}

if (tpl > 0) {
ret = sceHttpDeleteTemplate(tpl);
if (ret < 0) {
LOG("sceHttpDeleteTemplate() error: 0x%08X\n", ret);
}
}

return (http_res);
}

void http_end(void)
{
sceHttpTerm(libhttpCtxId);
sceSslTerm(libsslCtxId);
sceNetPoolDestroy(libnetMemId);
}

/*
void uri() {
int ret;
size_t mallocSize, outSize;
uchar8_t *data = "target string";
char *out=NULL;
ret = sceHttpUriEscape(NULL, &mallocSize, 0, data);
if (ret < 0){
printf("sceHttpUriEscape() returns %x.\n", ret);
goto error;
}
out = (uchar8_t*)malloc(mallocSize);
if (out == NULL){
printf("can't allocate memory\n");
goto error;
}
ret = sceHttpUriEscape(out, &outSize, mallocSize, data);
}
*/
Loading

0 comments on commit 4d843e7

Please sign in to comment.