Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…gi?id=46538) - it fixes the reported issue but introduces other issues.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@765902 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Apr 17, 2009
1 parent 24b030d commit f711963
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 165 deletions.
5 changes: 1 addition & 4 deletions java/org/apache/coyote/http11/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ public final class Constants {
ByteChunk.convertToBytes("400");
public static final byte[] _404_BYTES =
ByteChunk.convertToBytes("404");
public static final String VARY = "Vary";
public static final String VARY_UNSPECIFIED = "*";
public static final String ACCEPT_ENCODING = "Accept-Encoding";
public static final String ETAG = "ETag";


/**
* Identity filters (input and output).
Expand Down
79 changes: 24 additions & 55 deletions java/org/apache/coyote/http11/Http11AprProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1486,9 +1486,16 @@ public void parseHost(MessageBytes valueMB) {


/**
* Check if browser allows compression
* Check for compression
*/
private boolean isCompressableBrowser() {
private boolean isCompressable() {

// Nope Compression could works in HTTP 1.0 also
// cf: mod_deflate

// Compression only since HTTP 1.1
// if (! http11)
// return false;

// Check if browser support gzip encoding
MessageBytes acceptEncodingMB =
Expand All @@ -1498,7 +1505,15 @@ private boolean isCompressableBrowser() {
|| (acceptEncodingMB.indexOf("gzip") == -1))
return false;

// If force mode, always compress (test purposes only)
// Check if content is not allready gzipped
MessageBytes contentEncodingMB =
response.getMimeHeaders().getValue("Content-Encoding");

if ((contentEncodingMB != null)
&& (contentEncodingMB.indexOf("gzip") != -1))
return false;

// If force mode, allways compress (test purposes only)
if (compressionLevel == 2)
return true;

Expand All @@ -1515,23 +1530,8 @@ private boolean isCompressableBrowser() {
return false;
}
}
return true;
}

/*
* Check if response allows compression
*/
private boolean isCompressableResponse() {

// Check if content is not already gzipped
MessageBytes contentEncodingMB =
response.getMimeHeaders().getValue("Content-Encoding");

if ((contentEncodingMB != null)
&& (contentEncodingMB.indexOf("gzip") != -1))
return false;

// Check if sufficient length to trigger the compression
// Check if suffisant len to trig the compression
long contentLength = response.getContentLengthLong();
if ((contentLength == -1)
|| (contentLength > compressionMinSize)) {
Expand Down Expand Up @@ -1598,35 +1598,18 @@ protected void prepareResponse() {
((Long) request.getAttribute("org.apache.tomcat.sendfile.end")).longValue();
}
}

MimeHeaders headers = response.getMimeHeaders();


// Check for compression
boolean useCompression = false;
if (entityBody && (compressionLevel > 0) && (sendfileData == null)) {
if (isCompressableResponse()) {
// Always send the Vary header when response could be compressed
MessageBytes varyHeader = headers.getValue(Constants.VARY);
if (varyHeader == null) {
headers.addValue(Constants.VARY).setString(
Constants.ACCEPT_ENCODING);
} else {
if (varyHeader.indexOf(Constants.ACCEPT_ENCODING) == -1 &&
!varyHeader.equals(Constants.VARY_UNSPECIFIED)) {
varyHeader.setString(varyHeader.toString() + "," +
Constants.ACCEPT_ENCODING);
}
}
}

useCompression = isCompressableBrowser();

useCompression = isCompressable();
// Change content-length to -1 to force chunking
if (useCompression) {
response.setContentLength(-1);
}
}

MimeHeaders headers = response.getMimeHeaders();
if (!entityBody) {
response.setContentLength(-1);
} else {
Expand Down Expand Up @@ -1662,22 +1645,8 @@ protected void prepareResponse() {
if (useCompression) {
outputBuffer.addActiveFilter(outputFilters[Constants.GZIP_FILTER]);
headers.setValue("Content-Encoding").setString("gzip");

// Ensure eTag for compressed content is different to eTag for
// uncompressed content
MessageBytes eTagHeader = headers.getValue(Constants.ETAG);
if (eTagHeader != null) {
String eTag = eTagHeader.toString();
int len = eTag.length();
if (len > 1 && eTag.charAt(len - 1) == '"') {
// Add compression marker before closing quote
eTag = eTag.substring(0, len -1) + "-gz\"";
} else {
// Unquoted ETag - shouldn't happen - TODO complain
eTag = eTag + "-gz";
}
eTagHeader.setString(eTag);
}
// Make Proxies happy via Vary (from mod_deflate)
headers.setValue("Vary").setString("Accept-Encoding");
}

// Add date header
Expand Down
76 changes: 24 additions & 52 deletions java/org/apache/coyote/http11/Http11NioProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1529,9 +1529,16 @@ public void parseHost(MessageBytes valueMB) {


/**
* Check if browser allows compression
* Check for compression
*/
private boolean isCompressableBrowser() {
private boolean isCompressable() {

// Nope Compression could works in HTTP 1.0 also
// cf: mod_deflate

// Compression only since HTTP 1.1
// if (! http11)
// return false;

// Check if browser support gzip encoding
MessageBytes acceptEncodingMB =
Expand All @@ -1541,7 +1548,15 @@ private boolean isCompressableBrowser() {
|| (acceptEncodingMB.indexOf("gzip") == -1))
return false;

// If force mode, always compress (test purposes only)
// Check if content is not allready gzipped
MessageBytes contentEncodingMB =
response.getMimeHeaders().getValue("Content-Encoding");

if ((contentEncodingMB != null)
&& (contentEncodingMB.indexOf("gzip") != -1))
return false;

// If force mode, allways compress (test purposes only)
if (compressionLevel == 2)
return true;

Expand All @@ -1558,23 +1573,8 @@ private boolean isCompressableBrowser() {
return false;
}
}
return true;
}

/*
* Check if response allows compression
*/
private boolean isCompressableResponse() {

// Check if content is not already gzipped
MessageBytes contentEncodingMB =
response.getMimeHeaders().getValue("Content-Encoding");

if ((contentEncodingMB != null)
&& (contentEncodingMB.indexOf("gzip") != -1))
return false;

// Check if sufficient length to trigger the compression
// Check if suffisant len to trig the compression
long contentLength = response.getContentLengthLong();
if ((contentLength == -1)
|| (contentLength > compressionMinSize)) {
Expand Down Expand Up @@ -1639,33 +1639,19 @@ protected void prepareResponse() throws IOException {
}
}

MimeHeaders headers = response.getMimeHeaders();


// Check for compression
boolean useCompression = false;
if (entityBody && (compressionLevel > 0) && (sendfileData == null)) {
if (isCompressableResponse()) {
// Always send the Vary header when response could be compressed
MessageBytes varyHeader = headers.getValue(Constants.VARY);
if (varyHeader == null) {
headers.addValue(Constants.VARY).setString(
Constants.ACCEPT_ENCODING);
} else {
if (varyHeader.indexOf(Constants.ACCEPT_ENCODING) == -1 &&
!varyHeader.equals(Constants.VARY_UNSPECIFIED)) {
varyHeader.setString(varyHeader.toString() + "," +
Constants.ACCEPT_ENCODING);
}
}
}
useCompression = isCompressableBrowser();

useCompression = isCompressable();
// Change content-length to -1 to force chunking
if (useCompression) {
response.setContentLength(-1);
}
}

MimeHeaders headers = response.getMimeHeaders();
if (!entityBody) {
response.setContentLength(-1);
} else {
Expand Down Expand Up @@ -1701,22 +1687,8 @@ protected void prepareResponse() throws IOException {
if (useCompression) {
outputBuffer.addActiveFilter(outputFilters[Constants.GZIP_FILTER]);
headers.setValue("Content-Encoding").setString("gzip");

// Ensure eTag for compressed content is different to eTag for
// uncompressed content
MessageBytes eTagHeader = headers.getValue(Constants.ETAG);
if (eTagHeader != null) {
String eTag = eTagHeader.toString();
int len = eTag.length();
if (len > 1 && eTag.charAt(len - 1) == '"') {
// Add compression marker before closing quote
eTag = eTag.substring(0, len -1) + "-gz\"";
} else {
// Unquoted ETag - shouldn't happen - TODO complain
eTag = eTag + "-gz";
}
eTagHeader.setString(eTag);
}
// Make Proxies happy via Vary (from mod_deflate)
headers.setValue("Vary").setString("Accept-Encoding");
}

// Add date header
Expand Down
78 changes: 24 additions & 54 deletions java/org/apache/coyote/http11/Http11Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1399,9 +1399,16 @@ public void parseHost(MessageBytes valueMB) {


/**
* Check if browser allows compression
* Check for compression
*/
private boolean isCompressableBrowser() {
private boolean isCompressable() {

// Nope Compression could works in HTTP 1.0 also
// cf: mod_deflate

// Compression only since HTTP 1.1
// if (! http11)
// return false;

// Check if browser support gzip encoding
MessageBytes acceptEncodingMB =
Expand All @@ -1411,7 +1418,15 @@ private boolean isCompressableBrowser() {
|| (acceptEncodingMB.indexOf("gzip") == -1))
return false;

// If force mode, always compress (test purposes only)
// Check if content is not allready gzipped
MessageBytes contentEncodingMB =
response.getMimeHeaders().getValue("Content-Encoding");

if ((contentEncodingMB != null)
&& (contentEncodingMB.indexOf("gzip") != -1))
return false;

// If force mode, allways compress (test purposes only)
if (compressionLevel == 2)
return true;

Expand All @@ -1428,23 +1443,8 @@ private boolean isCompressableBrowser() {
return false;
}
}
return true;
}

/*
* Check if response allows compression
*/
private boolean isCompressableResponse() {

// Check if content is not already gzipped
MessageBytes contentEncodingMB =
response.getMimeHeaders().getValue("Content-Encoding");

if ((contentEncodingMB != null)
&& (contentEncodingMB.indexOf("gzip") != -1))
return false;

// Check if sufficient length to trigger the compression
// Check if suffisant len to trig the compression
long contentLength = response.getContentLengthLong();
if ((contentLength == -1)
|| (contentLength > compressionMinSize)) {
Expand Down Expand Up @@ -1495,34 +1495,18 @@ protected void prepareResponse() {
contentDelimitation = true;
}

MimeHeaders headers = response.getMimeHeaders();

// Check for compression
boolean useCompression = false;
if (entityBody && (compressionLevel > 0)) {
if (isCompressableResponse()) {
// Always send the Vary header when response could be compressed
MessageBytes varyHeader = headers.getValue(Constants.VARY);
if (varyHeader == null) {
headers.addValue(Constants.VARY).setString(
Constants.ACCEPT_ENCODING);
} else {
if (varyHeader.indexOf(Constants.ACCEPT_ENCODING) == -1 &&
!varyHeader.equals(Constants.VARY_UNSPECIFIED)) {
varyHeader.setString(varyHeader.toString() + "," +
Constants.ACCEPT_ENCODING);
}
}
}

useCompression = isCompressableBrowser();

useCompression = isCompressable();

// Change content-length to -1 to force chunking
if (useCompression) {
response.setContentLength(-1);
}
}

MimeHeaders headers = response.getMimeHeaders();
if (!entityBody) {
response.setContentLength(-1);
} else {
Expand Down Expand Up @@ -1558,22 +1542,8 @@ protected void prepareResponse() {
if (useCompression) {
outputBuffer.addActiveFilter(outputFilters[Constants.GZIP_FILTER]);
headers.setValue("Content-Encoding").setString("gzip");

// Ensure eTag for compressed content is different to eTag for
// uncompressed content
MessageBytes eTagHeader = headers.getValue(Constants.ETAG);
if (eTagHeader != null) {
String eTag = eTagHeader.toString();
int len = eTag.length();
if (len > 1 && eTag.charAt(len - 1) == '"') {
// Add compression marker before closing quote
eTag = eTag.substring(0, len -1) + "-gz\"";
} else {
// Unquoted ETag - shouldn't happen - TODO complain
eTag = eTag + "-gz";
}
eTagHeader.setString(eTag);
}
// Make Proxies happy via Vary (from mod_deflate)
headers.setValue("Vary").setString("Accept-Encoding");
}

// Add date header
Expand Down

0 comments on commit f711963

Please sign in to comment.