Bug 43241 - ServletContext.getResourceAsStream() does not follow API specs for Path
Summary: ServletContext.getResourceAsStream() does not follow API specs for Path
Alias: None
Product: Tomcat 5
Classification: Unclassified
Component: Servlet & JSP API (show other bugs)
Version: 5.5.23
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
Depends on:
Reported: 2007-08-29 12:54 UTC by George Sexton
Modified: 2008-01-06 15:16 UTC (History)
0 users

test war (7.52 KB, application/octet-stream)
2007-10-04 15:32 UTC, John Kew
one way of fixing this (558 bytes, patch)
2007-10-04 15:36 UTC, John Kew
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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.