Commit Graph

57 Commits

Author SHA1 Message Date
Matthew Draper
e06844f69b Expect Static to set lowercase values in Vary 2023-02-26 19:36:29 +10:30
Ryan Hall
3d9a98b474 Allow rails to serve brotli encoded assets
When using an external build process (webpack, grunt) it's helpful for
rails to be able to serve those assets. Brotli has better compression
than gzip and should eventually replace it for static assets.

When using an external build process (webpack, grunt) it's helpful for
rails to be able to serve those assets. Brotli has better compression
than gzip and will eventually replace it for static assets.
2020-06-01 08:57:02 -07:00
Ryuta Kamizono
c81af6ae72 Enable Layout/EmptyLinesAroundAccessModifier cop
We sometimes say "✂️ newline after `private`" in a code review (e.g.
https://github.com/rails/rails/pull/18546#discussion_r23188776,
https://github.com/rails/rails/pull/34832#discussion_r244847195).

Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style
`EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059).

That cop and enforced style will reduce the our code review cost.
2019-06-13 12:00:45 +09:00
yuuji.yaginuma
1b86d90136 Enable Performance/UnfreezeString cop
In Ruby 2.3 or later, `String#+@` is available and `+@` is faster than `dup`.

```ruby
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "benchmark-ips"
end

Benchmark.ips do |x|
  x.report('+@') { +"" }
  x.report('dup') { "".dup }
  x.compare!
end
```

```
$ ruby -v benchmark.rb
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
Warming up --------------------------------------
                  +@   282.289k i/100ms
                 dup   187.638k i/100ms
Calculating -------------------------------------
                  +@      6.775M (± 3.6%) i/s -     33.875M in   5.006253s
                 dup      3.320M (± 2.2%) i/s -     16.700M in   5.032125s

Comparison:
                  +@:  6775299.3 i/s
                 dup:  3320400.7 i/s - 2.04x  slower

```
2018-09-23 08:56:55 +09:00
Andrew White
2ef1458833 Use ASCII-8BIT paths in ActionDispatch::Static
The rack gem returns PATH_INFO as an ASCII-8BIT encoded string but it
was being converted to US-ASCII by the match? method because it was
calling Rack::Utils.escape_path. To prevent incompatibile encoding
warnings use ASCII-8BIT strings for the root path and let Ruby handle
any filename encoding conversion.

Fixes #32294, Closes #32314.
2018-03-22 04:23:49 +00:00
Koichi ITO
d2901bd517 [Action Pack] rubocop -a --only Layout/EmptyLineAfterMagicComment 2017-07-11 13:12:32 +09:00
Kir Shatrov
b3f3d49fd6 Prepare AP and AR to be frozen string friendly 2017-07-06 21:30:43 +03:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing
changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
2017-07-02 02:15:17 +09:30
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
Dylan Thacker-Smith
29c02709cd Add missing gzip footer check in ActiveSupport::Gzip.decompress
A gzip file has a checksum and length for the decompressed data in its
footer which isn't checked by just calling Zlib::GzipReader#read.
Calling Zlib::GzipReader#close must be called after reading to the end
of the file causes this check to be done, which is done by
Zlib::GzipReader.wrap after its block is called.
2017-02-24 17:33:36 -05:00
Benjamin Fleischer
c8b5d828e7 Correct spelling
```
go get -u github.com/client9/misspell/cmd/misspell
misspell  -w -error -source=text .
```
2017-02-05 19:00:18 -06:00
Akira Matsuda
e8ba0c0f21 "Use assert_nil if expecting nil. This will fail in minitest 6." 2016-12-25 02:29:52 +09:00
yuuji.yaginuma
75607a67b4 use Gem.win_platform? to check windows Ruby platforms
`Gem.win_platform?` check if it is Windows more accurately.
Ref: https://github.com/ruby/ruby/blob/ruby_2_2/lib/rubygems.rb#L945..L952
2016-11-30 20:57:50 +09:00
Rafael Mendonça França
fe1f4b2ad5
Add more rubocop rules about whitespaces 2016-10-29 01:17:49 -02:00
Rafael Mendonça França
a3ff9db69c
Remove deprecated cache_control argument from ActionDispatch::Static#initialize 2016-10-10 01:35:59 -03:00
Rafael Mendonça França
55f9b8129a
Add three new rubocop rules
Style/SpaceBeforeBlockBraces
Style/SpaceInsideBlockBraces
Style/SpaceInsideHashLiteralBraces

Fix all violations in the repository.
2016-08-16 04:30:11 -03:00
Ryuta Kamizono
762e3f05f3 Add Style/EmptyLines in .rubocop.yml and remove extra empty lines 2016-08-07 17:50:59 +09:00
Xavier Noria
b326e82dc0 applies remaining conventions across the project 2016-08-06 20:20:22 +02:00
Xavier Noria
35b3de8021 applies new string literal convention in actionpack/test
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
2016-08-06 18:54:50 +02:00
Grey Baker
25c1461766 Don't raise ActionController::UnknownHttpMethod from ActionDispatch::Static
The `ActionDispatch::Static` middleware is used low down in the stack to serve
static assets before doing much processing. Since it's called from so low in
the stack, we don't have access to the request ID at this point, and generally
won't have any exception handling defined (by default `ShowExceptions` is added
to the stack quite a bit higher and relies on logging and request ID).

