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

How to update request body with a longer content? #444

Open
wangqunfeng opened this issue Jan 11, 2021 · 0 comments
Open

How to update request body with a longer content? #444

wangqunfeng opened this issue Jan 11, 2021 · 0 comments

Comments

@wangqunfeng
Copy link

wangqunfeng commented Jan 11, 2021

hello guys,

how to update httpContent in filters ?

my code is here, it works if i change the capacity shorter or stay unchanged, but not works if i change it bigger. what's the problem?

`

private class MyHttpFiltersSourceAdapter extends HttpFiltersSourceAdapter {
    @Override
    public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext clientCtx) {
        Log.v(TAG, "request: " + originalRequest.hashCode() + " uri: " + originalRequest.getUri() + ", method: " + originalRequest.getMethod());
        if (originalRequest.getMethod() == HttpMethod.GET
                || originalRequest.getMethod() == HttpMethod.HEAD
                || originalRequest.getMethod() == HttpMethod.POST) {
            return new MyHttpFilter(originalRequest, clientCtx);
        }
    }
}

private class MyHttpFilter extends HttpFiltersAdapter {
    @Override
    public HttpResponse clientToProxyRequest(HttpObject httpObject) {
        if (httpObject instanceof LastHttpContent) {
            LastHttpContent httpContent = (LastHttpContent) httpObject;
            ByteBuf byteBuffer = httpContent.content().slice();
            byte[] body = new byte[byteBuffer.readableBytes()];
            byteBuffer.getBytes(0, body);
            String contentString = new String(body, io.netty.util.CharsetUtil.UTF_8);
            String newBody = "111111111111111111";
            try {
                int idx = byteBufIdx(byteBuffer, 0);
                if (idx >= 0) {
                    int writerIndex = byteBuffer.writerIndex();
                    int readSize = byteBuffer.readableBytes();
                    Log.d(TAG, "before update readable " + readSize + " writeidx " + byteBuffer.writerIndex());

                    ByteBuf bodyContent = Unpooled.copiedBuffer(newBody, io.netty.util.CharsetUtil.UTF_8);
                    int newSize = bodyContent.readableBytes();

                    // tricky: use internal api to avoid length check
                    int innerCap = byteBuffer.unwrap().capacity();
                    byteBuffer.unwrap().capacity(innerCap + newSize - readSize);
                    byteBuffer.unwrap().setBytes(idx, bodyContent, 0, newSize);

                    // update outter index&cap
                    int cc = byteBufAdjustCapacity(byteBuffer, newSize - readSize);
                    byteBuffer.writerIndex(writerIndex - readSize + newSize);
                    Log.d(TAG, "update post content " + contentString + " to " + newBody + " new cap " + cc + " writeidx " + byteBuffer.writerIndex());

                    byte[] readout = new byte[byteBuffer.readableBytes()];

                    byteBuffer.getBytes(byteBuffer.readerIndex(), readout);
                    Log.d(TAG, "bytebuf: " + new String(readout, io.netty.util.CharsetUtil.UTF_8));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}

`

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

1 participant