2005-03-23 jrandom

* Added support for Transfer-Encoding: chunked to the EepGet, so that the
      cvsweb.cgi doesn't puke on us.
This commit is contained in:
jrandom
2005-03-24 02:38:10 +00:00
committed by zzz
parent 21c7c043b3
commit a52f8b89dc
3 changed files with 51 additions and 6 deletions

View File

@ -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;
@ -398,6 +405,36 @@ public class EepGet {
}
}
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.
* e.g. "HTTP/1.1 206 OK" vs "HTTP/1.1 200 OK" vs
@ -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() + "]");

View File

@ -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

View File

@ -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);