summaryrefslogtreecommitdiff
path: root/client.c
blob: 2ecfc9deaece4578cc5d7bdf1bc8b4cc902e8376 (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
#include <stdlib.h>
#include <string.h>
#include "request.h"
#include "rtclient.h"

#ifdef __EMSCRIPTEN__
emscripten_fetch_attr_t attr;
#else
char *server_url = NULL;
char *cookies_path = NULL;
char *cainfo = NULL;
#endif

void rtclient_init(const char *url, const char *cookies,
		const char *certificate)
{
#ifdef __EMSCRIPTEN__
	emscripten_fetch_attr_init(&attr);
	attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
#else
	size_t length = strlen(url);
	size_t append = url[length - 1] != '/';
	server_url = malloc(length + append + 1);
	strcpy(server_url, url);
	if (append)
		strcat(server_url, "/");
	cookies_path = malloc(strlen(cookies) + 1);
	strcpy(cookies_path, cookies);
	curl_global_init(CURL_GLOBAL_SSL);
	if (certificate) {
		cainfo = malloc(strlen(certificate) + 1);
		strcpy(cainfo, certificate);
	}
#endif
}

void rtclient_login(const char *name, const char *password,
		void (*handler)(rtclient_response *))
{
	request(handler, NULL, &(struct body){ 2, {
			{ "user", name },
			{ "pass", password }
		}}, "");
}

void rtclient_free_response(rtclient_response *response)
{
#ifdef __EMSCRIPTEN__
	emscripten_fetch_close(response);
#else
	free(response->data);
	curl_easy_cleanup(response->curl);
	free(response);
#endif
}

void rtclient_cleanup()
{
#ifndef __EMSCRIPTEN__
	if (cainfo)
		free(cainfo);
	free(cookies_path);
	free(server_url);
	curl_global_cleanup();
#endif
}