rack 1.0.1 → 2.2.21

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.
Files changed (147) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +829 -0
  3. data/CONTRIBUTING.md +136 -0
  4. data/{COPYING → MIT-LICENSE} +4 -2
  5. data/README.rdoc +355 -0
  6. data/Rakefile +64 -98
  7. data/SPEC.rdoc +292 -0
  8. data/bin/rackup +3 -174
  9. data/contrib/rack.png +0 -0
  10. data/contrib/rack.svg +150 -0
  11. data/contrib/rack_logo.svg +164 -111
  12. data/contrib/rdoc.css +412 -0
  13. data/example/lobster.ru +2 -0
  14. data/example/protectedlobster.rb +4 -2
  15. data/example/protectedlobster.ru +3 -1
  16. data/lib/rack/auth/abstract/handler.rb +7 -5
  17. data/lib/rack/auth/abstract/request.rb +12 -2
  18. data/lib/rack/auth/basic.rb +6 -4
  19. data/lib/rack/auth/digest/md5.rb +20 -13
  20. data/lib/rack/auth/digest/nonce.rb +6 -4
  21. data/lib/rack/auth/digest/params.rb +11 -12
  22. data/lib/rack/auth/digest/request.rb +15 -12
  23. data/lib/rack/body_proxy.rb +45 -0
  24. data/lib/rack/builder.rb +213 -19
  25. data/lib/rack/cascade.rb +50 -18
  26. data/lib/rack/chunked.rb +95 -27
  27. data/lib/rack/common_logger.rb +84 -0
  28. data/lib/rack/conditional_get.rb +83 -0
  29. data/lib/rack/config.rb +22 -0
  30. data/lib/rack/content_length.rb +19 -10
  31. data/lib/rack/content_type.rb +12 -5
  32. data/lib/rack/core_ext/regexp.rb +14 -0
  33. data/lib/rack/deflater.rb +90 -42
  34. data/lib/rack/directory.rb +118 -72
  35. data/lib/rack/etag.rb +77 -0
  36. data/lib/rack/events.rb +153 -0
  37. data/lib/rack/file.rb +4 -85
  38. data/lib/rack/files.rb +218 -0
  39. data/lib/rack/handler/cgi.rb +22 -24
  40. data/lib/rack/handler/fastcgi.rb +34 -24
  41. data/lib/rack/handler/lsws.rb +16 -15
  42. data/lib/rack/handler/scgi.rb +38 -29
  43. data/lib/rack/handler/thin.rb +23 -7
  44. data/lib/rack/handler/webrick.rb +95 -37
  45. data/lib/rack/handler.rb +49 -14
  46. data/lib/rack/head.rb +19 -13
  47. data/lib/rack/lint.rb +466 -217
  48. data/lib/rack/lobster.rb +19 -14
  49. data/lib/rack/lock.rb +24 -8
  50. data/lib/rack/logger.rb +20 -0
  51. data/lib/rack/media_type.rb +53 -0
  52. data/lib/rack/method_override.rb +52 -0
  53. data/lib/rack/mime.rb +652 -171
  54. data/lib/rack/mock.rb +203 -64
  55. data/lib/rack/multipart/generator.rb +97 -0
  56. data/lib/rack/multipart/parser.rb +440 -0
  57. data/lib/rack/multipart/uploaded_file.rb +41 -0
  58. data/lib/rack/multipart.rb +64 -0
  59. data/lib/rack/null_logger.rb +39 -0
  60. data/lib/rack/query_parser.rb +266 -0
  61. data/lib/rack/recursive.rb +18 -11
  62. data/lib/rack/reloader.rb +13 -5
  63. data/lib/rack/request.rb +607 -191
  64. data/lib/rack/response.rb +245 -110
  65. data/lib/rack/rewindable_input.rb +15 -21
  66. data/lib/rack/runtime.rb +34 -0
  67. data/lib/rack/sendfile.rb +192 -0
  68. data/lib/rack/server.rb +466 -0
  69. data/lib/rack/session/abstract/id.rb +449 -68
  70. data/lib/rack/session/cookie.rb +159 -47
  71. data/lib/rack/session/memcache.rb +4 -103
  72. data/lib/rack/session/pool.rb +39 -49
  73. data/lib/rack/show_exceptions.rb +390 -0
  74. data/lib/rack/{showstatus.rb → show_status.rb} +21 -14
  75. data/lib/rack/static.rb +164 -14
  76. data/lib/rack/tempfile_reaper.rb +22 -0
  77. data/lib/rack/urlmap.rb +60 -18
  78. data/lib/rack/utils.rb +508 -277
  79. data/lib/rack/version.rb +29 -0
  80. data/lib/rack.rb +85 -34
  81. data/rack.gemspec +43 -51
  82. metadata +119 -207
  83. data/KNOWN-ISSUES +0 -18
  84. data/RDOX +0 -428
  85. data/README +0 -364
  86. data/SPEC +0 -164
  87. data/lib/rack/adapter/camping.rb +0 -22
  88. data/lib/rack/auth/openid.rb +0 -480
  89. data/lib/rack/commonlogger.rb +0 -61
  90. data/lib/rack/conditionalget.rb +0 -47
  91. data/lib/rack/handler/evented_mongrel.rb +0 -8
  92. data/lib/rack/handler/mongrel.rb +0 -87
  93. data/lib/rack/handler/swiftiplied_mongrel.rb +0 -8
  94. data/lib/rack/methodoverride.rb +0 -27
  95. data/lib/rack/showexceptions.rb +0 -349
  96. data/test/cgi/lighttpd.conf +0 -20
  97. data/test/cgi/test +0 -9
  98. data/test/cgi/test.fcgi +0 -8
  99. data/test/cgi/test.ru +0 -7
  100. data/test/multipart/binary +0 -0
  101. data/test/multipart/empty +0 -10
  102. data/test/multipart/ie +0 -6
  103. data/test/multipart/nested +0 -10
  104. data/test/multipart/none +0 -9
  105. data/test/multipart/semicolon +0 -6
  106. data/test/multipart/text +0 -10
  107. data/test/spec_rack_auth_basic.rb +0 -73
  108. data/test/spec_rack_auth_digest.rb +0 -226
  109. data/test/spec_rack_auth_openid.rb +0 -84
  110. data/test/spec_rack_builder.rb +0 -84
  111. data/test/spec_rack_camping.rb +0 -51
  112. data/test/spec_rack_cascade.rb +0 -50
  113. data/test/spec_rack_cgi.rb +0 -89
  114. data/test/spec_rack_chunked.rb +0 -62
  115. data/test/spec_rack_commonlogger.rb +0 -32
  116. data/test/spec_rack_conditionalget.rb +0 -41
  117. data/test/spec_rack_content_length.rb +0 -43
  118. data/test/spec_rack_content_type.rb +0 -30
  119. data/test/spec_rack_deflater.rb +0 -127
  120. data/test/spec_rack_directory.rb +0 -61
  121. data/test/spec_rack_fastcgi.rb +0 -89
  122. data/test/spec_rack_file.rb +0 -75
  123. data/test/spec_rack_handler.rb +0 -43
  124. data/test/spec_rack_head.rb +0 -30
  125. data/test/spec_rack_lint.rb +0 -521
  126. data/test/spec_rack_lobster.rb +0 -45
  127. data/test/spec_rack_lock.rb +0 -38
  128. data/test/spec_rack_methodoverride.rb +0 -60
  129. data/test/spec_rack_mock.rb +0 -157
  130. data/test/spec_rack_mongrel.rb +0 -189
  131. data/test/spec_rack_recursive.rb +0 -77
  132. data/test/spec_rack_request.rb +0 -504
  133. data/test/spec_rack_response.rb +0 -218
  134. data/test/spec_rack_rewindable_input.rb +0 -118
  135. data/test/spec_rack_session_cookie.rb +0 -82
  136. data/test/spec_rack_session_memcache.rb +0 -240
  137. data/test/spec_rack_session_pool.rb +0 -172
  138. data/test/spec_rack_showexceptions.rb +0 -21
  139. data/test/spec_rack_showstatus.rb +0 -72
  140. data/test/spec_rack_static.rb +0 -37
  141. data/test/spec_rack_thin.rb +0 -91
  142. data/test/spec_rack_urlmap.rb +0 -185
  143. data/test/spec_rack_utils.rb +0 -387
  144. data/test/spec_rack_webrick.rb +0 -130
  145. data/test/testrequest.rb +0 -57
  146. data/test/unregistered_handler/rack/handler/unregistered.rb +0 -7
  147. data/test/unregistered_handler/rack/handler/unregistered_long_one.rb +0 -7
