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

正则内存溢出 #8

Closed
l454124613 opened this issue Dec 19, 2017 · 5 comments
Closed

正则内存溢出 #8

l454124613 opened this issue Dec 19, 2017 · 5 comments

Comments

@l454124613
Copy link

lee.study.proxyee.util.ProtoUtil
Pattern pattern = Pattern.compile("^(?:https?://)?(?[^:]*)(?::(?\d+))?$");这里使用,在使用时,总是会java.lang.StackOverflowError,调节了-Xms100M -Xmx2200M,依旧没用
帮忙看一下

@monkeyWie
Copy link
Owner

monkeyWie commented Dec 19, 2017

这个bug是什么情况才会出现呢,看起来是栈内存溢出了,估计是有无限递归的情况

@l454124613
Copy link
Author

l454124613 commented Dec 19, 2017

使用方法如下:
new Thread(new Runnable() {
@OverRide
public void run() {
new HttpProxyServer()
.proxyInterceptInitializer(new HttpProxyInterceptInitializer() {
@OverRide
public void init(HttpProxyInterceptPipeline pipeline) {
pipeline.addLast(new CertDownIntercept());
pipeline.addLast(new HttpProxyIntercept() {
@OverRide
public void beforeRequest(Channel clientChannel, HttpRequest httpRequest,
HttpProxyInterceptPipeline pipeline) throws Exception {

                       AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
                            //转到下一个拦截器处理

//----以下是截取uri提取部分,和代理无关
map.put(clientChannel.id().asShortText(),httpRequest.uri());
if(lt.size()>0){
for (int i = 0; i <lt.size() ; i++) {
String tm= httpRequest.uri();
String ta=lt.get(i).getValue();
if(ta.contains("(")&&ta.contains(")")){
int t1=ta.indexOf("(");
int t2=ta.lastIndexOf(")");
String tb=ta.substring(t1,t2+1);
String re11=ta.replace(tb,"tmppmt112233qq");
ta= re11.replace(".","\.").replace("?","\?").replace("+","\+");
tb=tb.substring(1,t2-t1);
ta= ta.replace("tmppmt112233qq",tb);

                                    }else {
                                        ta=ta.replace(".","\\.").replace("?","\\?").replace("+","\\+");
                                    }

//----以上是截取uri提取部分,和代理无关

                                    if(httpRequest.uri().matches(ta)){
                                        String html = "<html><body>intercept</body><html>";
                                        HttpResponse hookResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK);
                                        hookResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
                                        hookResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, html.getBytes().length);
                                        pipeline.getDefault().afterResponse(clientChannel, null , hookResponse, pipeline);
                                        HttpContent hookContent = new DefaultLastHttpContent();
                                        hookContent.content().writeBytes(html.getBytes());
                                        pipeline.getDefault().afterResponse(clientChannel, null, hookContent, pipeline);

                                    }else {
                                        pipeline.beforeRequest(clientChannel, httpRequest);
                                    }

                                }
                            }

                        }

                        @Override
                        public void afterResponse(Channel clientChannel, Channel proxyChannel,
                                                  HttpResponse httpResponse,
                                                  HttpProxyInterceptPipeline pipeline) throws Exception {
                            //拦截响应,添加一个响应头
                            //httpResponse.headers().add("intercept", "test");
                            if(map.containsKey(proxyChannel.id().asShortText())){
                                map.remove(proxyChannel.id().asShortText());
                            }
                            pipeline.afterResponse(clientChannel, proxyChannel, httpResponse);
                        }
                    });
                }
            }).start(8102);

}

}).start();

}

@monkeyWie
Copy link
Owner

你是在for循环里调用了pipeline.beforeRequest(clientChannel, httpRequest)吗,这个是处理完逻辑之后再调用,使用下一个handler来处理的

@monkeyWie
Copy link
Owner

不经过服务器,直接本地拦截可以写的更简单点

pipeline.addLast(new HttpProxyIntercept() {
              @Override
              public void beforeRequest(Channel clientChannel, HttpRequest httpRequest,
                  HttpProxyInterceptPipeline pipeline) throws Exception {
                if (httpRequest.uri().equals("/test.html")) {  //匹配的uri直接返回response
                  String html = "<html><body>intercept</body><html>";
                  HttpResponse hookResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK);
                  hookResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
                  hookResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, html.getBytes().length);
                  //直接输出http响应体至客户端,不走后面的转发handler了
                  clientChannel.writeAndFlush(hookResponse);
                  HttpContent hookContent = new DefaultLastHttpContent();
                  hookContent.content().writeBytes(html.getBytes());
                  clientChannel.writeAndFlush(hookContent );
                }else{
                  pipeline.beforeRequest(clientChannel, httpRequest);
                }
              }
            });

@l454124613
Copy link
Author

成功啦,谢谢大神了,果然多次handle 了

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