rack 3.0.11 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rack might be problematic. Click here for more details.

data/lib/rack/chunked.rb DELETED
@@ -1,120 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'constants'
4
- require_relative 'utils'
5
-
6
- module Rack
7
- warn "Rack::Chunked is deprecated and will be removed in Rack 3.1", uplevel: 1
8
-
9
- # Middleware that applies chunked transfer encoding to response bodies
10
- # when the response does not include a content-length header.
11
- #
12
- # This supports the trailer response header to allow the use of trailing
13
- # headers in the chunked encoding. However, using this requires you manually
14
- # specify a response body that supports a +trailers+ method. Example:
15
- #
16
- # [200, { 'trailer' => 'expires'}, ["Hello", "World"]]
17
- # # error raised
18
- #
19
- # body = ["Hello", "World"]
20
- # def body.trailers
21
- # { 'expires' => Time.now.to_s }
22
- # end
23
- # [200, { 'trailer' => 'expires'}, body]
24
- # # No exception raised
25
- class Chunked
26
- include Rack::Utils
27
-
28
- # A body wrapper that emits chunked responses.
29
- class Body
30
- TERM = "\r\n"
31
- TAIL = "0#{TERM}"
32
-
33
- # Store the response body to be chunked.
34
- def initialize(body)
35
- @body = body
36
- end
37
-
38
- # For each element yielded by the response body, yield
39
- # the element in chunked encoding.
40
- def each(&block)
41
- term = TERM
42
- @body.each do |chunk|
43
- size = chunk.bytesize
44
- next if size == 0
45
-
46
- yield [size.to_s(16), term, chunk.b, term].join
47
- end
48
- yield TAIL
49
- yield_trailers(&block)
50
- yield term
51
- end
52
-
53
- # Close the response body if the response body supports it.
54
- def close
55
- @body.close if @body.respond_to?(:close)
56
- end
57
-
58
- private
59
-
60
- # Do nothing as this class does not support trailer headers.
61
- def yield_trailers
62
- end
63
- end
64
-
65
- # A body wrapper that emits chunked responses and also supports
66
- # sending Trailer headers. Note that the response body provided to
67
- # initialize must have a +trailers+ method that returns a hash
68
- # of trailer headers, and the rack response itself should have a
69
- # Trailer header listing the headers that the +trailers+ method
70
- # will return.
71
- class TrailerBody < Body
72
- private
73
-
74
- # Yield strings for each trailer header.
75
- def yield_trailers
76
- @body.trailers.each_pair do |k, v|
77
- yield "#{k}: #{v}\r\n"
78
- end
79
- end
80
- end
81
-
82
- def initialize(app)
83
- @app = app
84
- end
85
-
86
- # Whether the HTTP version supports chunked encoding (HTTP 1.1 does).
87
- def chunkable_version?(ver)
88
- case ver
89
- # pre-HTTP/1.0 (informally "HTTP/0.9") HTTP requests did not have
90
- # a version (nor response headers)
91
- when 'HTTP/1.0', nil, 'HTTP/0.9'
92
- false
93
- else
94
- true
95
- end
96
- end
97
-
98
- # If the rack app returns a response that should have a body,
99
- # but does not have content-length or transfer-encoding headers,
100
- # modify the response to use chunked transfer-encoding.
101
- def call(env)
102
- status, headers, body = response = @app.call(env)
103
-
104
- if chunkable_version?(env[SERVER_PROTOCOL]) &&
105
- !STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) &&
106
- !headers[CONTENT_LENGTH] &&
107
- !headers[TRANSFER_ENCODING]
108
-
109
- headers[TRANSFER_ENCODING] = 'chunked'
110
- if headers['trailer']
111
- response[2] = TrailerBody.new(body)
112
- else
113
- response[2] = Body.new(body)
114
- end
115
- end
116
-
117
- response
118
- end
119
- end
120
- end
data/lib/rack/file.rb DELETED
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'files'
4
-
5
- module Rack
6
- warn "Rack::File is deprecated and will be removed in Rack 3.1", uplevel: 1
7
-
8
- File = Files
9
- end