Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpStaticFileServerHandler Example: Fail to download file in ssl mode #3592

Closed
bobwenx opened this issue Apr 6, 2015 · 3 comments
Closed
Assignees
Labels
Milestone

Comments

@bobwenx
Copy link

bobwenx commented Apr 6, 2015

The netty http-file-server example are unable to download file if ssl mode is enabled:

sh run-example.sh -Dssl http-file-server               
[INFO] Running: http-file-server (io.netty.example.http.file.HttpStaticFileServer -D_ -Dssl)
19:25:32.426 [nioEventLoopGroup-0-0] INFO  i.n.handler.logging.LoggingHandler - [id: 0x88c83b40] REGISTERED
19:25:32.430 [nioEventLoopGroup-0-0] INFO  i.n.handler.logging.LoggingHandler - [id: 0x88c83b40] BIND: 0.0.0.0/0.0.0.0:8443
Open your web browser and navigate to https://127.0.0.1:8443/
19:25:32.434 [nioEventLoopGroup-0-0] INFO  i.n.handler.logging.LoggingHandler - [id: 0x88c83b40, /0:0:0:0:0:0:0:0:8443] ACTIVE

when i try to download file(https://localhost:8443/pom.xml) with chrome, the browser will hang there:
HTTP Request:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36
X-FirePHP-Version:0.0.6

HTTP Response:

pending...

i check the example code, and find the comment(line 200) say:

// HttpChunkedInput will write the end marker (LastHttpContent) for us.
lastContentFuture = sendFileFuture;

However, it didn't write HTTP end marker(that's why the browser hang).

if i change the code to manually write LastHttpContent.EMPTY_LAST_CONTENT, Chrome can download file correctly.

// Line 196
response.headers().set(TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
sendFileFuture =  ctx.write(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)), 
                                          ctx.newProgressivePromise());
// HttpChunkedInput will write the end marker (LastHttpContent) for us.
// lastContentFuture = sendFileFuture;
lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

So, There are something wrong in HttpChunkedInput processing?

@bobwenx bobwenx changed the title HttpStaticFileServerHandler: Fail to download file in ssl mode HttpStaticFileServerHandler Example: Fail to download file in ssl mode Apr 6, 2015
@normanmaurer
Copy link
Member

@bobwenx nope the code of the example is wrong. Could you try to replace:

ctx.write(new HttpChunkedInput(...));

with

ctx.writeAndFlush(new HttpChunkedInput(...));

and let me know if it works ?

@normanmaurer normanmaurer self-assigned this Apr 8, 2015
@bobwenx
Copy link
Author

bobwenx commented Apr 8, 2015

after change code to use writeAndFlush, it works as expected! Chrome can download file correctly.

One more thing, the javadoc of HttpChunkedInput says:

...
Ensure that your HTTP response header contains {@code Transfer-Encoding: chunked}.

However, the HTTP response didn't contains such header:

cache-control:private, max-age=60
connection:keep-alive
content-length:4787
content-type:application/octet-stream
date:Wed, 08 Apr 2015 10:22:12 GMT
expires:Wed, 08 Apr 2015 10:23:12 GMT
last-modified:Sat, 04 Apr 2015 17:47:57 GMT

do we need manually add such header?

// Line 196
response.headers().set(TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
sendFileFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)),
                            ctx.newProgressivePromise());
// HttpChunkedInput will write the end marker (LastHttpContent) for us.
lastContentFuture = sendFileFuture;

normanmaurer added a commit that referenced this issue Apr 10, 2015
Motivation:

We missed to flush the channel when using HttpChunkedInput (this is done when using SSL). This will result in a stale.

Modifications:

Replace ctx.write(...) with ctx.writeAndFlush(...)

Result:

Correctly working example.
normanmaurer added a commit that referenced this issue Apr 10, 2015
Motivation:

We missed to flush the channel when using HttpChunkedInput (this is done when using SSL). This will result in a stale.

Modifications:

Replace ctx.write(...) with ctx.writeAndFlush(...)

Result:

Correctly working example.
normanmaurer added a commit that referenced this issue Apr 10, 2015
Motivation:

We missed to flush the channel when using HttpChunkedInput (this is done when using SSL). This will result in a stale.

Modifications:

Replace ctx.write(...) with ctx.writeAndFlush(...)

Result:

Correctly working example.
@normanmaurer normanmaurer added this to the 4.0.28.Final milestone Apr 10, 2015
@normanmaurer
Copy link
Member

Fixed.. Thanks for reporting

pulllock pushed a commit to pulllock/netty that referenced this issue Oct 19, 2023
Motivation:

We missed to flush the channel when using HttpChunkedInput (this is done when using SSL). This will result in a stale.

Modifications:

Replace ctx.write(...) with ctx.writeAndFlush(...)

Result:

Correctly working example.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants