同步机架请求和响应 [英] sync rack request and response

查看:84
本文介绍了同步机架请求和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails 4应用程序中,我想同时对htmljs请求使用html进行响应.在请求为html时,呈现工作正常,但是当请求为js时,则不会在屏幕上呈现html文件(尽管在命令行中表示已呈现).

限制请求的方案有多种,因此,html POSTjs 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']

说明

  1. req 是一个对象,子类 Rack::Request.
  2. 机架在客户端的HTTP请求标头中添加 HTTP_并将其添加到env哈希中.
  3. 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

  1. req is an object that subclasses Rack::Request.
  2. Rack prepends HTTP_ to the client's HTTP request headers and adds them to the env hash.
  3. HTTP clients can indicate what MIME types they accept in the Accept header, as opposed to the Content-Type header, where they can indicate what type of data they are sending to you.

这篇关于同步机架请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