* EepGet:

- Fix format of last-modified header to use strictest RFC 822
  - Stop immediately if socket connection to proxy fails
  - Don't forget lastModified/etag headers after redirect
  - Note SocketTimeout API breakage for Syndie
This commit is contained in:
zzz
2013-01-12 18:12:35 +00:00
parent 41af00a7d6
commit ed12bcefdb
3 changed files with 49 additions and 8 deletions

View File

@@ -8,6 +8,7 @@ import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
@@ -64,6 +65,8 @@ public class EepGet {
protected boolean _shouldWriteErrorToOutput;
protected String _etag;
protected String _lastModified;
protected final String _etagOrig;
protected final String _lastModifiedOrig;
protected boolean _encodingChunked;
protected boolean _notModified;
protected String _contentType;
@@ -142,6 +145,8 @@ public class EepGet {
_listeners = new ArrayList(1);
_etag = etag;
_lastModified = lastModified;
_etagOrig = etag;
_lastModifiedOrig = lastModified;
}
/**
@@ -524,7 +529,8 @@ public class EepGet {
_listeners.get(i).attemptFailed(_url, _bytesTransferred, _bytesRemaining, _currentAttempt, _numRetries, ioe);
if (_log.shouldLog(Log.WARN))
_log.warn("ERR: doFetch failed ", ioe);
if (ioe instanceof MalformedURLException)
if (ioe instanceof MalformedURLException ||
ioe instanceof ConnectException) // proxy or nonproxied host Connection Refused
_keepFetching = false;
} finally {
if (_out != null) {
@@ -610,8 +616,8 @@ public class EepGet {
// reset some important variables, we don't want to save the values from the redirect
_bytesRemaining = -1;
_redirectLocation = null;
_etag = null;
_lastModified = null;
_etag = _etagOrig;
_lastModified = _lastModifiedOrig;
_contentType = null;
_encodingChunked = false;
@@ -724,8 +730,13 @@ public class EepGet {
if (_transferFailed) {
// 404, etc - transferFailed is called after all attempts fail, by fetch() above
for (int i = 0; i < _listeners.size(); i++)
_listeners.get(i).attemptFailed(_url, _bytesTransferred, _bytesRemaining, _currentAttempt, _numRetries, new Exception("Attempt failed"));
if (!_listeners.isEmpty()) {
Exception e = new IOException("Attempt failed " + _responseCode);
for (int i = 0; i < _listeners.size(); i++) {
_listeners.get(i).attemptFailed(_url, _bytesTransferred, _bytesRemaining, _currentAttempt,
_numRetries, e);
}
}
} else if ((_minSize > 0) && (_alreadyTransferred < _minSize)) {
throw new IOException("Bytes transferred " + _alreadyTransferred + " violates minimum of " + _minSize + " bytes");
} else if ( (_bytesRemaining == -1) || (remaining == 0) ) {

View File

@@ -7,7 +7,7 @@ import java.util.Date;
/**
* This should be deprecated.
* It is only used by EepGet.
* It is only used by EepGet and Syndie.
* The only advantage seems to be a total timeout period, which is the second
* argument to EepGet.fetch(headerTimeout, totalTimeout, inactivityTimeout),
* which is most likely always set to -1.
@@ -52,6 +52,11 @@ public class SocketTimeout extends SimpleTimer2.TimedEvent {
}
}
/**
* Change in return value from void to boolean in
* 0.9.3 accidentally broke Syndie, sorry.
* Recompile Syndie to fix it.
*/
public boolean cancel() {
_cancelled = true;
return super.cancel();

View File

@@ -4,6 +4,7 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* Moved from NewsFetcher
@@ -13,7 +14,7 @@ public abstract class RFC822Date {
// SimpleDateFormat is not thread-safe, methods must be synchronized
private static final SimpleDateFormat OUTPUT_FORMAT = new SimpleDateFormat("d MMM yyyy HH:mm:ss z", Locale.US);
private static final SimpleDateFormat OUTPUT_FORMAT = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
/**
* http://jimyjoshi.com/blog/2007/08/rfc822dateparsinginjava.html
@@ -22,7 +23,7 @@ public abstract class RFC822Date {
*/
private static final SimpleDateFormat rfc822DateFormats[] = new SimpleDateFormat[] {
OUTPUT_FORMAT,
new SimpleDateFormat("EEE, d MMM yy HH:mm:ss z", Locale.US),
new SimpleDateFormat("d MMM yy HH:mm:ss z", Locale.US),
new SimpleDateFormat("EEE, d MMM yy HH:mm z", Locale.US),
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US),
new SimpleDateFormat("EEE, d MMM yyyy HH:mm z", Locale.US),
@@ -31,6 +32,16 @@ public abstract class RFC822Date {
new SimpleDateFormat("d MMM yyyy HH:mm z", Locale.US)
};
//
// The router JVM is forced to UTC but do this just in case
//
static {
TimeZone utc = TimeZone.getTimeZone("GMT");
for (int i = 0; i < rfc822DateFormats.length; i++) {
rfc822DateFormats[i].setTimeZone(utc);
}
}
/**
* new Date(String foo) is deprecated, so let's do this the hard way
*
@@ -56,4 +67,18 @@ public abstract class RFC822Date {
public synchronized static String to822Date(long t) {
return OUTPUT_FORMAT.format(new Date(t));
}
/****
public static void main(String[] args) {
if (args.length == 1) {
try {
System.out.println(to822Date(Long.parseLong(args[0])));
} catch (NumberFormatException nfe) {
System.out.println(nfe.toString());
}
} else {
System.out.println("Usage: RFC822Date numericDate");
}
}
****/
}