@@ -0,0 +1,390 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ostruct'
4
+ require 'erb'
5
+
6
+ module Rack
7
+ # Rack::ShowExceptions catches all exceptions raised from the app it
8
+ # wraps. It shows a useful backtrace with the sourcefile and
9
+ # clickable context, the whole Rack environment and the request
10
+ # data.
11
+ #
12
+ # Be careful when you use this on public-facing sites as it could
13
+ # reveal information helpful to attackers.
14
+
15
+ class ShowExceptions
16
+ CONTEXT = 7
17
+
18
+ def initialize(app)
19
+ @app = app
20
+ end
21
+
22
+ def call(env)
23
+ @app.call(env)
24
+ rescue StandardError, LoadError, SyntaxError => e
25
+ exception_string = dump_exception(e)
26
+
27
+ env[RACK_ERRORS].puts(exception_string)
28
+ env[RACK_ERRORS].flush
29
+
30
+ if accepts_html?(env)
31
+ content_type = "text/html"
32
+ body = pretty(env, e)
33
+ else
34
+ content_type = "text/plain"
35
+ body = exception_string
36
+ end
37
+
38
+ [
39
+ 500,
40
+ {
41
+ CONTENT_TYPE => content_type,
42
+ CONTENT_LENGTH => body.bytesize.to_s,
43
+ },
44
+ [body],
45
+ ]
46
+ end
47
+
48
+ def prefers_plaintext?(env)
49
+ !accepts_html?(env)
50
+ end
51
+
52
+ def accepts_html?(env)
53
+ Rack::Utils.best_q_match(env["HTTP_ACCEPT"], %w[text/html])
54
+ end
55
+ private :accepts_html?
56
+
57
+ def dump_exception(exception)
58
+ string = "#{exception.class}: #{exception.message}\n".dup
59
+ string << exception.backtrace.map { |l| "\t#{l}" }.join("\n")
60
+ string
61
+ end
62
+
63
+ def pretty(env, exception)
64
+ req = Rack::Request.new(env)
65
+
66
+ # This double assignment is to prevent an "unused variable" warning.
67
+ # Yes, it is dumb, but I don't like Ruby yelling at me.
68
+ path = path = (req.script_name + req.path_info).squeeze("/")
69
+
70
+ # This double assignment is to prevent an "unused variable" warning.
71
+ # Yes, it is dumb, but I don't like Ruby yelling at me.
72
+ frames = frames = exception.backtrace.map { |line|
73
+ frame = OpenStruct.new
74
+ if line =~ /(.*?):(\d+)(:in `(.*)')?/
75
+ frame.filename = $1
76
+ frame.lineno = $2.to_i
77
+ frame.function = $4
78
+
79
+ begin
80
+ lineno = frame.lineno - 1
81
+ lines = ::File.readlines(frame.filename)
82
+ frame.pre_context_lineno = [lineno - CONTEXT, 0].max
83
+ frame.pre_context = lines[frame.pre_context_lineno...lineno]
84
+ frame.context_line = lines[lineno].chomp
85
+ frame.post_context_lineno = [lineno + CONTEXT, lines.size].min
86
+ frame.post_context = lines[lineno + 1..frame.post_context_lineno]
87
+ rescue
88
+ end
89
+
90
+ frame
91
+ else
92
+ nil
93
+ end
94
+ }.compact
95
+
96
+ template.result(binding)
97
+ end
98
+
99
+ def template
100
+ TEMPLATE
101
+ end
102
+
103
+ def h(obj) # :nodoc:
104
+ case obj
105
+ when String
106
+ Utils.escape_html(obj)
107
+ else
108
+ Utils.escape_html(obj.inspect)
109
+ end
110
+ end
111
+
112
+ # :stopdoc:
113
+
114
+ # adapted from Django <www.djangoproject.com>
115
+ # Copyright (c) Django Software Foundation and individual contributors.
116
+ # Used under the modified BSD license:
117
+ # http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5
118
+ TEMPLATE = ERB.new(<<-'HTML'.gsub(/^ /, ''))
119
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
120
+ <html lang="en">
121
+ <head>
122
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
123
+ <meta name="robots" content="NONE,NOARCHIVE" />
124
+ <title><%=h exception.class %> at <%=h path %></title>
125
+ <style type="text/css">
126
+ html * { padding:0; margin:0; }
127
+ body * { padding:10px 20px; }
128
+ body * * { padding:0; }
129
+ body { font:small sans-serif; }
130
+ body>div { border-bottom:1px solid #ddd; }
131
+ h1 { font-weight:normal; }
132
+ h2 { margin-bottom:.8em; }
133
+ h2 span { font-size:80%; color:#666; font-weight:normal; }
134
+ h3 { margin:1em 0 .5em 0; }
135
+ h4 { margin:0 0 .5em 0; font-weight: normal; }
136
+ table {
137
+ border:1px solid #ccc; border-collapse: collapse; background:white; }
138
+ tbody td, tbody th { vertical-align:top; padding:2px 3px; }
139
+ thead th {
140
+ padding:1px 6px 1px 3px; background:#fefefe; text-align:left;
141
+ font-weight:normal; font-size:11px; border:1px solid #ddd; }
142
+ tbody th { text-align:right; color:#666; padding-right:.5em; }
143
+ table.vars { margin:5px 0 2px 40px; }
144
+ table.vars td, table.req td { font-family:monospace; }
145
+ table td.code { width:100%;}
146
+ table td.code div { overflow:hidden; }
147
+ table.source th { color:#666; }
148
+ table.source td {
149
+ font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
150
+ ul.traceback { list-style-type:none; }
151
+ ul.traceback li.frame { margin-bottom:1em; }
152
+ div.context { margin: 10px 0; }
153
+ div.context ol {
154
+ padding-left:30px; margin:0 10px; list-style-position: inside; }
155
+ div.context ol li {
156
+ font-family:monospace; white-space:pre; color:#666; cursor:pointer; }
157
+ div.context ol.context-line li { color:black; background-color:#ccc; }
158
+ div.context ol.context-line li span { float: right; }
159
+ div.commands { margin-left: 40px; }
160
+ div.commands a { color:black; text-decoration:none; }
161
+ #summary { background: #ffc; }
162
+ #summary h2 { font-weight: normal; color: #666; }
163
+ #summary ul#quicklinks { list-style-type: none; margin-bottom: 2em; }
164
+ #summary ul#quicklinks li { float: left; padding: 0 1em; }
165
+ #summary ul#quicklinks>li+li { border-left: 1px #666 solid; }
166
+ #explanation { background:#eee; }
167
+ #template, #template-not-exist { background:#f6f6f6; }
168
+ #template-not-exist ul { margin: 0 0 0 20px; }
169
+ #traceback { background:#eee; }
170
+ #requestinfo { background:#f6f6f6; padding-left:120px; }
171
+ #summary table { border:none; background:transparent; }
172
+ #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
173
+ #requestinfo h3 { margin-bottom:-1em; }
174
+ .error { background: #ffc; }
175
+ .specific { color:#cc3300; font-weight:bold; }
176
+ </style>
177
+ <script type="text/javascript">
178
+ //<!--
179
+ function getElementsByClassName(oElm, strTagName, strClassName){
180
+ // Written by Jonathan Snook, http://www.snook.ca/jon;
181
+ // Add-ons by Robert Nyman, http://www.robertnyman.com
182
+ var arrElements = (strTagName == "*" && document.all)? document.all :
183
+ oElm.getElementsByTagName(strTagName);
184
+ var arrReturnElements = new Array();
185
+ strClassName = strClassName.replace(/\-/g, "\\-");
186
+ var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$$)");
187
+ var oElement;
188
+ for(var i=0; i<arrElements.length; i++){
189
+ oElement = arrElements[i];
190
+ if(oRegExp.test(oElement.className)){
191
+ arrReturnElements.push(oElement);
192
+ }
193
+ }
194
+ return (arrReturnElements)
195
+ }
196
+ function hideAll(elems) {
197
+ for (var e = 0; e < elems.length; e++) {
198
+ elems[e].style.display = 'none';
199
+ }
200
+ }
201
+ window.onload = function() {
202
+ hideAll(getElementsByClassName(document, 'table', 'vars'));
203
+ hideAll(getElementsByClassName(document, 'ol', 'pre-context'));
204
+ hideAll(getElementsByClassName(document, 'ol', 'post-context'));
205
+ }
206
+ function toggle() {
207
+ for (var i = 0; i < arguments.length; i++) {
208
+ var e = document.getElementById(arguments[i]);
209
+ if (e) {
210
+ e.style.display = e.style.display == 'none' ? 'block' : 'none';
211
+ }
212
+ }
213
+ return false;
214
+ }
215
+ function varToggle(link, id) {
216
+ toggle('v' + id);
217
+ var s = link.getElementsByTagName('span')[0];
218
+ var uarr = String.fromCharCode(0x25b6);
219
+ var darr = String.fromCharCode(0x25bc);
220
+ s.innerHTML = s.innerHTML == uarr ? darr : uarr;
221
+ return false;
222
+ }
223
+ //-->
224
+ </script>
225
+ </head>
226
+ <body>
227
+
228
+ <div id="summary">
229
+ <h1><%=h exception.class %> at <%=h path %></h1>
230
+ <h2><%=h exception.message %></h2>
231
+ <table><tr>
232
+ <th>Ruby</th>
233
+ <td>
234
+ <% if first = frames.first %>
235
+ <code><%=h first.filename %></code>: in <code><%=h first.function %></code>, line <%=h frames.first.lineno %>
236
+ <% else %>
237
+ unknown location
238
+ <% end %>
239
+ </td>
240
+ </tr><tr>
241
+ <th>Web</th>
242
+ <td><code><%=h req.request_method %> <%=h(req.host + path)%></code></td>
243
+ </tr></table>
244
+
245
+ <h3>Jump to:</h3>
246
+ <ul id="quicklinks">
247
+ <li><a href="#get-info">GET</a></li>
248
+ <li><a href="#post-info">POST</a></li>
249
+ <li><a href="#cookie-info">Cookies</a></li>
250
+ <li><a href="#env-info">ENV</a></li>
251
+ </ul>
252
+ </div>
253
+
254
+ <div id="traceback">
255
+ <h2>Traceback <span>(innermost first)</span></h2>
256
+ <ul class="traceback">
257
+ <% frames.each { |frame| %>
258
+ <li class="frame">
259
+ <code><%=h frame.filename %></code>: in <code><%=h frame.function %></code>
260
+
261
+ <% if frame.context_line %>
262
+ <div class="context" id="c<%=h frame.object_id %>">
263
+ <% if frame.pre_context %>
264
+ <ol start="<%=h frame.pre_context_lineno+1 %>" class="pre-context" id="pre<%=h frame.object_id %>">
265
+ <% frame.pre_context.each { |line| %>
266
+ <li onclick="toggle('pre<%=h frame.object_id %>', 'post<%=h frame.object_id %>')"><%=h line %></li>
267
+ <% } %>
268
+ </ol>
269
+ <% end %>
270
+
271
+ <ol start="<%=h frame.lineno %>" class="context-line">
272
+ <li onclick="toggle('pre<%=h frame.object_id %>', 'post<%=h frame.object_id %>')"><%=h frame.context_line %><span>...</span></li></ol>
273
+
274
+ <% if frame.post_context %>
275
+ <ol start='<%=h frame.lineno+1 %>' class="post-context" id="post<%=h frame.object_id %>">
276
+ <% frame.post_context.each { |line| %>
277
+ <li onclick="toggle('pre<%=h frame.object_id %>', 'post<%=h frame.object_id %>')"><%=h line %></li>
278
+ <% } %>
279
+ </ol>
280
+ <% end %>
281
+ </div>
282
+ <% end %>
283
+ </li>
284
+ <% } %>
285
+ </ul>
286
+ </div>
287
+
288
+ <div id="requestinfo">
289
+ <h2>Request information</h2>
290
+
291
+ <h3 id="get-info">GET</h3>
292
+ <% if req.GET and not req.GET.empty? %>
293
+ <table class="req">
294
+ <thead>
295
+ <tr>
296
+ <th>Variable</th>
297
+ <th>Value</th>
298
+ </tr>
299
+ </thead>
300
+ <tbody>
301
+ <% req.GET.sort_by { |k, v| k.to_s }.each { |key, val| %>
302
+ <tr>
303
+ <td><%=h key %></td>
304
+ <td class="code"><div><%=h val.inspect %></div></td>
305
+ </tr>
306
+ <% } %>
307
+ </tbody>
308
+ </table>
309
+ <% else %>
310
+ <p>No GET data.</p>
311
+ <% end %>
312
+
313
+ <h3 id="post-info">POST</h3>
314
+ <% if ((req.POST and not req.POST.empty?) rescue (no_post_data = "Invalid POST data"; nil)) %>
315
+ <table class="req">
316
+ <thead>
317
+ <tr>
318
+ <th>Variable</th>
319
+ <th>Value</th>
320
+ </tr>
321
+ </thead>
322
+ <tbody>
323
+ <% req.POST.sort_by { |k, v| k.to_s }.each { |key, val| %>
324
+ <tr>
325
+ <td><%=h key %></td>
326
+ <td class="code"><div><%=h val.inspect %></div></td>
327
+ </tr>
328
+ <% } %>
329
+ </tbody>
330
+ </table>
331
+ <% else %>
332
+ <p><%= no_post_data || "No POST data" %>.</p>
333
+ <% end %>
334
+
335
+
336
+ <h3 id="cookie-info">COOKIES</h3>
337
+ <% unless req.cookies.empty? %>
338
+ <table class="req">
339
+ <thead>
340
+ <tr>
341
+ <th>Variable</th>
342
+ <th>Value</th>
343
+ </tr>
344
+ </thead>
345
+ <tbody>
346
+ <% req.cookies.each { |key, val| %>
347
+ <tr>
348
+ <td><%=h key %></td>
349
+ <td class="code"><div><%=h val.inspect %></div></td>
350
+ </tr>
351
+ <% } %>
352
+ </tbody>
353
+ </table>
354
+ <% else %>
355
+ <p>No cookie data.</p>
356
+ <% end %>
357
+
358
+ <h3 id="env-info">Rack ENV</h3>
359
+ <table class="req">
360
+ <thead>
361
+ <tr>
362
+ <th>Variable</th>
363
+ <th>Value</th>
364
+ </tr>
365
+ </thead>
366
+ <tbody>
367
+ <% env.sort_by { |k, v| k.to_s }.each { |key, val| %>
368
+ <tr>
369
+ <td><%=h key %></td>
370
+ <td class="code"><div><%=h val.inspect %></div></td>
371
+ </tr>
372
+ <% } %>
373
+ </tbody>
374
+ </table>
375
+
376
+ </div>
377
+
378
+ <div id="explanation">
379
+ <p>
380
+ You're seeing this error because you use <code>Rack::ShowExceptions</code>.
381
+ </p>
382
+ </div>
383
+
384
+ </body>
385
+ </html>
386
+ HTML
387
+
388
+ # :startdoc:
389
+ end
390
+ end
@@ -1,10 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'erb'
2
- require 'rack/request'
3
- require 'rack/utils'
4
4
 
5
5
  module Rack
6
- # Rack::ShowStatus catches all empty responses the app it wraps and
7
- # replaces them with a site explaining the error.
6
+ # Rack::ShowStatus catches all empty responses and replaces them
7
+ # with a site explaining the error.
8
8
  #
9
9
  # Additional details can be put into <tt>rack.showstatus.detail</tt>
10
10
  # and will be shown as HTML. If such details exist, the error page
@@ -18,17 +18,24 @@ module Rack
18
18
 
19
19
  def call(env)
20
20
  status, headers, body = @app.call(env)
21
- headers = Utils::HeaderHash.new(headers)
22
- empty = headers['Content-Length'].to_i <= 0
21
+ headers = Utils::HeaderHash[headers]
22
+ empty = headers[CONTENT_LENGTH].to_i <= 0
23
23
 
24
24
  # client or server error, or explicit message
25
- if (status.to_i >= 400 && empty) || env["rack.showstatus.detail"]
26
- req = Rack::Request.new(env)
25
+ if (status.to_i >= 400 && empty) || env[RACK_SHOWSTATUS_DETAIL]
26
+ # This double assignment is to prevent an "unused variable" warning.
27
+ # Yes, it is dumb, but I don't like Ruby yelling at me.
28
+ req = req = Rack::Request.new(env)
29
+
27
30
  message = Rack::Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s
28
- detail = env["rack.showstatus.detail"] || message
31
+
32
+ # This double assignment is to prevent an "unused variable" warning.
33
+ # Yes, it is dumb, but I don't like Ruby yelling at me.
34
+ detail = detail = env[RACK_SHOWSTATUS_DETAIL] || message
35
+
29
36
  body = @template.result(binding)
30
- size = Rack::Utils.bytesize(body)
31
- [status, headers.merge("Content-Type" => "text/html", "Content-Length" => size.to_s), [body]]
37
+ size = body.bytesize
38
+ [status, headers.merge(CONTENT_TYPE => "text/html", CONTENT_LENGTH => size.to_s), [body]]
32
39
  else
33
40
  [status, headers, body]
34
41
  end
@@ -45,8 +52,8 @@ module Rack
45
52
 
46
53
  # :stopdoc:
47
54
 
48
- # adapted from Django <djangoproject.com>
49
- # Copyright (c) 2005, the Lawrence Journal-World
55
+ # adapted from Django <www.djangoproject.com>
56
+ # Copyright (c) Django Software Foundation and individual contributors.
50
57
  # Used under the modified BSD license:
51
58
  # http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5
52
59
  TEMPLATE = <<'HTML'
@@ -89,7 +96,7 @@ TEMPLATE = <<'HTML'
89
96
  </table>
90
97
  </div>
91
98
  <div id="info">
92
- <p><%= detail %></p>
99
+ <p><%=h detail %></p>
93
100
  </div>
94
101
 
95
102
  <div id="explanation">
data/lib/rack/static.rb CHANGED
@@ -1,38 +1,188 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rack
2
4
 
3
5
  # The Rack::Static middleware intercepts requests for static files
4
- # (javascript files, images, stylesheets, etc) based on the url prefixes
5
- # passed in the options, and serves them using a Rack::File object. This
6
- # allows a Rack stack to serve both static and dynamic content.
6
+ # (javascript files, images, stylesheets, etc) based on the url prefixes or
7
+ # route mappings passed in the options, and serves them using a Rack::Files
8
+ # object. This allows a Rack stack to serve both static and dynamic content.
7
9
  #
8
10
  # Examples:
11
+ #
12
+ # Serve all requests beginning with /media from the "media" folder located
13
+ # in the current directory (ie media/*):
14
+ #
9
15
  # use Rack::Static, :urls => ["/media"]
10
- # will serve all requests beginning with /media from the "media" folder
11
- # located in the current directory (ie media/*).
16
+ #
17
+ # Same as previous, but instead of returning 404 for missing files under
18
+ # /media, call the next middleware:
19
+ #
20
+ # use Rack::Static, :urls => ["/media"], :cascade => true
21
+ #
22
+ # Serve all requests beginning with /css or /images from the folder "public"
23
+ # in the current directory (ie public/css/* and public/images/*):
12
24
  #
13
25
  # use Rack::Static, :urls => ["/css", "/images"], :root => "public"
14
- # will serve all requests beginning with /css or /images from the folder
15
- # "public" in the current directory (ie public/css/* and public/images/*)
16
-
26
+ #
27
+ # Serve all requests to / with "index.html" from the folder "public" in the
28
+ # current directory (ie public/index.html):
29
+ #
30
+ # use Rack::Static, :urls => {"/" => 'index.html'}, :root => 'public'
31
+ #
32
+ # Serve all requests normally from the folder "public" in the current
33
+ # directory but uses index.html as default route for "/"
34
+ #
35
+ # use Rack::Static, :urls => [""], :root => 'public', :index =>
36
+ # 'index.html'
37
+ #
38
+ # Set custom HTTP Headers for based on rules:
39
+ #
40
+ # use Rack::Static, :root => 'public',
41
+ # :header_rules => [
42
+ # [rule, {header_field => content, header_field => content}],
43
+ # [rule, {header_field => content}]
44
+ # ]
45
+ #
46
+ # Rules for selecting files:
47
+ #
48
+ # 1) All files
49
+ # Provide the :all symbol
50
+ # :all => Matches every file
51
+ #
52
+ # 2) Folders
53
+ # Provide the folder path as a string
54
+ # '/folder' or '/folder/subfolder' => Matches files in a certain folder
55
+ #
56
+ # 3) File Extensions
57
+ # Provide the file extensions as an array
58
+ # ['css', 'js'] or %w(css js) => Matches files ending in .css or .js
59
+ #
60
+ # 4) Regular Expressions / Regexp
61
+ # Provide a regular expression
62
+ # %r{\.(?:css|js)\z} => Matches files ending in .css or .js
63
+ # /\.(?:eot|ttf|otf|woff2|woff|svg)\z/ => Matches files ending in
64
+ # the most common web font formats (.eot, .ttf, .otf, .woff2, .woff, .svg)
65
+ # Note: This Regexp is available as a shortcut, using the :fonts rule
66
+ #
67
+ # 5) Font Shortcut
68
+ # Provide the :fonts symbol
69
+ # :fonts => Uses the Regexp rule stated right above to match all common web font endings
70
+ #
71
+ # Rule Ordering:
72
+ # Rules are applied in the order that they are provided.
73
+ # List rather general rules above special ones.
74
+ #
75
+ # Complete example use case including HTTP header rules:
76
+ #
77
+ # use Rack::Static, :root => 'public',
78
+ # :header_rules => [
79
+ # # Cache all static files in public caches (e.g. Rack::Cache)
80
+ # # as well as in the browser
81
+ # [:all, {'Cache-Control' => 'public, max-age=31536000'}],
82
+ #
83
+ # # Provide web fonts with cross-origin access-control-headers
84
+ # # Firefox requires this when serving assets using a Content Delivery Network
85
+ # [:fonts, {'Access-Control-Allow-Origin' => '*'}]
86
+ # ]
87
+ #
17
88
  class Static
89
+ (require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4'
18
90
 
19
- def initialize(app, options={})
91
+ def initialize(app, options = {})
20
92
  @app = app
21
93
  @urls = options[:urls] || ["/favicon.ico"]
94
+ @index = options[:index]
95
+ @gzip = options[:gzip]
96
+ @cascade = options[:cascade]
22
97
  root = options[:root] || Dir.pwd
23
- @file_server = Rack::File.new(root)
98
+
99
+ # HTTP Headers
100
+ @header_rules = options[:header_rules] || []
101
+ # Allow for legacy :cache_control option while prioritizing global header_rules setting
102
+ @header_rules.unshift([:all, { CACHE_CONTROL => options[:cache_control] }]) if options[:cache_control]
103
+
104
+ @file_server = Rack::Files.new(root)
105
+ end
106
+
107
+ def add_index_root?(path)
108
+ @index && route_file(path) && path.end_with?('/')
109
+ end
110
+
111
+ def overwrite_file_path(path)
112
+ @urls.kind_of?(Hash) && @urls.key?(path) || add_index_root?(path)
113
+ end
114
+
115
+ def route_file(path)
116
+ @urls.kind_of?(Array) && @urls.any? { |url| path.index(url) == 0 }
117
+ end
118
+
119
+ def can_serve(path)
120
+ route_file(path) || overwrite_file_path(path)
24
121
  end
25
122
 
26
123
  def call(env)
27
- path = env["PATH_INFO"]
28
- can_serve = @urls.any? { |url| path.index(url) == 0 }
124
+ path = env[PATH_INFO]
125
+ actual_path = Utils.clean_path_info(Utils.unescape_path(path))
126
+
127
+ if can_serve(actual_path)
128
+ if overwrite_file_path(path)
129
+ env[PATH_INFO] = (add_index_root?(path) ? path + @index : @urls[path])
130
+ elsif @gzip && env['HTTP_ACCEPT_ENCODING'] && /\bgzip\b/.match?(env['HTTP_ACCEPT_ENCODING'])
131
+ path = env[PATH_INFO]
132
+ env[PATH_INFO] += '.gz'
133
+ response = @file_server.call(env)
134
+ env[PATH_INFO] = path
135
+
136
+ if response[0] == 404
137
+ response = nil
138
+ elsif response[0] == 304
139
+ # Do nothing, leave headers as is
140
+ else
141
+ if mime_type = Mime.mime_type(::File.extname(path), 'text/plain')
142
+ response[1][CONTENT_TYPE] = mime_type
143
+ end
144
+ response[1]['Content-Encoding'] = 'gzip'
145
+ end
146
+ end
147
+
148
+ path = env[PATH_INFO]
149
+ response ||= @file_server.call(env)
29
150
 
30
- if can_serve
31
- @file_server.call(env)
151
+ if @cascade && response[0] == 404
152
+ return @app.call(env)
153
+ end
154
+
155
+ headers = response[1]
156
+ applicable_rules(path).each do |rule, new_headers|
157
+ new_headers.each { |field, content| headers[field] = content }
158
+ end
159
+
160
+ response
32
161
  else
33
162
  @app.call(env)
34
163
  end
35
164
  end
36
165
 
166
+ # Convert HTTP header rules to HTTP headers
167
+ def applicable_rules(path)
168
+ @header_rules.find_all do |rule, new_headers|
169
+ case rule
170
+ when :all
171
+ true
172
+ when :fonts
173
+ /\.(?:ttf|otf|eot|woff2|woff|svg)\z/.match?(path)
174
+ when String
175
+ path = ::Rack::Utils.unescape(path)
176
+ path.start_with?(rule) || path.start_with?('/' + rule)
177
+ when Array
178
+ /\.(#{rule.join('|')})\z/.match?(path)
179
+ when Regexp
180
+ rule.match?(path)
181
+ else
182
+ false
183
+ end
184
+ end
185
+ end
186
+
37
187
  end
38
188
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rack
4
+
5
+ # Middleware tracks and cleans Tempfiles created throughout a request (i.e. Rack::Multipart)
6
+ # Ideas/strategy based on posts by Eric Wong and Charles Oliver Nutter
7
+ # https://groups.google.com/forum/#!searchin/rack-devel/temp/rack-devel/brK8eh-MByw/sw61oJJCGRMJ
8
+ class TempfileReaper
9
+ def initialize(app)
10
+ @app = app
11
+ end
12
+
13
+ def call(env)
14
+ env[RACK_TEMPFILES] ||= []
15
+ status, headers, body = @app.call(env)
16
+ body_proxy = BodyProxy.new(body) do
17
+ env[RACK_TEMPFILES].each(&:close!) unless env[RACK_TEMPFILES].nil?
18
+ end
19
+ [status, headers, body_proxy]
20
+ end
21
+ end
22
+ end