Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

handle cases where ServletContext#getResource returns an URL with a f…

…ile:// prefixed path - getRealPath is expected to return a FS path (related to #75)
  • Loading branch information...
commit 550f8aec7f512bbf9cd0d8de418a7f6d699da4f3 1 parent 3a5d3e4
@kares authored
View
18 src/main/java/org/jruby/rack/servlet/DefaultServletRackContext.java
@@ -52,13 +52,21 @@ public void log(String message, Throwable ex) {
public String getRealPath(String path) {
String realPath = context.getRealPath(path);
if (realPath == null) { // some servers don't like getRealPath, e.g. w/o exploded war
- URL u = null;
try {
- u = context.getResource(path);
- } catch (MalformedURLException ex) {}
- if (u != null) {
- realPath = u.getPath();
+ URL url = context.getResource(path);
+ if (url != null) {
+ String urlPath = url.getPath();
+ // still might end up as an URL with path "file:/home"
+ if (urlPath.startsWith("file:")) {
+ // handles "file:/home" and "file:///home" as well
+ realPath = new URL(urlPath).getPath(); // "/home"
+ }
+ else {
+ realPath = urlPath;
+ }
+ }
}
+ catch (MalformedURLException e) { /* ignored */ }
}
return realPath;
}
View
15 src/main/ruby/jruby/rack/app_layout.rb
@@ -29,8 +29,7 @@ def initialize(rack_context)
end
def real_path(path)
- rpath = @rack_context.getRealPath(path)
- if rpath
+ if rpath = @rack_context.getRealPath(path)
# protect windows paths from backrefs
rpath.sub!(/\\([0-9])/, '\\\\\\\\\1')
rpath.chomp!('/')
@@ -60,14 +59,16 @@ def app_uri
end
def gem_uri
- @gem_uri ||= @rack_context.getInitParameter('gem.path') ||
- @rack_context.getInitParameter('gem.home') || '/WEB-INF/gems'
+ @gem_uri ||=
+ @rack_context.getInitParameter('gem.path') ||
+ @rack_context.getInitParameter('gem.home') ||
+ '/WEB-INF/gems'
end
def real_path(path)
- rx = Regexp.quote(app_uri)
- if path =~ /^#{rx}\//
- path.sub(/^#{rx}/, app_path)
+ app_regex = Regexp.quote(app_uri) # app_uri = '/WEB-INF'
+ if path =~ /^#{app_regex}\// # gem_path = '/WEB-INF/gems'
+ path.sub(/^#{app_regex}/, app_path) # '[app_path]/gems'
else
super
end
View
3  src/spec/ruby/rack/application_spec.rb
@@ -353,8 +353,7 @@ def createRackServletWrapper(runtime, rackup); end
@pool.destroy
end
- it "should create applications during initialization according
- to the jruby.min.runtimes context parameter" do
+ it "should create applications during initialization according to the jruby.min.runtimes context parameter" do
@factory.should_receive(:init).with(@rack_context)
@factory.stub!(:newApplication).and_return do
app = mock "app"
View
18 src/spec/ruby/rack/servlet_spec.rb
@@ -36,6 +36,7 @@
end
describe ServletRackContext, "getRealPath" do
+
before :each do
@context = DefaultServletRackContext.new(ServletRackConfig.new(@servlet_context))
end
@@ -46,4 +47,21 @@
@servlet_context.should_receive(:getResource).with("/WEB-INF").and_return url
@context.getRealPath("/WEB-INF").should == "/var/tmp/foo.txt"
end
+
+ it "should strip file: prefix for getRealPath" do
+ @servlet_context.stub!(:getRealPath).and_return nil
+
+ # we're emulating a ServletContext.getResource returning an URL which might
+ # differ for different containers - WLS 10 might behave this way from time:
+ url = java.net.URL.new 'file', nil, 0, "file:/foo/bar", nil
+ # url.path.should == "file:/foo/bar"
+ @servlet_context.should_receive(:getResource).with("/bar").and_return url
+ @context.getRealPath("/bar").should == "/foo/bar"
+
+ url = java.net.URL.new 'file', nil, 0, "file:///foo/bar", nil
+ # url.path.should == "file:///foo/bar"
+ @servlet_context.should_receive(:getResource).with("/bar").and_return url
+ @context.getRealPath("/bar").should == "/foo/bar"
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.