Skip to content

Commit

Permalink
Fixes #153
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed May 6, 2022
1 parent 9d6f024 commit 4a5d587
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpChunkedInput;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedFile;
import io.netty.util.AttributeKey;
import io.netty.util.CharsetUtil;
import org.atmosphere.nettosphere.util.MimeType;
import org.atmosphere.nettosphere.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -75,7 +78,7 @@
import java.util.regex.Pattern;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpHeaders.Names.IF_MODIFIED_SINCE;
import static io.netty.handler.codec.http.HttpHeaders.Names.IF_MODIFIED_SINCE;
import static io.netty.handler.codec.http.HttpHeaders.Names.LOCATION;
import static io.netty.handler.codec.http.HttpMethod.GET;
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
Expand All @@ -85,6 +88,7 @@
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_MODIFIED;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_0;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

/**
Expand Down Expand Up @@ -163,6 +167,7 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr
sendError(ctx, METHOD_NOT_ALLOWED, request);
return;
}
final boolean keepAlive = HttpUtil.isKeepAlive(request);

File file = null;
RandomAccessFile raf = null;
Expand All @@ -180,15 +185,6 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr
continue;
}

// if (file.isDirectory()) {
// if (uri.endsWith("/")) {
// sendListing(ctx, file);
// } else {
// sendRedirect(ctx, uri + '/');
// }
// return;
// }

try {
raf = new RandomAccessFile(file, "r");
found = true;
Expand All @@ -205,7 +201,9 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr
}
request.headers().add(SERVICED, "true");

ctx.pipeline().addBefore(BridgeRuntime.class.getName(), "encoder", new HttpResponseEncoder());
if (Utils.isJersey()) {
ctx.pipeline().addBefore(BridgeRuntime.class.getName(), "encoder", new HttpResponseEncoder());
}

// Cache Validation
String ifModifiedSince = request.headers().get(IF_MODIFIED_SINCE);
Expand All @@ -223,17 +221,17 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr
}
}


long fileLength = raf.length();


HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
HttpHeaders.setContentLength(response, fileLength);
contentType(request, response, file);
HttpUtil.setContentLength(response, fileLength);
setContentTypeHeader(response, file);
setDateAndCacheHeaders(response, file);
// if (HttpHeaders.isKeepAlive(request)) {
// response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
// }

if (!keepAlive) {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
} else if (request.protocolVersion().equals(HTTP_1_0)) {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}

// Write the initial line and the header.
ctx.write(response);
Expand Down Expand Up @@ -270,8 +268,10 @@ public void operationComplete(ChannelProgressiveFuture future) {
}
});

// Close the connection when the whole content is written out.
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
if (!keepAlive) {
// Close the connection when the whole content is written out.
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.atmosphere.nettosphere.util.Utils;

import javax.net.ssl.SSLEngine;

Expand Down Expand Up @@ -52,8 +54,12 @@ protected void initChannel(Channel ch) throws Exception {
pipeline.addLast("ssl", config.nettySslContext().newHandler(ch.alloc()));
}

pipeline.addLast("decoder", new HttpRequestDecoder());

// For backward compatibility
if (Utils.isJersey()) {
pipeline.addLast("decoder", new HttpRequestDecoder());
} else {
pipeline.addLast(new HttpServerCodec());
}
pipeline.addLast("aggregator", new HttpObjectAggregator(config.maxChunkContentLength()));

if (config.supportChunking()) {
Expand Down
24 changes: 18 additions & 6 deletions server/src/main/java/org/atmosphere/nettosphere/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.atmosphere.nettosphere.util;

import io.netty.channel.Channel;
import org.atmosphere.util.IOUtils;

import java.io.BufferedOutputStream;
import java.io.File;
Expand All @@ -32,23 +33,25 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import static org.atmosphere.cpr.FrameworkConfig.JERSEY_CONTAINER;

public class Utils {

public static final IOException REMOTELY_CLOSED = new IOException("Connection remotely closed");
public static IOException REMOTELY_CLOSED = new IOException("Connection remotely closed");

{
REMOTELY_CLOSED.setStackTrace(new StackTraceElement[]{});
}

private final static NoAlloc NO_ALLOC = new NoAlloc();
private static NoAlloc NO_ALLOC = new NoAlloc();

public static final IOException ioExceptionForChannel(Channel channel, String uuid) {
public static IOException ioExceptionForChannel(Channel channel, String uuid) {
IOException ioe = new IOException(channel + ": content already processed for " + uuid);
ioe.setStackTrace(new StackTraceElement[]{});
return ioe;
}

public final static URLClassLoader createURLClassLoader(String dirPath) throws IOException {
public static URLClassLoader createURLClassLoader(String dirPath) throws IOException {

String path;
File file;
Expand Down Expand Up @@ -351,14 +354,23 @@ public static boolean copy(File src, File dest) {

}

private final static class NoAlloc {
private static class NoAlloc {
public String toString() {
return "config.noInternalAlloc == true";
}
}

public final static String id(Channel channel) {
public static String id(Channel channel) {
InetSocketAddress addr = (InetSocketAddress) channel.localAddress();
return addr.getAddress().getHostAddress() + "::" + addr.getPort();
}

public static boolean isJersey() {
try {
IOUtils.loadClass(Utils.class, JERSEY_CONTAINER);
return true;
} catch (Exception e) {
return false;
}
}
}

0 comments on commit 4a5d587

Please sign in to comment.