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

请问开启了中间人代理后,代理流式(会持续返回)接口,如何实现也流式(response输出)返回,而不是等到最后响应结束后才一次过返回一堆结果(如json) #281

Open
zzmgc4 opened this issue Jul 17, 2023 · 8 comments

Comments

@zzmgc4
Copy link

zzmgc4 commented Jul 17, 2023

{“result”:"1"}
{“result”:"12"}
{“result”:"123"}

@monkeyWie
Copy link
Owner

monkeyWie commented Jul 18, 2023

不要使用FullResponseIntercept就行了,因为要完整解码响应的话肯定是得拿到全部的报文才行。


Just don't use FullResponseIntercept, because if you want to fully decode the response, you must get all the packets.

@zzmgc4
Copy link
Author

zzmgc4 commented Jul 18, 2023

因为需要拿到请求的报文和响应的报文做内容审查,但是又不对原来的响应速度造成影响,可以一边先流式输出,等到结束才拿到所有报文存起来麽

@monkeyWie
Copy link
Owner

理论上是可以的,但是需要自己实现一个类似FullResponseIntercept的拦截器,不过如果只是做记录的话很简单的,把每次过来的Content缓存住,判断是最后一个Content的时候再解码处理

@zzmgc4
Copy link
Author

zzmgc4 commented Jul 18, 2023

pipeline.addLast(new 类似FullResponseIntercept的拦截器() {
public boolean match(HttpRequest httpRequest, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) {
return true;
}
});

如果是这样下面这段还需要吗
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) throws Exception {
this.fullHttpRequest.release();
FullHttpResponse fullHttpResponse = (FullHttpResponse) httpResponse;
//保存数据库
save();
pipeline.afterResponse(clientChannel, proxyChannel, httpResponse);
}

@monkeyWie
Copy link
Owner

monkeyWie commented Jul 18, 2023

实现这个最基础的拦截器HttpProxyIntercept里的这个方法就行了:

public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent,
                              HttpProxyInterceptPipeline pipeline)

Just implement this method in the most basic interceptor HttpProxyIntercept:

public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent,
                              HttpProxyInterceptPipeline pipeline)

@zzmgc4
Copy link
Author

zzmgc4 commented Jul 18, 2023

感谢大佬,我试试


Thanks guys, I'll try

@zzmgc4
Copy link
Author

zzmgc4 commented Jul 18, 2023

是这个意思嘛
public abstract class AsynFullResponseIntercept extends HttpProxyIntercept {
private List contentList = new ArrayList<>();

public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent,
                          HttpProxyInterceptPipeline pipeline) throws Exception {
    contentList.add(httpContent);

    if (httpContent instanceof LastHttpContent) {
        processContents(contentList);
        contentList.clear();

    }

    // Continue with the pipeline
    pipeline.afterResponse(clientChannel, proxyChannel, httpContent);
}

//say hello


private void processContents(List<HttpContent> contents) {
    StringBuilder stringBuilder = new StringBuilder();

    for (HttpContent content : contents) {
        ByteBuf byteBuf = content.content();
        stringBuilder.append(byteBuf.toString(CharsetUtil.UTF_8));
    }
    String result = stringBuilder.toString();
}

@monkeyWie
Copy link
Owner

monkeyWie commented Jul 19, 2023

嗯,就是这个意思,然后细节方面你再调下应该就行了


Well, that’s what it means, and then you should adjust the details

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