curl setup

This commit is contained in:
Sorunome 2019-10-16 21:26:06 +02:00
parent bdedf8d814
commit 7489df7e48
No known key found for this signature in database
GPG key ID: 63E31F7B5993A9C4
2 changed files with 54 additions and 106 deletions

View file

@ -11,7 +11,7 @@ private:
std::string token; std::string token;
public: public:
MatrixClient(std::string homeserverUrl, std::string matrixToken); MatrixClient(std::string homeserverUrl, std::string matrixToken);
Result doRequest(json_t* content, HTTPC_RequestMethod method, std::string path, json_t* body = NULL); Result doRequest(json_t* content, const char* method, std::string path, json_t* body = NULL);
}; };
#endif // _matrixclient_h_ #endif // _matrixclient_h_

View file

@ -3,130 +3,78 @@
#include <stdio.h> #include <stdio.h>
#include <3ds.h> #include <3ds.h>
#include <jansson.h> #include <jansson.h>
#include <malloc.h>
#include <curl/curl.h>
#include <string.h>
#include <sys/socket.h>
#define SOC_ALIGN 0x1000
#define SOC_BUFFERSIZE 0x100000
static u32 *SOC_buffer = NULL;
MatrixClient::MatrixClient(std::string homeserverUrl, std::string matrixToken) { MatrixClient::MatrixClient(std::string homeserverUrl, std::string matrixToken) {
hsUrl = homeserverUrl; hsUrl = homeserverUrl;
token = matrixToken; token = matrixToken;
} }
Result MatrixClient::doRequest(json_t* content, HTTPC_RequestMethod method, std::string path, json_t* body) { size_t DoRequestWriteCallback(char *contents, size_t size, size_t nmemb, void *userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
Result MatrixClient::doRequest(json_t* content, const char* method, std::string path, json_t* body) {
std::string url = hsUrl + path; std::string url = hsUrl + path;
Result ret = 0;
httpcContext context; printf("Opening Request\n");
std::string newUrl = ""; printf(url.c_str());
u32 statusCode = 0; printf("\n");
u32 contentSize = 0, readsize = 0, size = 0;
u8* buf, *lastbuf;
do {
printf("Opening Request\n");
printf(url.c_str());
printf("\n");
ret = httpcOpenContext(&context, method, url.c_str(), 1);
printf("return from httpcOpenContext: %" PRId32 "\n",ret);
ret = httpcSetSSLOpt(&context, SSLCOPT_DisableVerify); if (!SOC_buffer) {
printf("return from httpcSetSSLOpt: %" PRId32 "\n",ret); SOC_buffer = (u32*)memalign(0x1000, 0x100000);
if (!SOC_buffer) {
ret = httpcSetKeepAlive(&context, HTTPC_KEEPALIVE_ENABLED); return -1;
printf("return from httpcSetKeepAlive: %" PRId32 "\n",ret);
// ret = httpcAddRequestHeaderField(&context, "User-Agent", "httpc-example/1.0.0");
// printf("return from httpcAddRequestHeaderField: %" PRId32 "\n",ret);
ret = httpcAddRequestHeaderField(&context, "Authorization", ("Bearer " + token).c_str());
printf("return from httpcAddRequestHeaderField: %" PRId32 "\n",ret);
ret = httpcAddRequestHeaderField(&context, "Connection", "Keep-Alive");
printf("return from httpcAddRequestHeaderField: %" PRId32 "\n",ret);
if (body) {
// we have a body to send!
ret = httpcAddRequestHeaderField(&context, "Content-Type", "application/json");
printf("return from httpcAddRequestHeaderField: %" PRId32 "\n",ret);
} }
if (socInit(SOC_buffer, 0x100000) != 0) {
printf("Do Request\n"); return -1;
ret = httpcBeginRequest(&context);
printf("return from httpcBeginRequest: %" PRId32 "\n",ret);
if (ret != 0) {
printf("PANIC!!!\n");
httpcCloseContext(&context);
return ret;
} }
ret = httpcGetResponseStatusCode(&context, &statusCode);
printf("status code: %" PRId32 "\n", statusCode);
if(ret!=0){
httpcCloseContext(&context);
return ret;
}
if ((statusCode >= 301 && statusCode <= 303) || (statusCode >= 307 && statusCode <= 308)) {
char newUrl[0x100];
ret = httpcGetResponseHeader(&context, "Location", newUrl, 0x100);
url = std::string(newUrl);
}
} while ((statusCode >= 301 && statusCode <= 303) || (statusCode >= 307 && statusCode <= 308));
if (statusCode < 200 || statusCode > 299) {
printf("Non-200 status code: %" PRId32 "\n", statusCode);
httpcCloseContext(&context);
return -2;
} }
ret = httpcGetDownloadSizeState(&context, NULL, &contentSize); CURL* curl = curl_easy_init();
if (ret != 0) { CURLcode res;
httpcCloseContext(&context); if (!curl) {
return ret; printf("curl init failed\n");
}
printf("reported size: %" PRId32 "\n", contentSize);
// Start with a single page buffer
buf = (u8*)malloc(0x1000);
if (buf == NULL) {
httpcCloseContext(&context);
return -1; return -1;
} }
std::string readBuffer;
do { curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
// This download loop resizes the buffer as data is read. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
ret = httpcDownloadData(&context, buf+size, 0x1000, &readsize); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
size += readsize; curl_easy_setopt(curl, CURLOPT_USERAGENT, "3ds");
if (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING) { curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
lastbuf = buf; // Save the old pointer, in case realloc() fails. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
buf = (u8*)realloc(buf, size + 0x1000); curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
if (buf == NULL) { curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
httpcCloseContext(&context); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method);
free(lastbuf); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, DoRequestWriteCallback);
return -1; curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
}
} // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
} while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING); // curl_easy_setopt(curl, CURLOPT_STDERR, stdout);
printf("return from httpcDownloadData: %" PRId32 "\n",ret); res = curl_easy_perform(curl);
if (res != CURLE_OK) {
if (ret != 0) { printf("curl res not ok %" PRId32 "\n", res);
httpcCloseContext(&context); return res;
free(buf);
return -1;
} }
printf("Result successful!\n");
curl_easy_cleanup(curl);
// Resize the buffer back down to our actual final size printf(readBuffer.c_str());
lastbuf = buf;
buf = (u8*)realloc(buf, size);
if (buf == NULL) { // realloc() failed.
printf("WAT?!\n");
httpcCloseContext(&context);
free(lastbuf);
return -1;
}
printf("downloaded size: %" PRId32 "\n",size);
printf((const char*)buf);
httpcCloseContext(&context);
json_error_t error; json_error_t error;
content = json_loads((const char*)buf, 0, &error); content = json_loads(readBuffer.c_str(), 0, &error);
if (!content) { if (!content) {
free(buf);
return -3; return -3;
} }
free(buf);
return 0; return 0;
} }