You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2006/08/28 18:26:35 UTC
svn commit: r437741 - in
/jakarta/httpcomponents/httpcore/trunk/module-nio/src:
main/java/org/apache/http/nio/impl/NIOHttpDataReceiver.java
test/java/org/apache/http/nio/TestNIOHttpTransmitterAndReceiver.java
Author: olegk
Date: Mon Aug 28 09:26:27 2006
New Revision: 437741
URL: http://svn.apache.org/viewvc?rev=437741&view=rev
Log:
Implemented max line length check in NIOHttpDataReceiver
Modified:
jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataReceiver.java
jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestNIOHttpTransmitterAndReceiver.java
Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataReceiver.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataReceiver.java?rev=437741&r1=437740&r2=437741&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataReceiver.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/NIOHttpDataReceiver.java Mon Aug 28 09:26:27 2006
@@ -39,6 +39,7 @@
import org.apache.http.io.CharArrayBuffer;
import org.apache.http.io.HttpDataReceiver;
+import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
@@ -62,6 +63,8 @@
private CharsetDecoder chardecoder = null;
private CharBuffer chbuffer = null;
+ private int maxLineLen = -1;
+
protected void initBuffer(int buffersize) {
this.buffer = ByteBuffer.allocateDirect(buffersize);
this.buffer.flip();
@@ -73,6 +76,7 @@
public void reset(final HttpParams params) {
this.charset = Charset.forName(HttpProtocolParams.getHttpElementCharset(params));
this.chardecoder = createCharDecoder();
+ this.maxLineLen = params.getIntParameter(HttpConnectionParams.MAX_LINE_LENGTH, -1);
}
private CharsetDecoder createCharDecoder() {
@@ -121,7 +125,7 @@
return read(b, 0, b.length);
}
- public int read() throws IOException {
+ public int read() throws IOException {
int noRead = 0;
if (!this.buffer.hasRemaining()) {
noRead = doFillBuffer();
@@ -129,11 +133,7 @@
return -1;
}
}
- int b = this.buffer.get();
- if (b < 0) {
- b = 256 + b;
- }
- return b;
+ return this.buffer.get() & 0xff;
}
private int locateLF() {
@@ -198,6 +198,9 @@
this.chbuffer.position(), this.chbuffer.remaining());
}
this.chbuffer.clear();
+ if (this.maxLineLen > 0 && charbuffer.length() >= this.maxLineLen) {
+ throw new IOException("Maximum line length limit exceeded");
+ }
}
// flush the decoder
this.chardecoder.flush(this.chbuffer);
Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestNIOHttpTransmitterAndReceiver.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestNIOHttpTransmitterAndReceiver.java?rev=437741&r1=437740&r2=437741&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestNIOHttpTransmitterAndReceiver.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestNIOHttpTransmitterAndReceiver.java Mon Aug 28 09:26:27 2006
@@ -30,6 +30,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
@@ -39,6 +40,7 @@
import org.apache.http.nio.impl.NIOHttpDataTransmitter;
import org.apache.http.nio.mockup.NIOHttpDataReceiverMockup;
import org.apache.http.nio.mockup.NIOHttpDataTransmitterMockup;
+import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
@@ -310,6 +312,28 @@
}
assertEquals(-1, receiver.read());
assertEquals(-1, receiver.read());
+ }
+
+ public void testLineLimit() throws Exception {
+ HttpParams params = new DefaultHttpParams();
+ String s = "a very looooooooooooooooooooooooooooooooooooooong line\r\n ";
+ byte[] tmp = s.getBytes("US-ASCII");
+ NIOHttpDataReceiverMockup receiver1 = new NIOHttpDataReceiverMockup(tmp, 5);
+ // no limit
+ params.setIntParameter(HttpConnectionParams.MAX_LINE_LENGTH, 0);
+ receiver1.reset(params);
+ assertNotNull(receiver1.readLine());
+
+ NIOHttpDataReceiverMockup receiver2 = new NIOHttpDataReceiverMockup(tmp, 5);
+ // 15 char limit
+ params.setIntParameter(HttpConnectionParams.MAX_LINE_LENGTH, 15);
+ receiver2.reset(params);
+ try {
+ receiver2.readLine();
+ fail("IOException should have been thrown");
+ } catch (IOException ex) {
+ // expected
+ }
}
static final int SWISS_GERMAN_HELLO [] = {