2016-05-04 9 views
0

Transfer-Encoding: chunkedヘッダーの代わりにContent-Length: 42ヘッダーで応答したいと思います。Content-Lengthヘッダーに応答します

require File.expand_path('../boot', __FILE__) 

require "rails" 
# Pick the frameworks you want: 
require "active_model/railtie" 
require "active_job/railtie" 
require "active_record/railtie" 
require "action_controller/railtie" 
require "action_mailer/railtie" 
require "action_view/railtie" 
require "action_cable/engine" 
require "sprockets/railtie" 
# require "rails/test_unit/railtie" 

# Require the gems listed in Gemfile, including any gems 
# you've limited to :test, :development, or :production. 
Bundler.require(*Rails.groups) 

module FuuBarApp 
    class Application < Rails::Application 
    # Settings in config/environments/* take precedence over those specified here. 
    # Application configuration should go into files in config/initializers 
    # -- all .rb files in that directory are automatically loaded. 

    config.active_record.schema_format = :sql 
    # config.api_only = true 
    config.autoload_paths << Rails.root.join('lib') 

    # content compression 
    config.middleware.use(Rack::Deflater) 

    # looks like for view partial caching 
    config.middleware.delete(ActionView::Digestor::PerRequestDigestCacheExpiry) 

    # remove session related middleware 
    config.middleware.delete(ActionDispatch::Cookies) 
    config.middleware.delete(ActionDispatch::Session::CookieStore) 
    config.middleware.delete(ActionDispatch::Flash) 

    # remove browser related middleware 
    # config.middleware.delete(ActionDispatch::BestStandardsSupport) 
    config.middleware.delete(Rack::MethodOverride) 

    # used to serve static assets 
    config.middleware.delete(ActionDispatch::Static) 

    # sets env["rack.multithread"] flag to true and wraps the application within a Mutex 
    config.middleware.delete(Rack::Lock) 

    # used for memory caching. This cache is not thread safe 
    # config.middleware.delete(ActiveSupport::Cache::Strategy::LocalCache::Middleware) 

    # sets an X-Runtime header, containing the time (in seconds) taken to execute the request 
    config.middleware.delete(Rack::Runtime) 

    # makes a unique X-Request-Id header available to the response and enables the ActionDispatch::Request#uuid method 
    config.middleware.delete(ActionDispatch::RequestId) 

    # notifies the logs that the request has began 
    # after request is complete, flushes all the logs 
    config.middleware.delete(Rails::Rack::Logger) 

    # rescues any exception returned by the application and calls an exceptions app that will wrap it in a format for the end user 
    config.middleware.delete(ActionDispatch::ShowExceptions) 

    # responsible for logging exceptions and showing a debugging page in case the request is local 
    config.middleware.delete(ActionDispatch::DebugExceptions) 

    # checks for IP spoofing attacks 
    config.middleware.delete(ActionDispatch::RemoteIp) 

    # runs the prepare callbacks before serving the request 
    config.middleware.delete(ActionDispatch::Callbacks) 

    # cleans active connections after each request, unless the rack.test key in the request environment is set to true 
    config.middleware.delete(ActiveRecord::ConnectionAdapters::ConnectionManagement) 

    # adds support for "Conditional GET" so that server responds with nothing if page wasn't changed 
    config.middleware.delete(Rack::ConditionalGet) 

    # adds ETag header on all String bodies 
    # ETags are used to validate cache 
    config.middleware.delete(Rack::ETag) 

    config.middleware.use(Rack::ContentLength) 

    config.action_dispatch.default_headers = {} 
    end 
end 

我々が見ることができるように、Rack::ContentLengthが存在している:私はここにRailsの5.0.0.beta3上だと

は私のapplication.rbです。アクションで は、私もしてヘッダーを設定します。

response.headers['Content-Length'] = body.length 

しかし、HTTP応答のいずれかのContent-Lengthヘッダーがありませんがあります:

Allow:GET, HEAD, OPTIONS 
Cache-Control:max-age=31557600, public 
Content-Encoding:gzip 
Content-Language:en 
Content-Type:application/json; charset=utf-8 
ETag:3bfc269594ef649228e9a74bab00f042efc91d5acc6fbee31a382e80d42388fe 
Last-Modified:Tue, 03 May 2016 19:59:21 GMT 
Link:</>; class="home"; rel="self" 
Transfer-Encoding:chunked 
Vary:Accept-Encoding 

はところで、私はそれを削除する方法については考えているTransfer-Encoding不要なヘッダー。

ベストプラクティスまたはヒントをお持ちの場合は、歓迎してください。ありがとう。

答えて

1

私はこのようなコントローラでafter_actionresponse.headers["Content-Length"] = **を書く:

class ActivitiesController < ApplicationController 
    after_action :set_version_header 
    def index 
    return render plain: params[:echostr] 
    end 

    private 
    def set_version_header 
     response.headers["Content-Length"] = params[:echostr].length 
    end 
end 

それはここでの問題は、クライアント(ブラウザ)を求めたということであるように見えます応答ヘッダにContent-Length

1

Transfer-Encodingを交換します圧縮応答(accept-encodingヘッダーを使用)応答にgzipのContent-Encodingがあるため、これを伝えることができます。だからあなたのRailsアプリは、おそらく圧縮されていないコンテンツのContent-Lengthヘッダを設定したのですが、(クライアントのリクエストで)レスポンスを圧縮しました。ほとんどのサーバーは、ストリーム全体が圧縮される前にバイトを返すように、インライン(ストリーム/ブロック圧縮)の応答を圧縮します。つまり、最終的なサイズを知る手段がありません。応答バイトの前にヘッダーを設定する必要があるため、サーバーは、圧縮されていない(無効な)コンテンツ長をTransfer-Encoding:Chunkedに置き換えます。

クライアントで圧縮を要求しないでください(実際にはブラウザのオプションではありません)。または、Rubyサーバで圧縮をオフにする必要があります。デフォルトのRack :: Deflaterミドルウェアは、特定のコンテンツタイプのみを圧縮するように設定することもできますし、:ifブロックを設定して、必要な条件に基づいて圧縮を無効にすることもできます。設定オプションについては、Rack::Deflaterを参照してください。

関連する問題