rack-content_disposition_helper 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/MIT-LICENSE +21 -0
- data/README.md +70 -0
- data/lib/rack/content_disposition_helper/content_disposition.rb +43 -0
- data/lib/rack/content_disposition_helper/railtie.rb +11 -0
- data/lib/rack/content_disposition_helper/user_agent.rb +17 -0
- data/lib/rack/content_disposition_helper/version.rb +7 -0
- data/lib/rack/content_disposition_helper.rb +29 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 65bb8f8c0b81289e8992dddcdc32ddf7be95aebbadf8c981a4c11a193670f6f1
|
4
|
+
data.tar.gz: 07f5d4a25e13bd83dc705513720c7b0835f7d29b0b794aab7a840b497232e672
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9a3884c9d2a6f6e757596cdf6354e02cf1fb7bf21454b8dfb0a27112f713ecb3998df85d90eb9114daa74215a0df6712f0d2344f5194234c7b2972a738b0d21e
|
7
|
+
data.tar.gz: 5e78b8f97f0b1939177f7229524cae99f566034431454f3776aaeabb38aa29c4dcc293346a4ae12ea45f1287156f38d51ee4b0c514d87c7f2e6a9f58894600dc
|
data/CHANGELOG.md
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 hana-da
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Rack::ContentDispositionHelper
|
2
|
+
|
3
|
+
Rack::ContentDispositionHelper is Rack middleware that rewrites the decoded `filename*` directive in the Content-Disposition response header as the value of the `filename` directive.
|
4
|
+
|
5
|
+
Safari does not use `filename*` if the size of the Content-Disposition response header exceeds 254Bytes, but this middleware is helpful in such cases.
|
6
|
+
|
7
|
+
This middleware checks that the User-Agent value in the request header is Safari-like.
|
8
|
+
If the User-Agent is not Safari-like, no processing is performed.
|
9
|
+
|
10
|
+
## Usage example
|
11
|
+
|
12
|
+
### Rails app
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
# Gemfile
|
18
|
+
gem 'rack-content_disposition_helper'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
```
|
24
|
+
$ bundle
|
25
|
+
```
|
26
|
+
|
27
|
+
This will load rack-content_disposition_helper and set it up as a Rails middleware. The middleware is inserted for all of environment.
|
28
|
+
|
29
|
+
You don't need to add middleware to rails middleware stack.
|
30
|
+
|
31
|
+
### Sinatra and other Rack apps
|
32
|
+
|
33
|
+
Add this line to your application's Gemfile:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
# Gemfile
|
37
|
+
gem 'rack-content_disposition_helper'
|
38
|
+
```
|
39
|
+
|
40
|
+
And then execute:
|
41
|
+
|
42
|
+
```
|
43
|
+
$ bundle
|
44
|
+
```
|
45
|
+
|
46
|
+
For Sinatra and other Rack apps, add this to config.ru:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
# config.ru
|
50
|
+
use Rack::ContentDispositionHelper
|
51
|
+
```
|
52
|
+
|
53
|
+
## Development
|
54
|
+
|
55
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
56
|
+
|
57
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hana-da/rack-content_disposition_helper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/hana-da/rack-content_disposition_helper/blob/master/CODE_OF_CONDUCT.md).
|
62
|
+
|
63
|
+
|
64
|
+
## License
|
65
|
+
|
66
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
67
|
+
|
68
|
+
## Code of Conduct
|
69
|
+
|
70
|
+
Everyone interacting in the Rack::ContentDispositionHelper project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hana-da/rack-content_disposition_helper/blob/master/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cgi'
|
4
|
+
|
5
|
+
module Rack
|
6
|
+
class ContentDispositionHelper
|
7
|
+
class ContentDisposition
|
8
|
+
LIMIT = 254
|
9
|
+
FILENAME_ASTERISK_PREFIX = "filename*=UTF-8''"
|
10
|
+
|
11
|
+
attr_reader :value, :parts
|
12
|
+
|
13
|
+
def initialize(original_value)
|
14
|
+
@value = original_value
|
15
|
+
@parts = value&.split
|
16
|
+
end
|
17
|
+
|
18
|
+
def long?
|
19
|
+
return nil unless value
|
20
|
+
|
21
|
+
value.length > LIMIT
|
22
|
+
end
|
23
|
+
|
24
|
+
def disposition
|
25
|
+
parts&.first
|
26
|
+
end
|
27
|
+
|
28
|
+
def raw_filename_value
|
29
|
+
return unless disposition
|
30
|
+
|
31
|
+
"#{disposition} filename=\"#{raw_filename}\""
|
32
|
+
end
|
33
|
+
|
34
|
+
def raw_filename
|
35
|
+
filename_asterisk&.then { |v| CGI.unescape(v).delete_prefix!(FILENAME_ASTERISK_PREFIX) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def filename_asterisk
|
39
|
+
parts&.find { |d| d.start_with?(FILENAME_ASTERISK_PREFIX) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class ContentDispositionHelper
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
initializer 'rack-content_disposition_helper.middleware_use' do |app|
|
7
|
+
app.middleware.use Rack::ContentDispositionHelper
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class ContentDispositionHelper
|
5
|
+
class UserAgent
|
6
|
+
SAFARI_PATTERN = %r{ (:?Version|Mobile)/[0-9A-Z.]+ Safari/[0-9.]+\z}.freeze
|
7
|
+
|
8
|
+
def initialize(env)
|
9
|
+
@user_agent = env['HTTP_USER_AGENT']
|
10
|
+
end
|
11
|
+
|
12
|
+
def safari?
|
13
|
+
@user_agent&.match?(SAFARI_PATTERN)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rack/content_disposition_helper/version'
|
4
|
+
|
5
|
+
require 'rack/content_disposition_helper/user_agent'
|
6
|
+
require 'rack/content_disposition_helper/content_disposition'
|
7
|
+
|
8
|
+
require 'rack/content_disposition_helper/railtie' if defined?(::Rails::Railtie)
|
9
|
+
|
10
|
+
module Rack
|
11
|
+
class ContentDispositionHelper
|
12
|
+
def initialize(app)
|
13
|
+
@app = app
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
status_code, headers, body = @app.call(env)
|
18
|
+
|
19
|
+
user_agent = UserAgent.new(env)
|
20
|
+
content_disposition = ContentDisposition.new(headers['Content-Disposition'])
|
21
|
+
|
22
|
+
if content_disposition.long? && user_agent.safari?
|
23
|
+
headers = headers.merge('Content-Disposition' => content_disposition.raw_filename_value)
|
24
|
+
end
|
25
|
+
|
26
|
+
[status_code, headers, body]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-content_disposition_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hana-da
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-03-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3'
|
33
|
+
description: Rack::ContentDispositionHelper is Rack middleware that rewrites the decoded
|
34
|
+
filename* directive in the Content-Disposition response header as the value of the
|
35
|
+
filename directive.
|
36
|
+
email:
|
37
|
+
executables: []
|
38
|
+
extensions: []
|
39
|
+
extra_rdoc_files: []
|
40
|
+
files:
|
41
|
+
- CHANGELOG.md
|
42
|
+
- MIT-LICENSE
|
43
|
+
- README.md
|
44
|
+
- lib/rack/content_disposition_helper.rb
|
45
|
+
- lib/rack/content_disposition_helper/content_disposition.rb
|
46
|
+
- lib/rack/content_disposition_helper/railtie.rb
|
47
|
+
- lib/rack/content_disposition_helper/user_agent.rb
|
48
|
+
- lib/rack/content_disposition_helper/version.rb
|
49
|
+
homepage: https://github.com/hana-da/rack-content_disposition_helper
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata:
|
53
|
+
rubygems_mfa_required: 'true'
|
54
|
+
homepage_uri: https://github.com/hana-da/rack-content_disposition_helper
|
55
|
+
source_code_uri: https://github.com/hana-da/rack-content_disposition_helper
|
56
|
+
bug_tracker_uri: https://github.com/hana-da/rack-content_disposition_helper/issues
|
57
|
+
changelog_uri: https://github.com/hana-da/rack-content_disposition_helper/CHANGELOG.md
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 2.6.9
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.0.3.1
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Rack middleware that modifies Content-Disposition response header.
|
77
|
+
test_files: []
|