summaryrefslogtreecommitdiff
path: root/request.h
blob: a43a44ae21b3d77aa2d62f857640af4ccb2c3440 (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
#ifndef ICCLIENT_REQUEST_H
#define ICCLIENT_REQUEST_H

#if defined __ANDROID__ && defined DEBUG
#include <android/log.h>
#endif
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#ifndef __EMSCRIPTEN__
#include <curl/curl.h>
#endif
#include "icclient/typedefs.h"

struct icclient_request_data {
	size_t num_pairs;
	struct icclient_request_pair {
		const char *key;
		const char *value;
	} pairs[16];
};

#ifdef __EMSCRIPTEN__
extern emscripten_fetch_attr_t attr;
#else
extern CURL *curl;
extern char *server_url;
#endif

inline bool icclient_request_init(const char *url, const char *certificate)
{
#ifdef __EMSCRIPTEN__
	emscripten_fetch_attr_init(&attr);
	attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
	return true;
#else
	curl_global_init(CURL_GLOBAL_SSL);
	curl = curl_easy_init();
	if (curl) {
		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
		curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
		if (certificate)
			curl_easy_setopt(curl, CURLOPT_CAINFO, certificate);
#ifdef DEBUG
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
#endif
		size_t length = strlen(url);
		bool append = !(bool)(url[length - 1] == '/');
		server_url = malloc(length + (size_t)append + 1);
		strcpy(server_url, url);
		if (append)
			strcat(server_url, "/");
	}
	return (bool)curl;
#endif
}

inline void request(icclient_handler writefunction, void *writedata, struct icclient_request_data *body, char *fmt, ...)
{
	va_list ap;
	char *p, *sval;
	unsigned int ival;
	size_t length =
#ifndef __EMSCRIPTEN__
		strlen(server_url) +
#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, server_url);
#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__
	attr.onsuccess = writefunction;
	if (writedata)
		attr.userData = writedata;
	strcpy(attr.requestMethod, "GET");
	emscripten_fetch(&attr, url);
#else
	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);
	struct curl_httppost *post, *last = NULL;
	if (body) {
		for (size_t i = 0; i < body->num_pairs; i++) {
			struct icclient_request_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);
#ifdef DEBUG
	CURLcode res =
#endif
		curl_easy_perform(curl);
	if (post)
		curl_formfree(post);
#ifdef DEBUG
	if (res != CURLE_OK) {
		const char *error = curl_easy_strerror(res);
#ifdef __ANDROID__
		__android_log_print(ANDROID_LOG_ERROR, "libicclient.so", "%s: %s", __func__, error);
#else
		fprintf(stderr, "%s: %s\n", __func__, error);
#endif
#endif
	}
#endif
}

inline void icclient_request_cleanup()
{
#ifndef __EMSCRIPTEN__
	if (curl) {
		free(server_url);
		curl_easy_cleanup(curl);
	}
	curl_global_cleanup();
#endif
}

#endif