Bug 176405 - [server] Jetty HTTP Service reports an HTTP 500 when trying to access registered resource
Summary: [server] Jetty HTTP Service reports an HTTP 500 when trying to access registe...
Status: RESOLVED FIXED
Alias: None
Product: Equinox
Classification: Eclipse Project
Component: Compendium (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows Vista
: P3 normal (vote)
Target Milestone: 3.5 M3   Edit
Assignee: Simon Kaegi CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed
Depends on:
Blocks:
 
Reported: 2007-03-05 13:58 EST by Jochen Hiller CLA
Modified: 2008-10-27 16:51 EDT (History)
3 users (show)

See Also:


Attachments
Patch for detecting directories (7.88 KB, patch)
2008-10-15 06:07 EDT, Gunnar Wagenknecht CLA
no flags Details | Diff
Sample project exposing the problem. (10.23 KB, application/zip)
2008-10-15 09:41 EDT, Gunnar Wagenknecht CLA
no flags Details
Patch for org.eclipse.equinox.http.servlet (5.30 KB, patch)
2008-10-15 11:23 EDT, Gunnar Wagenknecht CLA
no flags Details | Diff
Patch for org.eclipse.equinox.http.servlet (5.37 KB, patch)
2008-10-15 11:37 EDT, Gunnar Wagenknecht CLA
tjwatson: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jochen Hiller CLA 2007-03-05 13:58:30 EST
Build ID: I20070222-0951

Steps To Reproduce:
You can register a resource using the HTTP service e.g. at "/sample". If you try to access this alias e.g. "http://localhost/sample", then you will get an HTTP 500 code returned.

Output is:
HTTP ERROR: 500

C:\data\p\SSE\EclipseCon2007\wsp-play\org.eclipsecon.serverside.sample02\WebContent (Access denied)

RequestURI=/sample

Powered by Jetty://

More information:
The expected behaviour would be to get an HTTP 403 (Access Denied) instead.
Comment 1 Simon Kaegi CLA 2007-03-06 09:49:10 EST
Yes the error should not be a 500.
To clarify, this is when we're try to directly access a directory as a resource.
Comment 2 Simon Kaegi CLA 2007-03-12 23:29:09 EDT
I've added a check for resources that end in "/" and 0 length resources that prevents the ResourceRegistration from handling the request and ultimately letting a default servlet if present handle the request.

If no default servlet is present a 404 is returned. I actually prefer this behaviour as the directory as a pure resource is not present. This is also consistent with what both Tomcat and Jetty return when accessing a directory resource.

--
Fixed in HEAD
Comment 3 Simon Kaegi CLA 2007-07-27 16:01:23 EDT
Re-opening.
I think we'll have to trap a 500 or similar error in the Http Service implementation, however I think the approach we've taken here is not correct. 

The Servlet spec says that it's up to the servlet container implementation to decide how to handle requests to a directory and in this case I think we've inadvertently hijacked control. "/" suffixed URLs should be legitimate and are up to the HttpContext to decide what resource to return. In addition, the 0 content-length check is not really valid; there's nothing inherently wrong with a 0 length resource.
Comment 4 Simon Kaegi CLA 2008-04-16 11:18:54 EDT
Fixed in HEAD
Comment 5 Gunnar Wagenknecht CLA 2008-10-15 06:04:00 EDT
Hmm, I still get 500 error when accessing directories. The fix is not trivial, though. We have to check if an URL resolves to a directory. Then, if we have a directory we need to make a decision ... should we return 403 or should we return a 404?
Comment 6 Gunnar Wagenknecht CLA 2008-10-15 06:07:28 EDT
Created attachment 115131 [details]
Patch for detecting directories

The attached patch properly detects directories (even those which do not end with a "/"). The key is an FileNotFoundException which is thrown by URLConnection#openInputStream for directories and not accessible resources.

The only downside is that it introduces a dependency on org.eclipse.equinox.common (FileLocator). But I used reflection to keep the dependency optional.
Comment 7 Simon Kaegi CLA 2008-10-15 08:53:24 EDT
Gunnar,

Can you describe your test case. I'm doing some simple sanity checks and am getting a mixture of 404s and 0 length 200 oks.

Are you saying that "/" terminated directories URLs work correctly but not non-"/" terminated URLs?

Which version of Jetty are you using?
Comment 8 Gunnar Wagenknecht CLA 2008-10-15 09:34:29 EDT
(In reply to comment #7)
> Can you describe your test case. I'm doing some simple sanity checks and am
> getting a mixture of 404s and 0 length 200 oks.

If the content length is 0 then everything is ok. However, I have a BundleURLConnection to a folder which returns 4096 as content length. Therefore, it's trying to deliver it as a file.

> Are you saying that "/" terminated directories URLs work correctly but not
> non-"/" terminated URLs?

It doesn't matter. "connection.getContentLength()" returns 4096 for the URL which points to that folder. It could be a Windows specific problem, though.
Comment 9 Gunnar Wagenknecht CLA 2008-10-15 09:36:26 EDT
(In reply to comment #7)
> Which version of Jetty are you using?

Sorry, forgot to mention. It fails with both (Jetty 5 as well as Jetty 6.1) from Orbit. Given the observations in comment #8, it might fail everywhere.

Comment 10 Gunnar Wagenknecht CLA 2008-10-15 09:41:13 EDT
Created attachment 115143 [details]
Sample project exposing the problem.

Simon, if the folder has a bit more files, its content length goes from 0 to 4096 (at least on Windows Vista). Try with the attached project. There is a launch configuration which should work with a vanilla Equinox 3.4 SDK.

http://localhost/bug176405
Comment 11 Simon Kaegi CLA 2008-10-15 10:39:35 EDT
re: Sample project exposing problem + patch
Thanks Gunnar. If only all my bugs were like this... ;)

--
After a bit more investigation this only seems to be a problem on Vista (Not XP). What's happening is the underlying URLConnection is reporting 4096 bytes of content but then throwing a FNF when the inputStream is retrieved. In this case (and only the FNF case) I'm currently thinking that returning a 403 (Forbidden) indeed makes sense, but I'll need to try this out some more.

As much as possible I want to keep things simple and so avoid any special treatment for directories. As a result I'm less inclined to do the apache redirect or detailed directory check. If need be one could write a specialized ResourceServlet to do this.
Comment 12 Gunnar Wagenknecht CLA 2008-10-15 11:23:32 EDT
Created attachment 115151 [details]
Patch for org.eclipse.equinox.http.servlet

Simon, please find attached a simplified patch as discussed. It just catches FileNotFoundException and SecurityException and sends a 403.
Comment 13 Gunnar Wagenknecht CLA 2008-10-15 11:37:32 EDT
Created attachment 115152 [details]
Patch for org.eclipse.equinox.http.servlet

Updated patch to not fail on closing the InputStream.
Comment 14 Simon Kaegi CLA 2008-10-25 15:13:20 EDT
Marking FIXED.
Thanks Gunnar, I've checked in a very slightly tweaked version of your patch that calls resp.reset() instead of just removing specific headers and also catches the potential IllegalStateException if the response is already committed. I did some manual tests without any problems but I'd appreciate it if you could also. More than ever it's apparent to me that we "need" to have automated test cases in place here to avoid regressions. I'll log a bug and set something up for 3.5.
Comment 15 Thomas Watson CLA 2008-10-27 16:51:08 EDT
Comment on attachment 115152 [details]
Patch for org.eclipse.equinox.http.servlet

Marking for the iplog.  Thanks Gunner.