From a52f8b89dcbf7a3d3d8b8c1f08eeeb089209c340 Mon Sep 17 00:00:00 2001 From: jrandom Date: Thu, 24 Mar 2005 02:38:10 +0000 Subject: [PATCH] 2005-03-23 jrandom * Added support for Transfer-Encoding: chunked to the EepGet, so that the cvsweb.cgi doesn't puke on us. --- core/java/src/net/i2p/util/EepGet.java | 47 +++++++++++++++++-- history.txt | 6 ++- .../src/net/i2p/router/RouterVersion.java | 4 +- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/core/java/src/net/i2p/util/EepGet.java b/core/java/src/net/i2p/util/EepGet.java index 49351c012..6c16d3291 100644 --- a/core/java/src/net/i2p/util/EepGet.java +++ b/core/java/src/net/i2p/util/EepGet.java @@ -47,6 +47,7 @@ public class EepGet { private long _bytesRemaining; private int _currentAttempt; private String _etag; + private boolean _encodingChunked; public EepGet(I2PAppContext ctx, String proxyHost, int proxyPort, int numRetries, String outputFile, String url) { this(ctx, true, proxyHost, proxyPort, numRetries, outputFile, url); @@ -304,13 +305,16 @@ public class EepGet { if (_log.shouldLog(Log.DEBUG)) _log.debug("Headers read completely, reading " + _bytesRemaining); + int remaining = (int)_bytesRemaining; byte buf[] = new byte[1024]; - while (_keepFetching) { - int read = _proxyIn.read(buf); + while (_keepFetching && remaining > 0) { + int toRead = buf.length; + int read = _proxyIn.read(buf, 0, (buf.length > remaining ? remaining : buf.length)); if (read == -1) break; _out.write(buf, 0, read); _bytesTransferred += read; + remaining -= read; if (read > 0) for (int i = 0; i < _listeners.size(); i++) ((StatusListener)_listeners.get(i)).bytesTransferred(_alreadyTransferred, read, _bytesTransferred, _bytesRemaining, _url); @@ -323,7 +327,7 @@ public class EepGet { if (_log.shouldLog(Log.DEBUG)) _log.debug("Done transferring " + _bytesTransferred); - if (_bytesRemaining == _bytesTransferred) { + if ( (_bytesRemaining == -1) || (remaining == 0) ){ for (int i = 0; i < _listeners.size(); i++) ((StatusListener)_listeners.get(i)).transferComplete(_alreadyTransferred, _bytesTransferred, _bytesRemaining, _url, _outputFile); } else { @@ -385,6 +389,9 @@ public class EepGet { if (isEndOfHeaders(lookahead)) { if (!rcOk) throw new IOException("Invalid HTTP response code: " + responseCode); + if (_encodingChunked) { + readChunkLength(); + } return; } break; @@ -397,6 +404,36 @@ public class EepGet { throw new IOException("Header line too long: " + buf.toString()); } } + + private void readChunkLength() throws IOException { + StringBuffer buf = new StringBuffer(8); + int nl = 0; + while (true) { + int cur = _proxyIn.read(); + switch (cur) { + case -1: + throw new IOException("Chunk ended too soon"); + case '\n': + case '\r': + nl++; + default: + buf.append((char)cur); + } + + if (nl >= 2) + break; + } + + String len = buf.toString().trim(); + try { + long bytes = Long.parseLong(len, 16); + _bytesRemaining = bytes; + if (_log.shouldLog(Log.DEBUG)) + _log.debug("Chunked length: " + bytes); + } catch (NumberFormatException nfe) { + throw new IOException("Invalid chunk length [" + len + "]"); + } + } /** * parse the first status line and grab the response code. @@ -438,6 +475,9 @@ public class EepGet { } } else if (key.equalsIgnoreCase("ETag")) { _etag = val.trim(); + } else if (key.equalsIgnoreCase("Transfer-encoding")) { + if (val.indexOf("chunked") != -1) + _encodingChunked = true; } else { // ignore the rest } @@ -505,6 +545,7 @@ public class EepGet { buf.append(_alreadyTransferred); buf.append("-\n"); } + buf.append("Accept-Encoding: identity;q=1, *;q=0\n"); buf.append("Connection: close\n\n"); if (_log.shouldLog(Log.DEBUG)) _log.debug("Request: [" + buf.toString() + "]"); diff --git a/history.txt b/history.txt index 2270be83d..0d836871e 100644 --- a/history.txt +++ b/history.txt @@ -1,4 +1,8 @@ -$Id: history.txt,v 1.176 2005/03/23 20:51:58 Connelly Exp $ +$Id: history.txt,v 1.177 2005/03/23 20:54:23 connelly Exp $ + +2005-03-23 jrandom + * Added support for Transfer-Encoding: chunked to the EepGet, so that the + cvsweb.cgi doesn't puke on us. 2005-03-23 Connelly * Fixed Bugzilla Bug #99 in the SAM Bridge, which caused pending diff --git a/router/java/src/net/i2p/router/RouterVersion.java b/router/java/src/net/i2p/router/RouterVersion.java index 305d6beee..aa3352e73 100644 --- a/router/java/src/net/i2p/router/RouterVersion.java +++ b/router/java/src/net/i2p/router/RouterVersion.java @@ -15,9 +15,9 @@ import net.i2p.CoreVersion; * */ public class RouterVersion { - public final static String ID = "$Revision: 1.169 $ $Date: 2005/03/23 16:13:05 $"; + public final static String ID = "$Revision: 1.170 $ $Date: 2005/03/23 20:19:53 $"; public final static String VERSION = "0.5.0.3"; - public final static long BUILD = 3; + public final static long BUILD = 4; public static void main(String args[]) { System.out.println("I2P Router version: " + VERSION); System.out.println("Router ID: " + RouterVersion.ID);