Skip to content

Commit

Permalink
Don't set Cache-Control for responses that don't use streams
Browse files Browse the repository at this point in the history
Cache-Control HTTP header with `no-cache`, even if the response wasn't a
stream. To fix this, we only set HTTP headers if there is an actual
stream.

Closes ruby-grape#2087
  • Loading branch information
stanhu committed Jul 10, 2020
1 parent 84d68bd commit 7253a7b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#### Fixes

* Your contribution here.
* [#2083](https://github.com/ruby-grape/grape/pull/2083): Don't set Cache-Control for endpoints that aren't streams - [@stanhu](https://github.com/stanhu).

### 1.4.0 (2020/07/10)

Expand Down
4 changes: 3 additions & 1 deletion lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ def sendfile(value = nil)
# * https://github.com/rack/rack/blob/99293fa13d86cd48021630fcc4bd5acc9de5bdc3/lib/rack/chunked.rb
# * https://github.com/rack/rack/blob/99293fa13d86cd48021630fcc4bd5acc9de5bdc3/lib/rack/etag.rb
def stream(value = nil)
return if value.nil? && !instance_variable_defined?(:@stream)

header 'Content-Length', nil
header 'Transfer-Encoding', nil
header 'Cache-Control', 'no-cache' # Skips ETag generation (reading the response up front)
Expand All @@ -339,7 +341,7 @@ def stream(value = nil)
elsif !value.is_a?(NilClass)
raise ArgumentError, 'Stream object must respond to :each.'
else
instance_variable_defined?(:@stream) ? @stream : nil
@stream
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/grape/dsl/inside_route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ def initialize

it 'returns default' do
expect(subject.stream).to be nil
expect(subject.header['Cache-Control']).to eq nil
end
end

Expand Down

0 comments on commit 7253a7b

Please sign in to comment.