Java源码示例:org.apache.coyote.http11.filters.GzipOutputFilter
示例1
/**
* Initialize standard input and output filters.
*/
protected void initializeFilters(int maxTrailerSize, Set<String> allowedTrailerHeaders,
int maxExtensionSize, int maxSwallowSize) {
// Create and add the identity filters.
getInputBuffer().addFilter(new IdentityInputFilter(maxSwallowSize));
getOutputBuffer().addFilter(new IdentityOutputFilter());
// Create and add the chunked filters.
getInputBuffer().addFilter( new ChunkedInputFilter(maxTrailerSize,allowedTrailerHeaders,
maxExtensionSize, maxSwallowSize));
getOutputBuffer().addFilter(new ChunkedOutputFilter());
// Create and add the void filters
getInputBuffer().addFilter(new VoidInputFilter());
getOutputBuffer().addFilter(new VoidOutputFilter());
// Create and add buffered input filter
getInputBuffer().addFilter(new BufferedInputFilter());
// Create and add the chunked filters.
//getInputBuffer().addFilter(new GzipInputFilter());
getOutputBuffer().addFilter(new GzipOutputFilter());
pluggableFilterIndex = getInputBuffer().getFilters().length;
}
示例2
/**
* Flush the response.
*
* @throws IOException an underlying I/O error occurred
*/
public void flush()
throws IOException {
if (!committed) {
// Send the connector a request for commit. The connector should
// then validate the headers, send them (using sendHeader) and
// set the filters accordingly.
response.action(ActionCode.COMMIT, null);
}
// go through the filters and if there is gzip filter
// invoke it to flush
for (int i = 0; i <= lastActiveFilter; i++) {
if (activeFilters[i] instanceof GzipOutputFilter) {
if (log.isDebugEnabled()) {
log.debug("Flushing the gzip filter at position " + i +
" of the filter chain...");
}
((GzipOutputFilter) activeFilters[i]).flush();
break;
}
}
}
示例3
/**
* Flush the response.
*
* @throws IOException an underlying I/O error occurred
*/
public void flush()
throws IOException {
if (!committed) {
// Send the connector a request for commit. The connector should
// then validate the headers, send them (using sendHeader) and
// set the filters accordingly.
response.action(ActionCode.COMMIT, null);
}
// go through the filters and if there is gzip filter
// invoke it to flush
for (int i = 0; i <= lastActiveFilter; i++) {
if (activeFilters[i] instanceof GzipOutputFilter) {
if (log.isDebugEnabled()) {
log.debug("Flushing the gzip filter at position " + i +
" of the filter chain...");
}
((GzipOutputFilter) activeFilters[i]).flush();
break;
}
}
}
示例4
static void prepareHeaders(Request coyoteRequest, Response coyoteResponse,
Http2Protocol protocol, Stream stream) {
MimeHeaders headers = coyoteResponse.getMimeHeaders();
int statusCode = coyoteResponse.getStatus();
// Add the pseudo header for status
headers.addValue(":status").setString(Integer.toString(statusCode));
// Check to see if a response body is present
if (!(statusCode < 200 || statusCode == 204 || statusCode == 205 || statusCode == 304)) {
String contentType = coyoteResponse.getContentType();
if (contentType != null) {
headers.setValue("content-type").setString(contentType);
}
String contentLanguage = coyoteResponse.getContentLanguage();
if (contentLanguage != null) {
headers.setValue("content-language").setString(contentLanguage);
}
// Add a content-length header if a content length has been set unless
// the application has already added one
long contentLength = coyoteResponse.getContentLengthLong();
if (contentLength != -1 && headers.getValue("content-length") == null) {
headers.addValue("content-length").setLong(contentLength);
}
} else {
if (statusCode == 205) {
// RFC 7231 requires the server to explicitly signal an empty
// response in this case
coyoteResponse.setContentLength(0);
} else {
coyoteResponse.setContentLength(-1);
}
}
// Add date header unless it is an informational response or the
// application has already set one
if (statusCode >= 200 && headers.getValue("date") == null) {
headers.addValue("date").setString(FastHttpDateFormat.getCurrentDate());
}
if (protocol != null && protocol.useCompression(coyoteRequest, coyoteResponse)) {
// Enable compression. Headers will have been set. Need to configure
// output filter at this point.
stream.addOutputFilter(new GzipOutputFilter());
}
}
示例5
/**
* Http11 NIO处理器.
* 拿到连接.传递连接.处理连接.处理的关键地方.
* 1.inputBuffer and outputBuffer绑定在了NioSocketWrapper.
* 2. Http11Processor持有NioSocketWrapper对象.
* 3. NioSocketWrapper去读写数据填充到对应的两个Buffer内.
* @param protocol
* @param endpoint
*/
@SuppressWarnings("deprecation")
public Http11Processor(AbstractHttp11Protocol<?> protocol, AbstractEndpoint<?> endpoint) {
super(endpoint);
this.protocol = protocol;
httpParser = new HttpParser(protocol.getRelaxedPathChars(),
protocol.getRelaxedQueryChars());
/**
* Http11 InputBuffer 绑定到 {@link org.apache.coyote.Request}
*/
inputBuffer = new Http11InputBuffer(request, protocol.getMaxHttpHeaderSize(),
protocol.getRejectIllegalHeaderName(), httpParser);
request.setInputBuffer(inputBuffer);
/**
* Http11 OutputBuffer 绑定到{@link org.apache.coyote.Response}
*/
outputBuffer = new Http11OutputBuffer(response, protocol.getMaxHttpHeaderSize(),
protocol.getSendReasonPhrase());
response.setOutputBuffer(outputBuffer);
// Create and add the identity filters.
inputBuffer.addFilter(new IdentityInputFilter(protocol.getMaxSwallowSize()));
outputBuffer.addFilter(new IdentityOutputFilter());
// Create and add the chunked filters.
inputBuffer.addFilter(new ChunkedInputFilter(protocol.getMaxTrailerSize(),
protocol.getAllowedTrailerHeadersInternal(), protocol.getMaxExtensionSize(),
protocol.getMaxSwallowSize()));
outputBuffer.addFilter(new ChunkedOutputFilter());
// Create and add the void filters.
inputBuffer.addFilter(new VoidInputFilter());
outputBuffer.addFilter(new VoidOutputFilter());
// Create and add buffered input filter
inputBuffer.addFilter(new BufferedInputFilter());
// Create and add the gzip filters.
//inputBuffer.addFilter(new GzipInputFilter());
outputBuffer.addFilter(new GzipOutputFilter());
pluggableFilterIndex = inputBuffer.getFilters().length;
}
示例6
/**
* Test the interaction betwen gzip and flushing. The idea is to: 1. create
* a internal output buffer, response, and attach an active gzipoutputfilter
* to the output buffer 2. set the output stream of the internal buffer to
* be a ByteArrayOutputStream so we can inspect the output bytes 3. write a
* chunk out using the gzipoutputfilter and invoke a flush on the
* InternalOutputBuffer 4. read from the ByteArrayOutputStream to find out
* what's being written out (flushed) 5. find out what's expected by wrting
* to GZIPOutputStream and close it (to force flushing) 6. Compare the size
* of the two arrays, they should be close (instead of one being much
* shorter than the other one)
*
* @throws Exception
*/
@Test
public void testFlushingWithGzip() throws Exception {
// set up response, InternalOutputBuffer, and ByteArrayOutputStream
Response res = new Response();
InternalOutputBuffer iob = new InternalOutputBuffer(res, 8 * 1024);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
iob.outputStream = bos;
res.setOutputBuffer(iob);
// set up GzipOutputFilter to attach to the InternalOutputBuffer
GzipOutputFilter gf = new GzipOutputFilter();
iob.addFilter(gf);
iob.addActiveFilter(gf);
// write a chunk out
ByteChunk chunk = new ByteChunk(1024);
byte[] d = "Hello there tomcat developers, there is a bug in JDK".getBytes();
chunk.append(d, 0, d.length);
iob.doWrite(chunk, res);
// flush the InternalOutputBuffer
iob.flush();
// read from the ByteArrayOutputStream to find out what's being written
// out (flushed)
byte[] dataFound = bos.toByteArray();
// find out what's expected by wrting to GZIPOutputStream and close it
// (to force flushing)
ByteArrayOutputStream gbos = new ByteArrayOutputStream(1024);
GZIPOutputStream gos = new GZIPOutputStream(gbos);
gos.write(d);
gos.close();
// read the expected data
byte[] dataExpected = gbos.toByteArray();
// most of the data should have been flushed out
assertTrue(dataFound.length >= (dataExpected.length - 20));
}
示例7
/**
* Test the interaction betwen gzip and flushing. The idea is to: 1. create
* a internal output buffer, response, and attach an active gzipoutputfilter
* to the output buffer 2. set the output stream of the internal buffer to
* be a ByteArrayOutputStream so we can inspect the output bytes 3. write a
* chunk out using the gzipoutputfilter and invoke a flush on the
* InternalOutputBuffer 4. read from the ByteArrayOutputStream to find out
* what's being written out (flushed) 5. find out what's expected by wrting
* to GZIPOutputStream and close it (to force flushing) 6. Compare the size
* of the two arrays, they should be close (instead of one being much
* shorter than the other one)
*
* @throws Exception
*/
@Test
public void testFlushingWithGzip() throws Exception {
// set up response, InternalOutputBuffer, and ByteArrayOutputStream
Response res = new Response();
InternalOutputBuffer iob = new InternalOutputBuffer(res, 8 * 1024);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
iob.outputStream = bos;
res.setOutputBuffer(iob);
// set up GzipOutputFilter to attach to the InternalOutputBuffer
GzipOutputFilter gf = new GzipOutputFilter();
iob.addFilter(gf);
iob.addActiveFilter(gf);
// write a chunk out
ByteChunk chunk = new ByteChunk(1024);
byte[] d = "Hello there tomcat developers, there is a bug in JDK".getBytes();
chunk.append(d, 0, d.length);
iob.doWrite(chunk, res);
// flush the InternalOutputBuffer
iob.flush();
// read from the ByteArrayOutputStream to find out what's being written
// out (flushed)
byte[] dataFound = bos.toByteArray();
// find out what's expected by wrting to GZIPOutputStream and close it
// (to force flushing)
ByteArrayOutputStream gbos = new ByteArrayOutputStream(1024);
GZIPOutputStream gos = new GZIPOutputStream(gbos);
gos.write(d);
gos.close();
// read the expected data
byte[] dataExpected = gbos.toByteArray();
// most of the data should have been flushed out
assertTrue(dataFound.length >= (dataExpected.length - 20));
}