the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at master 289 lines 6.8 kB view raw
1#include "stdafx.h" 2#include "SonyHttp_PS3.h" 3 4 5 6// static const int sc_HTTPPoolSize = (32 * 1024); 7// static const int sc_SSLPoolSize = (256 * 1024U); 8// static const int sc_CookiePoolSize = (256 * 1024U); 9 static const int sc_HTTPPoolSize = (32 * 1024); 10 static const int sc_SSLPoolSize = (512 * 1024U); 11 static const int sc_CookiePoolSize = (256 * 1024U); 12 13void* SonyHttp_PS3::uriPool = NULL; 14void* SonyHttp_PS3::httpPool = NULL; 15void* SonyHttp_PS3::sslPool = NULL; 16void* SonyHttp_PS3::cookiePool = NULL; 17CellHttpClientId SonyHttp_PS3::client; 18CellHttpTransId SonyHttp_PS3::trans; 19bool SonyHttp_PS3:: bInitialised = false; 20 21 22 23bool SonyHttp_PS3::loadCerts(size_t *numBufPtr, CellHttpsData **caListPtr) 24{ 25 CellHttpsData *caList = NULL; 26 size_t size = 0; 27 int ret = 0; 28 char *buf = NULL; 29 30 caList = (CellHttpsData *)malloc(sizeof(CellHttpsData)*2); 31 if (NULL == caList) { 32 app.DebugPrintf("failed to malloc cert data"); 33 return false; 34 } 35 36 ret = cellSslCertificateLoader(CELL_SSL_LOAD_CERT_ALL, NULL, 0, &size); 37 if (ret < 0) { 38 app.DebugPrintf("cellSslCertifacateLoader() failed(1): 0x%08x", ret); 39 return ret; 40 } 41 42 buf = (char*)malloc(size); 43 if (NULL == buf) { 44 app.DebugPrintf("failed to malloc cert buffer"); 45 free(caList); 46 return false; 47 } 48 49 ret = cellSslCertificateLoader(CELL_SSL_LOAD_CERT_ALL, buf, size, NULL); 50 if (ret < 0) { 51 app.DebugPrintf("cellSslCertifacateLoader() failed(2): 0x%08x", ret); 52 free(buf); 53 free(caList); 54 return false; 55 } 56 57 (&caList[0])->ptr = buf; 58 (&caList[0])->size = size; 59 60 *caListPtr = caList; 61 *numBufPtr = 1; 62 63 return true; 64} 65 66 67bool SonyHttp_PS3::init() 68{ 69 int ret; 70 client = 0; 71 trans = 0; 72 73 /*E startup procedures */ 74 httpPool = malloc(sc_HTTPPoolSize); 75 if (httpPool == NULL) { 76 app.DebugPrintf("failed to malloc libhttp memory pool\n"); 77 return false; 78 } 79 ret = cellHttpInit(httpPool, sc_HTTPPoolSize); 80 if (0 > ret) 81 { 82 app.DebugPrintf("unable to start libhttp... (0x%x)\n", ret); 83 return false; 84 } 85 86 cookiePool = malloc(sc_CookiePoolSize); 87 if (cookiePool == NULL) { 88 app.DebugPrintf("failed to malloc ssl memory pool\n"); 89 return false; 90 } 91 ret = cellHttpInitCookie(cookiePool, sc_CookiePoolSize); 92 if (ret < 0 && ret != CELL_HTTP_ERROR_ALREADY_INITIALIZED) 93 { 94 app.DebugPrintf("cellHttpInitCookie failed... (0x%x)\n", ret); 95 return false; 96 } 97 98 99 sslPool = malloc(sc_SSLPoolSize); 100 if (sslPool == NULL) { 101 app.DebugPrintf("failed to malloc ssl memory pool\n"); 102 return false; 103 } 104 ret = cellSslInit(sslPool, sc_SSLPoolSize); 105 if (0 > ret) 106 { 107 app.DebugPrintf("unable to start cellSslInit... (0x%x)\n", ret); 108 return false; 109 } 110 111 size_t numBuf = 0; 112 CellHttpsData *caList = NULL; 113 if(!loadCerts(&numBuf, &caList)) 114 return false; 115 116 ret = cellHttpsInit(numBuf, caList); 117 free(caList->ptr); 118 free(caList); 119 if (ret < 0 && ret != CELL_HTTP_ERROR_ALREADY_INITIALIZED) 120 { 121 app.DebugPrintf("unable to start https: 0x%08x", ret); 122 return false; 123 } 124 125 ret = cellHttpCreateClient(&client); 126 if (0 > ret) 127 { 128 app.DebugPrintf("unable to create http client... (0x%x)\n", ret); 129 return false; 130 } 131 bInitialised = true; 132 return true; 133} 134 135void SonyHttp_PS3::shutdown() 136{ 137 if (trans) 138 { 139 cellHttpDestroyTransaction(trans); 140 trans = 0; 141 } 142 if (client) 143 { 144 cellHttpDestroyClient(client); 145 client = 0; 146 } 147 cellHttpEnd(); 148 free(httpPool); 149} 150 151 152bool SonyHttp_PS3::parseUri(const char* szUri, CellHttpUri& parsedUri) 153{ 154 /*E the main part */ 155 size_t poolSize = 0; 156 int ret = cellHttpUtilParseUri(NULL, szUri, NULL, 0, &poolSize); 157 if (0 > ret) 158 { 159 app.DebugPrintf("error parsing URI... (0x%x)\n\n", ret); 160 return false; 161 } 162 if (NULL == (uriPool = malloc(poolSize))) 163 { 164 app.DebugPrintf("error mallocing uriPool (%d)\n", poolSize); 165 return false; 166 } 167 ret = cellHttpUtilParseUri(&parsedUri, szUri, uriPool, poolSize, NULL); 168 if (0 > ret) 169 { 170 free(uriPool); 171 app.DebugPrintf("error parsing URI... (0x%x)\n\n", ret); 172 return false; 173 } 174 return true; 175} 176 177void* SonyHttp_PS3::getData(const char* url, int* pDataSize) 178{ 179 CellHttpUri uri; 180 int ret; 181 bool has_cl = true; 182 uint64_t length = 0; 183 uint64_t recvd; 184 const char *serverName; 185 size_t localRecv = 0; 186 187 if(!parseUri(url, uri)) 188 return NULL; 189 190 app.DebugPrintf(" scheme: %s\n", uri.scheme); 191 app.DebugPrintf(" hostname: %s\n", uri.hostname); 192 app.DebugPrintf(" port: %d\n", uri.port); 193 app.DebugPrintf(" path: %s\n", uri.path); 194 app.DebugPrintf(" username: %s\n", uri.username); 195 app.DebugPrintf(" password: %s\n", uri.password); 196 197 ret = cellHttpCreateTransaction(&trans, client, CELL_HTTP_METHOD_GET, &uri); 198 free(uriPool); 199 if (0 > ret) 200 { 201 app.DebugPrintf("failed to create http transaction... (0x%x)\n\n", ret); 202 return NULL; 203 } 204 205 ret = cellHttpSendRequest(trans, NULL, 0, NULL); 206 if (0 > ret) 207 { 208 app.DebugPrintf("failed to complete http transaction... (0x%x)\n\n", ret); 209 /*E continue down to check status code, just in case */ 210 cellHttpDestroyTransaction(trans); 211 trans = 0; 212 } 213 214 { 215 int code = 0; 216 ret = cellHttpResponseGetStatusCode(trans, &code); 217 if (0 > ret) 218 { 219 app.DebugPrintf("failed to receive http response... (0x%x)\n\n", ret); 220 cellHttpDestroyTransaction(trans); 221 trans = 0; 222 return NULL; 223 } 224 app.DebugPrintf("Status Code is %d\n", code); 225 } 226 227 ret = cellHttpResponseGetContentLength(trans, &length); 228 if (0 > ret) 229 { 230 if (ret == CELL_HTTP_ERROR_NO_CONTENT_LENGTH) 231 { 232 app.DebugPrintf("Only supporting data that has a content length : CELL_HTTP_ERROR_NO_CONTENT_LENGTH\n", ret); 233 cellHttpDestroyTransaction(trans); 234 trans = 0; 235 return NULL; 236 } 237 else 238 { 239 app.DebugPrintf("error in receiving content length... (0x%x)\n\n", ret); 240 cellHttpDestroyTransaction(trans); 241 trans = 0; 242 return NULL; 243 } 244 } 245 246 int bufferSize = length; 247 char* buffer = (char*)malloc(bufferSize+1); 248 249 recvd = 0; 250 251 // 4J-PB - seems to be some limit to a read with cellHttpRecvResponse, even though it claims it's read all the data, it hasn't 252 // reducing this to read 10k at a time. 253 int iMaxRead=10240; 254 app.DebugPrintf("\n===BEGINNING OF TRANSMISSION===\n"); 255 while(recvd<length) 256 { 257 ret = cellHttpRecvResponse(trans, &buffer[recvd], iMaxRead, &localRecv); 258 if (0 > ret) 259 { 260 app.DebugPrintf("error receiving body... (0x%x)\n\n", ret); 261 free(buffer); 262 cellHttpDestroyTransaction(trans); 263 trans = 0; 264 return NULL; 265 } 266 else 267 { 268 if(localRecv==0) break; 269 } 270 recvd += localRecv; 271 } 272 buffer[bufferSize] = '\0'; 273 ret = 0; 274 app.DebugPrintf("\n===END OF TRANSMISSION===\n\n"); 275 *pDataSize = bufferSize; 276 cellHttpDestroyTransaction(trans); 277 trans = 0; 278 return buffer; 279} 280 281bool SonyHttp_PS3::getDataFromURL( const char* szURL, void** ppOutData, int* pDataSize) 282{ 283 if(!bInitialised) 284 return false; 285 *ppOutData = getData(szURL, pDataSize); 286 if(*ppOutData) 287 return true; 288 return false; 289}