Skip to content
Browse files

Added support for HEAD method

  • Loading branch information...
1 parent 68f7a64 commit 469ad36fb6927912a6ae6e3736102ad20b20fdf9 @pepite pepite committed Mar 16, 2010
Showing with 22 additions and 49 deletions.
  1. +0 −35 README
  2. +3 −2 samples-and-tests/test/app/controllers/Application.java
  3. +19 −12 src/play/modules/netty/PlayHandler.java
View
35 README
@@ -1,35 +0,0 @@
-/**
- *
- * Copyright 2010, Play! developers - Nicolas Leroux -
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-Play! Framework Netty module
-
-The netty module allows you to use JBoss Netty:http://jboss.org/netty as a play! server.
-
-Version 3.2.0 alpha 4 of Netty.
-
-* Enable the Netty module for the application
-
-In the /conf/application.conf file, enable the Netty module by adding this line:
-
-module.netty=${play.path}/modules/netty
-
-* Starting the Play! with netty as application server</a>
-
-play netty:run myapplication
-
View
5 samples-and-tests/test/app/controllers/Application.java
@@ -4,6 +4,7 @@
import play.Play;
import play.mvc.*;
import play.Logger;
+
import java.io.File;
public class Application extends Controller {
@@ -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() + "]");
}
}
View
31 src/play/modules/netty/PlayHandler.java
@@ -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;
@@ -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);
@@ -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);
@@ -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);
}
@@ -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);
@@ -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();

0 comments on commit 469ad36

Please sign in to comment.
Something went wrong with that request. Please try again.