Skip to content

Commit

Permalink
Added support for HEAD method
Browse files Browse the repository at this point in the history
  • Loading branch information
pepite committed Mar 16, 2010
1 parent 68f7a64 commit 469ad36
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 49 deletions.
35 changes: 0 additions & 35 deletions README

This file was deleted.

5 changes: 3 additions & 2 deletions samples-and-tests/test/app/controllers/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import play.Play;
import play.mvc.*;
import play.Logger;

import java.io.File;

public class Application extends Controller {
Expand All @@ -13,9 +14,9 @@ public static void index() {
}

public static void upload(File file) throws Exception {
Logger.info("File is " + file.getAbsolutePath() + "]");
Logger.info("File is " + file.getAbsolutePath() + "]");
FileUtils.copyFile(file, new File(Play.getFile("data/"), file.getName()));
Logger.info("File size [" + file.length() + "]");
Logger.info("File size [" + file.length() + "]");
}

}
31 changes: 19 additions & 12 deletions src/play/modules/netty/PlayHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import play.vfs.VirtualFile;

import java.io.*;
import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
Expand Down Expand Up @@ -218,9 +217,14 @@ protected static void addToResponse(Response response, HttpResponse nettyRespons
}

protected static void writeResponse(ChannelHandlerContext ctx, Response response, HttpResponse nettyResponse, HttpRequest nettyRequest) throws IOException {
final byte[] content = response.out.toByteArray();
final boolean keepAlive = isKeepAlive(nettyResponse);
byte[] content = null;

final boolean keepAlive = isKeepAlive(nettyResponse);
if (nettyRequest.getMethod().equals(HttpMethod.HEAD)) {
content = new byte[0];
} else {
content = response.out.toByteArray();
}

ChannelBuffer buf = ChannelBuffers.copiedBuffer(content);
nettyResponse.setContent(buf);
Expand Down Expand Up @@ -275,14 +279,16 @@ public static void copyResponse(ChannelHandlerContext ctx, Request request, Resp

Logger.trace("file length is [" + fileLength + "]");
setContentLength(nettyResponse, fileLength);

Channel ch = ctx.getChannel();

// Write the initial line and the header.
ch.write(nettyResponse);
ChannelFuture writeFuture = ch.write(nettyResponse);

// Write the content.
ChannelFuture writeFuture = ch.write(new ChunkedFile(raf, 0, fileLength, 8192));
// If it is not a HEAD
if (!nettyRequest.getMethod().equals(HttpMethod.HEAD)) {
writeFuture = ch.write(new ChunkedFile(raf, 0, fileLength, 8192));
}
if (!keepAlive) {
// Close the connection when the whole content is written out.
writeFuture.addListener(ChannelFutureListener.CLOSE);
Expand All @@ -291,8 +297,10 @@ public static void copyResponse(ChannelHandlerContext ctx, Request request, Resp
throw e;
}
} else if (is != null) {
ctx.getChannel().write(nettyResponse);
ChannelFuture writeFuture = ctx.getChannel().write(new ChunkedStream(is));
ChannelFuture writeFuture = ctx.getChannel().write(nettyResponse);
if (!nettyRequest.getMethod().equals(HttpMethod.HEAD)) {
writeFuture = ctx.getChannel().write(new ChunkedStream(is));
}
if (!keepAlive) {
writeFuture.addListener(ChannelFutureListener.CLOSE);
}
Expand Down Expand Up @@ -508,7 +516,6 @@ public static void serve500(Exception e, ChannelHandlerContext ctx, HttpRequest
format = "txt";
}

// TODO: is that correct? xxx.?
nettyResponse.setHeader("Content-Type", (MimeTypes.getContentType("500." + format, "text/plain")));
try {
String errorHtml = TemplateLoader.load("errors/500." + format).render(binding);
Expand Down Expand Up @@ -584,9 +591,9 @@ public static void serveStatic(RenderStatic renderStatic, ChannelHandlerContext
Logger.trace("keep alive " + keepAlive);
Logger.trace("content type " + (MimeTypes.getContentType(localFile.getName(), "text/plain")));

if (isKeepAlive(nettyRequest)) {
setContentLength(nettyResponse, fileLength);
}
//if (isKeepAlive(nettyRequest)) {
setContentLength(nettyResponse, fileLength);
//}
nettyResponse.setHeader(CONTENT_TYPE, (MimeTypes.getContentType(localFile.getName(), "text/plain")));

Channel ch = e.getChannel();
Expand Down

0 comments on commit 469ad36

Please sign in to comment.