Open fcp12138 opened 6 years ago
+1 on the issue. If I am not mistaken it should be enough to call gzipOutputStream.close();
after gzipOutputStream.finish();
in sendBodyWithCorrectEncoding
?
Edit: I think I was wrong.
I think a recent PR fixed that. Do you guys mind testing with the current master branch?
@LordFokas will there be a new release with this? I see that there is no release since Aug 2016. Is this repo dead?
use NanoHttpd on android,I find this error:
03-14 17:28:25.858 E/StrictMode( 2530): java.lang.Throwable: Explicit termination method 'end' not called 03-14 17:28:25.858 E/StrictMode( 2530): at dalvik.system.CloseGuard.open(CloseGuard.java:184) 03-14 17:28:25.858 E/StrictMode( 2530): at java.util.zip.Deflater.<init>(Deflater.java:192) 03-14 17:28:25.858 E/StrictMode( 2530): at java.util.zip.GZIPOutputStream.<init>(GZIPOutputStream.java:81) 03-14 17:28:25.858 E/StrictMode( 2530): at java.util.zip.GZIPOutputStream.<init>(GZIPOutputStream.java:54) 03-14 17:28:25.858 E/StrictMode( 2530): at org.nanohttpd.protocols.http.response.Response.sendBodyWithCorrectEncoding(Response.java:308) 03-14 17:28:25.858 E/StrictMode( 2530): at org.nanohttpd.protocols.http.response.Response.sendBodyWithCorrectTransferAndEncoding(Response.java:299) 03-14 17:28:25.858 E/StrictMode( 2530): at org.nanohttpd.protocols.http.response.Response.send(Response.java:268) 03-14 17:28:25.858 E/StrictMode( 2530): at org.nanohttpd.protocols.http.HTTPSession.execute(HTTPSession.java:435) 03-14 17:28:25.858 E/StrictMode( 2530): at org.nanohttpd.protocols.http.ClientHandler.run(ClientHandler.java:75) 03-14 17:28:25.858 E/StrictMode( 2530): at java.lang.Thread.run(Thread.java:818)
And this is My solution
For more details
org.nanohttpd.protocols.http.response.Response
private OutputStream sendBodyWithCorrectTransferAndEncoding(OutputStream outputStream, long pending) throws IOException { if (this.requestMethod != Method.HEAD && this.chunkedTransfer) { ChunkedOutputStream chunkedOutputStream = new ChunkedOutputStream(outputStream); OutputStream end = sendBodyWithCorrectEncoding(chunkedOutputStream, -1); chunkedOutputStream.finish(); return end; } else { return sendBodyWithCorrectEncoding(outputStream, pending); } } private OutputStream sendBodyWithCorrectEncoding(OutputStream outputStream, long pending) throws IOException { if (useGzipWhenAccepted()) { GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream); sendBody(gzipOutputStream, -1); gzipOutputStream.finish(); return gzipOutputStream; } else { sendBody(outputStream, pending); return outputStream; } }
` /**
Sends given response to the socket. */ public OutputStream send(OutputStream outputStream) { SimpleDateFormat gmtFrmt = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss 'GMT'", Locale.getDefault()); gmtFrmt.setTimeZone(TimeZone.getTimeZone("GMT"));
}
`
org.nanohttpd.protocols.http.HTTPSession
` @Override public void execute() throws IOException { Response r = null; try { // Read the first 8192 bytes. // The full header should fit in here. // Apache's default header limit is 8KB. // Do NOT assume that a single read will get the entire header // at once! byte[] buf = new byte[HTTPSession.BUFSIZE]; this.splitbyte = 0; this.rlen = 0; int read = -1; this.inputStream.mark(HTTPSession.BUFSIZE); try { read = this.inputStream.read(buf, 0, HTTPSession.BUFSIZE); } catch (SSLException e) { throw e; } catch (IOException e) { NanoHTTPD.safeClose(this.inputStream); NanoHTTPD.safeClose(this.outputStream); throw new SocketException("NanoHttpd Shutdown"); } if (read == -1) { // socket was been closed NanoHTTPD.safeClose(this.inputStream); NanoHTTPD.safeClose(this.outputStream); throw new SocketException("NanoHttpd Shutdown"); } while (read > 0) { this.rlen += read; this.splitbyte = findHeaderEnd(buf, this.rlen); if (this.splitbyte > 0) { break; } read = this.inputStream.read(buf, this.rlen, HTTPSession.BUFSIZE - this.rlen); }
`