-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrss_export.c
320 lines (279 loc) · 7.88 KB
/
rss_export.c
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
/* This file contains features to export site contents through
Netscape's RSS.
Netscape has some documentation up at:
http://my.netscape.com/publish/help/mnn20/quickstart.html
Module written by Martijn van Beers.
*/
#include <string.h>
#include <ctype.h>
#include <apr.h>
#include <apr_strings.h>
#include <httpd.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include "private.h"
#include "buffer.h"
#include "db.h"
#include "req.h"
#include "util.h"
#include "db_xml.h"
#include "xml_util.h"
#include "style.h"
#include "rss_export.h"
/* Set the #ifs to 0 to turn off the HREF stipping actions */
static void
rss_render_from_xml (VirguleReq *vr, int art_num, xmlDoc *doc, xmlNodePtr tree)
{
apr_pool_t *p = vr->r->pool;
xmlNode *root = doc->xmlRootNode;
xmlNodePtr subtree;
char *title;
char *link;
char *pubdate, *tmpdate;
char *raw_description;
char *clean_description;
#if 1
char *tmp1, *tmp2;
#endif
title = virgule_xml_find_child_string (root, "title", "(no title)");
link = apr_psprintf (p, "%s/article/%d.html", vr->priv->base_uri, art_num);
raw_description = virgule_xml_find_child_string (root, "lead", "(no description)");
tmpdate = virgule_xml_find_child_string(root, "date", "(no date)");
pubdate = virgule_render_date (vr, tmpdate, 2);
/* strip anchor tags */
clean_description = apr_pstrdup (p, raw_description);
#if 1
tmp1 = raw_description;
tmp2 = clean_description;
while(*tmp1!=0)
{
if(strncasecmp(tmp1,"<a",2)==0)
{
while(*tmp1!=0&&*tmp1!='>')
tmp1++;
if(*tmp1!=0)
tmp1++;
}
if(strncasecmp(tmp1,"</a>",4)==0)
tmp1+=4;
*tmp2 = *tmp1;
tmp1++;
tmp2++;
}
*tmp2=0;
#endif
subtree = xmlNewTextChild (tree, NULL, (xmlChar *)"title", (xmlChar *)title);
subtree = xmlNewTextChild (tree, NULL, (xmlChar *)"link", (xmlChar *)link);
subtree = xmlNewTextChild (tree, NULL, (xmlChar *)"guid", (xmlChar *)link);
subtree = xmlNewTextChild (tree, NULL, (xmlChar *)"pubDate", (xmlChar *)pubdate);
subtree = xmlNewTextChild (tree, NULL, (xmlChar *)"description", (xmlChar *)clean_description);
}
static void
rss_render (VirguleReq *vr, int art_num, xmlNodePtr tree)
{
apr_pool_t *p = vr->r->pool;
char *key;
xmlDoc *doc;
xmlNodePtr subtree;
key = apr_psprintf (p, "articles/_%d/article.xml", art_num);
doc = virgule_db_xml_get (p, vr->db, key);
if (doc != NULL)
{
subtree = xmlNewChild (tree, NULL, (xmlChar *)"item", NULL);
rss_render_from_xml (vr, art_num, doc, subtree);
}
}
static int
rss_index_serve (VirguleReq *vr)
{
apr_pool_t *p = vr->r->pool;
Buffer *b = vr->b;
xmlDocPtr doc;
xmlNodePtr tree, subtree;
int art_num;
int n_arts;
xmlChar *mem;
int size;
char *pubdate;
doc = xmlNewDoc ((xmlChar *)"1.0");
if (doc == NULL)
return virgule_send_error_page (vr, vERROR, "internal", "xmlNewDoc() failed");
vr->r->content_type = "text/xml; charset=UTF-8";
doc->xmlRootNode = xmlNewDocNode (doc, NULL, (xmlChar *)"rss", NULL);
xmlSetProp (doc->xmlRootNode, (xmlChar *)"version", (xmlChar *)"2.0");
tree = xmlNewChild (doc->xmlRootNode, NULL, (xmlChar *)"channel", NULL);
subtree = xmlNewTextChild (tree, NULL, (xmlChar *)"title", (xmlChar *)vr->priv->site_name);
subtree = xmlNewTextChild (tree, NULL, (xmlChar *)"link",
(xmlChar *)apr_psprintf (p, "%s/", vr->priv->base_uri));
subtree = xmlNewTextChild (tree, NULL, (xmlChar *)"description",
(xmlChar *)apr_psprintf (p, "Recent %s articles", vr->priv->site_name));
subtree = xmlNewChild (tree, NULL, (xmlChar *)"language", (xmlChar *)"en-us");
subtree = xmlNewChild (tree, NULL, (xmlChar *)"generator", (xmlChar *)"mod_virgule");
pubdate = virgule_render_date (vr, virgule_iso_now(vr->r->pool), 2);
subtree = xmlNewTextChild (tree, NULL, (xmlChar *)"pubDate", (xmlChar *)pubdate);
art_num = virgule_db_dir_max (vr->db, "articles");
for (n_arts = 0; n_arts < 15 && art_num >= 0; n_arts++)
{
rss_render (vr, art_num, tree);
art_num--;
}
xmlDocDumpFormatMemory (doc, &mem, &size, 1);
if (size <= 0)
return virgule_send_error_page (vr, vERROR, "internal", "xmlDocDumpFormatMemory() failed");
virgule_buffer_write (b, (char *)mem, size);
xmlFree (mem);
xmlFreeDoc (doc);
return virgule_send_response (vr);
}
int
virgule_rss_serve (VirguleReq *vr)
{
const char *p;
if ((p = virgule_match_prefix (vr->uri, "/rss/")) == NULL)
return DECLINED;
if (!strcmp (p, "articles.xml") || !strcmp (p, "articles-2.0.xml"))
return rss_index_serve (vr);
return DECLINED;
}
static int
rss_massage_parse_attr (const char *text, int offs[4])
{
int i = 0;
offs[0] = 0;
offs[1] = 0;
offs[2] = 0;
offs[3] = 0;
while (isspace(text[i])) i++;
if (text[i] == 0 || text[i] == '/' || text[i] == '>')
return i;
offs[0] = i;
while (isalnum(text[i])) i++;
offs[1] = i;
while (isspace(text[i])) i++;
if (text[i] != '=')
return i;
i++;
while (isspace(text[i])) i++;
if (text[i] == '"')
{
i++;
offs[2] = i;
while (text[i] && text[i] != '"') i++;
offs[3] = i;
if (text[i] == '"') i++;
}
else
{
offs[2] = i;
while (isalnum(text[i])) i++;
offs[3] = i;
}
return i;
}
static void
rss_massage_write_attr (Buffer *b, const char *val, int size)
{
int i, last = 0;
char *repl;
char c;
for (i = 0; i < size; i++)
{
if ((c = val[i]) == '&') repl = "&";
else if (c == '<') repl = "<";
else if (c == '>') repl = ">";
else if (c == '"') repl = """;
else continue;
if (i > last)
virgule_buffer_write (b, val + last, i - last);
virgule_buffer_puts (b, repl);
last = i + 1;
}
if (i > last)
virgule_buffer_write (b, val + last, i - last);
}
static void
rss_massage_write_url (Buffer *b, const char *url, int urlsize,
const char *baseurl)
{
if (url[0] == '/')
{
int i;
for (i = 0; baseurl[i] && baseurl[i] != '/'; i++);
if (baseurl[i] == '/' && baseurl[i + 1] == '/')
{
for (i += 2; baseurl[i] && baseurl[i] != '/'; i++);
virgule_buffer_write (b, baseurl, i);
}
}
rss_massage_write_attr (b, url, urlsize);
}
static int
rss_massage_tag (Buffer *b, const char *text, const char *baseurl)
{
int i, next;
if (!memcmp(text, "<a ", 3))
{
virgule_buffer_puts (b, "<a ");
for (i = 3; text[i] && text[i] != '>' && text[i] != '/'; i = next)
{
int offs[4];
next = i + rss_massage_parse_attr (text + i, offs);
if (offs[0] == offs[1]) break;
virgule_buffer_write (b, text + i + offs[0], offs[1] - offs[0]);
if (offs[2])
{
virgule_buffer_puts (b, "="");
if (offs[1] - offs[0] == 4 && !memcmp (text + i + offs[0], "href", 4))
rss_massage_write_url (b, text + i + offs[2], offs[3] - offs[2], baseurl);
else
rss_massage_write_attr (b, text + i + offs[2], offs[3] - offs[2]);
virgule_buffer_puts (b, """);
}
virgule_buffer_puts (b, " ");
}
return i;
}
else
{
virgule_buffer_puts (b, "<");
return 1;
}
}
/**
* rss_massage_text: Massage description into valid RSS.
*
* There are two primary functions here: first, convert valid HTML
* references such as " " into XML, and second, convert all
* relative URL's into absolute.
**/
char *
virgule_rss_massage_text (apr_pool_t *p, const char *text, const char *baseurl)
{
Buffer *b = virgule_buffer_new (p);
int i, end;
char c;
for (i = 0; text[i]; i = end)
{
for (end = i;
(c = text[end]) &&
c != '&' && c != '<' && c != '>';
end++);
if (end > i)
virgule_buffer_write (b, text + i, end - i);
if (c == '&')
{
virgule_buffer_puts (b, "&");
end++;
}
else if (c == '<')
{
end += rss_massage_tag (b, text + end, baseurl);
}
else if (c == '>')
{
virgule_buffer_puts (b, ">");
end++;
}
}
return virgule_buffer_extract (b);
}