the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 285 lines 7.6 kB view raw
1#include "stdafx.h" 2#include "SonyHttp_Orbis.h" 3 4static const int sc_SSLHeapSize = (304 * 1024U); 5static const int sc_HTTPHeapSize = (48 * 1024); 6static const int sc_NetHeapSize = (16 * 1024); 7 8#define TEST_USER_AGENT "SimpleSample/1.00" 9 10 11int SonyHttp_Orbis::libnetMemId = 0; 12int SonyHttp_Orbis::libsslCtxId = 0; 13int SonyHttp_Orbis::libhttpCtxId = 0; 14bool SonyHttp_Orbis:: bInitialised = false; 15 16 17 18 19 20bool SonyHttp_Orbis::init() 21{ 22 int ret = sceNetPoolCreate("simple", sc_NetHeapSize, 0); 23 assert(ret >= 0); 24 libnetMemId = ret; 25 26 ret = sceSslInit(sc_SSLHeapSize); 27 assert(ret >= 0); 28 libsslCtxId = ret; 29 30 ret = sceHttpInit(libnetMemId, libsslCtxId, sc_HTTPHeapSize); 31 assert(ret >= 0); 32 libhttpCtxId = ret; 33 34 bInitialised = true; 35 return true; 36} 37 38void SonyHttp_Orbis::shutdown() 39{ 40 int ret = sceHttpTerm(libhttpCtxId); 41 assert(ret == SCE_OK); 42 43 ret = sceSslTerm(libsslCtxId); 44 assert(ret == SCE_OK); 45 46 /* libnet */ 47 ret = sceNetPoolDestroy(libnetMemId); 48 assert(ret == SCE_OK); 49 50} 51void SonyHttp_Orbis::printSslError(SceInt32 sslErr, SceUInt32 sslErrDetail) 52{ 53 switch (sslErr) 54 { 55 case (SCE_HTTPS_ERROR_CERT): /* Verify error */ 56 /* Internal error at verifying certificate*/ 57 if (sslErrDetail & SCE_HTTPS_ERROR_SSL_INTERNAL){ 58 app.DebugPrintf("ssl verify error: unexpcted error\n"); 59 } 60 /* Error of server certificate or CA certificate */ 61 if (sslErrDetail & SCE_HTTPS_ERROR_SSL_INVALID_CERT){ 62 app.DebugPrintf("ssl verify error: invalid server cert or CA cert\n"); 63 } 64 /* Server hostname and server certificate are mismatched*/ 65 if (sslErrDetail & SCE_HTTPS_ERROR_SSL_CN_CHECK){ 66 app.DebugPrintf("ssl verify error: invalid server hostname\n"); 67 } 68 /* Server certificate or CA certificate is expired.*/ 69 if (sslErrDetail & SCE_HTTPS_ERROR_SSL_NOT_AFTER_CHECK){ 70 app.DebugPrintf("ssl verify error: server cert or CA cert had expired\n"); 71 } 72 /* Server certificate or CA certificate is before validated.*/ 73 if (sslErrDetail & SCE_HTTPS_ERROR_SSL_NOT_BEFORE_CHECK){ 74 app.DebugPrintf("ssl verify error: server cert or CA cert isn't validated yet.\n"); 75 } 76 /* Unknown CA error */ 77 if (sslErrDetail & SCE_HTTPS_ERROR_SSL_UNKNOWN_CA){ 78 app.DebugPrintf("ssl verify error: unknown CA\n"); 79 } 80 break; 81 case (SCE_HTTPS_ERROR_HANDSHAKE): /* fail to ssl-handshake */ 82 app.DebugPrintf("ssl error: handshake error\n"); 83 break; 84 case (SCE_HTTPS_ERROR_IO): /* Error of Socket IO */ 85 app.DebugPrintf("ssl error: io error\n"); 86 break; 87 case (SCE_HTTP_ERROR_OUT_OF_MEMORY): /* Out of memory*/ 88 app.DebugPrintf("ssl error: out of memory\n"); 89 break; 90 case (SCE_HTTPS_ERROR_INTERNAL): /* Unexpected Internal Error*/ 91 app.DebugPrintf("ssl error: unexpcted error\n"); 92 break; 93 default: 94 break; 95 } 96 return; 97} 98 99 100void SonyHttp_Orbis::printSslCertInfo(int libsslCtxId,SceSslCert *sslCert) 101{ 102 SceInt32 ret; 103 SceUChar8 *sboData = NULL ; 104 SceSize sboLen, counter; 105 106 ret = sceSslGetSerialNumber(libsslCtxId, sslCert, NULL, &sboLen); 107 if (ret < 0){ 108 app.DebugPrintf("sceSslGetSerialNumber() returns 0x%x\n", ret); 109 } 110 else { 111 sboData = (SceUChar8*)malloc(sboLen); 112 if ( sboData != NULL ) { 113 ret = sceSslGetSerialNumber(libsslCtxId, sslCert, sboData, &sboLen); 114 if (ret < 0){ 115 app.DebugPrintf ("sceSslGetSerialNumber() returns 0x%x\n", ret); 116 } 117 else { 118 app.DebugPrintf("Serial number="); 119 for (counter = 0; counter < sboLen; counter++){ 120 app.DebugPrintf("%02X", sboData[counter]); 121 } 122 app.DebugPrintf("\n"); 123 } 124 free(sboData); 125 } 126 } 127} 128 129 130bool SonyHttp_Orbis::getDataFromURL( const char* szURL, void** ppOutData, int* pDataSize) 131{ 132 if(!bInitialised) 133 return false; 134 return http_get(szURL, ppOutData, pDataSize); 135} 136 137 138SceInt32 SonyHttp_Orbis::sslCallback(int libsslCtxId,unsigned int verifyErr,SceSslCert * const sslCert[],int certNum,void *userArg) 139{ 140 SceInt32 i; 141 (void)userArg; 142 143 app.DebugPrintf("Ssl callback:\n"); 144 app.DebugPrintf("\tbase tmpl[%x]\n", (*(SceInt32*)(userArg)) ); 145 146 if (verifyErr != 0){ 147 printSslError((SceInt32)SCE_HTTPS_ERROR_CERT, verifyErr); 148 } 149 for (i = 0; i < certNum; i++){ 150 printSslCertInfo(libsslCtxId,sslCert[i]); 151 } 152 if (verifyErr == 0){ 153 return SCE_OK; 154 } else { 155 return -1; 156 } 157} 158 159bool SonyHttp_Orbis::http_get_close(bool bOK, SceInt32 tmplId, SceInt32 connId, SceInt32 reqId) 160{ 161 SceInt32 ret; 162 if (reqId > 0) 163 { 164 ret = sceHttpDeleteRequest(reqId); 165 assert(ret >= 0); 166 } 167 if (connId > 0) 168 { 169 ret = sceHttpDeleteConnection(connId); 170 assert(ret >= 0); 171 } 172 if (tmplId > 0) 173 { 174 ret = sceHttpDeleteTemplate(tmplId); 175 assert(ret >= 0); 176 } 177 assert(bOK); 178 return bOK; 179} 180 181bool SonyHttp_Orbis::http_get(const char *targetUrl, void** ppOutData, int* pDataSize) 182{ 183 SceInt32 ret, tmplId=0, connId=0, reqId=0, statusCode; 184 SceULong64 contentLength=0; 185 SceBool finFlag=SCE_FALSE; 186 SceUChar8* recvBuf; 187 188 ret = sceHttpCreateTemplate(libhttpCtxId, TEST_USER_AGENT, SCE_HTTP_VERSION_1_1, SCE_TRUE); 189 if (ret < 0) 190 { 191 app.DebugPrintf("sceHttpCreateTemplate() error: 0x%08X\n", ret); 192 return http_get_close(false, tmplId, connId, reqId); 193 } 194 tmplId = ret; 195 196 /* Perform http_get without server verification */ 197 ret = sceHttpsDisableOption(tmplId,SCE_HTTPS_FLAG_SERVER_VERIFY); 198 if (ret < 0) 199 { 200 app.DebugPrintf("sceHttpsDisableOption() error: 0x%08X\n", ret); 201 return http_get_close(false, tmplId, connId, reqId); 202 } 203 204 /* Register SSL callback */ 205 ret = sceHttpsSetSslCallback(tmplId, sslCallback, (void*)&tmplId); 206 if (ret < 0) 207 { 208 app.DebugPrintf("sceHttpsSetSslCallback() error: 0x%08X\n", ret); 209 return http_get_close(false, tmplId, connId, reqId); 210 } 211 212 ret = sceHttpCreateConnectionWithURL(tmplId, targetUrl, SCE_TRUE); 213 if (ret < 0) 214 { 215 app.DebugPrintf("sceHttpCreateConnectionWithURL() error: 0x%08X\n", ret); 216 return http_get_close(false, tmplId, connId, reqId); 217 } 218 connId = ret; 219 220 ret = sceHttpCreateRequestWithURL(connId, SCE_HTTP_METHOD_GET, targetUrl, 0); 221 if (ret < 0) 222 { 223 app.DebugPrintf("sceHttpCreateRequestWithURL() error: 0x%08X\n", ret); 224 return http_get_close(false, tmplId, connId, reqId); 225 } 226 reqId = ret; 227 228 ret = sceHttpSendRequest(reqId, NULL, 0); 229 if (ret < 0) 230 { 231 app.DebugPrintf("sceHttpSendRequest() error: 0x%08X\n", ret); 232 return http_get_close(false, tmplId, connId, reqId); 233 } 234 235 ret = sceHttpGetStatusCode(reqId, &statusCode); 236 if (ret < 0) 237 { 238 app.DebugPrintf("sceHttpGetStatusCode() error: 0x%08X\n", ret); 239 return http_get_close(false, tmplId, connId, reqId); 240 } 241 app.DebugPrintf("response code = %d\n", statusCode); 242 243 if(statusCode == 200) 244 { 245 int contentLengthType; 246 ret = sceHttpGetResponseContentLength(reqId, &contentLengthType, &contentLength); 247 if(ret < 0) 248 { 249 app.DebugPrintf("sceHttpGetContentLength() error: 0x%08X\n", ret); 250 return http_get_close(false, tmplId, connId, reqId); 251 } 252 else 253 { 254 if (contentLengthType == SCE_HTTP_CONTENTLEN_EXIST) 255 { 256 app.DebugPrintf("Content-Length = %lu\n", contentLength); 257 } 258 } 259 recvBuf = new SceUChar8[contentLength+1]; 260 int bufferLeft = contentLength+1; 261 SceUChar8* pCurrBuffPos = recvBuf; 262 int totalBytesRead = 0; 263 while(finFlag != SCE_TRUE) 264 { 265 ret = sceHttpReadData(reqId, pCurrBuffPos, bufferLeft); 266 if (ret < 0) 267 { 268 app.DebugPrintf("\n sceHttpReadData() failed 0x%08X\n", ret); 269 return http_get_close(false, tmplId, connId, reqId); 270 } 271 else if (ret == 0) 272 { 273 finFlag = SCE_TRUE; 274 } 275 app.DebugPrintf("\n sceHttpReadData() read %d bytes\n", ret); 276 pCurrBuffPos += ret; 277 totalBytesRead += ret; 278 bufferLeft -= ret; 279 } 280 } 281 282 *ppOutData = recvBuf; 283 *pDataSize = contentLength; 284 return http_get_close(true, tmplId, connId, reqId); 285}