EepGet: Allow override of the User-Agent

i2psnark: Set User-Agent
This commit is contained in:
zzz
2013-12-15 15:16:54 +00:00
parent efff25a87c
commit 8cb503d8bb
5 changed files with 30 additions and 7 deletions

View File

@@ -76,6 +76,7 @@ public class I2PSnarkUtil {
public static final int MAX_CONNECTIONS = 16; // per torrent public static final int MAX_CONNECTIONS = 16; // per torrent
public static final String PROP_MAX_BW = "i2cp.outboundBytesPerSecond"; public static final String PROP_MAX_BW = "i2cp.outboundBytesPerSecond";
public static final boolean DEFAULT_USE_DHT = true; public static final boolean DEFAULT_USE_DHT = true;
public static final String EEPGET_USER_AGENT = "I2PSnark";
public I2PSnarkUtil(I2PAppContext ctx) { public I2PSnarkUtil(I2PAppContext ctx) {
this(ctx, "i2psnark"); this(ctx, "i2psnark");
@@ -393,6 +394,7 @@ public class I2PSnarkUtil {
} }
} }
EepGet get = new I2PSocketEepGet(_context, _manager, retries, out.getAbsolutePath(), fetchURL); EepGet get = new I2PSocketEepGet(_context, _manager, retries, out.getAbsolutePath(), fetchURL);
get.addHeader("User-Agent", EEPGET_USER_AGENT);
if (get.fetch(timeout)) { if (get.fetch(timeout)) {
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Fetch successful [" + url + "]: size=" + out.length()); _log.debug("Fetch successful [" + url + "]: size=" + out.length());
@@ -434,6 +436,7 @@ public class I2PSnarkUtil {
} }
ByteArrayOutputStream out = new ByteArrayOutputStream(initialSize); ByteArrayOutputStream out = new ByteArrayOutputStream(initialSize);
EepGet get = new I2PSocketEepGet(_context, _manager, retries, -1, maxSize, null, out, fetchURL); EepGet get = new I2PSocketEepGet(_context, _manager, retries, -1, maxSize, null, out, fetchURL);
get.addHeader("User-Agent", EEPGET_USER_AGENT);
if (get.fetch(timeout)) { if (get.fetch(timeout)) {
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Fetch successful [" + url + "]: size=" + out.size()); _log.debug("Fetch successful [" + url + "]: size=" + out.size());

View File

@@ -18,6 +18,7 @@ import net.i2p.util.I2PAppThread;
import net.i2p.util.Log; import net.i2p.util.Log;
import net.i2p.util.SecureFile; import net.i2p.util.SecureFile;
import org.klomp.snark.I2PSnarkUtil;
import org.klomp.snark.MetaInfo; import org.klomp.snark.MetaInfo;
import org.klomp.snark.Snark; import org.klomp.snark.Snark;
import org.klomp.snark.SnarkManager; import org.klomp.snark.SnarkManager;
@@ -130,6 +131,7 @@ public class FetchAndAdd extends Snark implements EepGet.StatusListener, Runnabl
return null; return null;
_eepGet = new I2PSocketEepGet(_ctx, manager, RETRIES, out.getAbsolutePath(), _url); _eepGet = new I2PSocketEepGet(_ctx, manager, RETRIES, out.getAbsolutePath(), _url);
_eepGet.addStatusListener(this); _eepGet.addStatusListener(this);
_eepGet.addHeader("User-Agent", I2PSnarkUtil.EEPGET_USER_AGENT);
if (_eepGet.fetch()) { if (_eepGet.fetch()) {
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Fetch successful [" + _url + "]: size=" + out.length()); _log.debug("Fetch successful [" + _url + "]: size=" + out.length());

View File

@@ -6,6 +6,7 @@ import java.io.OutputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.net.URL; import java.net.URL;
import java.util.Locale;
import java.util.Properties; import java.util.Properties;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
@@ -193,13 +194,17 @@ public class I2PSocketEepGet extends EepGet {
buf.append("Accept-Encoding: \r\n" + buf.append("Accept-Encoding: \r\n" +
"Cache-control: no-cache\r\n" + "Cache-control: no-cache\r\n" +
"Pragma: no-cache\r\n" + "Pragma: no-cache\r\n" +
"User-Agent: " + USER_AGENT + "\r\n" +
"Connection: close\r\n"); "Connection: close\r\n");
boolean uaOverridden = false;
if (_extraHeaders != null) { if (_extraHeaders != null) {
for (String hdr : _extraHeaders) { for (String hdr : _extraHeaders) {
if (hdr.toLowerCase(Locale.US).startsWith("user-agent: "))
uaOverridden = true;
buf.append(hdr).append("\r\n"); buf.append(hdr).append("\r\n");
} }
} }
if(!uaOverridden)
buf.append("User-Agent: " + USER_AGENT + "\r\n");
buf.append("\r\n"); buf.append("\r\n");
return buf.toString(); return buf.toString();
} }

View File

@@ -1156,14 +1156,18 @@ public class EepGet {
// we don't want to transparently gunzip it and save it as a .gz file. // we don't want to transparently gunzip it and save it as a .gz file.
(!path.endsWith(".gz")) && (!path.endsWith(".tgz"))) (!path.endsWith(".gz")) && (!path.endsWith(".tgz")))
buf.append("gzip"); buf.append("gzip");
buf.append("\r\nUser-Agent: " + USER_AGENT + "\r\n" + buf.append("\r\n");
"Connection: close\r\n"); boolean uaOverridden = false;
if (_extraHeaders != null) { if (_extraHeaders != null) {
for (String hdr : _extraHeaders) { for (String hdr : _extraHeaders) {
if (hdr.toLowerCase(Locale.US).startsWith("user-agent: "))
uaOverridden = true;
buf.append(hdr).append("\r\n"); 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) if (post)
buf.append(_postData); buf.append(_postData);
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
@@ -1237,6 +1241,9 @@ public class EepGet {
/** /**
* Add an extra header to the request. * Add an extra header to the request.
* Must be called before fetch(). * 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 * @since 0.8.8
*/ */
@@ -1250,6 +1257,7 @@ public class EepGet {
* Add basic authorization header for the proxy. * Add basic authorization header for the proxy.
* Only added if the request is going through a proxy. * Only added if the request is going through a proxy.
* Must be called before fetch(). * Must be called before fetch().
* Not supported by EepHead.
* *
* @since 0.8.9 * @since 0.8.9
*/ */

View File

@@ -5,6 +5,7 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Locale;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
@@ -143,16 +144,20 @@ public class PartialEepGet extends EepGet {
buf.append("\r\n"); buf.append("\r\n");
buf.append("Cache-control: no-cache\r\n" + buf.append("Cache-control: no-cache\r\n" +
"Pragma: 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" +
"Accept-Encoding: \r\n" + "Accept-Encoding: \r\n" +
"Connection: close\r\n"); "Connection: close\r\n");
boolean uaOverridden = false;
if (_extraHeaders != null) { if (_extraHeaders != null) {
for (String hdr : _extraHeaders) { for (String hdr : _extraHeaders) {
if (hdr.toLowerCase(Locale.US).startsWith("user-agent: "))
uaOverridden = true;
buf.append(hdr).append("\r\n"); 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"); buf.append("\r\n");
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))