Before 8f27d6036a
this middleware would ignore unknown HTTP methods, and an exception about these
would be raised higher in the stack. After that commit, however, that exception
will be raised here.

If we want to keep `ActionDispatch::Static` so low in the stack (I think we do)
we should suppress the `ActionController::UnknownHttpMethod` exception here,
and instead let it be raised higher up the stack, once we've had a chance to
define exception handling behaviour.

This PR updates `ActionDispatch::Static` so it passes `Rack::Request` objects to
`ActionDispatch::FileHandler`, which won't raise an
`ActionController::UnknownHttpMethod` error. If an unknown method is
passed, it should exception higher in the stack instead, once we've had a
chance to define exception handling behaviour.`
2016-07-13 17:52:36 +01:00
schneems
fe55163f11 Revert "Its ideal to set Vary: Accept-Encoding, irrespective of whether gzipped version exists or not. This is helpful for CDN's to later distinguish assets, based on previous, current copies and introduced gzip version if any."
This reverts commit 067c52f608568e35181830a5c1016e382650e655.

Conversation: https://github.com/rails/rails/pull/23120#issuecomment-173007011
2016-01-19 16:22:40 -06:00
Vipul A M
067c52f608 Its ideal to set Vary: Accept-Encoding, irrespective of whether gzipped version exists or not. This is helpful for CDN's to later distinguish assets, based on previous, current copies and introduced gzip version if any.
For ref: https://www.fastly.com/blog/best-practices-for-using-the-vary-header

This change sets `Vary` header always, to be on safer side
2016-01-19 23:48:59 +05:30
Jordan Owens
918f0eea97 Prevent attempt to serve a request with a null byte
File paths cannot contain null byte characters and methods that do path
operations such as Rack::Utils#clean_path_info will raise unwanted
errors.
2016-01-12 14:05:54 -05:00
Guo Xiang Tan
190c69ece3 Remove skipped JRuby tests that are passing on 9.0.3.0. 2015-10-27 01:13:28 -02:00
Jeremy Daer
9d05430c95 Merge pull request #19135 from yuki24/access-control-support
Add basic support for access control headers to ActionDispatch::Static
2015-10-13 11:16:50 -07:00
Aaron Patterson
8f27d6036a use a request object to reduce string allocations and not know about ENV keys 2015-08-06 15:12:19 -07:00
Yuki Nishijima
5226058163 Add the ability of returning arbitrary headers to ActionDispatch::Static
Now ActionDispatch::Static can accept HTTP headers so that developers
will have control of returning arbitrary headers like
'Access-Control-Allow-Origin' when a response is delivered. They can
be configured through `#config.public_file_server.headers`:

  config.public_file_server.headers = {
    "Cache-Control"               => "public, max-age=60",
    "Access-Control-Allow-Origin" => "http://rubyonrails.org"
  }

