How to access html request parameters for a .rhtml page served by webrick?

I use webrick (embedded ruby ​​web server) to serve .rhtml files (html with embedded ruby ​​code - like jsp).

It works fine, but I can't figure out how to access the parameters (e.g. http: //localhost/mypage.rhtml? Foo = bar ) from the ruby ​​code in the .rhtml file. (Note that I am not using rails framework, only webrick + .rhtml files)

thanks

+4
source share
4 answers

According to the erbhandler source code, it runs rhtml files as follows:

Module.new.module_eval{ meta_vars = servlet_request.meta_vars query = servlet_request.query erb.result(binding) } 

Thus, the binding should contain query (which contains the hash of the query string) and the meta_vars variable (which contains the hash of the environment, for example SERVER_NAME ), which you can access in rhtml files (both servlet_request and servlet_response can also be accessed, but I'm not sure in them).

If this is not the case, you can also try to query the CGI ENV["QUERY_STRING"] parameter and analyze it, but this should only be a last resort (and it can only work with CGI files).

+3
source

This solution:

(suppose the request is http://your.server.com/mypage.rhtml?foo=bar )

  <html> <body> This is my page (mypage.rhtml, served by webrick) <% # embedded ruby code servlet_request.query ["foo"] # this simply prints bar on console %> </body> </html> 
+3
source

HTTPRequest at the documentation , it looks like you should have an HTTPRequest from which you can get the query string. Then you can use parse_query to get a hash of the name / value.

Alternatively, it is possible that just calling query() will give you a hash directly ... my Ruby-fu doesn't quite fit, but you can at least give it a try.

+1
source

You do not give much details, but I think you have a servlet to serve the files that you will process with erb, and by default the web server serves any static file in the public directory.

 require 'webrick' include WEBrick require 'erb' s = HTTPServer.new( :Port => 8080,:DocumentRoot => Dir::pwd + "/public" ) class MyServlet < HTTPServlet::AbstractServlet def do_GET(req, response) File.open('public/my.rhtml','r') do |f| @template = ERB.new(f.read) end response.body = @template.result(binding) response['Content-Type'] = "text/html" end end s.mount("/my", MyServlet) trap("INT"){ s.shutdown } s.start 

This example is limited when you go to / my, the same file is always processed. Here you should build the file path based on the request path. Here I said an important word: "request", all you need is.

To get the HTTP header parameters, use req [header_name]. To get parameters in the query string, use req.query [param_name]. req - The HTTPRequest object passed to the servlet.

Once you have the required parameter, you should bind it to the template. In the example, we pass the binding object from ourselves (the binding is defined in Kernel, and it represents the context where the code is executed), so each local variable defined in the do_GET method will be available in the template. However, you can create your own binding, for example, pass a Proc object and pass it to the ERB processor when you call "result".

All together, your solution will look like this:

  def do_GET(req, response) File.open('public/my.rhtml','r') do |f| @template = ERB.new(f.read) end foo = req.query["foo"] response.body = @template.result(binding) response['Content-Type'] = "text/html" end 
+1
source

Source: https://habr.com/ru/post/1308586/


All Articles