Skip to content

Commit

Permalink
[FLINK-5109] [webfrontend] Fix invalid content-encoding
Browse files Browse the repository at this point in the history
This closes apache#2898
  • Loading branch information
tibor.moger authored and greghogan committed Dec 3, 2016
1 parent e4f802d commit 08e7ba4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
public class HttpRequestHandler extends SimpleChannelInboundHandler<HttpObject> {

private static final Charset ENCODING = Charset.forName("UTF-8");

/** A decoder factory that always stores POST chunks on disk */
private static final HttpDataFactory DATA_FACTORY = new DefaultHttpDataFactory(true);

Expand All @@ -80,7 +80,7 @@ public class HttpRequestHandler extends SimpleChannelInboundHandler<HttpObject>
public HttpRequestHandler(File tmpDir) {
this.tmpDir = tmpDir;
}

@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
if (currentDecoder != null) {
Expand All @@ -94,12 +94,12 @@ public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
if (msg instanceof HttpRequest) {
currentRequest = (HttpRequest) msg;
currentRequestPath = null;

if (currentDecoder != null) {
currentDecoder.destroy();
currentDecoder = null;
}

if (currentRequest.getMethod() == HttpMethod.GET || currentRequest.getMethod() == HttpMethod.DELETE) {
// directly delegate to the router
ctx.fireChannelRead(currentRequest);
Expand All @@ -118,43 +118,43 @@ else if (currentDecoder != null && msg instanceof HttpContent) {
// received new chunk, give it to the current decoder
HttpContent chunk = (HttpContent) msg;
currentDecoder.offer(chunk);

try {
while (currentDecoder.hasNext()) {
InterfaceHttpData data = currentDecoder.next();

// IF SOMETHING EVER NEEDS POST PARAMETERS, THIS WILL BE THE PLACE TO HANDLE IT
// all fields values will be passed with type Attribute.

if (data.getHttpDataType() == HttpDataType.FileUpload) {
DiskFileUpload file = (DiskFileUpload) data;
if (file.isCompleted()) {
String name = file.getFilename();

File target = new File(tmpDir, UUID.randomUUID() + "_" + name);
file.renameTo(target);

QueryStringEncoder encoder = new QueryStringEncoder(currentRequestPath);
encoder.addParam("filepath", target.getAbsolutePath());
encoder.addParam("filename", name);

currentRequest.setUri(encoder.toString());
}
}

data.release();
}
}
catch (EndOfDataDecoderException ignored) {}

if (chunk instanceof LastHttpContent) {
HttpRequest request = currentRequest;
currentRequest = null;
currentRequestPath = null;

currentDecoder.destroy();
currentDecoder = null;

// fire next channel handler
ctx.fireChannelRead(request);
}
Expand All @@ -163,20 +163,19 @@ else if (currentDecoder != null && msg instanceof HttpContent) {
catch (Throwable t) {
currentRequest = null;
currentRequestPath = null;

if (currentDecoder != null) {
currentDecoder.destroy();
currentDecoder = null;
}

if (ctx.channel().isActive()) {
byte[] bytes = ExceptionUtils.stringifyException(t).getBytes(ENCODING);

DefaultFullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR,
Unpooled.wrappedBuffer(bytes));

response.headers().set(HttpHeaders.Names.CONTENT_ENCODING, "utf-8");

response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ private void sendError(ChannelHandlerContext ctx, String error) {
HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(error.getBytes()));

response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaders.Names.CONTENT_ENCODING, "utf-8");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

ctx.writeAndFlush(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class RuntimeMonitorHandler extends RuntimeMonitorHandlerBase {
private static final Charset ENCODING = Charset.forName("UTF-8");

public static final String WEB_MONITOR_ADDRESS_KEY = "web.monitor.address";

private final RequestHandler handler;

public RuntimeMonitorHandler(
Expand Down Expand Up @@ -102,7 +102,6 @@ protected void respondAsLeader(ChannelHandlerContext ctx, Routed routed, ActorGa
: Unpooled.wrappedBuffer(e.getMessage().getBytes(ENCODING));
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, message);
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaders.Names.CONTENT_ENCODING, "utf-8");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
LOG.debug("Error while handling request", e);
}
Expand All @@ -111,7 +110,6 @@ protected void respondAsLeader(ChannelHandlerContext ctx, Routed routed, ActorGa
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaders.Names.CONTENT_ENCODING, "utf-8");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

LOG.debug("Error while handling request", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
*/
@ChannelHandler.Sharable
public class ConstantTextHandler extends SimpleChannelInboundHandler<Routed> {

private final byte[] encodedText;

public ConstantTextHandler(String text) {
try {
this.encodedText = text.getBytes("UTF-8");
Expand All @@ -48,16 +48,15 @@ public ConstantTextHandler(String text) {
throw new RuntimeException(e.getMessage(), e);
}
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, Routed routed) throws Exception {
HttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(encodedText));

response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, encodedText.length);
response.headers().set(HttpHeaders.Names.CONTENT_ENCODING, "utf-8");
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");

KeepAliveWrite.flush(ctx, routed.request(), response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public static HttpResponse getRedirectResponse(String redirectAddress, String pa
HttpResponse redirectResponse = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.TEMPORARY_REDIRECT);
redirectResponse.headers().set(HttpHeaders.Names.LOCATION, newLocation);
redirectResponse.headers().set(HttpHeaders.Names.CONTENT_ENCODING, "utf-8");
redirectResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);

return redirectResponse;
Expand All @@ -102,7 +101,6 @@ public static HttpResponse getUnavailableResponse() throws UnsupportedEncodingEx
HttpResponse unavailableResponse = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.SERVICE_UNAVAILABLE, Unpooled.wrappedBuffer(bytes));

unavailableResponse.headers().set(HttpHeaders.Names.CONTENT_ENCODING, "utf-8");
unavailableResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, bytes.length);
unavailableResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, MimeTypes.getMimeTypeForExtension("txt"));

Expand Down

0 comments on commit 08e7ba4

Please sign in to comment.