summaryrefslogtreecommitdiffabout
path: root/lib/istore.c
blob: f8244b82d1c8a5d7189a40378c981b7311c3bda3 (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
/* This file is part of Eclat.
   Copyright (C) 2012-2018 Sergey Poznyakoff.
 
   Eclat is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Eclat is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include "libeclat.h"
#include <sysexits.h>

static size_t
acc_cb(void *ptr, size_t size, size_t nmemb, void *data)
{
	size_t realsize = size * nmemb;
	struct grecs_txtacc *acc = data;
	grecs_txtacc_grow(acc, ptr, realsize);
	return realsize;
}

CURL *
instance_store_curl_new(struct grecs_txtacc *acc)
{
	CURL *curl;
	
	curl = curl_easy_init();
	if (!curl)
		die(EX_UNAVAILABLE, "curl_easy_init failed");
	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, acc_cb);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, acc);
	return curl;
}

int
instance_store_read(const char *url, CURL *curl)
{
	CURLcode res;
	long http_resp;
	
	curl_easy_setopt(curl, CURLOPT_URL, url);
		
	res = curl_easy_perform(curl);
	if (res != CURLE_OK)
		die(EX_UNAVAILABLE, "CURL: %s", curl_easy_strerror(res));

	res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
	if (res != CURLE_OK)
		die(EX_UNAVAILABLE, "CURL: %s", curl_easy_strerror(res));
	
	switch (http_resp) {
	case 200:
		break;

	case 404:
		return -1;

	default:
		die(EX_UNAVAILABLE, "CURL: got response %3d, url %s",
		    http_resp, url);
	}
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.