同步机架请求和响应 [英] sync rack request and response
问题描述
在我的Rails 4应用程序中,我想同时对html
和js
请求使用html
进行响应.在请求为html
时,呈现工作正常,但是当请求为js
时,则不会在屏幕上呈现html文件(尽管在命令行中表示已呈现).>
限制请求的方案有多种,因此,html POST
和js POST
请求也可以触发限制代码.
Rack::Attack.throttle(key, limit: from_config(key, :limit), period: from_config(key, :period)) do |req|
if req.path.ends_with?(from_config(key, :path).to_s) && from_config(key, :method) == req.env['REQUEST_METHOD']
### This is the snippet I try to change the req type with but not working
if req.media_type == 'application/javascript'
req.media_type = 'text/html'
end
##### till here
req.ip
end
end
这是我要呈现的内容.如您所见,这是html
响应.
Rack::Attack.throttled_response = lambda do |env|
[429, {}, [ActionView::Base.new.render(file: 'public/429.html', content_type: 'text/html')]]
end
我该怎么办?
更新
这是我的最新版本,但无法弄清楚如何检查请求content_type:
Rack::Attack.throttled_response = lambda do |env|
retry_after = (env['rack.attack.match_data'] || {})[10]
if env['rack.attack.content_type'] == 'text/html'
[429, {'Retry-After' => retry_after.to_s}, [ActionView::Base.new.render(file: 'public/429.html', content_type: 'text/html')]]
elsif env['rack.attack.content_type'] == 'application/javascript'
[429, {'Retry-After' => retry_after.to_s}, window.location.href = '/429.html']
end
end
docs: https://github.com/kickstarter/rack-attack
我同意@max.原则上,您不应使用HTML来响应专门针对JS的请求.
但要回答这一部分问题:
如何检查请求content_type:
尝试对此进行检查:
req.env['HTTP_ACCEPT']
说明
-
req
是一个对象,子类Rack::Request
. - 机架在客户端的HTTP请求标头中添加
HTTP_
并将其添加到env
哈希中. - HTTP客户端可以在
Accept
标头,而不是Content-Type
标头,在那里他们可以指示要发送给您的数据类型.
In my rails 4 app I'd like to response with html
both for html
and js
request. At the moment when the request is html
type the rendering works fine, but when the request is js
then the html file doesn't get rendered on the screen (although in the command line it says it's rendered).
There are different scenarios for limiting requests so the throttle code can be triggered by html POST
and js POST
request as well.
Rack::Attack.throttle(key, limit: from_config(key, :limit), period: from_config(key, :period)) do |req|
if req.path.ends_with?(from_config(key, :path).to_s) && from_config(key, :method) == req.env['REQUEST_METHOD']
### This is the snippet I try to change the req type with but not working
if req.media_type == 'application/javascript'
req.media_type = 'text/html'
end
##### till here
req.ip
end
end
Here is what I'm trying to render. As you see this is html
response.
Rack::Attack.throttled_response = lambda do |env|
[429, {}, [ActionView::Base.new.render(file: 'public/429.html', content_type: 'text/html')]]
end
What should I do?
UPDATE
This is my newest version, but can't figure out how to check the request content_type:
Rack::Attack.throttled_response = lambda do |env|
retry_after = (env['rack.attack.match_data'] || {})[10]
if env['rack.attack.content_type'] == 'text/html'
[429, {'Retry-After' => retry_after.to_s}, [ActionView::Base.new.render(file: 'public/429.html', content_type: 'text/html')]]
elsif env['rack.attack.content_type'] == 'application/javascript'
[429, {'Retry-After' => retry_after.to_s}, window.location.href = '/429.html']
end
end
docs: https://github.com/kickstarter/rack-attack
I agree with @max. In principle, you should not respond with HTML to a request specifically for JS.
But to answer this part of the question:
how to check the request content_type:
Try checking this instead:
req.env['HTTP_ACCEPT']
Explanation
req
is an object that subclassesRack::Request
.- Rack prepends
HTTP_
to the client's HTTP request headers and adds them to theenv
hash. - HTTP clients can indicate what MIME types they accept in the
Accept
header, as opposed to theContent-Type
header, where they can indicate what type of data they are sending to you.
这篇关于同步机架请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!