hmdne / roda-sprockets

MIT License
5 stars 1 forks source link

Rack::Lint::LintError: Content-Type header found in 304 response, not allowed #1

Closed hmdne closed 3 years ago

hmdne commented 3 years ago

To trigger this bug, one must trigger a 304 response for any (CSS/JS) asset in development mode.

Rack::Lint::LintError: Content-Type header found in 304 response, not allowed
    /home/user/Code/.gem/gems/rack-2.2.3/lib/rack/lint.rb:21:in `assert'
    /home/user/Code/.gem/gems/rack-2.2.3/lib/rack/lint.rb:710:in `block in check_content_type'
    /home/user/Code/.gem/gems/rack-2.2.3/lib/rack/lint.rb:706:in `each'
    /home/user/Code/.gem/gems/rack-2.2.3/lib/rack/lint.rb:706:in `check_content_type'
    /home/user/Code/.gem/gems/rack-2.2.3/lib/rack/lint.rb:70:in `_call'
    /home/user/Code/.gem/gems/rack-2.2.3/lib/rack/lint.rb:38:in `call'
    /home/user/Code/.gem/gems/rack-2.2.3/lib/rack/show_exceptions.rb:23:in `call'
    /home/user/Code/.gem/gems/rack-2.2.3/lib/rack/common_logger.rb:38:in `call'
    /home/user/Code/.gem/gems/rack-2.2.3/lib/rack/content_length.rb:17:in `call'
    /home/user/Code/.gem/gems/puma-5.1.1/lib/puma/configuration.rb:246:in `call'
    /home/user/Code/.gem/gems/puma-5.1.1/lib/puma/request.rb:76:in `block in handle_request'
    /home/user/Code/.gem/gems/puma-5.1.1/lib/puma/thread_pool.rb:337:in `with_force_shutdown'
    /home/user/Code/.gem/gems/puma-5.1.1/lib/puma/request.rb:75:in `handle_request'
    /home/user/Code/.gem/gems/puma-5.1.1/lib/puma/server.rb:431:in `process_client'
    /home/user/Code/.gem/gems/puma-5.1.1/lib/puma/thread_pool.rb:145:in `block in spawn_thread'

There exists a workaround:

if ENV['RACK_ENV'] != 'production'
  require 'rack/lint'

  module Rack::Lint::Assertion
    def assert message
      unless yield
        puts "Rack::Lint error: #{message}"
      end
    end
  end
end

Then, it outputs the following:

Rack::Lint error: Content-Type header found in 304 response, not allowed
Rack::Lint error: Content-Length header found in 304 response, not allowed
zw963 commented 3 years ago

Yes, i meet same issue too, following is my solution, replace Rack::Lint with a empty middleware.

# For disable Rack::Lint
module Rack
  class Lint
    def call(env = nil)
      @app.call(env)
    end
  end
end

Anyway, i propose this issue can be resolved from gem, thank you.

you can check my sample repo here, maybe can help on resolve this.

https://github.com/zw963/marketbet_crawler

you need set following ENV to make this repo run.

export DEVELOPMENT_DATABASE_URL="sqlite://db/files/marketbet_crawler_development.db"
export APP_SESSION_SECRET="909f017cc94c96f8a1aff843d95920485376f4c997143cc3c39ca945c883ec88e310a2177a69b8b714d22af1b5fd7864833568b6bf93fc3bc811bcf6e112"
hmdne commented 3 years ago

This is a problem I can't reliably locate as of now, I assume it's a problem located somewhere in Roda. I will look into it today.

jeremyevans commented 3 years ago

The error comes from a 304 response with a Content-Type header. Roda automatically deletes the Content-Type header if the response body is empty. However, it looks like roda-sprockets is returning a non-empty body (it may be returning an empty string as the body, but that is not treated the same as an empty body). You could try having https://github.com/hmdne/roda-sprockets/blob/master/lib/roda/plugins/sprockets.rb#L149 return nil for 304 responses.

If that doesn't work, you could consider using the Roda drop_body plugin, which will automatically drop the body and remove Content-Type header for 304 responses.

hmdne commented 3 years ago

@jeremyevans Thank you! I took a very long time to try to debug this problem, but your suggestion shown exactly what's the problem here.

zw963 commented 3 years ago

@hmdne , this issue seem like not fixed.

i test on newest master in development.

127.0.0.1 - - [21/Aug/2021:13:54:02 +0800] "GET /stocks HTTP/1.1" 200 4098 19.6698
127.0.0.1 - - [21/Aug/2021:13:54:03 +0800] "GET /assets/app.debug.css HTTP/1.1" 200 164560 1.0283
Rack::Lint::LintError: Content-Length header found in 304 response, not allowed
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:21:in `assert'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:724:in `block in check_content_length'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:720:in `each'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:720:in `check_content_length'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:71:in `_call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:38:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/show_exceptions.rb:23:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/common_logger.rb:38:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/content_length.rb:17:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/configuration.rb:249:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/request.rb:77:in `block in handle_request'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/thread_pool.rb:338:in `with_force_shutdown'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/request.rb:76:in `handle_request'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/server.rb:438:in `process_client'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/thread_pool.rb:145:in `block in spawn_thread'
127.0.0.1 - - [21/Aug/2021:13:54:03 +0800] "GET /assets/app.debug.js HTTP/1.1" 500 56261 0.0577
zw963 commented 3 years ago

BTW: i test use drop_body plugin yesterday, it works quite well.

hmdne commented 3 years ago

I can't reproduce this behavior though. Seems like it's related to Sass yet still I can't reproduce it neither here nor in my app.

zw963 commented 3 years ago

I still can reproduce this when development. (though, not every time)

127.0.0.1 - - [22/Aug/2021:13:12:49 +0800] "GET /latest-institutions?days=60 HTTP/1.1" 200 372733 18.1439
Rack::Lint::LintError: Content-Length header found in 304 response, not allowed
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:21:in `assert'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:724:in `block in check_content_length'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:720:in `each'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:720:in `check_content_length'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:71:in `_call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:38:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/show_exceptions.rb:23:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/common_logger.rb:38:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/content_length.rb:17:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/configuration.rb:249:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/request.rb:77:in `block in handle_request'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/thread_pool.rb:338:in `with_force_shutdown'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/request.rb:76:in `handle_request'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/server.rb:438:in `process_client'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/thread_pool.rb:145:in `block in spawn_thread'
127.0.0.1 - - [22/Aug/2021:13:12:49 +0800] "GET /assets/app.debug.css HTTP/1.1" 500 56307 0.0155
Rack::Lint::LintError: Content-Length header found in 304 response, not allowed
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:21:in `assert'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:724:in `block in check_content_length'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:720:in `each'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:720:in `check_content_length'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:71:in `_call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/lint.rb:38:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/show_exceptions.rb:23:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/common_logger.rb:38:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/rack-2.2.3/lib/rack/content_length.rb:17:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/configuration.rb:249:in `call'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/request.rb:77:in `block in handle_request'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/thread_pool.rb:338:in `with_force_shutdown'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/request.rb:76:in `handle_request'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/server.rb:438:in `process_client'
        /home/zw963/.rvm/gems/ruby-3.0.2@marketbet_crawler/gems/puma-5.3.2/lib/puma/thread_pool.rb:145:in `block in spawn_thread'
