summaryrefslogtreecommitdiffabout
path: root/src/io.c
blob: e2a496a7035f9d22e751a4b0908009c019d86ca7 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* This file is part of Eclat.
   Copyright (C) 2012-2014 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 "eclat.h"

extern FILE *xml_dump_file;


static void
dump(const char *text, FILE *stream, unsigned char *ptr, size_t size)
{
	size_t i;
	size_t c;
	unsigned int width = 0x10;
	int hex = debug_level(ECLAT_DEBCAT_CURL) > 2;
		
	if (!hex)
		/* without the hex output, we can fit more on screen */
		width = 0x40;

	fprintf(stream, "%s, %zd bytes (0x%zx)\n", text, size, size);

	for (i = 0; i < size; i += width) {
		fprintf(stream, "%04zx: ", i);

		if (hex) {
			for (c = 0; c < width; c++)
				if (i+c < size)
					fprintf(stream, "%02x ", ptr[i+c]);
				else
					fputs("   ", stream);
		}

		for(c = 0; (c < width) && (i+c < size); c++) {
			/* check for CRLf; if found, skip past and start a
			   new line of output */
			if (!hex && (i + c + 1 < size) &&
			    ptr[i+c] == '\r' && ptr[i+c+1] == '\n') {
				i += (c + 2 -width);
				break;
			}
			fprintf(stream, "%c",
				isprint(ptr[i+c]) ? ptr[i+c] : '.');
			/* check again for CRLF, to avoid an extra \n if
			   it's at width */
			if (!hex && (i + c + 2 < size) &&
			    ptr[i+c+1] == '\r' && ptr[i+c+2] == '\n') {
				i += (c + 3 - width);
				break;
			}
		}
		fputc('\n', stream); /* newline */
	}
	fflush(stream);
}

static int
eclat_trace_fun(CURL *handle, curl_infotype type,
		char *data, size_t size,
		void *userp)
{
/*	struct data *config = (struct data *)userp;*/
	const char *text;

	switch (type) {
	case CURLINFO_TEXT:
		fprintf(stderr, "== Info: %s", data);
	default: /* in case a new one is introduced to shock us */
		return 0;

	case CURLINFO_HEADER_OUT:
		text = "=> Send header";
		break;
	case CURLINFO_DATA_OUT:
		text = "=> Send data";
		break;
	case CURLINFO_SSL_DATA_OUT:
		text = "=> Send SSL data";
		break;
	case CURLINFO_HEADER_IN:
		text = "<= Recv header";
		break;
	case CURLINFO_DATA_IN:
		text = "<= Recv data";
		break;
	case CURLINFO_SSL_DATA_IN:
		text = "<= Recv SSL data";
		break;
	}
	
	dump(text, stderr, (unsigned char *)data, size);
	return 0;
}

static void
dumpxml(void *ptr, size_t realsize)
{
	static int open_failed = 0;

	if (open_failed)
		return;
	if (!xml_dump_file) {
		xml_dump_file = fopen(XML_DUMP_FILE_NAME, "w");
		if (!xml_dump_file) {
			err("cannot open dump file %s: %s",
			    XML_DUMP_FILE_NAME, strerror(errno));
			open_failed = 1;
			return;
		}
	}
	fwrite(ptr, realsize, 1, xml_dump_file);
}

static size_t
write_callback(void *ptr, size_t size, size_t nmemb, void *data)
{
	size_t realsize = size * nmemb;
	XML_Parser parser = data;
	enum XML_Status status;
	int line = XML_GetCurrentLineNumber(parser);
	int column = XML_GetCurrentColumnNumber(parser);

	if (debug_level(ECLAT_DEBCAT_MAIN) >= 10)
		dumpxml(ptr, realsize);

	status = XML_Parse(parser, ptr, realsize, 0);
	if (status == XML_STATUS_ERROR) {
		enum XML_Error error = XML_GetErrorCode(parser);

		line = XML_GetCurrentLineNumber(parser);
		column = XML_GetCurrentColumnNumber(parser);

		/* FIXME: better diagnostics. */
		die(EX_SOFTWARE, "XML parse error at %d:%d: %s",
		    line, column, XML_ErrorString(error));
	}
	return realsize;
}

static int
eclat_io_setup(struct eclat_io *io, int errfatal)
{
	io->curl = curl_easy_init();
	if (!io->curl) {
		if (errfatal)
			die(EX_UNAVAILABLE, "curl_easy_init failed");
		else {
			err("curl_easy_init failed");
			return 1;
		}
	}

	if (debug_level(ECLAT_DEBCAT_CURL)) {
		curl_easy_setopt(io->curl, CURLOPT_VERBOSE, 1L);
		if (debug_level(ECLAT_DEBCAT_CURL) > 1)
			curl_easy_setopt(io->curl, CURLOPT_DEBUGFUNCTION,
					 eclat_trace_fun);
	}
		
	/* Create XML parser */
	io->parser = XML_ParserCreate("UTF-8");
	if (!io->parser) {
		if (errfatal)
			die(EX_SOFTWARE, "cannot create XML parser");
		else {
			err("cannot create XML parser");
			return 1;
		}
	}
	
	XML_SetElementHandler(io->parser,
			      eclat_partial_tree_start_handler,
			      eclat_partial_tree_end_handler);
	XML_SetCharacterDataHandler(io->parser,
				    eclat_partial_tree_data_handler);
	io->part = eclat_partial_tree_create();
	XML_SetUserData(io->parser, io->part);

	curl_easy_setopt(io->curl, CURLOPT_WRITEFUNCTION, write_callback);
	curl_easy_setopt(io->curl, CURLOPT_WRITEDATA, io->parser);
	if (use_ssl) {
		curl_easy_setopt(io->curl, CURLOPT_SSL_VERIFYPEER,
				 (long) ssl_verify_peer);
		if (ssl_verify_peer) {
			if (ssl_ca_file)
				curl_easy_setopt(io->curl, CURLOPT_CAINFO,
						 ssl_ca_file);
			if (ssl_ca_path)
				curl_easy_setopt(io->curl, CURLOPT_CAPATH,
						 ssl_ca_path);
		}
	}
	return 0;
}

void
eclat_io_free(struct eclat_io *io)
{
	if (!io)
		return;
	eclat_io_shutdown(io);
	if (io->part)
		eclat_partial_tree_destroy(io->part);
	if (io->parser)
		XML_ParserFree(io->parser);
	free(io);
}

struct eclat_io *
eclat_io_init(int onerr)
{
	struct eclat_io *io;

	io = grecs_zalloc(sizeof(*io));
	if (eclat_io_setup(io, onerr)) {
		eclat_io_free(io);
		return NULL;
	}
	return io;
}

void
eclat_io_shutdown(struct eclat_io *io)
{
	if (io->curl) {
		curl_easy_cleanup(io->curl);
		io->curl = NULL;
	}
}

struct grecs_node *
eclat_io_finish(struct eclat_io *io)
{
	struct grecs_node *p;
	
	eclat_io_shutdown(io);
	XML_Parse(io->parser, "", 0, 1);
	p = eclat_partial_tree_finish(io->part);
	io->part = NULL;
	return p;
}

Return to:

Send suggestions and report system problems to the System administrator.