Skip to content

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

@wangqunfeng

Description

@wangqunfeng

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();
            }

        }
    }
}

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions