From 12cde42c929b63a1ef1b2ad7f3482336419980b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Wed, 2 Feb 2022 09:29:10 +0800 Subject: Asynchronous connection Important updates: 1. Emscripten port. 2. HTTP request code copied from libicclient & slightly fixed. 3. Cookies, for maintaining authorisation between different async handles. --- request.h | 113 +++++++------------------------------------------------------- 1 file changed, 12 insertions(+), 101 deletions(-) (limited to 'request.h') diff --git a/request.h b/request.h index 932f75e..4c559fb 100644 --- a/request.h +++ b/request.h @@ -1,105 +1,16 @@ -#ifndef RTCLIENT_REQUEST_H -#define RTCLIENT_REQUEST_H +#ifndef REQUEST_H +#define REQUEST_H -#if defined(ANDROID) && defined(DEBUG) -#include -#endif -#include -#include -#include -#include - -extern CURL *curl; -extern char *server_url; - -inline void request(size_t (*writefunction)(void *, size_t, size_t, void *) - , void *writedata, struct curl_httppost *post, char *fmt, ...) -{ - va_list ap; - char *p, *sval; - unsigned int uval; - size_t length = strlen(server_url) + strlen(fmt); - - va_start(ap, fmt); - for (p = fmt; *p; p++) { - if (*p != '%') - continue; - switch(*++p) { - case 's': - sval = va_arg(ap, char *); - length += strlen(sval) - 2; - break; - case 'u': - uval = va_arg(ap, unsigned int); - do { - length++; - } while ((uval /= 10)); - length -= 2; - break; - case 'c': - length++; - break; - default: - break; - } - } - va_end(ap); +#include "rtclient/typedefs.h" - char url[length + 1]; - length = 0; - strcpy(url, server_url); +struct body { + size_t num_pairs; + struct pair { + const char *key; + const char *value; + } pairs[20]; +}; - va_start(ap, fmt); - for (p = fmt; *p; p++) { - if (*p != '%') - continue; - switch(*++p) { - case 's': - sval = va_arg(ap, char *); - strcat(url, sval); - length = strlen(url); - break; - case 'u': - uval = va_arg(ap, unsigned int); - sprintf(url, "%s%u", url, uval); - length = strlen(url); - break; - case 'c': - url[length++] = (char)va_arg(ap, int); - url[length] = '\0'; - break; - default: - break; - } - } - va_end(ap); +void request(void (*)(rtclient_response *), void (*)(void *), struct body *, char *, ...); - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunction); - if (writedata) - curl_easy_setopt(curl, CURLOPT_WRITEDATA, writedata); - else - curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); - if (post) - curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); - else - curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); - -#ifdef DEBUG - CURLcode res = -#endif // DEBUG - curl_easy_perform(curl); -#ifdef DEBUG - if (res != CURLE_OK) { - const char *error = curl_easy_strerror(res); -#ifdef ANDROID - __android_log_print(ANDROID_LOG_ERROR, "librtclient", "%s: %s" - , __func__, error); -#else - fprintf(stderr, "%s: %s\n", __func__, error); -#endif // ANDROID - } -#endif // DEBUG -} - -#endif // RTCLIENT_REQUEST_H +#endif -- cgit v1.2.3