Advertisement
BaSs_HaXoR

SPRX webRequests (Credits: SONY & BLB)

Aug 15th, 2014
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <cell/http.h>
  5. #include <netex/net.h>
  6.  
  7.  
  8.  
  9. #define HTTP_POOL_SIZE      (64 * 1024)
  10. #define HTTP_POOL_BUFFER 0x2110000            //change this to an address with a free size of 0x10000 bytes ! This one is for ghosts 1.14
  11. static char r_buffer[0x6000];  //MAX is 0x6000
  12.  
  13.  
  14. /*Some function remade due to them making the sprx freeze for some reasons, might fix eventually*/
  15.  
  16. void memCpy(void * destination, const void * source, size_t num){for(int i = 0; i < num; i++){ *((char*)destination+i) = *((char*)source+i); } }
  17. void memFree(char* ptr, int len){       for(int i=0;i < len; i++) { *(char*)(ptr+i) = 0x00;}}
  18. int strCmp(const char* str1, const char* str2)
  19. {
  20.         int diff = 0;
  21.  
  22.         if(*(str1) == 0x00 || *(str2) == 0x00) {return -1;}
  23.  
  24.      for(int i = 0; i < 0x600; i++)
  25.      {
  26.                  if(*(str1+i) == 0x00 || *(str2+i) == 0x00) {break;}
  27.                  if(*(str1+i) != *(str2+i)) {diff++;}
  28.      }
  29.  
  30.          return diff;
  31. }
  32. void SendRequest(char* url,  char* retBuffer, int bufMaxLen) //url = url to request ("http://www.google.com/")    | retBuffer = ptr where the answer will be written to  | bufMaxLen = Max length of the buffer
  33. {
  34.         if(bufMaxLen > 0x6000) {return;} //ERROR, bufMaxLen is TOO BIG
  35.  
  36.                 size_t numBuf = 0;
  37.                 CellHttpClientId client = 0;
  38.                 CellHttpTransId trans = 0;
  39.                 CellHttpUri uri;
  40.                 int ret;
  41.                 bool has_cl = true;
  42.                 uint64_t length = 0;
  43.                 uint64_t recvd;
  44.                 size_t poolSize = 0;
  45.                 void *uriPool = NULL;
  46.                         void *httpPool = NULL;
  47.                         const char *serverName;
  48.                         size_t localRecv = 0;
  49.  
  50.             serverName = url;  //set url
  51.                         memFree(r_buffer, bufMaxLen);
  52.  
  53.                         sys_net_initialize_network(); //init network
  54.  
  55.                         httpPool = (void*)HTTP_POOL_BUFFER; //address to: 0x10000 free bytes
  56.                         if (httpPool == NULL) { ret = -1; goto end; }
  57.  
  58.                         ret = cellHttpInit(httpPool, HTTP_POOL_SIZE);
  59.                         if (ret < 0) { goto end; }
  60.        
  61.                         ret = cellHttpCreateClient(&client);
  62.                         if (ret < 0) { goto end; }
  63.  
  64.                         ret = cellHttpUtilParseUri(NULL, serverName, NULL, 0, &poolSize);
  65.                         if (ret < 0) { goto end; }
  66.        
  67.                         char uriPoolAlloc[0x1024]; //allocate some space for the uri (a bit too much but eh)
  68.                         uriPool = uriPoolAlloc;
  69.                         if (NULL == uriPool) { goto end; } //fail
  70.        
  71.                         ret = cellHttpUtilParseUri(&uri, serverName, uriPool, poolSize, NULL);
  72.                         if (ret < 0) {memFree((char*)uriPool, sizeof(uriPool)); goto end; }  
  73.  
  74.                         ret = cellHttpCreateTransaction(&trans, client, CELL_HTTP_METHOD_GET, &uri);
  75.                         memFree((char*)uriPool, sizeof(uriPool));
  76.                         if (ret < 0) { goto end; }
  77.  
  78.                         ret = cellHttpSendRequest(trans, NULL, 0, NULL); //send it :D
  79.  
  80.                         {//make a new scope for the status
  81.                                 int code = 0;
  82.                                 ret = cellHttpResponseGetStatusCode(trans, &code);
  83.                                 if (ret < 0) { goto end; }
  84.                
  85.                         }//end of status scope
  86.  
  87.                         ret = cellHttpResponseGetContentLength(trans, &length);
  88.                         if (ret < 0) {
  89.                                 if (ret == CELL_HTTP_ERROR_NO_CONTENT_LENGTH) { has_cl = false;}
  90.                             else { goto end; }}
  91.                
  92.                         recvd = 0;
  93.  
  94.                         while ((!has_cl) || (recvd < length))
  95.                         {
  96.                                 ret = cellHttpRecvResponse(trans, r_buffer, bufMaxLen-1, &localRecv);
  97.                                 if (ret < 0) {goto end; } else if (localRecv == 0) break;
  98.                                 recvd += localRecv;
  99.                                 r_buffer[localRecv] = '\0'; //null terminate it
  100.                     }
  101.  
  102.                         ret = 0;
  103.  
  104.                         {for(int i = 0;i<bufMaxLen;i++){retBuffer[i] = r_buffer[i];}}  //OUTPUT
  105.  
  106.  
  107.  
  108.                         /* shutdown procedures */
  109.     end:
  110.         if (trans) {
  111.                 cellHttpDestroyTransaction(trans);
  112.                 trans = 0;
  113.         }
  114.  
  115.         if (client) {cellHttpDestroyClient(client);client = 0;}
  116.        
  117.  
  118.         cellHttpEnd();  //END OF HTTP
  119.  
  120.         if (httpPool) {  memFree((char*)httpPool, sizeof(httpPool)); httpPool = NULL;}
  121.  
  122.  
  123.         sys_net_finalize_network();
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement