-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathrss.c
More file actions
325 lines (247 loc) · 8.56 KB
/
Copy pathrss.c
File metadata and controls
325 lines (247 loc) · 8.56 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
Copyright (C) 2005-2020 Marius L. Jøhndal
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include "channel.h"
#include "htmlent.h"
#include "libxmlutil.h"
#include "rss.h"
#include "urlget.h"
#include "utils.h"
#include <assert.h>
#include <glib/gprintf.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define MRSS_NAMESPACE "http://search.yahoo.com/mrss"
static char *_dup_child_node_value(const xmlNode *node, const gchar *tag)
{
const xmlNode *n;
n = libxmlutil_child_node_by_name(node, NULL, tag);
if (n)
return libxmlutil_dup_value(n);
else
return NULL;
}
static void _item_iterator(const void *user_data, int i, const xmlNode *node)
{
rss_file *f = (rss_file *)user_data;
const xmlNode *encl;
const xmlNode *mrss_content;
const xmlNode *mrss_group;
/* Allocate item structure. */
f->items[i] = (rss_item *)malloc(sizeof(struct _rss_item));
/* Copy item meta-information. */
f->items[i]->title = _dup_child_node_value(node, "title");
f->items[i]->link = _dup_child_node_value(node, "link");
f->items[i]->description = _dup_child_node_value(node, "description");
f->items[i]->pub_date = _dup_child_node_value(node, "pubDate");
/* Look for mrss information first, if there is any. It may be
located either directly under the "item" tag, or inside an mrss
"group" tag. */
mrss_content = libxmlutil_child_node_by_name(node, MRSS_NAMESPACE, "content");
if (!mrss_content) {
mrss_group = libxmlutil_child_node_by_name(node, MRSS_NAMESPACE, "group");
if (mrss_group)
mrss_content =
libxmlutil_child_node_by_name(mrss_group, MRSS_NAMESPACE, "content");
}
/* Figure out if there is an "enclosure" tag here. */
encl = libxmlutil_child_node_by_name(node, NULL, "enclosure");
if (mrss_content || encl) {
f->items[i]->enclosure = (enclosure *)malloc(sizeof(struct _enclosure));
/* Set default values */
f->items[i]->enclosure->url = NULL;
f->items[i]->enclosure->length = 0;
f->items[i]->enclosure->type = NULL;
/* Now read attributes. Prefer mrss over enclosure. */
if (mrss_content) {
f->items[i]->enclosure->url = libxmlutil_dup_attr(mrss_content, "url");
f->items[i]->enclosure->length =
libxmlutil_attr_as_long(mrss_content, "fileSize");
f->items[i]->enclosure->type = libxmlutil_dup_attr(encl, "type");
}
if (encl) {
if (!f->items[i]->enclosure->url)
f->items[i]->enclosure->url = libxmlutil_dup_attr(encl, "url");
if (!f->items[i]->enclosure->length)
f->items[i]->enclosure->length =
libxmlutil_attr_as_long(encl, "length");
if (!f->items[i]->enclosure->type)
f->items[i]->enclosure->type = libxmlutil_dup_attr(encl, "type");
}
/* Clean up garbage values from the feed */
if (f->items[i]->enclosure->length < 0)
f->items[i]->enclosure->length = 0;
} else
f->items[i]->enclosure = NULL;
}
static rss_file *rss_parse(const gchar *url, const xmlNode *root_element,
gchar *fetched_time)
{
const char *version_string;
const xmlNode *channel;
rss_file *f;
enum rss_version version;
/* Do some sanity checking and extract the RSS version number. */
if (strcmp((char *)root_element->name, "rss")) {
fprintf(stderr,
"Error parsing RSS file %s: Unrecognized top-level element %s.\n",
url, (char *)root_element->name);
return NULL;
}
version_string = libxmlutil_attr_as_string(root_element, "version");
if (!strcmp(version_string, "2.0")) {
version = RSS_VERSION_2_0;
} else if (!strcmp(version_string, "0.91")) {
version = RSS_VERSION_0_91;
} else if (!strcmp(version_string, "0.92")) {
version = RSS_VERSION_0_92;
} else {
version = RSS_UNKNOWN;
}
/* Find the channel tag and parse it. */
channel = libxmlutil_child_node_by_name(root_element, NULL, "channel");
if (channel) {
/* Allocate RSS file structure and room for pointers to all entries. */
f = (rss_file *)malloc(sizeof(struct _rss_file));
f->fetched_time = g_strdup(fetched_time);
f->num_items = libxmlutil_count_by_tag_name(channel, "item");
f->items = (rss_item **)malloc(sizeof(rss_item *) * f->num_items);
f->version = version;
/* Copy channel meta-information. */
f->channel_info.title = _dup_child_node_value(channel, "title");
f->channel_info.link = _dup_child_node_value(channel, "link");
f->channel_info.description = _dup_child_node_value(channel, "description");
f->channel_info.language = _dup_child_node_value(channel, "language");
libxmlutil_iterate_by_tag_name(channel, "item", f, _item_iterator);
} else
f = NULL;
return f;
}
static xmlEntityPtr _get_entity(void *ctxt, const xmlChar *name)
{
static GHashTable *html_entities = NULL;
xmlEntityPtr entity;
gchar *contents;
/* Check if entity is any of the predefined entities such as & */
entity = xmlGetPredefinedEntity(name);
if (!entity) {
/* Some of the RSS "specifications" are vague on whether HTML
entities are allowed or not, so we will assume that they are,
and look up HTML entities whenever we encounter them. */
if (!html_entities)
html_entities = htmlent_hash_new();
contents = (gchar *)g_hash_table_lookup(html_entities, name);
if (contents) {
/* TODO: Where do we free this memory? */
entity = (xmlEntityPtr)g_new0(xmlEntity, 1);
entity->type = XML_ENTITY_DECL;
entity->name = name;
entity->orig = (xmlChar *)contents;
entity->content = (xmlChar *)contents;
entity->length = g_utf8_strlen(contents, -1);
entity->etype = XML_INTERNAL_PREDEFINED_ENTITY;
}
}
return entity;
}
rss_file *rss_open_file(const char *filename)
{
xmlParserCtxtPtr ctxt;
xmlDocPtr doc;
rss_file *f;
xmlNode *root_element = NULL;
gchar *fetched_time;
ctxt = xmlNewParserCtxt();
ctxt->sax->getEntity = _get_entity;
doc = xmlSAXParseFile(ctxt->sax, filename, 0);
if (!doc) {
fprintf(stderr, "Error parsing RSS file %s.\n", filename);
xmlFreeParserCtxt(ctxt);
return NULL;
}
root_element = xmlDocGetRootElement(doc);
if (!root_element) {
xmlFreeDoc(doc);
xmlFreeParserCtxt(ctxt);
fprintf(stderr, "Error parsing RSS file %s.\n", filename);
return NULL;
}
/* Establish the time the RSS file was 'fetched'. */
fetched_time = get_rfc822_time();
if (!fetched_time) {
xmlFreeDoc(doc);
xmlFreeParserCtxt(ctxt);
g_fprintf(stderr, "Error retrieving current time.\n");
return NULL;
}
f = rss_parse(filename, root_element, fetched_time);
xmlFreeDoc(doc);
xmlFreeParserCtxt(ctxt);
g_free(fetched_time);
return f;
}
static int _rss_open_url_cb(FILE *f, gpointer user_data, int debug, channel *c)
{
gchar *url = (gchar *)user_data;
return urlget_file(url, f, debug, c);
}
rss_file *rss_open_url(const char *url, int debug, channel *c)
{
rss_file *f;
gchar *rss_filename;
if (write_by_temporary_file(NULL, _rss_open_url_cb, (gpointer)url,
&rss_filename, debug, c))
return NULL;
f = rss_open_file(rss_filename);
unlink(rss_filename);
g_free(rss_filename);
return f;
}
void rss_close(rss_file *f)
{
int i;
rss_item *item;
for (i = 0; i < f->num_items; i++) {
item = f->items[i];
if (item->enclosure) {
if (item->enclosure->url)
free(item->enclosure->url);
if (item->enclosure->type)
free(item->enclosure->type);
free(item->enclosure);
}
if (item->title)
free(item->title);
if (item->pub_date)
free(item->pub_date);
free(item);
}
if (f->channel_info.title)
free(f->channel_info.title);
g_free(f->fetched_time);
free(f);
}
long rss_total_enclosure_size(rss_file *f)
{
int i;
long n = 0;
for (i = 0; i < f->num_items; i++)
if (f->items[i]->enclosure)
n += f->items[i]->enclosure->length;
return n;
}