0

Rails 4 アプリでは、forとrequesthtmlの両方で応答したいと思います。リクエストがタイプの場合、レンダリングは正常に機能しますが、リクエストがタイプの場合、htmlファイルは画面にレンダリングされません(コマンドラインではレンダリングされたと表示されますが)。htmljshtmljs

リクエストを制限するためのさまざまなシナリオがあるため、スロットル コードはリクエストによってもトリガーさ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

ドキュメント: https://github.com/kickstarter/rack-attack

4

1 に答える 1