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