[webrick] How to extract filename from a request?

D

Daniel Völkerts

Hi,

I went on playing on my syslog analyser and tried the webrick framework
wich I like a lot as it is simple but powerful. I defined a servlet
which should read a definition file and render its result.

My dir structur looks like

htdocs
- filter/
- filter1.fd
- filter2.fd

I mount my servlet to /filter and it should take any file argument in
the url after /filter as a filename to a filter definition file e.g.
http://localhost/filter/filter1.fd should render the output produced by
filter one.

I browsed the webrick source (httprequest.rb etc. but I can't find a var
for that. Is there a better way then extract the information from the
request_uri var?

Other solutions are also welcome.
 
J

James Britt

Daniel said:
Hi,

I went on playing on my syslog analyser and tried the webrick framework
wich I like a lot as it is simple but powerful. I defined a servlet
which should read a definition file and render its result.

My dir structur looks like

htdocs
- filter/
- filter1.fd
- filter2.fd

I mount my servlet to /filter and it should take any file argument in
the url after /filter as a filename to a filter definition file e.g.
http://localhost/filter/filter1.fd should render the output produced by
filter one.

And it doesn't?
I browsed the webrick source (httprequest.rb etc. but I can't find a var
for that. Is there a better way then extract the information from the
request_uri var?

I believe request.script_name gives the the text after the leading part
of the URL. But it looks to me that this should just work as it is.

You should not have to explicitly extract the file name; WEBrick should
just serve back the file, since filter/ maps to the directory where the
filter files are.

For example, if I start a server, running from /some/dir/, with this:

port = 12000
puts "URL: http://#{Socket.gethostname}:#{port}"
s = HTTPServer.new(
:port => port
)
s.mount("/", WEBrick::HTTPServlet::FileHandler, dir, true)


then if some/dir/ has a file foo.txt, I can load it with

http://localhost:12000/foo.txt

James
 
D

Daniel Völkerts

James Britt wrote:

Thanks for your reply.

And it doesn't?

No, I'd like to pass the called file to the servlet which regonize the
request...

This is my flow

Browser is pointed to http://localhost/filter/filter1.fd

As my Servlet ist mounted to /filter he regonize the request and thats
the part I'd like to implement -> now the servlet should look up the
filter to use (here filter1.fd) and should parse this filter file for
directives which controll the output.

So I've to extract the filename from the request_uri. The way I went
today is to call

filterName = req.request_uri.to_s.scan(/\w+\.fd/)

My question was if there is another possible better ruby way or variable
included with webrick to accomplish this task.
 
J

James Britt

Daniel said:
This is my flow

Browser is pointed to http://localhost/filter/filter1.fd

As my Servlet ist mounted to /filter he regonize the request and thats
the part I'd like to implement -> now the servlet should look up the
filter to use (here filter1.fd) and should parse this filter file for
directives which controll the output.

So, the part of the URL that comes after /filter/ is data to be passed
to some method?
So I've to extract the filename from the request_uri. The way I went
today is to call

filterName = req.request_uri.to_s.scan(/\w+\.fd/)

My question was if there is another possible better ruby way or variable
included with webrick to accomplish this task.


If you have /filter/ mapped to a servlet, and call
http://localhost/filter/someFilterFile, then, in your servlet,
request.path should give you "/filter/someFilterFile"

def do_GET(req, res)
url_parts = req.path.split( '/' )
base = url_parts[0]
filter_file_path = url_parts[1]
end


That's one way to grab the data. (Not the best; no error checking, etc.)

James
 
D

Daniel Völkerts

James said:
So, the part of the URL that comes after /filter/ is data to be passed
to some method?

Yeah.That's the way I'd like to process the file through the servlet.

If you have /filter/ mapped to a servlet, and call
http://localhost/filter/someFilterFile, then, in your servlet,
request.path should give you "/filter/someFilterFile"

def do_GET(req, res)
url_parts = req.path.split( '/' )
base = url_parts[0]
filter_file_path = url_parts[1]
end


That's one way to grab the data. (Not the best; no error checking, etc.)

So do I. I extend it with a kind of error correction and it works.
Thanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top