From 4e463d57cecf6aeb41654d53e66833db960eb2c3 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sat, 5 Jul 2014 18:41:09 +0000
Subject: [PATCH]  * Reseed: Send If-Modified-Since in fetches  * EepGet: Fixes
 for adding etag or lastmod headers before fetch

---
 core/java/src/net/i2p/util/EepGet.java        | 48 ++++++++++++-------
 .../i2p/router/networkdb/reseed/Reseeder.java |  9 +++-
 2 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/core/java/src/net/i2p/util/EepGet.java b/core/java/src/net/i2p/util/EepGet.java
index 2f59baeac7..87af550efb 100644
--- a/core/java/src/net/i2p/util/EepGet.java
+++ b/core/java/src/net/i2p/util/EepGet.java
@@ -117,27 +117,34 @@ public class EepGet {
         this(ctx, false, null, -1, numRetries, outputFile, url, allowCaching, null);
     }
 
-    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, String outputFile, String url) {
+    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
+                  int numRetries, String outputFile, String url) {
         this(ctx, shouldProxy, proxyHost, proxyPort, numRetries, outputFile, url, true, null);
     }
 
-    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, String outputFile, String url, String postData) {
+    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
+                  int numRetries, String outputFile, String url, String postData) {
         this(ctx, shouldProxy, proxyHost, proxyPort, numRetries, -1, -1, outputFile, null, url, true, null, postData);
     }
 
-    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, String outputFile, String url, boolean allowCaching, String etag) {
+    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
+                  int numRetries, String outputFile, String url, boolean allowCaching, String etag) {
         this(ctx, shouldProxy, proxyHost, proxyPort, numRetries, -1, -1, outputFile, null, url, allowCaching, etag, null);
     }
 
-    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, String outputFile, String url, boolean allowCaching, String etag, String lastModified) {
+    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
+                  int numRetries, String outputFile, String url, boolean allowCaching, String etag, String lastModified) {
         this(ctx, shouldProxy, proxyHost, proxyPort, numRetries, -1, -1, outputFile, null, url, allowCaching, etag, lastModified, null);
     }
 
-    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, long minSize, long maxSize, String outputFile, OutputStream outputStream, String url, boolean allowCaching, String etag, String postData) {
+    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
+                  int numRetries, long minSize, long maxSize, String outputFile, OutputStream outputStream,
+                  String url, boolean allowCaching, String etag, String postData) {
         this(ctx, shouldProxy, proxyHost, proxyPort, numRetries, minSize, maxSize, outputFile, outputStream, url, allowCaching, etag, null, postData);
     }
 
-    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, long minSize, long maxSize,
+    public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
+                  int numRetries, long minSize, long maxSize,
                   String outputFile, OutputStream outputStream, String url, boolean allowCaching,
                   String etag, String lastModified, String postData) {
         _context = ctx;
@@ -1254,12 +1261,27 @@ public class EepGet {
             buf.append("Cache-control: no-cache\r\n" +
                        "Pragma: no-cache\r\n");
         }
-        if ((_etag != null) && (_alreadyTransferred <= 0)) {
+        boolean uaOverridden = false;
+        boolean etagOverridden = false;
+        boolean lastmodOverridden = false;
+        if (_extraHeaders != null) {
+            for (String hdr : _extraHeaders) {
+                String hlc = hdr.toLowerCase(Locale.US);
+                if (hlc.startsWith("user-agent: "))
+                    uaOverridden = true;
+                else if (hlc.startsWith("if-none-match: "))
+                    etagOverridden = true;
+                else if (hlc.startsWith("if-modified-since: "))
+                    lastmodOverridden = true;
+                buf.append(hdr).append("\r\n");
+            }
+        }
+        if ((_etag != null) && (_alreadyTransferred <= 0) && !etagOverridden) {
             buf.append("If-None-Match: ");
             buf.append(_etag);
             buf.append("\r\n");
         }
-        if ((_lastModified != null) && (_alreadyTransferred <= 0)) {
+        if ((_lastModified != null) && (_alreadyTransferred <= 0) && !lastmodOverridden) {
             buf.append("If-Modified-Since: ");
             buf.append(_lastModified);
             buf.append("\r\n");
@@ -1274,14 +1296,6 @@ public class EepGet {
             (!path.endsWith(".gz")) && (!path.endsWith(".tgz")))
             buf.append("gzip");
         buf.append("\r\n");
-        boolean uaOverridden = false;
-        if (_extraHeaders != null) {
-            for (String hdr : _extraHeaders) {
-                if (hdr.toLowerCase(Locale.US).startsWith("user-agent: "))
-                    uaOverridden = true;
-                buf.append(hdr).append("\r\n");
-            }
-        }
         if(!uaOverridden)
             buf.append("User-Agent: " + USER_AGENT + "\r\n");
         if (_authState != null && _shouldProxy && _authState.authMode != AUTH_MODE.NONE) {
@@ -1366,6 +1380,8 @@ public class EepGet {
      *  Must be called before fetch().
      *  Not supported by EepHead.
      *  As of 0.9.10, If name is User-Agent, this will replace the default User-Agent header.
+     *  As of 0.9.14, If name is If-None-Match or If-Modified-Since,
+     *  this will replace the etag or last-modified value given in the constructor.
      *  Note that headers may be subsequently modified or removed in the I2PTunnel HTTP Client proxy.
      *
      *  @since 0.8.8
diff --git a/router/java/src/net/i2p/router/networkdb/reseed/Reseeder.java b/router/java/src/net/i2p/router/networkdb/reseed/Reseeder.java
index 911fd4365e..af8ba5504f 100644
--- a/router/java/src/net/i2p/router/networkdb/reseed/Reseeder.java
+++ b/router/java/src/net/i2p/router/networkdb/reseed/Reseeder.java
@@ -575,7 +575,6 @@ public class Reseeder {
         /** @return null on error */
         private byte[] readURL(URL url) throws IOException {
             ByteArrayOutputStream baos = new ByteArrayOutputStream(4*1024);
-
             EepGet get;
             boolean ssl = url.toString().startsWith("https");
             if (ssl) {
@@ -603,6 +602,10 @@ public class Reseeder {
                         get.addAuthorization(user, pass);
                 }
             }
+            if (!url.toString().endsWith("/")) {
+                String minLastMod = RFC822Date.to822Date(_context.clock().now() - MAX_FILE_AGE);
+                get.addHeader("If-Modified-Since", minLastMod);
+            }
             get.addStatusListener(ReseedRunner.this);
             if (get.fetch() && get.getStatusCode() == 200)
                 return baos.toByteArray();
@@ -644,6 +647,10 @@ public class Reseeder {
                         get.addAuthorization(user, pass);
                 }
             }
+            if (!url.toString().endsWith("/")) {
+                String minLastMod = RFC822Date.to822Date(_context.clock().now() - MAX_FILE_AGE);
+                get.addHeader("If-Modified-Since", minLastMod);
+            }
             get.addStatusListener(ReseedRunner.this);
             if (get.fetch() && get.getStatusCode() == 200)
                 return out;
-- 
GitLab