timeout for httpc

This commit is contained in:
Sorunome 2019-10-18 21:22:10 +02:00
parent 08ed4523f3
commit 8786f18ced
No known key found for this signature in database
GPG key ID: 63E31F7B5993A9C4
4 changed files with 32 additions and 20 deletions

View file

@ -44,11 +44,11 @@ public:
RequestError lastRequestError; RequestError lastRequestError;
void (* sync_event_callback)(std::string roomId, json_t* event) = 0; void (* sync_event_callback)(std::string roomId, json_t* event) = 0;
void processSync(json_t* sync); void processSync(json_t* sync);
json_t* doSync(std::string token, int timeout); json_t* doSync(std::string token, u32 timeout);
void startSync(); void startSync();
json_t* doRequest(const char* method, std::string path, json_t* body = NULL, int timeout = 5); json_t* doRequest(const char* method, std::string path, json_t* body = NULL, u32 timeout = 5);
json_t* doRequestCurl(const char* method, std::string url, json_t* body, int timeout); json_t* doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout);
json_t* doRequestHttpc(const char* method, std::string url, json_t* body, int timeout); json_t* doRequestHttpc(const char* method, std::string url, json_t* body, u32 timeout);
public: public:
Client(std::string homeserverUrl, std::string matrixToken = "", Store* clientStore = NULL); Client(std::string homeserverUrl, std::string matrixToken = "", Store* clientStore = NULL);
std::string getToken(); std::string getToken();

View file

