curl setup
This commit is contained in:
parent
bdedf8d814
commit
7489df7e48
2 changed files with 54 additions and 106 deletions
|
@ -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_
|
||||||
|
|
|
@ -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;
|
|
||||||
std::string newUrl = "";
|
|
||||||
u32 statusCode = 0;
|
|
||||||
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);
|
printf("Opening Request\n");
|
||||||
printf("return from httpcSetSSLOpt: %" PRId32 "\n",ret);
|
printf(url.c_str());
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
ret = httpcSetKeepAlive(&context, HTTPC_KEEPALIVE_ENABLED);
|
if (!SOC_buffer) {
|
||||||
printf("return from httpcSetKeepAlive: %" PRId32 "\n",ret);
|
SOC_buffer = (u32*)memalign(0x1000, 0x100000);
|
||||||
|
if (!SOC_buffer) {
|
||||||
// ret = httpcAddRequestHeaderField(&context, "User-Agent", "httpc-example/1.0.0");
|
return -1;
|
||||||
// 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;
|
||||||
|
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "3ds");
|
||||||
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, DoRequestWriteCallback);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
||||||
|
|
||||||
do {
|
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||||
// This download loop resizes the buffer as data is read.
|
// curl_easy_setopt(curl, CURLOPT_STDERR, stdout);
|
||||||
ret = httpcDownloadData(&context, buf+size, 0x1000, &readsize);
|
res = curl_easy_perform(curl);
|
||||||
size += readsize;
|
if (res != CURLE_OK) {
|
||||||
if (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING) {
|
printf("curl res not ok %" PRId32 "\n", res);
|
||||||
lastbuf = buf; // Save the old pointer, in case realloc() fails.
|
return res;
|
||||||
buf = (u8*)realloc(buf, size + 0x1000);
|
|
||||||
if (buf == NULL) {
|
|
||||||
httpcCloseContext(&context);
|
|
||||||
free(lastbuf);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING);
|
|
||||||
printf("return from httpcDownloadData: %" PRId32 "\n",ret);
|
|
||||||
|
|
||||||
if (ret != 0) {
|
|
||||||
httpcCloseContext(&context);
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue