{"action":"opened","issue":{"url":"https://api.github.com/repos/spring-cloud/spring-cloud-gateway/issues/610","repository_url":"https://api.github.com/repos/spring-cloud/spring-cloud-gateway","labels_url":"https://api.github.com/repos/spring-cloud/spring-cloud-gateway/issues/610/labels{/name}","comments_url":"https://api.github.com/repos/spring-cloud/spring-cloud-gateway/issues/610/comments","events_url":"https://api.github.com/repos/spring-cloud/spring-cloud-gateway/issues/610/events","html_url":"https://github.com/spring-cloud/spring-cloud-gateway/issues/610","id":371869901,"node_id":"MDU6SXNzdWUzNzE4Njk5MDE=","number":610,"title":"How to filter my html element by gateway?","user":{"login":"lindaosong","id":26646223,"node_id":"MDQ6VXNlcjI2NjQ2MjIz","avatar_url":"https://avatars2.githubusercontent.com/u/26646223?v=4","gravatar_id":"","url":"https://api.github.com/users/lindaosong","html_url":"https://github.com/lindaosong","followers_url":"https://api.github.com/users/lindaosong/followers","following_url":"https://api.github.com/users/lindaosong/following{/other_user}","gists_url":"https://api.github.com/users/lindaosong/gists{/gist_id}","starred_url":"https://api.github.com/users/lindaosong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lindaosong/subscriptions","organizations_url":"https://api.github.com/users/lindaosong/orgs","repos_url":"https://api.github.com/users/lindaosong/repos","events_url":"https://api.github.com/users/lindaosong/events{/privacy}","received_events_url":"https://api.github.com/users/lindaosong/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-19T09:08:23Z","updated_at":"2018-10-19T09:08:23Z","closed_at":null,"author_association":"NONE","body":"When I send a request through the gateway, I hope that I can parse the elements of the page in the gateway and then block some of the elements. I try to create a filter to get the page information, then process the page elements according to the rules, and finally The modified page is returned to the browser.\r\n\r\nThe problem came. When I tried to use it, I found that the program reported a \"Only one connection receive subscriber allowed\" error. I found that because the obtained page body only allowed one read operation, so I tried to cache the page first. Load processing, but found that there are still problems, the following is part of my code:\r\npublic class HtmlContentVisibleFilter implements GlobalFilter, Ordered {\r\n@Override\r\n    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {\r\n       return chain.filter(exchange).then(\r\n                Mono.fromRunnable(() -> {\r\n                    ServerHttpRequest request = exchange.getRequest();\r\n                    ServerHttpResponse response = exchange.getResponse();\r\n                    HttpClientResponse clientResponse = exchange.getAttribute(ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR);\r\n                    boolean isOkStatus = response.getStatusCode() == HttpStatus.OK;\r\n                    boolean filterFlag = isOkStatus && clientResponse != null;\r\n                    if (filterFlag) {\r\n                        NettyDataBufferFactory factory = (NettyDataBufferFactory) response.bufferFactory();\r\n                        Flux<DataBuffer> body = clientResponse.receive().retain().map(factory::wrap);\r\n                        MediaType contentType = response.getHeaders().getContentType();\r\n                        boolean pageFlag = contentType.includes(MediaType.TEXT_HTML);\r\n                        if (pageFlag) {\r\n                             //TODO to modify page body\r\n                       AtomicReference<String> bodyRef = new AtomicReference<>();\r\n                        body.subscribe(dataBuffer -> {\r\n                           CharBuffer charBuffer = StandardCharsets.UTF_8.decode(dataBuffer.asByteBuffer());\r\n                            DataBufferUtils.release(dataBuffer);\r\n                           bodyRef.set(charBuffer.toString());\r\n                        });\r\n\r\n                       String bodyStr = bodyRef.get();\r\n                       System.out.println(bodyStr);\r\n                        DataBuffer bodyDataBuffer = stringBuffer(bodyStr);\r\n                       Flux<DataBuffer> bodyFlux = Flux.just(bodyDataBuffer);\r\n                       request = new ServerHttpRequestDecorator(request) {\r\n                            @Override\r\n                            public Flux<DataBuffer> getBody() {\r\n                               return bodyFlux;\r\n                           }                        };\r\n                        return chain.filter(exchange.mutate().request(request).build());\r\n                             response.writeAndFlushWith(body.map(Flux::just));\r\n                        }\r\n               }else{\r\n                       response.writeWith(body);\r\n                 }\r\n}\r\n\r\n I do not know if anyone knows where the problem is? Or does anyone know if the gateway has a more convenient way to deal with this problem?"}}
{"actor":{"display_login":"lindaosong"}}