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: Added md5 checksum. Reorganized some code. Created Nov. 28, 2017, 10:46 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);
180 159
181 if (parser.redirect) 160 if (parser.redirect)
182 { 161 {
183 let {redirect} = parser; 162 let {redirect} = parser;
184 parser.delete(); 163 parser.delete();
185 return redirectCallback(redirect); 164 return redirectCallback(redirect);
186 } 165 }
187 166
188 // Handle redirects 167 // Handle redirects
189 let subscription = Subscription.fromURL(downloadable.redirectURL || 168 let subscription = Subscription.fromURL(downloadable.redirectURL ||
190 downloadable.url); 169 downloadable.url);
191 if (downloadable.redirectURL && 170 if (downloadable.redirectURL &&
192 downloadable.redirectURL != downloadable.url) 171 downloadable.redirectURL != downloadable.url)
193 { 172 {
194 let oldSubscription = Subscription.fromURL(downloadable.url); 173 let oldSubscription = Subscription.fromURL(downloadable.url);
195 subscription.title = oldSubscription.title; 174 subscription.title = oldSubscription.title;
196 subscription.disabled = oldSubscription.disabled; 175 subscription.disabled = oldSubscription.disabled;
197 subscription.lastCheck = oldSubscription.lastCheck; 176 subscription.lastCheck = oldSubscription.lastCheck;
198 177
199 let listed = (oldSubscription.url in FilterStorage.knownSubscriptions); 178 let {listed} = oldSubscription;
200 if (listed) 179 if (listed)
201 FilterStorage.removeSubscription(oldSubscription); 180 FilterStorage.removeSubscription(oldSubscription);
202
203 delete Subscription.knownSubscriptions[oldSubscription.url];
204 181
205 if (listed) 182 if (listed)
206 FilterStorage.addSubscription(subscription); 183 FilterStorage.addSubscription(subscription);
207 } 184 }
208 185
209 // The download actually succeeded 186 // The download actually succeeded
210 subscription.lastSuccess = subscription.lastDownload = Math.round( 187 subscription.lastSuccess = subscription.lastDownload = Math.round(
211 Date.now() / MILLIS_IN_SECOND 188 Date.now() / MILLIS_IN_SECOND
212 ); 189 );
213 subscription.downloadStatus = "synchronize_ok"; 190 subscription.downloadStatus = "synchronize_ok";
(...skipping 16 matching lines...) Expand all
230 if (url && (url.protocol == "http:" || url.protocol == "https:")) 207 if (url && (url.protocol == "http:" || url.protocol == "https:"))
231 subscription.homepage = url.href; 208 subscription.homepage = url.href;
232 } 209 }
233 210
234 if (minVersion) 211 if (minVersion)
235 subscription.requiredVersion = minVersion; 212 subscription.requiredVersion = minVersion;
236 else 213 else
237 delete subscription.requiredVersion; 214 delete subscription.requiredVersion;
238 215
239 let expirationInterval = DEFAULT_EXPIRATION_INTERVAL; 216 let expirationInterval = DEFAULT_EXPIRATION_INTERVAL;
240 let expiration = parser.finalize(); 217 let expiration = parser.finalize(subscription);
241 if (expiration != 0) 218 if (expiration != 0)
242 expirationInterval = expiration; 219 expirationInterval = expiration;
243 220
244 let [ 221 let [
245 softExpiration, 222 softExpiration,
246 hardExpiration 223 hardExpiration
247 ] = downloader.processExpirationInterval(expirationInterval); 224 ] = downloader.processExpirationInterval(expirationInterval);
248 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND); 225 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND);
249 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND); 226 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND);
250 227
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 request.open("GET", fallbackURL); 268 request.open("GET", fallbackURL);
292 request.overrideMimeType("text/plain"); 269 request.overrideMimeType("text/plain");
293 request.channel.loadFlags = request.channel.loadFlags | 270 request.channel.loadFlags = request.channel.loadFlags |
294 request.channel.INHIBIT_CACHING | 271 request.channel.INHIBIT_CACHING |
295 request.channel.VALIDATE_ALWAYS; 272 request.channel.VALIDATE_ALWAYS;
296 request.addEventListener("load", ev => 273 request.addEventListener("load", ev =>
297 { 274 {
298 if (onShutdown.done) 275 if (onShutdown.done)
299 return; 276 return;
300 277
301 if (!(subscription.url in FilterStorage.knownSubscriptions)) 278 if (!subscription.listed)
302 return; 279 return;
303 280
304 let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText); 281 let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText);
305 if (match && match[1] == "301" && // Moved permanently 282 if (match && match[1] == "301" && // Moved permanently
306 match[2] && /^https?:\/\//i.test(match[2])) 283 match[2] && /^https?:\/\//i.test(match[2]))
307 { 284 {
308 redirectCallback(match[2]); 285 redirectCallback(match[2]);
309 } 286 }
310 else if (match && match[1] == "410") // Gone 287 else if (match && match[1] == "410") // Gone
311 { 288 {
312 let data = "[Adblock]\n" + 289 let data = "[Adblock]\n" +
313 subscription.filters.map(f => f.text).join("\n"); 290 Array.from(subscription.filters, f => f.text).join("\n");
314 redirectCallback("data:text/plain," + encodeURIComponent(data)); 291 redirectCallback("data:text/plain," + encodeURIComponent(data));
315 } 292 }
316 }, false); 293 }, false);
317 request.send(null); 294 request.send(null);
318 } 295 }
319 } 296 }
320 } 297 }
321 }; 298 };
322 Synchronizer.init(); 299 Synchronizer.init();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld