From 8cb503d8bb4baf1cd542577573e73c821fb6ec49 Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Sun, 15 Dec 2013 15:16:54 +0000 Subject: [PATCH] EepGet: Allow override of the User-Agent i2psnark: Set User-Agent --- .../java/src/org/klomp/snark/I2PSnarkUtil.java | 3 +++ .../java/src/org/klomp/snark/web/FetchAndAdd.java | 2 ++ .../net/i2p/client/streaming/I2PSocketEepGet.java | 7 ++++++- core/java/src/net/i2p/util/EepGet.java | 14 +++++++++++--- core/java/src/net/i2p/util/PartialEepGet.java | 11 ++++++++--- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java b/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java index 36de31ba40..d1038534f9 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java +++ b/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java @@ -76,6 +76,7 @@ public class I2PSnarkUtil { public static final int MAX_CONNECTIONS = 16; // per torrent public static final String PROP_MAX_BW = "i2cp.outboundBytesPerSecond"; public static final boolean DEFAULT_USE_DHT = true; + public static final String EEPGET_USER_AGENT = "I2PSnark"; public I2PSnarkUtil(I2PAppContext ctx) { this(ctx, "i2psnark"); @@ -393,6 +394,7 @@ public class I2PSnarkUtil { } } EepGet get = new I2PSocketEepGet(_context, _manager, retries, out.getAbsolutePath(), fetchURL); + get.addHeader("User-Agent", EEPGET_USER_AGENT); if (get.fetch(timeout)) { if (_log.shouldLog(Log.DEBUG)) _log.debug("Fetch successful [" + url + "]: size=" + out.length()); @@ -434,6 +436,7 @@ public class I2PSnarkUtil { } ByteArrayOutputStream out = new ByteArrayOutputStream(initialSize); EepGet get = new I2PSocketEepGet(_context, _manager, retries, -1, maxSize, null, out, fetchURL); + get.addHeader("User-Agent", EEPGET_USER_AGENT); if (get.fetch(timeout)) { if (_log.shouldLog(Log.DEBUG)) _log.debug("Fetch successful [" + url + "]: size=" + out.size()); diff --git a/apps/i2psnark/java/src/org/klomp/snark/web/FetchAndAdd.java b/apps/i2psnark/java/src/org/klomp/snark/web/FetchAndAdd.java index cf2a55c6f4..270bba2ce5 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/web/FetchAndAdd.java +++ b/apps/i2psnark/java/src/org/klomp/snark/web/FetchAndAdd.java @@ -18,6 +18,7 @@ import net.i2p.util.I2PAppThread; import net.i2p.util.Log; import net.i2p.util.SecureFile; +import org.klomp.snark.I2PSnarkUtil; import org.klomp.snark.MetaInfo; import org.klomp.snark.Snark; import org.klomp.snark.SnarkManager; @@ -130,6 +131,7 @@ public class FetchAndAdd extends Snark implements EepGet.StatusListener, Runnabl return null; _eepGet = new I2PSocketEepGet(_ctx, manager, RETRIES, out.getAbsolutePath(), _url); _eepGet.addStatusListener(this); + _eepGet.addHeader("User-Agent", I2PSnarkUtil.EEPGET_USER_AGENT); if (_eepGet.fetch()) { if (_log.shouldLog(Log.DEBUG)) _log.debug("Fetch successful [" + _url + "]: size=" + out.length()); diff --git a/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java b/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java index 5b29d9d20b..99c81afdd6 100644 --- a/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java +++ b/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java @@ -6,6 +6,7 @@ import java.io.OutputStream; import java.net.MalformedURLException; import java.net.UnknownHostException; import java.net.URL; +import java.util.Locale; import java.util.Properties; import net.i2p.I2PAppContext; @@ -193,13 +194,17 @@ public class I2PSocketEepGet extends EepGet { buf.append("Accept-Encoding: \r\n" + "Cache-control: no-cache\r\n" + "Pragma: no-cache\r\n" + - "User-Agent: " + USER_AGENT + "\r\n" + "Connection: close\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"); buf.append("\r\n"); return buf.toString(); } diff --git a/core/java/src/net/i2p/util/EepGet.java b/core/java/src/net/i2p/util/EepGet.java index 92fef09968..97d84f4f79 100644 --- a/core/java/src/net/i2p/util/EepGet.java +++ b/core/java/src/net/i2p/util/EepGet.java @@ -1156,14 +1156,18 @@ public class EepGet { // we don't want to transparently gunzip it and save it as a .gz file. (!path.endsWith(".gz")) && (!path.endsWith(".tgz"))) buf.append("gzip"); - buf.append("\r\nUser-Agent: " + USER_AGENT + "\r\n" + - "Connection: close\r\n"); + 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"); } } - buf.append("\r\n"); + if(!uaOverridden) + buf.append("User-Agent: " + USER_AGENT + "\r\n"); + buf.append("Connection: close\r\n\r\n"); if (post) buf.append(_postData); if (_log.shouldLog(Log.DEBUG)) @@ -1237,6 +1241,9 @@ public class EepGet { /** * Add an extra header to the request. * 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. + * Note that headers may be subsequently modified or removed in the I2PTunnel HTTP Client proxy. * * @since 0.8.8 */ @@ -1250,6 +1257,7 @@ public class EepGet { * Add basic authorization header for the proxy. * Only added if the request is going through a proxy. * Must be called before fetch(). + * Not supported by EepHead. * * @since 0.8.9 */ diff --git a/core/java/src/net/i2p/util/PartialEepGet.java b/core/java/src/net/i2p/util/PartialEepGet.java index cc93fbb480..4f3cf95519 100644 --- a/core/java/src/net/i2p/util/PartialEepGet.java +++ b/core/java/src/net/i2p/util/PartialEepGet.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; +import java.util.Locale; import net.i2p.I2PAppContext; @@ -143,16 +144,20 @@ public class PartialEepGet extends EepGet { buf.append("\r\n"); buf.append("Cache-control: no-cache\r\n" + - "Pragma: no-cache\r\n"); - // This will be replaced if we are going through I2PTunnelHTTPClient - buf.append("User-Agent: " + USER_AGENT + "\r\n" + + "Pragma: no-cache\r\n" + "Accept-Encoding: \r\n" + "Connection: close\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"); } } + // This will be replaced if we are going through I2PTunnelHTTPClient + if(!uaOverridden) + buf.append("User-Agent: " + USER_AGENT + "\r\n"); buf.append("\r\n"); if (_log.shouldLog(Log.DEBUG)) -- GitLab