Skip to content

Commit

Permalink
i18n for ChunkedInputFilter error message
Browse files Browse the repository at this point in the history
Add error flag to allow subsequent attempts at reading after an error to fail fast

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1600984 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Jun 6, 2014
1 parent a733f7f commit 593a244
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 24 deletions.
86 changes: 62 additions & 24 deletions java/org/apache/coyote/http11/filters/ChunkedInputFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.tomcat.util.buf.HexUtils;
import org.apache.tomcat.util.buf.MessageBytes;
import org.apache.tomcat.util.http.MimeHeaders;
import org.apache.tomcat.util.res.StringManager;

/**
* Chunked input filter. Parses chunked data according to
Expand All @@ -37,6 +38,9 @@
*/
public class ChunkedInputFilter implements InputFilter {

private static final StringManager sm = StringManager.getManager(
ChunkedInputFilter.class.getPackage().getName());


// -------------------------------------------------------------- Constants

Expand Down Expand Up @@ -133,6 +137,12 @@ public class ChunkedInputFilter implements InputFilter {
private long extensionSize;


/**
* Flag that indicates if an error has occurred.
*/
private boolean error;


// ----------------------------------------------------------- Constructors

public ChunkedInputFilter(int maxTrailerSize, int maxExtensionSize) {
Expand All @@ -155,6 +165,7 @@ public ChunkedInputFilter(int maxTrailerSize, int maxExtensionSize) {
*/
@Override
public int doRead(ByteChunk chunk, Request req) throws IOException {
checkError();

if (endChunk) {
return -1;
Expand All @@ -167,7 +178,7 @@ public int doRead(ByteChunk chunk, Request req) throws IOException {

if (remaining <= 0) {
if (!parseChunkHeader()) {
throw new IOException("Invalid chunk header");
throwIOException(sm.getString("chunkedInputFilter.invalidHeader"));
}
if (endChunk) {
parseEndChunk();
Expand All @@ -179,8 +190,7 @@ public int doRead(ByteChunk chunk, Request req) throws IOException {

if (pos >= lastValid) {
if (readBytes() < 0) {
throw new IOException(
"Unexpected end of stream whilst reading request body");
throwIOException(sm.getString("chunkedInputFilter.eos"));
}
}

Expand Down Expand Up @@ -224,6 +234,7 @@ public void setRequest(Request request) {
*/
@Override
public long end() throws IOException {
checkError();

// Consume extra bytes : parse the stream until the end chunk is found
while (doRead(readChunk, null) >= 0) {
Expand Down Expand Up @@ -266,6 +277,7 @@ public void recycle() {
trailingHeaders.recycle();
trailingHeaders.setLimit(maxTrailerSize);
extensionSize = 0;
error = false;
}


Expand All @@ -279,6 +291,12 @@ public ByteChunk getEncodingName() {
}


@Override
public boolean isFinished() {
return endChunk;
}


// ------------------------------------------------------ Protected Methods

/**
Expand Down Expand Up @@ -346,7 +364,7 @@ protected boolean parseChunkHeader() throws IOException {
// validated. Currently it is simply ignored.
extensionSize++;
if (maxExtensionSize > -1 && extensionSize > maxExtensionSize) {
throw new IOException("maxExtensionSize exceeded");
throwIOException(sm.getString("chunkedInputFilter.maxExtension"));
}
}

Expand Down Expand Up @@ -387,20 +405,23 @@ protected void parseCRLF(boolean tolerant) throws IOException {

while (!eol) {
if (pos >= lastValid) {
if (readBytes() <= 0)
throw new IOException("Invalid CRLF");
if (readBytes() <= 0) {
throwIOException(sm.getString("chunkedInputFilter.invalidCrlfNoData"));
}
}

if (buf[pos] == Constants.CR) {
if (crfound) throw new IOException("Invalid CRLF, two CR characters encountered.");
if (crfound) {
throwIOException(sm.getString("chunkedInputFilter.invalidCrlfCRCR"));
}
crfound = true;
} else if (buf[pos] == Constants.LF) {
if (!tolerant && !crfound) {
throw new IOException("Invalid CRLF, no CR character encountered.");
throwIOException(sm.getString("chunkedInputFilter.invalidCrlfNoCR"));
}
eol = true;
} else {
throw new IOException("Invalid CRLF");
throwIOException(sm.getString("chunkedInputFilter.invalidCrlf"));
}

pos++;
Expand All @@ -412,7 +433,6 @@ protected void parseCRLF(boolean tolerant) throws IOException {
* Parse end chunk data.
*/
protected void parseEndChunk() throws IOException {

// Handle optional trailer headers
while (parseHeader()) {
// Loop until we run out of headers
Expand All @@ -428,8 +448,9 @@ private boolean parseHeader() throws IOException {

// Read new bytes if needed
if (pos >= lastValid) {
if (readBytes() <0)
throw new EOFException("Unexpected end of stream whilst reading trailer headers for chunked request");
if (readBytes() <0) {
throwEOFException(sm.getString("chunkedInputFilter.eosTrailer"));
}
}

chr = buf[pos];
Expand All @@ -453,8 +474,9 @@ private boolean parseHeader() throws IOException {

// Read new bytes if needed
if (pos >= lastValid) {
if (readBytes() <0)
throw new EOFException("Unexpected end of stream whilst reading trailer headers for chunked request");
if (readBytes() <0) {
throwEOFException(sm.getString("chunkedInputFilter.eosTrailer"));
}
}

chr = buf[pos];
Expand Down Expand Up @@ -494,8 +516,9 @@ private boolean parseHeader() throws IOException {

// Read new bytes if needed
if (pos >= lastValid) {
if (readBytes() <0)
throw new EOFException("Unexpected end of stream whilst reading trailer headers for chunked request");
if (readBytes() <0) {
throwEOFException(sm.getString("chunkedInputFilter.eosTrailer"));
}
}

chr = buf[pos];
Expand All @@ -505,7 +528,7 @@ private boolean parseHeader() throws IOException {
// limit placed on trailing header size
int newlimit = trailingHeaders.getLimit() -1;
if (trailingHeaders.getEnd() > newlimit) {
throw new IOException("Exceeded maxTrailerSize");
throw new IOException(sm.getString("chunkedInputFilter.maxTrailer"));
}
trailingHeaders.setLimit(newlimit);
} else {
Expand All @@ -519,8 +542,9 @@ private boolean parseHeader() throws IOException {

// Read new bytes if needed
if (pos >= lastValid) {
if (readBytes() <0)
throw new EOFException("Unexpected end of stream whilst reading trailer headers for chunked request");
if (readBytes() <0) {
throwEOFException(sm.getString("chunkedInputFilter.eosTrailer"));
}
}

chr = buf[pos];
Expand All @@ -544,8 +568,9 @@ private boolean parseHeader() throws IOException {

// Read new bytes if needed
if (pos >= lastValid) {
if (readBytes() <0)
throw new EOFException("Unexpected end of stream whilst reading trailer headers for chunked request");
if (readBytes() <0) {
throwEOFException(sm.getString("chunkedInputFilter.eosTrailer"));
}
}

chr = buf[pos];
Expand All @@ -568,8 +593,21 @@ private boolean parseHeader() throws IOException {
}


@Override
public boolean isFinished() {
return endChunk;
private void throwIOException(String msg) throws IOException {
error = true;
throw new IOException(msg);
}


private void throwEOFException(String msg) throws IOException {
error = true;
throw new EOFException(msg);
}


private void checkError() throws IOException {
if (error) {
throw new IOException(sm.getString("chunkedInputFilter.error"));
}
}
}
25 changes: 25 additions & 0 deletions java/org/apache/coyote/http11/filters/LocalStrings.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

chunkedInputFilter.error=No data available due to previous error
chunkedInputFilter.eos=Unexpected end of stream while reading request body
chunkedInputFilter.eosTrailer=Unexpected end of stream while reading trailer headers
chunkedInputFilter.invalidCrlf=Invalid end of line sequence (character other than CR or LF found)
chunkedInputFilter.invalidCrlfCRCR=Invalid end of line sequence (CRCR)
chunkedInputFilter.invalidCrlfNoCR=Invalid end of line sequence (No CR before LF)
chunkedInputFilter.invalidCrlfNoData=Invalid end of line sequence (no data available to read)
chunkedInputFilter.invalidHeader=Invalid chunk header
chunkedInputFilter.maxExtension=maxExtensionSize exceeded
chunkedInputFilter.maxTrailer=maxTrailerSize exceeded

0 comments on commit 593a244

Please sign in to comment.