java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
net.i2p.i2ptunnel.HTTPResponseOutputStream
net.i2p.i2ptunnel.I2PTunnelHTTPServer.CompressedResponseOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
- Enclosing class:
I2PTunnelHTTPServer
private static class I2PTunnelHTTPServer.CompressedResponseOutputStreamextends HTTPResponseOutputStream
Gzips the response on its way to the browser.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate GZIPOutputStreamThe gzip stream for the response.private final StringRequest headers used to judge compression.Content types that are already compressed; skip re-compression.Fields inherited from class HTTPResponseOutputStream
_contentEncoding, _contentType, _dataExpected, _gzip, _headerBuffer, _keepAliveIn, _keepAliveOutModifier and TypeFieldDescriptionprotected StringResponse Content-Encoding header value, lower-case and trimmed.protected StringResponse Content-Type header value, lower-case and trimmed.protected longContent-Length of the body in bytes, -1 if unknown or chunked, 0 for a bodyless response.protected booleanSet when the response carries Content-Encoding: x-i2p-gzip, so the body is gunzipped.protected ByteArrayPooled buffer holding the response header block while it is parsed, null once emitted.protected booleanTrue if the outproxy (I2P) side connection may be reused after this response.protected booleanTrue if the browser side socket may be reused after this response.Fields inherited from class FilterOutputStream
outConstructor Summary
ConstructorsConstructorDescriptionCompressedResponseOutputStream(OutputStream o, boolean keepalive, String requestHeaders) Create a compressed response output stream.Method Summary
Public MethodsModifier and TypeMethodDescriptionprotected voidWrap the output stream with gzip compression if compression is enabled.voidfinish()Finish gzipping but don't close the output stream if keepalive is true.protected voidWrite the Content-Encoding header if compression is enabled.protected booleanDetermine if the response should be gzip-compressed.Methods inherited from class HTTPResponseOutputStream
canRangeResume, clearTransientResumeFailure, close, getBodyReceived, getDataExpected, getHeaderWritten, getKeepAliveIn, getKeepAliveOut, isTransientResumeFailure, prepareBodyResume, write, writeModifier and TypeMethodDescriptionbooleanWhether a Content-Length body can be safely spliced with Range resume: headers written, a positive Content-Length, some body still missing, and no transparent gzip decode (which would desync the Range offset from wire bytes).voidClear the transient-failure flag after the resume loop has observed it (budget refund / non-Range fallback decided).voidclose()Release the pooled header buffer and close the underlying stream.longEntity-body bytes forwarded to the browser after the first headers.longOriginal response Content-Length, or -1 if unknown/chunked/HEAD-empty.booleanWhether the first response header block has been written to the browser.booleanShould we keep the input stream alive when done?booleanShould we keep the output stream alive when done? Only supported for the browser socket side.booleanTrue when a transient status (408/502/503/504) aborted a Range resume.voidEnter mid-body resume mode: the next header block parsed from a fresh tunnel is consumed for bookkeeping only and must not be written to the browser (the original status line and Content-Length were already sent).voidwrite(byte[] buf, int off, int len) Buffer bytes into the header block until the blank line arrives, then write the header out and forward the remaining bytes as the entity body.voidwrite(int c) Write a single byte, buffering it until the header block is complete.Methods inherited from class FilterOutputStream
flush, writeMethods inherited from class OutputStream
nullOutputStream
Field Details
_gzipOut
The gzip stream for the response._requestHeaders
Request headers used to judge compression.COMPRESSED_TYPES
Constructor Details
CompressedResponseOutputStream
Create a compressed response output stream.- Parameters:
o- the underlying output streamkeepalive- if true, don't close the stream on finishrequestHeaders- the HTTP request headers for Accept-Encoding check
Method Details
finish
Finish gzipping but don't close the output stream if keepalive is true. Overridden to peek at response code. Always returns line.- Throws:
IOException- if finishing the gzip stream fails
shouldCompress
protected boolean shouldCompress()Determine if the response should be gzip-compressed. Skips small responses, images, audio, video, and already-compressed formats. Compression is inline on the server side, decompression on the client side, so we avoid re-compressing formats that are already compressed.- Overrides:
shouldCompressin classHTTPResponseOutputStream- Returns:
- true if the response should be compressed
finishHeaders
Write the Content-Encoding header if compression is enabled.- Overrides:
finishHeadersin classHTTPResponseOutputStream- Throws:
IOException- if writing the header fails
beginProcessing
Wrap the output stream with gzip compression if compression is enabled.- Overrides:
beginProcessingin classHTTPResponseOutputStream- Throws:
IOException- if creating the gzip stream fails