Also deprecate `config.static_cache_control` in favor of
`config.public_file_server.headers`.
2015-06-13 09:30:23 -07:00
Yuki Nishijima
a888c3cdc9 Change the index arg of ActionDispatch::Static#new to a kwarg 2015-06-11 04:34:11 -07:00
Eliot Sykes
3ff39494cd config.static_index configures directory index "index.html" filename
Set `config.static_index` to serve a static directory index file not
named `index`. For example, to serve `main.html` instead of `index.html`
for directory requests, set `config.static_index` to `"main"`.
2015-05-28 09:41:00 +01:00
eileencodes
db8897c2de Use silence_warnings on StaticTests
When 7e504927 was merged setting `Encoding.default_internal` and
`Encoding.default_external` would throw a warning when the ActionPack
tests were run.

Example warning: `actionpack/test/dispatch/static_test.rb:12: warning:
setting Encoding.default_external`

This patch silences the warnings as other similar tests do for setting
default_internal and default_external.
2015-04-14 08:54:13 -04:00
Toshi MARUYAMA
7e50492709 [Rails4 regression] prevent thin and puma cause error in Non ASCII URL on Windows
* https://github.com/rails/rails/issues/19187
* https://github.com/rails/rails/pull/19533
* https://github.com/macournoyer/thin/issues/268

These are serious Rails 4 regression for Redmine Bitnami Windows users.

https://community.bitnami.com/t/problems-with-3-0-1-installation-see-report-inside/30195/

It is not caused on webrick users.

Related:

* https://github.com/rack/rack/issues/732#issuecomment-67677272
* https://github.com/phusion/passenger/issues/1328
2015-04-09 19:25:12 -03:00
Kohei Suzuki
b60c1fe97b Also skip Content-Encoding and Vary header if 304 2015-03-12 23:32:07 +09:00
Kohei Suzuki
d4103fc232 304 response should not include Content-Type header
Rack::Lint raises an error saying "Content-Type header found in 304
response, not allowed".
2015-03-10 23:39:08 +09:00
Vipul A M
6eced6a1fe Removed magic comments # encoding: utf-8 , since its default from ruby 2.0 onwards. 2015-02-03 20:51:40 +05:30
Kuldeep Aggarwal
90aef23e38 No need of requiring rbconfig, it is by-default loaded 2015-01-10 20:27:18 +05:30
Aaron Patterson
d1123f2056 FileHandler should not be called for files outside the root
FileHandler#matches? should return false for files that are outside the
"root" path.
2014-10-10 16:00:03 -07:00
schneems
0b1a87f73c Refactor out Dir.glob from ActionDispatch::Static
Dir.glob can be a security concern. The original use was to provide logic of fallback files. Example a request to `/` should render the file from `/public/index.html`. We can replace the dir glob with the specific logic it represents. The glob {,index,index.html} will look for the current path, then in the directory of the path with index file and then in the directory of the path with index.html. This PR replaces the glob logic by manually checking each potential match. Best case scenario this results in one less file API request, worst case, this has one more file API request.

Related to #16464

