开发者

How can I use local resources on a server?

开发者 https://www.devze.com 2023-03-11 22:12 出处:网络
How can I use local resources like css, js, png, etc. within a dynamically rendered page using webrick? In other words, how are things like Ruby on Rails linking made to work?I suppose this is one of

How can I use local resources like css, js, png, etc. within a dynamically rendered page using webrick? In other words, how are things like Ruby on Rails linking made to work? I suppose this is one of the mo开发者_开发百科st basic things, and there should be a simple way to do it.

Possible Solution

I managed to do what I wanted using two servlets as follows:

require 'webrick' 

class WEBrick::HTTPServlet::AbstractServlet
  def do_GET request, response
    response.body = '<html>
      <head><base href="http://localhost:2000"/></head>
      <body><img src="path/image.png" /></body>
    </html>'
  end
end

s1 = WEBrick::HTTPServer.new(Port: 2000, BindAddress: "localhost")
s2 = WEBrick::HTTPServer.new(Port: 3000, BindAddress: "localhost")
%w[INT TERM].each{|signal| trap(signal){s1.stop}}
%w[INT TERM].each{|signal| trap(signal){s2.shutdown}}
s1.mount("/", WEBrick::HTTPServlet::FileHandler, '/')
s2.mount("/", WEBrick::HTTPServlet::AbstractServlet)

Thread.new{s1.start}
s2.start

Is this the right way to do it? I do not feel so. In addition, I am not completely satisfied with it. For one thing, I do not like the fact that I have to specify http://localhost:2000 in the body. Another is the use of thread does not seem right. Is there a better way to do this? If you think this is the right way, please answer so.


Generally speaking, because of security concerns browsers likely won't link to local files (using file:// schema) from an internet site (using http:// or https:// schema). See Can Google Chrome open local links?. This is unrelated to any server side technology.

Outside of that, it seems your server is working perfectly. You've made it so it responds to all requests with a HTML page containing a link to /. When you click on that link, something does indeed happen; a request is sent and you are served the same page again.

It kind of sounds like you want to expose your entire filesystem via HTTP. If that is what you're trying to accomplish, you can simply get away with not mounting a servlet:

server = WEBrick::HTTPServer.new(Port: 3000, BindAddress: "localhost", DocumentRoot: "/")
%w[INT TERM].each{|signal| trap(signal){server.shutdown}}
server.start


Try code like this:

require 'webrick'

class WEBrick::HTTPServlet::AbstractServlet
  def do_GET request, response
    if request.unparsed_uri == "/"
      response.body = '<html><body><a href = "/path/to/file">test</a></body></html>'
    end
  end
end

server = WEBrick::HTTPServer.new(Port: 3000, BindAddress: "localhost", DocumentRoot: "/")
%w[INT TERM].each { |signal| trap(signal) { server.shutdown } }
server.mount("/", WEBrick::HTTPServlet::AbstractServlet)
server.start

This works for me, I'm not sure why but it seems to work whenever I call at least one method on the request object.


It sounds like you are confusing web pages that are served vs. pages that are opened by the browser directly from your drive, and how file: differs from http:, https:, and ftp:.

file: is a locally available resource when a page is directly opened from the drive. The others are remotely available resources when a page is served from a httpd host.

The browser can't tell that a page from a server came from your drive; It only knows it got it from a server, somewhere, and doesn't know or care whether that server is on the same hardware. Browsers will not allow access to local resources from remotely retrieved pages. That was an exploit that was closed years ago.

See RFC 1738's specification 3.10 FILES for file: URLs for the official statements.


I finally found out that I can mount multiple servlets on a single server. It took a long time until I found such example.

require 'webrick' 

class WEBrick::HTTPServlet::AbstractServlet
  def do_GET request, response
    response.body = '<html>
      <head><base href="/resource/"/></head>
      <body>
        <img src="path_to_image/image.png";alt="picture"/>
        <a href="path_to_directory/" />link</a>
        ...
      </body>
    </html>'
  end
end

server = WEBrick::HTTPServer.new(Port: 3000, BindAddress: "localhost")
%w[INT TERM].each{|signal| trap(signal){server.shutdown}}
server.mount("/resource/", WEBrick::HTTPServlet::FileHandler, '/')
server.mount("/", WEBrick::HTTPServlet::AbstractServlet)
server.start

The path /resource/ can be anything else. The link will now correctly redirect to the expected directory, showing that there is no access permission, which indicates that things are working right; it's now just a matter of permission.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号