Result http_getactual_payloadurl(char *requrl, char *outurl, u32 outurl_maxsize) { Result ret=0; httpcContext context; ret = httpcOpenContext(&context, requrl, 1); if(ret!=0)return ret; ret = httpcAddRequestHeaderField(&context, "User-Agent", "hblauncher_loader/"VERSION); if(ret!=0) { httpcCloseContext(&context); return ret; } ret = httpcBeginRequest(&context); if(ret!=0) { httpcCloseContext(&context); return ret; } ret = httpcGetResponseHeader(&context, "Location", outurl, outurl_maxsize); httpcCloseContext(&context); return 0; }
/*** Close the context. @function :close */ static int httpc_close(lua_State *L) { httpcContext *context = lua_touserdata(L, 1); httpcCloseContext(context); return 0; }
static int lua_downstring(lua_State *L){ int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if (argc != 1) return luaL_error(L, "wrong number of arguments"); #endif const char* url = luaL_checkstring(L,1); httpcContext context; Result ret = httpcOpenContext(&context, (char*)url , 0); #ifndef SKIP_ERROR_HANDLING if(ret==0){ #endif httpcBeginRequest(&context); /*httpcReqStatus loading; httpcGetRequestState(&context, &loading); while (loading == 0x5){ httpcGetRequestState(&context, &loading); }*/ u32 statuscode=0; u32 contentsize=0; httpcGetResponseStatusCode(&context, &statuscode, 0); char text[128]; sprintf(text,"%i",statuscode); if (statuscode != 200) luaL_error(L, text); httpcGetDownloadSizeState(&context, NULL, &contentsize); unsigned char *buffer = (unsigned char*)malloc(contentsize+1); httpcDownloadData(&context, buffer, contentsize, NULL); buffer[contentsize] = 0; lua_pushlstring(L,(const char*)buffer,contentsize); free(buffer); #ifndef SKIP_ERROR_HANDLING }else luaL_error(L, "error opening url"); #endif httpcCloseContext(&context); return 1; }
const char* HttpService::get(char *url) { u32 statuscode = 0; u32 contentsize = 0; u8 *buf; httpcContext context; Result ret = 0; ret = httpcOpenContext(&context, url, 1); ret = httpcBeginRequest(&context); if(ret!=0)return NULL; ret = httpcGetResponseStatusCode(&context, &statuscode, 0); if(ret!=0)return NULL; if(statuscode!=200)return NULL; ret=httpcGetDownloadSizeState(&context, NULL, &contentsize); if(ret!=0)return NULL; buf = (u8*)malloc(contentsize + 1); if(buf==NULL)return NULL; memset(buf, 0, contentsize + 1); ret = httpcDownloadData(&context, buf, contentsize, NULL); if(ret!=0) { free(buf); return NULL; } httpcCloseContext(&context); return (const char*)buf; }
Result DownloadFile_Internal(const char *url, void *out, bool bProgress, void (*write)(void* out, unsigned char* buffer, u32 readSize)) { httpcContext context; u32 fileSize = 0; u32 procSize = 0; Result ret = 0; Result dlret = HTTPC_RESULTCODE_DOWNLOADPENDING; u32 status; u32 bufSize = 0x100000; u32 readSize = 0; httpcOpenContext(&context, HTTPC_METHOD_GET, (char*)url, 1); ret = httpcBeginRequest(&context); if (ret != 0) goto _out; ret = httpcGetResponseStatusCode(&context, &status, 0); if (ret != 0) goto _out; if (status != 200) { ret = status; goto _out; } ret = httpcGetDownloadSizeState(&context, NULL, &fileSize); if (ret != 0) goto _out; { unsigned char *buffer = (unsigned char *)linearAlloc(bufSize); if (buffer == NULL) { printf("Error allocating download buffer\n"); ret = -1; goto _out; } while (dlret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING) { memset(buffer, 0, bufSize); dlret = httpcDownloadData(&context, buffer, bufSize, &readSize); write(out, buffer, readSize); procSize += readSize; if (bProgress) { PrintProgress(fileSize, procSize); } } linearFree(buffer); } _out: httpcCloseContext(&context); return ret; }
static Result action_url_install_open_src(void* data, u32 index, u32* handle) { url_install_data* installData = (url_install_data*) data; Result res = 0; httpcContext* context = (httpcContext*) calloc(1, sizeof(httpcContext)); if(context != NULL) { if(R_SUCCEEDED(res = httpcOpenContext(context, HTTPC_METHOD_GET, installData->urls[index], 1))) { char userAgent[128]; snprintf(userAgent, sizeof(userAgent), "Mozilla/5.0 (Nintendo 3DS; Mobile; rv:10.0) Gecko/20100101 FBI/%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_MICRO); if(R_SUCCEEDED(res = httpcSetSSLOpt(context, SSLCOPT_DisableVerify)) && R_SUCCEEDED(res = httpcAddRequestHeaderField(context, "User-Agent", userAgent)) && R_SUCCEEDED(res = httpcBeginRequest(context)) && R_SUCCEEDED(res = httpcGetResponseStatusCode(context, &installData->responseCode, 0))) { if(installData->responseCode == 200) { *handle = (u32) context; } else if(installData->responseCode == 301 || installData->responseCode == 302 || installData->responseCode == 303) { memset(installData->urls[index], '\0', URL_MAX); if(R_SUCCEEDED(res = httpcGetResponseHeader(context, "Location", installData->urls[index], URL_MAX))) { httpcCloseContext(context); free(context); return action_url_install_open_src(data, index, handle); } } else { res = R_FBI_HTTP_RESPONSE_CODE; } } if(R_FAILED(res)) { httpcCloseContext(context); } } if(R_FAILED(res)) { free(context); } } else { res = R_FBI_OUT_OF_MEMORY; } return res; }
Result get_redirect(char *url, char *out, size_t out_size, char *user_agent) { Result ret; httpcContext context; ret = httpcOpenContext(&context, HTTPC_METHOD_GET, url, 0); if(R_FAILED(ret)) return ret; ret = httpcAddRequestHeaderField(&context, "User-Agent", user_agent); if(R_SUCCEEDED(ret)) ret = httpcBeginRequest(&context); if(R_FAILED(ret)) { httpcCloseContext(&context); return ret; } ret = httpcGetResponseHeader(&context, "Location", out, out_size); httpcCloseContext(&context); return ret; }
int httpGet(const char* url, u8** buf, u32* size) { httpcContext context; CHECK(httpcOpenContext(&context, HTTPC_METHOD_GET, (char*)url, 0), "Could not open HTTP context"); // Add User Agent field (required by Github API calls) CHECK(httpcAddRequestHeaderField(&context, (char*)"User-Agent", (char*)"ARN-UPDATER"), "Could not set User Agent"); CHECK(httpcBeginRequest(&context), "Could not begin request"); // Add root CA required for Github and AWS URLs CHECK(httpcAddTrustedRootCA(&context, digicert_cer, digicert_cer_len), "Could not add Digicert root CA"); CHECK(httpcAddTrustedRootCA(&context, cybertrust_cer, cybertrust_cer_len), "Could not add Cybertrust root CA"); u32 statuscode = 0; CHECK(httpcGetResponseStatusCode(&context, &statuscode, 0), "Could not get status code"); if (statuscode != 200) { // Handle 3xx codes if (statuscode >= 300 && statuscode < 400) { char newUrl[1024]; CHECK(httpcGetResponseHeader(&context, (char*)"Location", newUrl, 1024), "Could not get Location header for 3xx reply"); CHECK(httpcCloseContext(&context), "Could not close HTTP context"); return httpGet(newUrl, buf, size); } throw formatErrMessage("Non-200 status code", statuscode); } CHECK(httpcGetDownloadSizeState(&context, NULL, size), "Could not get file size"); *buf = (u8*)std::malloc(*size); if (*buf == NULL) throw formatErrMessage("Could not allocate enough memory", *size); std::memset(*buf, 0, *size); CHECK(httpcDownloadData(&context, *buf, *size, NULL), "Could not download data"); CHECK(httpcCloseContext(&context), "Could not close HTTP context"); return 1; }
int main() { Result ret=0; httpcContext context; // Initialize services srvInit(); aptInit(); hidInit(NULL); gfxInit(); //gfxSet3D(true); // uncomment if using stereoscopic 3D httpcInit(); ret = httpcOpenContext(&context, "http://10.0.0.3/httpexample_rawimg.bin", 0);//Change this to your own URL. if(ret==0) { ret=http_download(&context); httpcCloseContext(&context); } // Main loop while (aptMainLoop()) { gspWaitForVBlank(); hidScanInput(); // Your code goes here u32 kDown = hidKeysDown(); if (kDown & KEY_START) break; // break in order to return to hbmenu // Flush and swap framebuffers gfxFlushBuffers(); gfxSwapBuffers(); } // Exit services httpcExit(); gfxExit(); hidExit(); aptExit(); srvExit(); return 0; }
static int lua_download(lua_State *L){ int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if (argc != 2) return luaL_error(L, "wrong number of arguments"); #endif const char* url = luaL_checkstring(L,1); const char* file = luaL_checkstring(L,2); httpcContext context; Result ret = httpcOpenContext(&context, (char*)url , 0); #ifndef SKIP_ERROR_HANDLING if(ret==0){ #endif httpcBeginRequest(&context); /*httpcReqStatus loading; httpcGetRequestState(&context, &loading); while (loading == 0x5){ httpcGetRequestState(&context, &loading); }*/ u32 statuscode=0; u32 contentsize=0; httpcGetResponseStatusCode(&context, &statuscode, 0); #ifndef SKIP_ERROR_HANDLING if (statuscode != 200) luaL_error(L, "download request error"); #endif httpcGetDownloadSizeState(&context, NULL, &contentsize); u8* buf = (u8*)malloc(contentsize); memset(buf, 0, contentsize); httpcDownloadData(&context, buf, contentsize, NULL); Handle fileHandle; u32 bytesWritten; FS_Archive sdmcArchive=(FS_Archive){ARCHIVE_SDMC, (FS_Path){PATH_EMPTY, 1, (u8*)""}}; FS_Path filePath=fsMakePath(PATH_ASCII, file); FSUSER_OpenFileDirectly( &fileHandle, sdmcArchive, filePath, FS_OPEN_CREATE|FS_OPEN_WRITE, 0x00000000); FSFILE_Write(fileHandle, &bytesWritten, 0, buf, contentsize,0x10001); FSFILE_Close(fileHandle); svcCloseHandle(fileHandle); free(buf); #ifndef SKIP_ERROR_HANDLING }else luaL_error(L, "error opening url"); #endif httpcCloseContext(&context); return 0; }
static Result action_install_cdn_open_src(void* data, u32 index, u32* handle) { install_cdn_data* installData = (install_cdn_data*) data; Result res = 0; httpcContext* context = (httpcContext*) calloc(1, sizeof(httpcContext)); if(context != NULL) { char url[256]; if(index == 0) { snprintf(url, 256, "http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/%016llX/tmd", installData->ticket->titleId); } else { snprintf(url, 256, "http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/%016llX/%08lX", installData->ticket->titleId, installData->contentIds[index - 1]); } if(R_SUCCEEDED(res = httpcOpenContext(context, HTTPC_METHOD_GET, url, 1))) { httpcSetSSLOpt(context, SSLCOPT_DisableVerify); if(R_SUCCEEDED(res = httpcBeginRequest(context)) && R_SUCCEEDED(res = httpcGetResponseStatusCode(context, &installData->responseCode, 0))) { if(installData->responseCode == 200) { *handle = (u32) context; } else { res = R_FBI_HTTP_RESPONSE_CODE; } } if(R_FAILED(res)) { httpcCloseContext(context); } } if(R_FAILED(res)) { free(context); } } else { res = R_FBI_OUT_OF_MEMORY; } return res; }
Result downloadPage(Handle httpcHandle, char* url, const short* filename) { Result ret; httpcContext context; u32 statuscode, size; ret = httpcOpenContext(httpcHandle, &context, url); if(!ret) { ret = httpcBeginRequest(&context); if(ret) goto exit; ret = httpcGetResponseStatusCode(&context, &statuscode); if(ret || statuscode != 200) goto exit; ret = httpcGetDownloadSizeState(&context, 0, &size); if(ret) goto exit; ret = downloadPageToSDCard(&context, filename, size); exit: httpcCloseContext(httpcHandle, &context); } return ret; }
void downloadfile() { consoleSelect(&topScreen); char *url2 = "http://mabel.nonm.co.uk/woop/download.php"; //char *url2 = url; char *file_name; file_name = strrchr( url2, '/' ) + 1; printf("Downloading %s to %s\n",url2,file_name); gfxFlushBuffers(); ret = httpcOpenContext(&context, url2 , 0); gfxFlushBuffers(); if(ret==0) { ret=http_downloadsave(&context, file_name); gfxFlushBuffers(); httpcCloseContext(&context); } }
int main() { const char build_time[] = __DATE__ " " __TIME__; dlCounter = 0; ret = 0; gfxInitDefault(); consoleInit(GFX_TOP, &topScreen); consoleInit(GFX_BOTTOM, &bottomScreen); consoleSelect(&topScreen); /*printf("%s by %s\n", APPTITLE, APPAUTHOR);*/ printf("Koopa Cruiser by jsa\n"); printf("Version: %s\n", VERSION); //printf("--dev build--\n"); printf("Modified: %s\n", __TIMESTAMP__); printf("Built: %s\n\n", build_time); printf("Press X to save the page\n"); printf("Press START to exit.\n"); printf("Press B to start the swkbd applet\n\n"); gfxFlushBuffers(); httpcInit(); //Change this to your own URL. url = "http://mabel.nonm.co.uk/woop/view.php"; printf("Loading %s\n\n",url); gfxFlushBuffers(); ret = httpcOpenContext(&context, url , 0); //printf("return from httpcOpenContext: %"PRId32"\n",ret); gfxFlushBuffers(); if(ret==0) { ret=http_download(&context); //printf("return from http_download: %"PRId32"\n",ret); gfxFlushBuffers(); httpcCloseContext(&context); } // Main loop while (aptMainLoop()) { gspWaitForVBlank(); hidScanInput(); // Your code goes here u32 kDown = hidKeysDown(); if (kDown & KEY_START) break; // break in order to return to hbmenu if (kDown & KEY_X) { downloadfile(); } /*if (kDown & KEY_B) { printf("Launching swkbd.\n"); if(APT_PrepareToStartLibraryApplet(APPID_MIIVERSE_POSTING)) printf("Preparing to launch applet\n"); Result rc = APT_LaunchLibraryApplet(APPID_MIIVERSE_POSTING, 0, NULL, 0); if (rc) printf("APT_LaunchLibraryApplet: %08lX\n", rc); printf("this is probably broken!\n"); }*/ if(kDown & KEY_B) { printf("woah!\n"); swkbd_Init(); char buf[256]; strcpy(buf,"Hello world!"); } if(kDown & KEY_SELECT) { swkbd_GetStr(buf, 256); printf(buf); printf("\n"); } // Flush and swap framebuffers gfxFlushBuffers(); gfxSwapBuffers(); } // Exit services httpcExit(); gfxExit(); return 0; }
static int lua_downstring(lua_State *L){ int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if (argc < 1 || argc > 4) return luaL_error(L, "wrong number of arguments"); #endif const char* url = luaL_checkstring(L,1); const char* headers = (argc >= 2) ? luaL_checkstring(L,2) : NULL; u8 method = (argc >= 3) ? luaL_checkinteger(L,3) : 0; const char* postdata = (argc >= 4) ? luaL_checkstring(L,4) : NULL; httpcContext context; HTTPC_RequestMethod useMethod = HTTPC_METHOD_GET; if(method <= 3 && method >= 1) useMethod = (HTTPC_RequestMethod)method; u32 statuscode=0; do { if (statuscode >= 301 && statuscode <= 308) { char newurl[4096]; httpcGetResponseHeader(&context, (char*)"Location", &newurl[0], 4096); url = &newurl[0]; httpcCloseContext(&context); } Result ret = httpcOpenContext(&context, useMethod, (char*)url , 0); // Lets just disable SSL verification instead of loading default certs. httpcSetSSLOpt(&context, SSLCOPT_DisableVerify); if(headers != NULL){ char *tokenheader = (char*)malloc(strlen(headers)+1); strcpy(tokenheader, headers); char *toker = tokenheader; char *headername = NULL; char *headervalue = NULL; do { headername = strtok(toker, ":"); if (headername == NULL) break; headervalue = strtok(NULL, "\n"); if (headervalue == NULL) break; if (headervalue[0] == ' ') headervalue++; httpcAddRequestHeaderField(&context, headername, headervalue); toker = NULL; } while (headername != NULL && headervalue != NULL); free(tokenheader); } if (useMethod == HTTPC_METHOD_POST && postdata != NULL) { httpcAddPostDataRaw(&context, (u32*)postdata, strlen(postdata)); } #ifndef SKIP_ERROR_HANDLING if(ret==0){ #endif httpcBeginRequest(&context); long int contentsize=0; // Crash on the += if u32. WTF? u32 readSize=0; httpcGetResponseStatusCode(&context, &statuscode, 0); if (statuscode == 200) { unsigned char *buffer = (unsigned char*)malloc(0x1000); do { ret = httpcDownloadData(&context, buffer+contentsize, 0x1000, &readSize); contentsize += readSize; if (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING) buffer = (unsigned char*)realloc(buffer, contentsize + 0x1000); } while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING); buffer = (unsigned char*)realloc(buffer, contentsize + 1); buffer[contentsize] = 0; lua_pushlstring(L,(const char*)buffer,contentsize); free(buffer); } #ifndef SKIP_ERROR_HANDLING } #endif } while ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308)); #ifndef SKIP_ERROR_HANDLING if ((statuscode < 200 && statuscode > 226) && statuscode != 304) luaL_error(L, "error opening url"); #endif httpcCloseContext(&context); return 1; }
Result http_download_payload(char *url, u32 *payloadsize) { Result ret=0; u32 statuscode=0; u32 contentsize=0; httpcContext context; ret = httpcOpenContext(&context, url, 1); if(ret!=0)return ret; ret = httpcAddRequestHeaderField(&context, "User-Agent", "hblauncher_loader/"VERSION); if(ret!=0) { httpcCloseContext(&context); return ret; } ret = httpcBeginRequest(&context); if(ret!=0) { httpcCloseContext(&context); return ret; } ret = httpcGetResponseStatusCode(&context, &statuscode, 0); if(ret!=0) { httpcCloseContext(&context); return ret; } if(statuscode!=200) { printf("Error: server returned HTTP statuscode %u.\n", (unsigned int)statuscode); httpcCloseContext(&context); return -2; } ret=httpcGetDownloadSizeState(&context, NULL, &contentsize); if(ret!=0) { httpcCloseContext(&context); return ret; } if(contentsize==0 || contentsize>PAYLOAD_TEXTMAXSIZE) { printf("Invalid HTTP content-size: 0x%08x.\n", (unsigned int)contentsize); ret = -3; httpcCloseContext(&context); return ret; } ret = httpcDownloadData(&context, filebuffer, contentsize, NULL); if(ret!=0) { httpcCloseContext(&context); return ret; } httpcCloseContext(&context); *payloadsize = contentsize; return 0; }
Result http_haxx(char *requrl, u8 *cert, u32 certsize, targeturlctx *first_targeturlctx) { Result ret=0; httpcContext context; u32 *linearaddr = NULL; Handle httpheap_sharedmem_handle=0; Handle ropvmem_sharedmem_handle=0; Handle httpc_sslc_handle = 0; u32 i; ret = httpcOpenContext(&context, HTTPC_METHOD_POST, requrl, 1); if(ret!=0)return ret; ret = httpcAddPostDataAscii(&context, "form_name", "form_value"); if(ret!=0) { httpcCloseContext(&context); return ret; } //Locate the physmem for the httpc sharedmem. With the current cmpblock, there can only be one POST struct that was ever written into sharedmem, with the name/value from above. printf("Searching for the httpc sharedmem in physmem...\n"); ret = locate_sharedmem_linearaddr(&linearaddr); if(ret!=0) { printf("Failed to locate the sharedmem in physmem.\n"); httpcCloseContext(&context); return ret; } printf("Writing the haxx to physmem...\n"); ret = writehax_sharedmem_physmem(linearaddr); if(ret!=0) { printf("Failed to setup the haxx.\n"); httpcCloseContext(&context); return ret; } printf("Triggering the haxx...\n"); ret = _httpcCloseContext(&context, &httpheap_sharedmem_handle, &ropvmem_sharedmem_handle, &httpc_sslc_handle); if(R_FAILED(ret)) { printf("httpcCloseContext returned 0x%08x.\n", (unsigned int)ret); return ret; } httpheap_sharedmem = (vu32*)mappableAlloc(httpheap_size); if(httpheap_sharedmem==NULL) { ret = -2; svcCloseHandle(httpheap_sharedmem_handle); svcCloseHandle(ropvmem_sharedmem_handle); svcCloseHandle(httpc_sslc_handle); return ret; } ropvmem_sharedmem = (vu32*)mappableAlloc(ropvmem_size); if(ropvmem_sharedmem==NULL) { ret = -3; mappableFree((void*)httpheap_sharedmem); svcCloseHandle(httpheap_sharedmem_handle); svcCloseHandle(ropvmem_sharedmem_handle); svcCloseHandle(httpc_sslc_handle); return ret; } if(R_FAILED(ret=svcMapMemoryBlock(httpheap_sharedmem_handle, (u32)httpheap_sharedmem, MEMPERM_READ | MEMPERM_WRITE, MEMPERM_READ | MEMPERM_WRITE))) { svcCloseHandle(httpheap_sharedmem_handle); mappableFree((void*)httpheap_sharedmem); httpheap_sharedmem = NULL; svcCloseHandle(ropvmem_sharedmem_handle); mappableFree((void*)ropvmem_sharedmem); ropvmem_sharedmem = NULL; svcCloseHandle(httpc_sslc_handle); printf("svcMapMemoryBlock with the httpheap sharedmem failed: 0x%08x.\n", (unsigned int)ret); return ret; } if(R_FAILED(ret=svcMapMemoryBlock(ropvmem_sharedmem_handle, (u32)ropvmem_sharedmem, MEMPERM_READ | MEMPERM_WRITE, MEMPERM_READ | MEMPERM_WRITE))) { svcUnmapMemoryBlock(httpheap_sharedmem_handle, (u32)httpheap_sharedmem); svcCloseHandle(httpheap_sharedmem_handle); mappableFree((void*)httpheap_sharedmem); httpheap_sharedmem = NULL; svcCloseHandle(ropvmem_sharedmem_handle); mappableFree((void*)ropvmem_sharedmem); ropvmem_sharedmem = NULL; svcCloseHandle(httpc_sslc_handle); printf("svcMapMemoryBlock with the ropvmem sharedmem failed: 0x%08x.\n", (unsigned int)ret); return ret; } printf("Finishing haxx setup with sysmodule memory...\n"); ret = setuphaxx_httpheap_sharedmem(first_targeturlctx); if(R_FAILED(ret)) { printf("Failed to finish haxx setup: 0x%08x.\n", (unsigned int)ret); } else { printf("Finalizing...\n"); } svcUnmapMemoryBlock(httpheap_sharedmem_handle, (u32)httpheap_sharedmem); svcCloseHandle(httpheap_sharedmem_handle); mappableFree((void*)httpheap_sharedmem); httpheap_sharedmem = NULL; svcUnmapMemoryBlock(ropvmem_sharedmem_handle, (u32)ropvmem_sharedmem); svcCloseHandle(ropvmem_sharedmem_handle); mappableFree((void*)ropvmem_sharedmem); ropvmem_sharedmem = NULL; if(R_FAILED(ret)) { svcCloseHandle(httpc_sslc_handle); return ret; } printf("Running setup with sslc...\n"); ret = setuphax_http_sslc(httpc_sslc_handle, cert, certsize); svcCloseHandle(httpc_sslc_handle);//Normally sslcExit should close this, but close it here too just in case. if(R_FAILED(ret)) { printf("Setup failed with sslc: 0x%08x.\n", (unsigned int)ret); return ret; } printf("Testing httpc with non-targeted URLs...\n"); for(i=0; i<2; i++) { ret = httpcOpenContext(&context, HTTPC_METHOD_POST, requrl, 1); if(R_FAILED(ret)) { printf("httpcOpenContext returned 0x%08x, i=%u.\n", (unsigned int)ret, (unsigned int)i); return ret; } ret = httpcAddRequestHeaderField(&context, "User-Agent", "ctr-httpwn/"VERSION); if(R_FAILED(ret)) { printf("httpcAddRequestHeaderField returned 0x%08x, i=%u.\n", (unsigned int)ret, (unsigned int)i); httpcCloseContext(&context); return ret; } ret = httpcAddPostDataAscii(&context, "form_name", "form_value"); if(R_FAILED(ret)) { printf("httpcAddPostDataAscii returned 0x%08x, i=%u.\n", (unsigned int)ret, (unsigned int)i); httpcCloseContext(&context); return ret; } ret = httpcCloseContext(&context); if(R_FAILED(ret)) { printf("httpcCloseContext returned 0x%08x, i=%u.\n", (unsigned int)ret, (unsigned int)i); return ret; } } return 0; }
static int lua_sendmail(lua_State *L){ //BETA func int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if (argc != 3) return luaL_error(L, "wrong number of arguments"); #endif char* to = (char*)luaL_checkstring(L,1); char* subj = (char*)luaL_checkstring(L,2); char* mex = (char*)luaL_checkstring(L,3); int req_size = 70; int nss = SpaceCounter(subj); int nsm = SpaceCounter(mex); req_size = req_size + nss * 2 + nsm * 2 + strlen(subj) + strlen(mex) + strlen(to); char* url = (char*)malloc(req_size); strcpy(url,"http://rinnegatamante.netsons.org/tmp_mail_lpp_beta.php?t="); strcat(url,to); strcat(url,"&s="); char* subj_p = subj; char* url_p = &url[strlen(url)]; while (*subj_p){ if (subj_p[0] == 0x20){ url_p[0] = '%'; url_p++; url_p[0] = '2'; url_p++; url_p[0] = '0'; }else url_p[0] = subj_p[0]; url_p++; subj_p++; } strcat(url,"&b="); char* mex_p = mex; url_p = &url[strlen(url)]; while (*mex_p){ if (mex_p[0] == 0x20){ url_p[0] = '%'; url_p++; url_p[0] = '2'; url_p++; url_p[0] = '0'; }else url_p[0] = mex_p[0]; url_p++; mex_p++; } url_p[0] = 0; httpcContext context; Result ret = httpcOpenContext(&context, (char*)url , 0); #ifndef SKIP_ERROR_HANDLING if(ret==0){ #endif httpcBeginRequest(&context); HTTPC_RequestStatus loading; httpcGetRequestState(&context, &loading); while (loading == HTTPC_STATUS_REQUEST_IN_PROGRESS){ httpcGetRequestState(&context, &loading); } u32 statuscode=0; u32 contentsize=0; httpcGetResponseStatusCode(&context, &statuscode, 0); if (statuscode != 200) luaL_error(L, "request error"); httpcGetDownloadSizeState(&context, NULL, &contentsize); u8 response; httpcDownloadData(&context, &response, contentsize, NULL); lua_pushboolean(L,response); free(url); #ifndef SKIP_ERROR_HANDLING }else luaL_error(L, "error opening url"); #endif httpcCloseContext(&context); return 1; }
Result download_config(char *url, u8 *cert, u32 certsize, u8 *filebuffer, u32 dlsize, u32 *out_statuscode) { Result ret=0; u32 statuscode=0; httpcContext context; ret = httpcOpenContext(&context, HTTPC_METHOD_GET, url, 1); if(R_FAILED(ret)) { printf("httpcOpenContext returned 0x%08x.\n", (unsigned int)ret); return ret; } ret = httpcAddRequestHeaderField(&context, "User-Agent", "ctr-httpwn/"VERSION); if(R_FAILED(ret)) { printf("httpcAddRequestHeaderField returned 0x%08x.\n", (unsigned int)ret); httpcCloseContext(&context); return ret; } ret = httpcAddTrustedRootCA(&context, cert, certsize); if(R_FAILED(ret)) { printf("httpcAddTrustedRootCA returned 0x%08x.\n", (unsigned int)ret); httpcCloseContext(&context); return ret; } ret = httpcBeginRequest(&context); if(R_FAILED(ret)) { printf("httpcBeginRequest returned 0x%08x.\n", (unsigned int)ret); httpcCloseContext(&context); return ret; } ret = httpcGetResponseStatusCode(&context, &statuscode, 0); if(R_FAILED(ret)) { printf("httpcGetResponseStatusCode returned 0x%08x.\n", (unsigned int)ret); httpcCloseContext(&context); return ret; } if(out_statuscode)*out_statuscode = statuscode; if(statuscode==200 || statuscode==500) { ret = httpcDownloadData(&context, filebuffer, dlsize, NULL); if(ret!=0 && statuscode==200) { printf("httpcDownloadData returned 0x%08x.\n", (unsigned int)ret); httpcCloseContext(&context); return ret; } } ret = httpcCloseContext(&context); if(R_FAILED(ret)) { printf("httpcCloseContext returned 0x%08x.\n", (unsigned int)ret); return ret; } if(statuscode!=200) { printf("Invalid statuscode: %u.\n", (unsigned int)statuscode); return -5; } return 0; }
Result DownloadTitle(std::string titleId, std::string encTitleKey, std::string titleName, std::string region) { // Convert the titleid to a u64 for later use char* nTitleId = parse_string(titleId); u64 uTitleId = u8_to_u64((u8*)nTitleId, BIG_ENDIAN); free (nTitleId); // Wait for wifi to be available u32 wifi = 0; Result ret = 0; Result res = 0; while(R_SUCCEEDED(ret = ACU_GetWifiStatus(&wifi)) && wifi == 0) { hidScanInput(); if (hidKeysDown() & KEY_B) { ret = -1; break; } } if (R_FAILED(ret)) { printf("Unable to access internet.\n"); return ret; } std::string outputDir = "/CIAngel"; if (titleName.length() == 0) { titleName = titleId; } // Include region in filename if (region.length() > 0) { titleName = titleName + " (" + region + ")"; } std::string mode_text; if(config.GetMode() == CConfig::Mode::DOWNLOAD_CIA) { mode_text = "create"; } else if(config.GetMode() == CConfig::Mode::INSTALL_CIA) { mode_text = "install"; } printf("Starting - %s\n", titleName.c_str()); // If in install mode, download/install the SEED entry if (config.GetMode() == CConfig::Mode::INSTALL_CIA) { // Download and install the SEEDDB entry if install mode // Code based on code from FBI: https://github.com/Steveice10/FBI/blob/master/source/core/util.c#L254 // Copyright (C) 2015 Steveice10 u8 seed[16]; static const char* regionStrings[] = {"JP", "US", "GB", "GB", "HK", "KR", "TW"}; u8 region = CFG_REGION_USA; CFGU_GetSystemLanguage(®ion); if(region <= CFG_REGION_TWN) { char url[128]; snprintf(url, 128, SEED_URL "0x%016llX/ext_key?country=%s", uTitleId, regionStrings[region]); httpcContext context; if(R_SUCCEEDED(res = httpcOpenContext(&context, HTTPC_METHOD_GET, url, 1))) { httpcSetSSLOpt(&context, SSLCOPT_DisableVerify); u32 responseCode = 0; if(R_SUCCEEDED(res = httpcBeginRequest(&context)) && R_SUCCEEDED(res = httpcGetResponseStatusCode(&context, &responseCode, 0))) { if(responseCode == 200) { u32 pos = 0; u32 bytesRead = 0; while(pos < sizeof(seed) && (R_SUCCEEDED(res = httpcDownloadData(&context, &seed[pos], sizeof(seed) - pos, &bytesRead)) || (u32)res == HTTPC_RESULTCODE_DOWNLOADPENDING)) { pos += bytesRead; } } else { res = -1; } } httpcCloseContext(&context); } if (R_SUCCEEDED(res)) { res = InstallSeed(uTitleId, seed); if (R_FAILED(res)) { printf("Error installing SEEDDB entry: 0x%lx\n", res); } } } } // Make sure the CIA doesn't already exist std::string cp = outputDir + "/" + titleName + ".cia"; if (config.GetMode() == CConfig::Mode::DOWNLOAD_CIA && FileExists(cp.c_str())) { printf("%s already exists.\n", cp.c_str()); return 0; } std::ofstream ofs; FILE *oh = fopen((outputDir + "/tmp/tmd").c_str(), "wb"); if (!oh) { printf("Error opening %s/tmp/tmd\n", outputDir.c_str()); return -1; } res = DownloadFile((NUS_URL + titleId + "/tmd").c_str(), oh, false); fclose(oh); if (res != 0) { printf("Could not download TMD. Internet/Title ID is OK?\n"); return res; } // Read version std::ifstream tmdfs; tmdfs.open(outputDir + "/tmp/tmd", std::ofstream::out | std::ofstream::in | std::ofstream::binary); char titleVersion[2]; tmdfs.seekg(top+0x9C, std::ios::beg); tmdfs.read(titleVersion, 0x2); tmdfs.close(); CreateTicket(titleId, encTitleKey, titleVersion, outputDir + "/tmp/ticket"); printf("Now %s the CIA...\n", mode_text.c_str()); res = ProcessCIA(outputDir + "/tmp", titleName); if (res != 0) { printf("Could not %s the CIA.\n", mode_text.c_str()); return res; } if (config.GetMode() == CConfig::Mode::DOWNLOAD_CIA) { rename((outputDir + "/tmp/" + titleName + ".cia").c_str(), (outputDir + "/" + titleName + ".cia").c_str()); } printf(" DONE!\n"); return res; }
static int lua_download(lua_State *L){ int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if (argc < 2 || argc > 5) return luaL_error(L, "wrong number of arguments"); #endif const char* url = luaL_checkstring(L,1); const char* file = luaL_checkstring(L,2); const char* headers = (argc >= 3) ? luaL_checkstring(L,3) : NULL; u8 method = (argc >= 4) ? luaL_checkinteger(L,4) : 0; const char* postdata = (argc >= 5) ? luaL_checkstring(L,5) : NULL; httpcContext context; u32 statuscode=0; HTTPC_RequestMethod useMethod = HTTPC_METHOD_GET; if(method <= 3 && method >= 1) useMethod = (HTTPC_RequestMethod)method; do { if (statuscode >= 301 && statuscode <= 308) { char newurl[4096]; httpcGetResponseHeader(&context, (char*)"Location", &newurl[0], 4096); url = &newurl[0]; httpcCloseContext(&context); } Result ret = httpcOpenContext(&context, useMethod, (char*)url, 0); // Just disable SSL verification instead of loading default certs. httpcSetSSLOpt(&context, SSLCOPT_DisableVerify); if(headers != NULL){ char *tokenheader = (char*)malloc(strlen(headers)+1); strcpy(tokenheader, headers); char *toker = tokenheader; char *headername = NULL; char *headervalue = NULL; do { headername = strtok(toker, ":"); if (headername == NULL) break; headervalue = strtok(NULL, "\n"); if (headervalue == NULL) break; if (headervalue[0] == ' ') headervalue++; httpcAddRequestHeaderField(&context, headername, headervalue); toker = NULL; } while (headername != NULL && headervalue != NULL); free(tokenheader); } if (useMethod == HTTPC_METHOD_POST && postdata != NULL) { httpcAddPostDataRaw(&context, (u32*)postdata, strlen(postdata)); } #ifndef SKIP_ERROR_HANDLING if(ret==0){ #endif httpcBeginRequest(&context); u32 contentsize=0; httpcGetResponseStatusCode(&context, &statuscode, 0); if (statuscode == 200){ u32 readSize = 0; long int bytesWritten = 0; u8* buf = (u8*)malloc(0x1000); memset(buf, 0, 0x1000); Handle fileHandle; FS_Archive sdmcArchive=(FS_Archive){ARCHIVE_SDMC, (FS_Path){PATH_EMPTY, 1, (u8*)""}}; FS_Path filePath=fsMakePath(PATH_ASCII, file); FSUSER_OpenFileDirectly( &fileHandle, sdmcArchive, filePath, FS_OPEN_CREATE|FS_OPEN_WRITE, 0x00000000); do { ret = httpcDownloadData(&context, buf, 0x1000, &readSize); FSFILE_Write(fileHandle, NULL, bytesWritten, buf, readSize, 0x10001); bytesWritten += readSize; } while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING); FSFILE_Close(fileHandle); svcCloseHandle(fileHandle); free(buf); } #ifndef SKIP_ERROR_HANDLING } #endif } while ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308)); #ifndef SKIP_ERROR_HANDLING if ((statuscode < 200 && statuscode > 226) && statuscode != 304) luaL_error(L, "error opening url"); #endif httpcCloseContext(&context); lua_pushinteger(L, statuscode); return 1; }
static Result action_install_cdn_close_src(void* data, u32 index, bool succeeded, u32 handle) { return httpcCloseContext((httpcContext*) handle); }