~lattis/muon

ref: f9a8cbad39b665a9496cd3adb4756e85ee5ec97c muon/src/external/curl.c -rw-r--r-- 2.7 KiB
f9a8cbadStone Tickle print stdout/stderr of failing test 8 months ago
                                                                                
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
#include "posix.h"

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <string.h>

#include <curl/curl.h>

#include "external/curl.h"
#include "log.h"
#include "platform/mem.h"

const bool have_curl = true;

static struct {
	bool init;
} fetch_ctx = { 0 };

void
muon_curl_init(void)
{
	if (curl_global_init(CURL_GLOBAL_DEFAULT) == 0) {
		fetch_ctx.init = true;
	}
}

void
muon_curl_deinit(void)
{
	if (!fetch_ctx.init) {
		LOG_E("curl is not initialized");
		return;
	}

	curl_global_cleanup();
	fetch_ctx.init = false;
}

struct write_data_ctx {
	uint8_t *buf;
	uint64_t len, cap;
};

static size_t
write_data(void *src, size_t size, size_t nmemb, void *_ctx)
{
	struct write_data_ctx *ctx = _ctx;
	uint64_t want_to_write = size * nmemb;

	if (want_to_write + ctx->len > ctx->cap) {
		ctx->cap = want_to_write + ctx->len;
		ctx->buf = z_realloc(ctx->buf, ctx->cap);
	}

	memcpy(&ctx->buf[ctx->len], src, want_to_write);
	ctx->len += want_to_write;

	return nmemb;
}

bool
muon_curl_fetch(const char *url, uint8_t **buf, uint64_t *len)
{
	CURL *curl_handle;
	CURLcode err;
	char errbuf[CURL_ERROR_SIZE] = { 0 };

	LOG_I("fetching '%s'", url);

	if (!fetch_ctx.init) {
		LOG_E("curl is not initialized");
		goto err0;
	}

	if (!(curl_handle = curl_easy_init())) {
		LOG_E("failed to get curl handle");
		goto err0;
	}

	if ((err = curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errbuf)) != CURLE_OK) {
		goto err1;
	}

	/* set URL to get here */
	if ((err = curl_easy_setopt(curl_handle, CURLOPT_URL, url)) != CURLE_OK) {
		goto err1;
	}

	/* set URL to get here */
	if ((err = curl_easy_setopt(curl_handle, CURLOPT_URL, url)) != CURLE_OK) {
		goto err1;
	}

	/* Switch on full protocol/debug output while testing */
	if ((err = curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0L)) != CURLE_OK) {
		goto err1;
	}

	/* disable progress meter, set to 0L to enable it */
	if ((err = curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L)) != CURLE_OK) {
		goto err1;
	}

	/* send all data to this function  */
	if ((err = curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data)) != CURLE_OK) {
		goto err1;
	}

	struct write_data_ctx ctx = { 0 };
	/* write the page body to this file handle */
	if ((err = curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &ctx)) != CURLE_OK) {
		goto err1;
	}

	/* get it! */
	if ((err = curl_easy_perform(curl_handle)) != CURLE_OK) {
		goto err1;
	}

	*buf = ctx.buf;
	*len = ctx.len;

	/* cleanup curl stuff */
	curl_easy_cleanup(curl_handle);
	return true;

err1:
	if (*errbuf) {
		LOG_E("curl failed to fetch '%s': %s", url, errbuf);
	} else if (err != CURLE_OK) {
		LOG_E("curl failed to fetch '%s': %s", url, curl_easy_strerror(err));
	} else {
		LOG_E("curl failed to fetch '%s'", url);
	}
	curl_easy_cleanup(curl_handle);
err0:
	return false;
}