127.0.0.1 - - [22/Aug/2021:13:12:50 +0800] "GET /assets/app.debug.js HTTP/1.1" 500 56282 0.0498

maybe you want test use my app, following is a development database, you can unzip it throw it to 'db/files' folder.

development.zip

you need set env like this too:

export DEVELOPMENT_DATABASE_URL="sqlite://db/files/marketbet_crawler_development.db"
export TEST_DATABASE_URL="sqlite://db/files/marketbet_crawler_test.db"
export DATABASE_URL="sqlite://db/files/marketbet_crawler_production.db"
export APP_SESSION_SECRET="909f017cc94c96f8a1aff843d95920485376f4c997143cc3c39ca945c883ec88e310a2177a69b8b714d22af1b5fd7864833568b6bf93fc3bc811bcf6e112"

And test it with: (if port is 9393)

http://127.0.0.1:9393/latest-institutions?days=60

zw963 commented 3 years ago

BTW: if you want reproduce, please use branch test_304 instead.

https://github.com/zw963/marketbet_crawler/tree/test_304

i disable drop_body plugin in this branch.

hmdne commented 3 years ago

This is the reason for the issue:

https://github.com/zw963/marketbet_crawler/blob/master/app/app.rb#L17

So I will close this bug now and release v1.1.0 tonight.

zw963 commented 3 years ago

sorry for confusing, that works, thank you!