@ -407,7 +407,7 @@ void Client::processSync(json_t* sync) {
} }
void Client::startSync() { void Client::startSync() {
int timeout = 60; u32 timeout = 60;
while (true) { while (true) {
std::string token = store->getSyncToken(); std::string token = store->getSyncToken();
if (stopSyncing) { if (stopSyncing) {
@ -428,14 +428,14 @@ void Client::startSync() {
} else { } else {
if (lastRequestError == RequestError::timeout) { if (lastRequestError == RequestError::timeout) {
timeout += 10*60; timeout += 10*60;
D printf_top("Timeout reached, increasing it to %d\n", timeout); D printf_top("Timeout reached, increasing it to %lu\n", timeout);
} }
} }
svcSleepThread((u64)1000000ULL * (u64)200); svcSleepThread((u64)1000000ULL * (u64)200);
} }
} }
json_t* Client::doSync(std::string token, int timeout) { json_t* Client::doSync(std::string token, u32 timeout) {
// D printf_top("Doing sync with token %s\n", token.c_str()); // D printf_top("Doing sync with token %s\n", token.c_str());
std::string query = "?full_state=false&timeout=" + std::to_string(SYNC_TIMEOUT); std::string query = "?full_state=false&timeout=" + std::to_string(SYNC_TIMEOUT);
@ -453,11 +453,10 @@ size_t DoRequestWriteCallback(char *contents, size_t size, size_t nmemb, void *u
bool doingCurlRequest = false; bool doingCurlRequest = false;
bool doingHttpcRequest = false; bool doingHttpcRequest = false;
json_t* Client::doRequest(const char* method, std::string path, json_t* body, int timeout) { json_t* Client::doRequest(const char* method, std::string path, json_t* body, u32 timeout) {
std::string url = hsUrl + path; std::string url = hsUrl + path;
requestId++; requestId++;
return doRequestCurl(method, url, body, timeout);
/*
if (!doingCurlRequest) { if (!doingCurlRequest) {
doingCurlRequest = true; doingCurlRequest = true;
json_t* ret = doRequestCurl(method, url, body, timeout); json_t* ret = doRequestCurl(method, url, body, timeout);
@ -471,10 +470,9 @@ json_t* Client::doRequest(const char* method, std::string path, json_t* body, in
} else { } else {
return doRequestCurl(method, url, body, timeout); return doRequestCurl(method, url, body, timeout);
} }
*/
} }
json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body, int timeout) { json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout) {
D printf_top("Opening Request %d with CURL\n%s\n", requestId, url.c_str()); D printf_top("Opening Request %d with CURL\n%s\n", requestId, url.c_str());
if (!SOC_buffer) { if (!SOC_buffer) {
@ -498,9 +496,10 @@ json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body,
if (token != "") { if (token != "") {
headers = curl_slist_append(headers, ("Authorization: Bearer " + token).c_str()); headers = curl_slist_append(headers, ("Authorization: Bearer " + token).c_str());
} }
char* bodyStr = NULL;
if (body) { if (body) {
headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json");
const char* bodyStr = json_dumps(body, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH); bodyStr = json_dumps(body, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, bodyStr); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, bodyStr);
} }
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L); curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
@ -521,6 +520,7 @@ json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body,
// curl_easy_setopt(curl, CURLOPT_STDERR, stdout); // curl_easy_setopt(curl, CURLOPT_STDERR, stdout);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if (bodyStr) free(bodyStr);
if (res != CURLE_OK) { if (res != CURLE_OK) {
D printf_top("curl res not ok %d\n", res); D printf_top("curl res not ok %d\n", res);
if (res == CURLE_OPERATION_TIMEDOUT) { if (res == CURLE_OPERATION_TIMEDOUT) {
@ -539,7 +539,7 @@ json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body,
return content; return content;
} }
json_t* Client::doRequestHttpc(const char* method, std::string url, json_t* body, int timeout) { json_t* Client::doRequestHttpc(const char* method, std::string url, json_t* body, u32 timeout) {
D printf_top("Opening Request %d with HTTPC\n%s\n", requestId, url.c_str()); D printf_top("Opening Request %d with HTTPC\n%s\n", requestId, url.c_str());
if (!HTTPC_inited) { if (!HTTPC_inited) {
@ -577,12 +577,14 @@ json_t* Client::doRequestHttpc(const char* method, std::string url, json_t* body
httpcAddRequestHeaderField(&context, "Authorization", ("Bearer " + token).c_str()); httpcAddRequestHeaderField(&context, "Authorization", ("Bearer " + token).c_str());
} }
httpcAddRequestHeaderField(&context, "Connection", "Keep-Alive"); httpcAddRequestHeaderField(&context, "Connection", "Keep-Alive");
char* bodyStr = NULL;
if (body) { if (body) {
httpcAddRequestHeaderField(&context, "Content-Type", "application/json"); httpcAddRequestHeaderField(&context, "Content-Type", "application/json");
const char* bodyStr = json_dumps(body, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH); bodyStr = json_dumps(body, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH);
httpcAddPostDataRaw(&context, (u32*)bodyStr, strlen(bodyStr)); httpcAddPostDataRaw(&context, (u32*)bodyStr, strlen(bodyStr));
} }
ret = httpcBeginRequest(&context); ret = httpcBeginRequest(&context);
if (bodyStr) free(bodyStr);
if (ret) { if (ret) {
D printf_top("Failed to perform request %ld\n", ret); D printf_top("Failed to perform request %ld\n", ret);
httpcCloseContext(&context); httpcCloseContext(&context);
@ -595,7 +597,6 @@ json_t* Client::doRequestHttpc(const char* method, std::string url, json_t* body
url = std::string(newUrl); url = std::string(newUrl);
} }
} while ((statusCode >= 301 && statusCode <= 303) || (statusCode >= 307 && statusCode <= 308)); } while ((statusCode >= 301 && statusCode <= 303) || (statusCode >= 307 && statusCode <= 308));
D printf_top("Request http thingy successful\n");
ret = httpcGetDownloadSizeState(&context, NULL, &contentSize); ret = httpcGetDownloadSizeState(&context, NULL, &contentSize);
if (ret != 0) { if (ret != 0) {
httpcCloseContext(&context); httpcCloseContext(&context);
@ -609,10 +610,14 @@ json_t* Client::doRequestHttpc(const char* method, std::string url, json_t* body
return NULL; return NULL;
} }
u64 timeoutReal = timeout * 1000000000ULL;
do { do {
// This download loop resizes the buffer as data is read. // This download loop resizes the buffer as data is read.
ret = httpcDownloadData(&context, buf+size, 0x1000, &readsize); u64 timeStartMs = osGetTime();
size += readsize; ret = httpcDownloadDataTimeout(&context, buf+size, 0x1000, &readsize, timeoutReal);
u64 timeDifMs = osGetTime() - timeStartMs;
timeoutReal -= timeDifMs*1000000ULL;
size += readsize;
if (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING) { if (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING) {
lastbuf = buf; // Save the old pointer, in case realloc() fails. lastbuf = buf; // Save the old pointer, in case realloc() fails.
buf = (u8*)realloc(buf, size + 0x1000); buf = (u8*)realloc(buf, size + 0x1000);
@ -625,6 +630,10 @@ json_t* Client::doRequestHttpc(const char* method, std::string url, json_t* body
} while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING); } while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING);
if (ret) { if (ret) {
D printf_top("httpc res not ok %lu\n", ret);
// let's just assume it was a timeout...
// TODO: better detection
lastRequestError = RequestError::timeout;
httpcCloseContext(&context); httpcCloseContext(&context);
free(buf); free(buf);
return NULL; return NULL;

View file

@ -1,6 +1,7 @@
#include "util.h" #include "util.h"
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <3ds.h>
// from http://www.zedwood.com/article/cpp-urlencode-function // from http://www.zedwood.com/article/cpp-urlencode-function
std::string urlencode(std::string s) { std::string urlencode(std::string s) {
@ -22,7 +23,7 @@ std::string urlencode(std::string s) {
} }
return e.str(); return e.str();
} }
/*
Result httpcDownloadDataTimeout(httpcContext *context, u8* buffer, u32 size, u32 *downloadedsize, u64 timeout) Result httpcDownloadDataTimeout(httpcContext *context, u8* buffer, u32 size, u32 *downloadedsize, u64 timeout)
{ {
Result ret=0; Result ret=0;
@ -52,4 +53,3 @@ Result httpcDownloadDataTimeout(httpcContext *context, u8* buffer, u32 size, u32
return dlret; return dlret;
} }
*/

View file

@ -2,7 +2,10 @@
#define _UTIL_H_ #define _UTIL_H_
#include <string> #include <string>
#include <3ds.h>
std::string urlencode(std::string str); std::string urlencode(std::string str);
Result httpcDownloadDataTimeout(httpcContext *context, u8* buffer, u32 size, u32 *downloadedsize, u64 timeout);
#endif // _UTIL_H_ #endif // _UTIL_H_