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

Fix Bytebuf leak issue in resteasy. #593

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ private void setupHandlers(SocketChannel ch, RequestDispatcher dispatcher,
channelPipeline.addLast(new HttpObjectAggregator(maxRequestSize));
channelPipeline.addLast(new HttpResponseEncoder());
channelPipeline.addLast(httpChannelHandlers.toArray(new ChannelHandler[httpChannelHandlers.size()]));
channelPipeline.addLast(new RestEasyHttpRequestDecoder(dispatcher.getDispatcher(), root, protocol));
channelPipeline.addLast(new SofaRestEasyHttpRequestDecoder(dispatcher.getDispatcher(), root, protocol)); // CHANGE: Use sofa class
channelPipeline.addLast(new RestEasyHttpResponseEncoder());
channelPipeline.addLast(eventExecutor, new SofaRestRequestHandler(dispatcher)); // CHANGE: 用sofa的处理类
channelPipeline.addLast(eventExecutor, new SofaRestRequestHandler(dispatcher)); // CHANGE: Use sofa class
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.server.rest;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpUtil;
import org.jboss.resteasy.core.SynchronousDispatcher;
import org.jboss.resteasy.logging.Logger;
import org.jboss.resteasy.plugins.server.netty.NettyHttpRequest;
import org.jboss.resteasy.plugins.server.netty.NettyHttpResponse;
import org.jboss.resteasy.plugins.server.netty.NettyUtil;
import org.jboss.resteasy.plugins.server.netty.RestEasyHttpRequestDecoder;
import org.jboss.resteasy.specimpl.ResteasyHttpHeaders;
import org.jboss.resteasy.spi.ResteasyUriInfo;

import java.util.List;

/**
* Code base on RestEasyHttpRequestDecoder, Code improved please search CHANGE
* @see org.jboss.resteasy.plugins.server.netty.RestEasyHttpRequestDecoder
*/
@Sharable
public class SofaRestEasyHttpRequestDecoder extends MessageToMessageDecoder<io.netty.handler.codec.http.HttpRequest> {
private final static Logger logger = Logger
.getLogger(org.jboss.resteasy.plugins.server.netty.RestEasyHttpRequestDecoder.class);
private final SynchronousDispatcher dispatcher;
private final String servletMappingPrefix;
private final String proto;

public SofaRestEasyHttpRequestDecoder(SynchronousDispatcher dispatcher, String servletMappingPrefix,
RestEasyHttpRequestDecoder.Protocol protocol) {
this.dispatcher = dispatcher;
this.servletMappingPrefix = servletMappingPrefix;
if (protocol == RestEasyHttpRequestDecoder.Protocol.HTTP) {
proto = "http";
} else {
proto = "https";
}
}

@Override
protected void decode(ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpRequest request, List<Object> out)
throws Exception {
boolean keepAlive = HttpUtil.isKeepAlive(request);
final NettyHttpResponse response = new NettyHttpResponse(ctx, keepAlive, dispatcher.getProviderFactory());
final ResteasyHttpHeaders headers;
final ResteasyUriInfo uriInfo;
try {
headers = NettyUtil.extractHttpHeaders(request);

uriInfo = NettyUtil.extractUriInfo(request, servletMappingPrefix, proto);
NettyHttpRequest nettyRequest = new NettyHttpRequest(ctx, headers, uriInfo, request.method().name(),
dispatcher, response, HttpUtil.is100ContinueExpected(request));
if (request instanceof HttpContent) {
HttpContent content = (HttpContent) request;

// Does the request contain a body that will need to be retained
if (content.content().readableBytes() > 0) {
ByteBuf buf = content.content().retain();
ByteBufInputStream in = new ByteBufInputStream(buf, true); // CHANGE: set releaseOnClose to true
nettyRequest.setInputStream(in);
}

out.add(nettyRequest);
}
} catch (Exception e) {
response.sendError(400);
// made it warn so that people can filter this.
logger.warn("Failed to parse request.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jboss.resteasy.spi.Failure;

import javax.ws.rs.core.HttpHeaders;
import java.io.InputStream;
import java.net.InetSocketAddress;

import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE;
Expand Down Expand Up @@ -120,6 +121,10 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Except
response.finish();
}
} finally {
InputStream input = request.getInputStream();
if (input != null) {
input.close();
}
if (EventBus.isEnable(ServerEndHandleEvent.class)) {
EventBus.post(new ServerEndHandleEvent());
}
Expand Down