Update: added a test for when a file of a given name (`public/bar.html` and a directory `public/bar` both exist in the same root directory. Changed logic to accommodate this scenario.
2014-08-27 13:03:08 -05:00
schneems
8e31fa3b72 Address comments on Gzip implementation
- don't mutate PATH_INFO in env, test
- test fallback content type matches Rack::File
- change assertion style
- make HTTP_ACCEPT_ENCODING comparison case insensitive
- return gzip path from method instead of true/false so we don't have to assume later
- don't allocate un-needed hash.

Original comments:

https://github.com/rails/rails/commit/
cfaaacd9763642e91761de54c90669a88d772e5a#commitcomment-7468728

cc @jeremy
2014-08-24 15:58:16 -05:00
schneems
cfaaacd976 Enable gzip compression by default
If someone is using ActionDispatch::Static to serve assets and makes it past the `match?` then the file exists on disk and it will be served. This PR adds in logic that checks to see if the file being served is already compressed (via gzip) and on disk, if it is it will be served as long as the client can handle gzip encoding. If not, then a non gzip file will be served.

This additional logic slows down an individual asset request but should speed up the consumer experience as compressed files are served and production applications should be delivered with a CDN. This PR allows a CDN to cache a gzip file by setting the `Vary` header appropriately. In net this should speed up a production application that are using Rails as an origin for a CDN. Non-asset request speed is not affected in this PR.
2014-08-20 22:33:06 -05:00
SHIBATA Hiroshi
a09c07890a use File.exist? instead of File.exists? 2014-02-09 17:39:55 +09:00
Mikko Johansson
ccd1c435ee Skips tests on Windows that create files with illegal characters 2014-02-09 02:02:44 +02:00
Gaurish Sharma
35e56f6fa5 standardize on jruby_skip & rbx_skip
This Adds helpers(jruby_skip & rbx_skip). In Future, Plan is to use
these helpers instead of calls directly to
RUBY_ENGINE/RbConfig/JRUBY_VERSION
2014-01-13 19:51:47 +05:30
Andrew White
436ed51711 Fix Encoding::CompatibilityError when public path is UTF-8
In #5337 we forced the path encoding to ASCII-8BIT to prevent static
file handling from blowing up before an application has had chance to
deal with possibly invalid urls. However this has a negative side
effect of making it an incompatible encoding if the application's
public path has UTF-8 characters in it.

To work around the problem we check to see if the path has a valid
encoding once it has been unescaped. If it is not valid then we can
return early since it will not match any file anyway.

Fixes #13518
2013-12-29 18:38:53 +00:00
Kuldeep Aggarwal
fc32b7f99b skiping => skipping 2013-11-19 03:08:08 +05:30
Gaurish Sharma
5122bbb3c2 Skip test which is broken on jruby
This test is broken from quite a while & is expected to remain broken as
encoding issues are hardest to fix in JRuby. so lets skip this test for
now
2013-11-09 02:36:24 +05:30
Andrew White
af73e3cb15 Revert "Invert precedence of content in ActionDispatch::Static"
This reverts commit c59734f756b79c39486c45273d2cc5d42cd0c864.
2012-12-07 06:02:43 +00:00
Andrew White
c59734f756 Invert precedence of content in ActionDispatch::Static
This commit inverts the precedence in ActionDispatch::Static so that
dynamic content will be served before static content. This is so that
precompiled assets do not inadvertently get included when running in
development mode - it should have no effect in production where static
files are usually handled by the web server.

Closes #6421
2012-12-06 17:20:53 +00:00
Michael Reinsch
7d5fbf846b fix ArgumentError being raised in case of invalid byte sequences 2012-03-08 20:41:25 +09:00
Andrew White
1ef12fd172 Remove fixture files with Windows incompatible filenames
Windows doesn't allow `\ / : * ? " < > |` in filenames so create
the fixture files at runtime and ignore the incompatible ones when
running on Windows.
2012-02-20 04:46:45 +00:00
Andrew White
ceb288b8ce Fix ActionDispatch::Static to serve files with unencoded PCHAR
RFC 3986[1] allows sub-delim characters in path segments unencoded,
however Rack::File requires them to be encoded so we use URI's
unescape method to leave them alone and then escape them again.

Also since the path gets passed to Dir[] we need to escape any glob
characters in the path.

[1]: http://www.ietf.org/rfc/rfc3986.txt
2012-02-17 14:07:48 +00:00