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

HttpClientRequest handle correctly Buffers with a readerIndex > 0 #1951

Closed
TanyaGaleyev opened this issue Apr 19, 2017 · 19 comments
Closed

HttpClientRequest handle correctly Buffers with a readerIndex > 0 #1951

TanyaGaleyev opened this issue Apr 19, 2017 · 19 comments

Comments

@TanyaGaleyev
Copy link

We are using code similar to example. And IndexOutOfBoundsException was observed. After debugging for a while I found a reason of the problem.
In proxy code we have something like:

public void processRequest(HttpServerRequest request) {
  HttpClientRequest destRequest = ...
  request.handler(destRequest::write);
  ...
}

In file HttpClientRequestImpl there is a code:

void write(ByteBuf buf, boolean end) {
  ...
  CompositeByteBuf pending;
  ...
  pending.addComponent(buff).writerIndex(pending.writerIndex() + buff.writerIndex());
  ...
}

And if buff has readerIndex not equal to 0 then call to writerIndex fails with reasonable IndexOutOfBoundsException.
Current workaround is using Buffer.slice() before passing buffer read by http server to http client. But I think that buffers with non 0 readerIndex should be handled properly by HttpClientRequestImpl.

@vietj
Copy link
Member

vietj commented Apr 19, 2017

can you provide a reproducer ?

@TanyaGaleyev
Copy link
Author

@vietj
The problem is reproduced in unit test in this repo. It turns out that with plain everything works fine with plain HTTP, but starts failing with HTTPS.

@vietj
Copy link
Member

vietj commented Apr 20, 2017

thanks

@vietj
Copy link
Member

vietj commented Apr 20, 2017

it seems due to this change in Netty : netty/netty@9306333#diff-7191c076eacd4a1a53d60604a5d2a251R219

previous version of duplicate called by HttpClientRequestImpl#write(Buffer) really got a slice and now the duplicate buffer has a readerIndex > 0 because the original duplicated buffer as an adjustment > 0

@TanyaGaleyev
Copy link
Author

Thanks for investigation! What are there the next steps for this issue?
Also I wonder why should we assume that buffer coming to HttpClientRequestImpl#write(Buffer) has readerIndex == 0? May be one can use:

pending.addComponent(buff).writerIndex(pending.writerIndex() + buff.READABLEBYTES());

Or even more use CompositeByteBuf.addComponent(boolean increaseWriterIndex, ByteBuf buffer) with increaseWriterIndex == true?
Sorry if my suggestions are silly =)

@vietj
Copy link
Member

vietj commented Apr 20, 2017

@TanyaGaleyev actually the buffer we obtain is via io.vertx.core.buffer.Buffer#getByteBuf() that returns a duplicate of the buffer with its readerIndex == 0... until now.

@vietj
Copy link
Member

vietj commented Apr 20, 2017

I think it will be fixed in 3.4.2 with improvements in Buffer class and also a test with a buffer that has readerIndex > 0 for write method and a fix.

@vietj
Copy link
Member

vietj commented Apr 20, 2017

CompositeByteBuf.addComponent(boolean increaseWriterIndex, ByteBuf buffer) seems like a good fix as you said, we just need to have a corresponding test for it that write a Buffer wrapping a ByteBuf with a readerIndex > 0 which can be obtained with something like Buffer.buffer("foobar").slice(3, 6) I think.

@vietj vietj changed the title Vertx based proxy fails with IndexOutOfBoundsException HttpClientRequest#write fails to write Buffer returning a ByteBuf with its readerIndex > 0 Apr 20, 2017
@vietj vietj closed this as completed in 58bb608 Apr 20, 2017
@vietj
Copy link
Member

vietj commented Apr 20, 2017

I pushed a fix based on your suggestion with a test, would you mind to check it's fine on your side ?

@TanyaGaleyev
Copy link
Author

@vietj
I noticed that there is also another line in HttpClientRequestImpl that can be also affected:

cachedChunks.addComponent(buff).writerIndex(cachedChunks.writerIndex() + buff.writerIndex());

@TanyaGaleyev
Copy link
Author

@vietj
I hope to check it in my environment soon. Is there any maven repository where I can take latest dev version?

@vietj
Copy link
Member

vietj commented Apr 21, 2017

good catch, I'll try to make a test for this one.

you can get the snapshots from the Sonatype OSS snapshot repository https://oss.sonatype.org/content/repositories/snapshots/

@vietj
Copy link
Member

vietj commented Apr 22, 2017

@TanyaGaleyev the computation of the content-length needs also to be fixed headers().set(CONTENT_LENGTH, String.valueOf(buff.writerIndex()));

@vietj vietj changed the title HttpClientRequest#write fails to write Buffer returning a ByteBuf with its readerIndex > 0 HttpClientRequest#write handles correctly Buffers with a readerIndex > 0 Apr 22, 2017
@vietj vietj changed the title HttpClientRequest#write handles correctly Buffers with a readerIndex > 0 HttpClientRequest handle correctly Buffers with a readerIndex > 0 Apr 22, 2017
@vietj
Copy link
Member

vietj commented Apr 22, 2017

I fixed those in HttpClientRequest

@TanyaGaleyev
Copy link
Author

@vietj
Thanks for fixes. I checked my production code with 3.4.2-SNAPSHOT, everything works fine.

@TanyaGaleyev
Copy link
Author

When 3.4.2 will be released?

@vietj
Copy link
Member

vietj commented Apr 24, 2017

around May, we aim to start the release process early may and conclude before june

@vietj
Copy link
Member

vietj commented Apr 24, 2017

not saying it's a slow process, just saying that we shall do it peacefully :-)

@TanyaGaleyev
Copy link
Author

Thanks for info.

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

No branches or pull requests

2 participants