Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/synchronizer.js

Issue 29606600: Issue 5146 - Implement DownloadableSubscription parsing in C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Nov. 13, 2017, 10:25 p.m.
Right Patch Set: Removed Md5sum and associated code Created Aug. 14, 2018, 12:38 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/subscription/Subscription.h ('k') | test/stub-modules/utils.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 10 matching lines...) Expand all
21 * @fileOverview Manages synchronization of filter subscriptions. 21 * @fileOverview Manages synchronization of filter subscriptions.
22 */ 22 */
23 23
24 const {Downloader, Downloadable, 24 const {Downloader, Downloadable,
25 MILLIS_IN_SECOND, MILLIS_IN_MINUTE, 25 MILLIS_IN_SECOND, MILLIS_IN_MINUTE,
26 MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader"); 26 MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader");
27 const {FilterStorage} = require("filterStorage"); 27 const {FilterStorage} = require("filterStorage");
28 const {FilterNotifier} = require("filterNotifier"); 28 const {FilterNotifier} = require("filterNotifier");
29 const {Prefs} = require("prefs"); 29 const {Prefs} = require("prefs");
30 const {Subscription, DownloadableSubscription} = require("subscriptionClasses"); 30 const {Subscription, DownloadableSubscription} = require("subscriptionClasses");
31 const {Utils} = require("utils");
32 31
33 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; 32 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE;
34 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; 33 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
35 const DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY; 34 const DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY;
36 35
37 /** 36 /**
38 * The object providing actual downloading functionality. 37 * The object providing actual downloading functionality.
39 * @type {Downloader} 38 * @type {Downloader}
40 */ 39 */
41 let downloader = null; 40 let downloader = null;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 144
146 _onDownloadSuccess(downloadable, responseText, errorCallback, 145 _onDownloadSuccess(downloadable, responseText, errorCallback,
147 redirectCallback) 146 redirectCallback)
148 { 147 {
149 let lines = responseText.split(/[\r\n]+/); 148 let lines = responseText.split(/[\r\n]+/);
150 let headerMatch = /\[Adblock(?:\s*Plus\s*([\d.]+)?)?\]/i.exec(lines[0]); 149 let headerMatch = /\[Adblock(?:\s*Plus\s*([\d.]+)?)?\]/i.exec(lines[0]);
151 if (!headerMatch) 150 if (!headerMatch)
152 return errorCallback("synchronize_invalid_data"); 151 return errorCallback("synchronize_invalid_data");
153 let minVersion = headerMatch[1]; 152 let minVersion = headerMatch[1];
154 153
155 let parser = subscription.parseDownload(); 154 let parser = DownloadableSubscription.parseDownload();
156
157 // Don't remove parameter comments immediately but add them to a list first,
158 // they need to be considered in the checksum calculation.
159 for (let i = 0; i < lines.length; i++)
160 {
161 let match = /^\s*!\s*(\w+)\s*:\s*(.*)/.exec(lines[i]);
162 if (match)
163 {
164 let keyword = match[1].toLowerCase();
165 let value = match[2];
166 if (keyword == "checksum")
167 {
168 lines.splice(i--, 1);
169 let checksum = Utils.generateChecksum(lines);
170 if (checksum && checksum != value.replace(/=+$/, ""))
171 return errorCallback("synchronize_checksum_mismatch");
172 }
173 }
174 }
175 155
176 // Process filters 156 // Process filters
177 lines.shift();
178 for (let line of lines) 157 for (let line of lines)
179 parser.process(line); 158 parser.process(line);
hub 2017/11/13 22:35:28 I intend to parse the whole buffer, but for now we
180 159
181 if (parser.redirect) 160 if (parser.redirect)
182 return redirectCallback(parser.redirect); 161 {
hub 2017/11/13 22:35:28 This can be better as we should catch the redirect
162 let {redirect} = parser;
163 parser.delete();
164 return redirectCallback(redirect);
165 }
183 166
184 // Handle redirects 167 // Handle redirects
185 let subscription = Subscription.fromURL(downloadable.redirectURL || 168 let subscription = Subscription.fromURL(downloadable.redirectURL ||
186 downloadable.url); 169 downloadable.url);
187 if (downloadable.redirectURL && 170 if (downloadable.redirectURL &&
188 downloadable.redirectURL != downloadable.url) 171 downloadable.redirectURL != downloadable.url)
189 { 172 {
190 let oldSubscription = Subscription.fromURL(downloadable.url); 173 let oldSubscription = Subscription.fromURL(downloadable.url);
191 subscription.title = oldSubscription.title; 174 subscription.title = oldSubscription.title;
192 subscription.disabled = oldSubscription.disabled; 175 subscription.disabled = oldSubscription.disabled;
193 subscription.lastCheck = oldSubscription.lastCheck; 176 subscription.lastCheck = oldSubscription.lastCheck;
194 177
195 let listed = (oldSubscription.url in FilterStorage.knownSubscriptions); 178 let {listed} = oldSubscription;
196 if (listed) 179 if (listed)
197 FilterStorage.removeSubscription(oldSubscription); 180 FilterStorage.removeSubscription(oldSubscription);
198
199 delete Subscription.knownSubscriptions[oldSubscription.url];
200 181
201 if (listed) 182 if (listed)
202 FilterStorage.addSubscription(subscription); 183 FilterStorage.addSubscription(subscription);
203 } 184 }
204 185
205 // The download actually succeeded 186 // The download actually succeeded
206 subscription.lastSuccess = subscription.lastDownload = Math.round( 187 subscription.lastSuccess = subscription.lastDownload = Math.round(
207 Date.now() / MILLIS_IN_SECOND 188 Date.now() / MILLIS_IN_SECOND
208 ); 189 );
209 subscription.downloadStatus = "synchronize_ok"; 190 subscription.downloadStatus = "synchronize_ok";
(...skipping 16 matching lines...) Expand all
226 if (url && (url.protocol == "http:" || url.protocol == "https:")) 207 if (url && (url.protocol == "http:" || url.protocol == "https:"))
227 subscription.homepage = url.href; 208 subscription.homepage = url.href;
228 } 209 }
229 210
230 if (minVersion) 211 if (minVersion)
231 subscription.requiredVersion = minVersion; 212 subscription.requiredVersion = minVersion;
232 else 213 else
233 delete subscription.requiredVersion; 214 delete subscription.requiredVersion;
234 215
235 let expirationInterval = DEFAULT_EXPIRATION_INTERVAL; 216 let expirationInterval = DEFAULT_EXPIRATION_INTERVAL;
236 let expiration = parser.finalize(); 217 let expiration = parser.finalize(subscription);
237 if (expiration != 0) 218 if (expiration != 0)
238 expirationInterval = expiration; 219 expirationInterval = expiration;
239 220
240 let [ 221 let [
241 softExpiration, 222 softExpiration,
242 hardExpiration 223 hardExpiration
243 ] = downloader.processExpirationInterval(expirationInterval); 224 ] = downloader.processExpirationInterval(expirationInterval);
244 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND); 225 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND);
245 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND); 226 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND);
246 227
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 request.open("GET", fallbackURL); 268 request.open("GET", fallbackURL);
288 request.overrideMimeType("text/plain"); 269 request.overrideMimeType("text/plain");
289 request.channel.loadFlags = request.channel.loadFlags | 270 request.channel.loadFlags = request.channel.loadFlags |
290 request.channel.INHIBIT_CACHING | 271 request.channel.INHIBIT_CACHING |
291 request.channel.VALIDATE_ALWAYS; 272 request.channel.VALIDATE_ALWAYS;
292 request.addEventListener("load", ev => 273 request.addEventListener("load", ev =>
293 { 274 {
294 if (onShutdown.done) 275 if (onShutdown.done)
295 return; 276 return;
296 277
297 if (!(subscription.url in FilterStorage.knownSubscriptions)) 278 if (!subscription.listed)
298 return; 279 return;
299 280
300 let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText); 281 let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText);
301 if (match && match[1] == "301" && // Moved permanently 282 if (match && match[1] == "301" && // Moved permanently
302 match[2] && /^https?:\/\//i.test(match[2])) 283 match[2] && /^https?:\/\//i.test(match[2]))
303 { 284 {
304 redirectCallback(match[2]); 285 redirectCallback(match[2]);
305 } 286 }
306 else if (match && match[1] == "410") // Gone 287 else if (match && match[1] == "410") // Gone
307 { 288 {
308 let data = "[Adblock]\n" + 289 let data = "[Adblock]\n" +
309 subscription.filters.map(f => f.text).join("\n"); 290 Array.from(subscription.filters, f => f.text).join("\n");
310 redirectCallback("data:text/plain," + encodeURIComponent(data)); 291 redirectCallback("data:text/plain," + encodeURIComponent(data));
311 } 292 }
312 }, false); 293 }, false);
313 request.send(null); 294 request.send(null);
314 } 295 }
315 } 296 }
316 } 297 }
317 }; 298 };
318 Synchronizer.init(); 299 Synchronizer.init();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld