2.2.1 server returns 404 on HEAD request #2280

Closed
StefanGIT opened this Issue Jan 17, 2014 · 20 comments

Comments

Projects
None yet
10 participants

If i create a new application in play 2.2.1 and try to get the HEAD request, i get a 404 error:
~/play-2.2.1/play new test
_
_ __ | | __ _ _ _
| '_ | |/ ' | || |
| __/|
|___|__ /
|
| |__/

play 2.2.1 built with Scala 2.10.2 (running Java 1.7.0_25), http://www.playframework.com

The new application will be created in /home/richtesn/teste/test

What is the application name? [test]

Which template do you want to use for this new application?

1 - Create a simple Scala application
2 - Create a simple Java application

2
OK, application test is created.

Have fun!

[richtesn@gaia:/teste]$ cd test
[richtesn@gaia:
/teste/test]$ play run
[info] Loading project definition from /home/richtesn/teste/test/project
[info] Set current project to test (in build file:/home/richtesn/teste/test/)
[info] Updating {file:/home/richtesn/teste/test/}test...
[info] Resolving org.hibernate.javax.persistence#hibernate-jpa-2.0-api;1.0.1.Fin[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.

--- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

[info] Compiling 4 Scala sources and 2 Java sources to /home/richtesn/teste/test/target/scala-2.10/classes...
[info] play - Application started (Dev)

Then using a different terminal (HEAD is part of the libwww-perl package:
libwww-perl: /usr/bin/HEAD):

HEAD http://localhost:9000
404 Not Found
Content-Length: 4024
Content-Type: text/html; charset=utf-8
Client-Date: Fri, 17 Jan 2014 15:35:39 GMT
Client-Peer: 127.0.0.1:9000
Client-Response-Num: 1

The page is still delivered (using a GET request), but if i want to enter the link to the server in a system that checks the response code it is refused due to the 404 Not found.

EDIT (January, 23rd, 2014): if i contact the server via telnet, i see the repsonsce code:

telnet localhost 9000
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
Escape character is '^]'.
HEAD /index.html HTTP/1.1

HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
Content-Length: 4034

I confirm this problem too !!

HEAD requests should also be allowed where there is the same in GET/POST/PUT/DELETE since HEAD is a sub part of these.

Using the play version 1.2.7 the HEAD request works as excpected.

Owner

jroper commented Jan 31, 2014

To whoever wants to implement this, here is an example HEAD action (it's written for Play 2.1, should be simpler to implement in Play 2.2):

https://github.com/reactivemanifesto/website-manifesto/blob/master/app/controllers/HeadAction.scala

And then where this is used in Global:

https://github.com/reactivemanifesto/website-manifesto/blob/master/app/Global.scala

When putting this into Play, it should be added to the default implementation of GlobalSettings.onRequestReceived.

Owner

jroper commented Jan 31, 2014

@cnicodeme HEAD should only be routed to GET actions, not POST/PUT/DELETE. Routing a HEAD request to POST, PUT or DELETE would be very dangerous, since many clients/browsers etc might issue a HEAD request just to see if something is there - if that were to result in that thing being deleted.... well...

yes you're right, but still, why Play refuses HEAD for GET request ? it shouldn't.

But exactly this is the problem:
"since many clients/browsers etc might issue a HEAD request just to see if something is there "
Since play then returns 404 on this HEAD request, the client thinks the resource is not there.
I have this problem where i want to enter the URL in a content management system, but the system checks with a HEAD request if the link is active. The link is then refused, since the HEAD request returns 404.

Contributor

Damiya commented Feb 4, 2014

@jroper I'd like to go ahead and tackle this; quick question though, where should the HeadAction be put in, from a package perspective?

Collaborator

huntc commented Feb 5, 2014

I believe James was referring to play.api.GlobalSettings. Here is the Reactive Manifesto sample he was referring to:

https://github.com/reactivemanifesto/website-manifesto/blob/master/app/Global.scala#L21

tjunnone commented Feb 6, 2014

The HeadAction linked above doesn't exactly work as HEAD is supposed to. HeadAction causes the Content-Length header on the response to be 0 due to the omitted body. For example, a CDN may do a HEAD request on a resource, and only if some property (such as content-length) differs perform the GET.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html:
The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request.

Owner

jroper commented Feb 10, 2014

Fair enough, should probably update the HEAD action to include the content length then.

@Damiya How about play.api.controllers.HeadAction?

Owner

jroper commented Feb 10, 2014

Oh and make sure plenty of tests are added in play-integration-test/src/scala/..., Expect100ContinueSpec might be a good one to copy.

Contributor

Damiya commented Feb 10, 2014

@jroper Sounds good. I'll tackle that in the morning and get a PR up.

@Damiya Damiya added a commit to Damiya/playframework that referenced this issue Feb 10, 2014

@Damiya Damiya Adds HeadAction class to play.api.mvc.controllers. Closes #2280
* This is an RFC2616 compliant impementation of the HEAD verb (Provides headers for a GET route)

* Adds HTTPVerbs object to StandardValues, providing non-MagicStrings lookup of GET/POST/HEAD/etc.

* Replaced magic string usage in ContentTypes
632eb1c

@Damiya Damiya added a commit to Damiya/playframework that referenced this issue Feb 10, 2014

@Damiya Damiya Adds HeadAction class to play.api.mvc.controllers. Closes #2280
* This is an RFC2616 compliant impementation of the HEAD verb (Provides headers for a GET route)

* Adds HTTPVerbs object to StandardValues, providing non-MagicStrings lookup of GET/POST/HEAD/etc.

* Replaced magic string usage in ContentTypes
848879b

@Damiya Damiya added a commit to Damiya/playframework that referenced this issue Feb 10, 2014

@Damiya Damiya Adds HeadAction class to play.api.mvc.controllers. Closes #2280
* This is an RFC2616 compliant impementation of the HEAD verb (Provides headers for a GET route)

* Adds HTTPVerbs object to StandardValues, providing non-MagicStrings lookup of GET/POST/HEAD/etc.

* Replaced magic string usage in ContentTypes
7cc48df

@Damiya Damiya added a commit to Damiya/playframework that referenced this issue Feb 11, 2014

@Damiya Damiya Adds HeadAction class to play.api.mvc.controllers. Closes #2280
* This is an RFC2616 compliant impementation of the HEAD verb (Provides headers for a GET route)

* Adds HttpVerbs object to StandardValues, providing non-MagicStrings lookup of GET/POST/HEAD/etc.

* Update "GET" and "HEAD" references in ContentTypes to use HttpVerbs instead
5981fde
Contributor

Damiya commented Feb 11, 2014

PR Submitted as #2355

@Damiya Damiya added a commit to Damiya/playframework that referenced this issue Feb 12, 2014

@Damiya Damiya Adds HeadAction class to play.api.mvc.controllers. Closes #2280
* This is an RFC2616 compliant impementation of the HEAD verb (Provides headers for a GET route)

* Adds HttpVerbs object to StandardValues, providing non-MagicStrings lookup of GET/POST/HEAD/etc.

* Update "GET" and "HEAD" references in ContentTypes to use HttpVerbs instead
9caa4de

@Damiya Damiya added a commit to Damiya/playframework that referenced this issue Feb 14, 2014

@Damiya Damiya Adds HeadAction class to play.api.mvc.controllers. Closes #2280
* This is an RFC2616 compliant impementation of the HEAD verb (Provides headers for a GET route)

* Adds HttpVerbs object to StandardValues, providing non-MagicStrings lookup of GET/POST/HEAD/etc.

* Update "GET" and "HEAD" references in ContentTypes to use HttpVerbs instead
6e2ed2e

jroper closed this in b79d4d8 Feb 17, 2014

@jroper jroper added a commit that referenced this issue Feb 17, 2014

@jroper jroper Merge pull request #2355 from Damiya/feature/head_action
Adds HeadAction class to play.api.mvc.controllers. Closes #2280
c06267e
Contributor

sief commented Apr 3, 2014

I'm still seeing this in play2.2.2, any idea when the fix will be released?

Owner

jroper commented Apr 7, 2014

It's not a fix, it's a new feature in Play 2.3.

Could you help in fixing in Play 2.2.3 Java?

Owner

jroper commented Oct 27, 2014

@dhirenpratap copy the Scala code pointed to in #2280 (comment) to your project if you want to implement this feature for Play 2.2.

gbsmith commented Feb 19, 2015

@jroper, echoing dhirenpratap's remarks, is there some trick to getting this to work with our 2.2.x Global.java file? I am not sure what to do with the RequestHeader copy call which doesn't seem exist in the Java version.

Owner

jroper commented Feb 20, 2015

@gbsmith Copy the scala code into your project - you can't implement it in Java. Just because you're using Java doesn't mean you can't have Scala code in your project.

gbsmith commented Feb 20, 2015

@jroper So just copy the Global.scala in wholesale and have both a Global.java and a Global.scala?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment