I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit c28d060d authored by zzz's avatar zzz
Browse files

Update: Use last-modified instead of last-checked for the next

if-modified-since fetch, to fix failing to fetch the latest news
parent bf3fdbb1
No related branches found
No related tags found
No related merge requests found
...@@ -49,6 +49,7 @@ import net.i2p.util.VersionComparator; ...@@ -49,6 +49,7 @@ import net.i2p.util.VersionComparator;
*/ */
class NewsFetcher extends UpdateRunner { class NewsFetcher extends UpdateRunner {
private String _lastModified; private String _lastModified;
private long _newLastModified;
private final File _newsFile; private final File _newsFile;
private final File _tempFile; private final File _tempFile;
/** is the news newer */ /** is the news newer */
...@@ -61,7 +62,7 @@ class NewsFetcher extends UpdateRunner { ...@@ -61,7 +62,7 @@ class NewsFetcher extends UpdateRunner {
super(ctx, mgr, NEWS, uris); super(ctx, mgr, NEWS, uris);
_newsFile = new File(ctx.getRouterDir(), NewsHelper.NEWS_FILE); _newsFile = new File(ctx.getRouterDir(), NewsHelper.NEWS_FILE);
_tempFile = new File(ctx.getTempDir(), "tmp-" + ctx.random().nextLong() + TEMP_NEWS_FILE); _tempFile = new File(ctx.getTempDir(), "tmp-" + ctx.random().nextLong() + TEMP_NEWS_FILE);
long lastMod = NewsHelper.lastChecked(ctx); long lastMod = NewsHelper.lastUpdated(ctx);
if (lastMod > 0) if (lastMod > 0)
_lastModified = RFC822Date.to822Date(lastMod); _lastModified = RFC822Date.to822Date(lastMod);
} }
...@@ -100,11 +101,16 @@ class NewsFetcher extends UpdateRunner { ...@@ -100,11 +101,16 @@ class NewsFetcher extends UpdateRunner {
get = new EepGet(_context, false, null, 0, 0, _tempFile.getAbsolutePath(), newsURL, true, null, _lastModified); get = new EepGet(_context, false, null, 0, 0, _tempFile.getAbsolutePath(), newsURL, true, null, _lastModified);
get.addStatusListener(this); get.addStatusListener(this);
long start = _context.clock().now(); long start = _context.clock().now();
// will be adjusted in headerReceived() below
_newLastModified = start;
if (get.fetch()) { if (get.fetch()) {
int status = get.getStatusCode(); int status = get.getStatusCode();
if (status == 200 || status == 304) { if (status == 200 || status == 304) {
_context.router().saveConfig(NewsHelper.PROP_LAST_CHECKED, Map<String, String> opts = new HashMap<String, String>(2);
Long.toString(start)); opts.put(NewsHelper.PROP_LAST_CHECKED, Long.toString(start));
if (status == 200 && _isNewer)
opts.put(NewsHelper.PROP_LAST_UPDATED, Long.toString(_newLastModified));
_context.router().saveConfig(opts, null);
return; return;
} }
} }
...@@ -329,6 +335,19 @@ class NewsFetcher extends UpdateRunner { ...@@ -329,6 +335,19 @@ class NewsFetcher extends UpdateRunner {
@Override @Override
public void bytesTransferred(long alreadyTransferred, int currentWrite, long bytesTransferred, long bytesRemaining, String url) {} public void bytesTransferred(long alreadyTransferred, int currentWrite, long bytesTransferred, long bytesRemaining, String url) {}
/**
* Overriden to get the last-modified header
*/
@Override
public void headerReceived(String url, int attemptNum, String key, String val) {
if ("Last-Modified".equals(key)) {
long lm = RFC822Date.parse822Date(val);
// _newLastModified was set to start time in fetchNews() above
if (lm > 0 && lm < _newLastModified)
_newLastModified = lm;
}
}
/** /**
* Copies the file from temp dir to the news location, * Copies the file from temp dir to the news location,
* calls checkForUpdates() * calls checkForUpdates()
...@@ -338,8 +357,7 @@ class NewsFetcher extends UpdateRunner { ...@@ -338,8 +357,7 @@ class NewsFetcher extends UpdateRunner {
if (_log.shouldLog(Log.INFO)) if (_log.shouldLog(Log.INFO))
_log.info("News fetched from " + url + " with " + (alreadyTransferred+bytesTransferred)); _log.info("News fetched from " + url + " with " + (alreadyTransferred+bytesTransferred));
long now = _context.clock().now(); if (_tempFile.exists() && _tempFile.length() > 0) {
if (_tempFile.exists()) {
File from; File from;
if (url.endsWith(".su3")) { if (url.endsWith(".su3")) {
try { try {
...@@ -355,8 +373,8 @@ class NewsFetcher extends UpdateRunner { ...@@ -355,8 +373,8 @@ class NewsFetcher extends UpdateRunner {
boolean copied = FileUtil.rename(from, _newsFile); boolean copied = FileUtil.rename(from, _newsFile);
_tempFile.delete(); _tempFile.delete();
if (copied) { if (copied) {
String newVer = Long.toString(now); // this is either the start time or the Last-Modified header
_context.router().saveConfig(NewsHelper.PROP_LAST_UPDATED, newVer); String newVer = Long.toString(_newLastModified);
// fixme su3 version ? but it will be older than file version, which is older than now. // fixme su3 version ? but it will be older than file version, which is older than now.
_mgr.notifyVersionAvailable(this, _currentURI, NEWS, "", HTTP, _mgr.notifyVersionAvailable(this, _currentURI, NEWS, "", HTTP,
null, newVer, ""); null, newVer, "");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment