Bug 43241

Summary: ServletContext.getResourceAsStream() does not follow API specs for Path
Product: Tomcat 5 Reporter: George Sexton <gsexton>
Component: Servlet & JSP APIAssignee: Tomcat Developers Mailing List <dev>
Severity: normal    
Priority: P2    
Version: 5.5.23   
Target Milestone: ---   
Hardware: All   
OS: All   
Attachments: test war
one way of fixing this

Description George Sexton 2007-08-29 12:54:18 UTC
Say for example, you have a file style.css deployed in your context. The call to
ServletContext.getResourceAsStream("style.css") returns an input stream. This is
not correct behavior.

The spec (2.3) says:

> The path must be specified according to 
> the rules given in getResource. 

getResource() says:

> The path must begin with a "/" and is interpreted 
> as relative to the current context root.

My reading of these two things is that getResourceAsStream("style.css") should
return null, not an input stream to the file.

I got bit on this because my app behavior changed (broke) when ran on the
current version of WebSphere.
Comment 1 John Kew 2007-10-04 15:32:40 UTC
Created attachment 20919 [details]
test war

Adding test war.
Comment 2 John Kew 2007-10-04 15:36:32 UTC
Created attachment 20920 [details]
one way of fixing this

Here's a simple patch which "fixes" the problem. getResourceAsStream can not
throw an Exception (according to the spec), so I just return null. 

I'm ambivalent about this patch. This may create problems for some people who
depend on the incorrect usage of this function. Plus, there may be a better
solution, such as always returning an input stream containing "42"
Comment 3 Mark Thomas 2007-12-18 15:12:46 UTC
Whilst I like the '42' idea, I am fairly sure not everyone else shares my sense
of humour so null it will have to be.

Patch applied to trunk and proposed for 5.5.x and 6.0.x.

Many thanks for the patch.
Comment 4 Mark Thomas 2007-12-23 12:09:25 UTC
Fixed in 6.0.x
Comment 5 Mark Thomas 2008-01-06 15:16:04 UTC
Fixed in 5.5.x and will be included in 5.5.26 onwards.
Comment 6 Mark Thomas 2008-01-06 15:16:18 UTC
Fixed in 5.5.x and will be included in 5.5.26 onwards.