comparison mod_pubsub_feeds/mod_pubsub_feeds.lua @ 5578:b9821e9a21a2

mod_pubsub_feeds: Pass feed data as argument instead of storing on object Feeds can be quite large, why were we keeping them after parsing???
author Kim Alvefur <zash@zash.se>
date Sun, 25 Jun 2023 20:15:44 +0200
parents e7792188540a
children bc292c84f56c
comparison
equal deleted inserted replaced
5577:e7792188540a 5578:b9821e9a21a2
66 feed_list[node] = nil; 66 feed_list[node] = nil;
67 end 67 end
68 end 68 end
69 end 69 end
70 70
71 function update_entry(item) 71 function update_entry(item, data)
72 local node = item.node; 72 local node = item.node;
73 module:log("debug", "parsing %d bytes of data in node %s", #item.data or 0, node) 73 module:log("debug", "parsing %d bytes of data in node %s", #data or 0, node)
74 local feed, err = parse_feed(item.data); 74 local feed, err = parse_feed(data);
75 if not feed then 75 if not feed then
76 module:log("error", "Could not parse feed %q: %s", item.url, err); 76 module:log("error", "Could not parse feed %q: %s", item.url, err);
77 module:log("debug", "Feed data:\n%s\n.", item.data); 77 module:log("debug", "Feed data:\n%s\n.", data);
78 return; 78 return;
79 end 79 end
80 local entries = {}; 80 local entries = {};
81 for entry in feed:childtags("entry") do 81 for entry in feed:childtags("entry") do
82 table.insert(entries, entry); 82 table.insert(entries, entry);
150 end 150 end
151 end 151 end
152 152
153 function fetch(item, callback) -- HTTP Pull 153 function fetch(item, callback) -- HTTP Pull
154 local headers = { }; 154 local headers = { };
155 if item.data and item.etag then 155 if item.etag then
156 headers["If-None-Match"] = item.etag; 156 headers["If-None-Match"] = item.etag;
157 end 157 end
158 http.request(item.url, { headers = headers }, function(data, code, resp) 158 http.request(item.url, { headers = headers }, function(data, code, resp)
159 if code == 200 then 159 if code == 200 then
160 item.data = data; 160 if callback then callback(item, data) end
161 if callback then callback(item) end
162 if resp.headers then 161 if resp.headers then
163 item.etag = resp.headers.etag 162 item.etag = resp.headers.etag
164 end 163 end
165 elseif code == 304 then 164 elseif code == 304 then
166 module:log("debug", "No updates to %q", item.url); 165 module:log("debug", "No updates to %q", item.url);
268 module:log("debug", "Invalid signature, got %s but wanted %s", tostring(signature), tostring(localsig)); 267 module:log("debug", "Invalid signature, got %s but wanted %s", tostring(signature), tostring(localsig));
269 return 401; 268 return 401;
270 end 269 end
271 module:log("debug", "Valid signature"); 270 module:log("debug", "Valid signature");
272 end 271 end
273 feed.data = body; 272 update_entry(feed, body);
274 update_entry(feed);
275 return 202; 273 return 202;
276 end 274 end
277 return 400; 275 return 400;
278 end 276 end
279 return 501; 277 return 501;