Skip to content

Commit b56f707

Browse files
committed
Set Cache-Control only for streamed responses
The pull request ruby-grape#1520 introduced a regression that always caused the `Cache-Control` HTTP header to be set to `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
1 parent 84d68bd commit b56f707

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#### Fixes
88

9-
* Your contribution here.
9+
* [#2083](https://github.com/ruby-grape/grape/pull/2083): Set Cache-Control only for streamed responses - [@stanhu](https://github.com/stanhu).
1010

1111
### 1.4.0 (2020/07/10)
1212

lib/grape/dsl/inside_route.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ def sendfile(value = nil)
328328
# * https://github.com/rack/rack/blob/99293fa13d86cd48021630fcc4bd5acc9de5bdc3/lib/rack/chunked.rb
329329
# * https://github.com/rack/rack/blob/99293fa13d86cd48021630fcc4bd5acc9de5bdc3/lib/rack/etag.rb
330330
def stream(value = nil)
331+
return if value.nil? && !instance_variable_defined?(:@stream)
332+
331333
header 'Content-Length', nil
332334
header 'Transfer-Encoding', nil
333335
header 'Cache-Control', 'no-cache' # Skips ETag generation (reading the response up front)
@@ -339,7 +341,7 @@ def stream(value = nil)
339341
elsif !value.is_a?(NilClass)
340342
raise ArgumentError, 'Stream object must respond to :each.'
341343
else
342-
instance_variable_defined?(:@stream) ? @stream : nil
344+
@stream
343345
end
344346
end
345347

spec/grape/dsl/inside_route_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ def initialize
419419

420420
it 'returns default' do
421421
expect(subject.stream).to be nil
422+
expect(subject.header['Cache-Control']).to eq nil
422423
end
423424
end
424425

0 commit comments

Comments
 (0)