summaryrefslogtreecommitdiff
path: root/request.c
blob: 3b1d7155540b8eddc8e9908f42a8c1fbe8b494d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#if defined __ANDROID__ && defined DEBUG
#include <android/log.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "request.h"

#ifdef __EMSCRIPTEN__

extern emscripten_fetch_attr_t attr;

#else

#ifdef HAVE_THREADS_H
#include <threads.h>
#else
#include <pthread.h>
typedef pthread_t thrd_t;
#endif
#include <curl/curl.h>

extern char *sampleurl;
extern char *cainfo;

struct container {
	struct curl_httppost *post;
	void (*handler)(icclient_response *);
	icclient_response *response;
};

static size_t append(char *data, size_t size, size_t nmemb, icclient_response *response)
{
	size_t realsize = size * nmemb;
	response->data = realloc(response->data, response->numBytes + realsize + 1);
	memcpy(&(response->data[response->numBytes]), data, realsize);
	response->numBytes += realsize;
	response->data[response->numBytes] = '\0';
	return realsize;
}

static
#ifdef HAVE_THREADS_H
int
#else
void *
#endif
async(void *arg)
{
#ifdef HAVE_THREADS_H
	int ret = thrd_success;
#endif
	struct container *container = (struct container *)arg;
	CURLcode res = curl_easy_perform(container->response->curl);
	if (container->post)
		curl_formfree(container->post);
	if (res == CURLE_OK && container->handler)
		container->handler(container->response);
	else {
#ifdef HAVE_THREADS_H
		ret = thrd_error;
#endif
#ifdef DEBUG
		const char *error = curl_easy_strerror(res);
#ifdef __ANDROID__
		__android_log_print(ANDROID_LOG_ERROR, "libicclient.so", "%s", error);
#else
		fprintf(stderr, "%s\n", error);
#endif
#endif
	}
	free(container);
#ifdef HAVE_THREADS_H
	return ret;
#else
	return NULL;
#endif
}
#endif

void request(void (*handler)(icclient_response *), void (*callback)(void *), struct body *body, char *fmt, ...)
{
	va_list ap;
	char *p, *sval;
	unsigned int ival;
	size_t length =
#ifndef __EMSCRIPTEN__
		strlen(sampleurl) +
#endif
		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 'd':
				ival = va_arg(ap, unsigned int);
				do {
					length++;
				} while ((ival /= 10));
				length -= 2;
				break;
			default:
				break;
		}
	}
	va_end(ap);

	char url[length + 1];
#ifdef __EMSCRIPTEN__
	memset(url, '\0', length + 1);
#else
	strcpy(url, sampleurl);
#endif

	va_start(ap, fmt);
	for (p = fmt; *p; p++) {
		if (*p != '%')
			continue;
		switch(*++p) {
			case 's':
				sval = va_arg(ap, char *);
				strcat(url, sval);
				break;
			case 'd':
				ival = va_arg(ap, unsigned int);
				sprintf(url, "%s%d", url, ival);
				break;
			default:
				break;
		}
	}
	va_end(ap);

#ifdef __EMSCRIPTEN__
	if (handler)
		attr.onsuccess = handler;
	if (body) {
		size_t length = 0;
		char *post = malloc(1);
		memset(post, '\0', 1);
		for (size_t i = 0; i < body->num_pairs; i++) {
			struct pair pair = body->pairs[i];
			if (!pair.value)
				continue;
			length += strlen(pair.key) + strlen(pair.value) + (i ? 1 : 0) + 1;
			post = realloc(post, length + 1);
			if (i)
				strcat(post, "&");
			sprintf(post, "%s%s=%s", post, pair.key, pair.value);
		}
		strcpy(attr.requestMethod, "POST");
		const char *headers[] = { "Content-Type", "application/x-www-form-urlencoded", NULL };
		attr.requestHeaders = headers;
		attr.requestData = post;
		attr.requestDataSize = length + 1;
		attr.userData = post;
	} else {
		strcpy(attr.requestMethod, "GET");
		attr.userData = callback;
	}
	emscripten_fetch(&attr, url);
#else
	CURL *curl = curl_easy_init();
	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
	curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
	if (cainfo)
		curl_easy_setopt(curl, CURLOPT_CAINFO, cainfo);
#ifdef DEBUG
	curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
#endif
	curl_easy_setopt(curl, CURLOPT_URL, url);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append);
	icclient_response *response = malloc(sizeof(icclient_response));
	response->data = malloc(1);
	response->numBytes = 0;
	response->userData = callback;
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
	struct curl_httppost *post = NULL;
	struct curl_httppost *last = NULL;
	if (body) {
		for (size_t i = 0; i < body->num_pairs; i++) {
			struct pair pair = body->pairs[i];
			if (!pair.value)
				continue;
			curl_formadd(&post, &last, CURLFORM_COPYNAME, pair.key, CURLFORM_PTRCONTENTS, pair.value, CURLFORM_END);
		}
		last = NULL;
		curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
	} else
		curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
	struct container *container = malloc(sizeof(struct container));
	response->curl = curl;
	container->post = post;
	container->handler = handler;
	container->response = response;
	thrd_t thread;
#ifdef HAVE_THREADS_H
	thrd_create(&thread, async, container);
#else
	pthread_create(&thread, NULL, async, container);
#endif
#endif
}