sinatra 1.4.8 → 2.0.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.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +111 -47
  3. data/CONTRIBUTING.md +1 -1
  4. data/Gemfile +41 -49
  5. data/LICENSE +4 -1
  6. data/MAINTENANCE.md +42 -0
  7. data/README.de.md +644 -436
  8. data/README.es.md +6 -6
  9. data/README.fr.md +9 -9
  10. data/README.hu.md +37 -3
  11. data/README.ja.md +103 -45
  12. data/README.ko.md +8 -8
  13. data/README.md +471 -363
  14. data/README.pt-br.md +3 -3
  15. data/README.pt-pt.md +2 -2
  16. data/README.ru.md +42 -64
  17. data/README.zh.md +8 -8
  18. data/Rakefile +72 -49
  19. data/SECURITY.md +35 -0
  20. data/lib/sinatra/base.rb +137 -195
  21. data/lib/sinatra/indifferent_hash.rb +150 -0
  22. data/lib/sinatra/main.rb +1 -0
  23. data/lib/sinatra/show_exceptions.rb +63 -55
  24. data/lib/sinatra/version.rb +1 -1
  25. data/sinatra.gemspec +19 -7
  26. metadata +30 -164
  27. data/lib/sinatra/ext.rb +0 -17
  28. data/test/asciidoctor_test.rb +0 -72
  29. data/test/base_test.rb +0 -167
  30. data/test/builder_test.rb +0 -91
  31. data/test/coffee_test.rb +0 -96
  32. data/test/compile_test.rb +0 -183
  33. data/test/contest.rb +0 -91
  34. data/test/creole_test.rb +0 -65
  35. data/test/delegator_test.rb +0 -160
  36. data/test/encoding_test.rb +0 -20
  37. data/test/erb_test.rb +0 -116
  38. data/test/extensions_test.rb +0 -98
  39. data/test/filter_test.rb +0 -487
  40. data/test/haml_test.rb +0 -109
  41. data/test/helper.rb +0 -132
  42. data/test/helpers_test.rb +0 -1917
  43. data/test/integration/app.rb +0 -79
  44. data/test/integration_helper.rb +0 -236
  45. data/test/integration_test.rb +0 -104
  46. data/test/less_test.rb +0 -69
  47. data/test/liquid_test.rb +0 -77
  48. data/test/mapped_error_test.rb +0 -285
  49. data/test/markaby_test.rb +0 -80
  50. data/test/markdown_test.rb +0 -85
  51. data/test/mediawiki_test.rb +0 -68
  52. data/test/middleware_test.rb +0 -68
  53. data/test/nokogiri_test.rb +0 -67
  54. data/test/public/favicon.ico +0 -0
  55. data/test/public/hello+world.txt +0 -1
  56. data/test/rabl_test.rb +0 -89
  57. data/test/rack_test.rb +0 -45
  58. data/test/radius_test.rb +0 -59
  59. data/test/rdoc_test.rb +0 -66
  60. data/test/readme_test.rb +0 -130
  61. data/test/request_test.rb +0 -100
  62. data/test/response_test.rb +0 -63
  63. data/test/result_test.rb +0 -76
  64. data/test/route_added_hook_test.rb +0 -59
  65. data/test/routing_test.rb +0 -1456
  66. data/test/sass_test.rb +0 -115
  67. data/test/scss_test.rb +0 -88
  68. data/test/server_test.rb +0 -56
  69. data/test/settings_test.rb +0 -582
  70. data/test/sinatra_test.rb +0 -12
  71. data/test/slim_test.rb +0 -102
  72. data/test/static_test.rb +0 -266
  73. data/test/streaming_test.rb +0 -149
  74. data/test/stylus_test.rb +0 -90
  75. data/test/templates_test.rb +0 -382
  76. data/test/textile_test.rb +0 -65
  77. data/test/views/a/in_a.str +0 -1
  78. data/test/views/ascii.erb +0 -2
  79. data/test/views/b/in_b.str +0 -1
  80. data/test/views/calc.html.erb +0 -1
  81. data/test/views/error.builder +0 -3
  82. data/test/views/error.erb +0 -3
  83. data/test/views/error.haml +0 -3
  84. data/test/views/error.sass +0 -2
  85. data/test/views/explicitly_nested.str +0 -1
  86. data/test/views/foo/hello.test +0 -1
  87. data/test/views/hello.asciidoc +0 -1
  88. data/test/views/hello.builder +0 -1
  89. data/test/views/hello.coffee +0 -1
  90. data/test/views/hello.creole +0 -1
  91. data/test/views/hello.erb +0 -1
  92. data/test/views/hello.haml +0 -1
  93. data/test/views/hello.less +0 -5
  94. data/test/views/hello.liquid +0 -1
  95. data/test/views/hello.mab +0 -1
  96. data/test/views/hello.md +0 -1
  97. data/test/views/hello.mediawiki +0 -1
  98. data/test/views/hello.nokogiri +0 -1
  99. data/test/views/hello.rabl +0 -2
  100. data/test/views/hello.radius +0 -1
  101. data/test/views/hello.rdoc +0 -1
  102. data/test/views/hello.sass +0 -2
  103. data/test/views/hello.scss +0 -3
  104. data/test/views/hello.slim +0 -1
  105. data/test/views/hello.str +0 -1
  106. data/test/views/hello.styl +0 -2
  107. data/test/views/hello.test +0 -1
  108. data/test/views/hello.textile +0 -1
  109. data/test/views/hello.wlang +0 -1
  110. data/test/views/hello.yajl +0 -1
  111. data/test/views/layout2.builder +0 -3
  112. data/test/views/layout2.erb +0 -2
  113. data/test/views/layout2.haml +0 -2
  114. data/test/views/layout2.liquid +0 -2
  115. data/test/views/layout2.mab +0 -2
  116. data/test/views/layout2.nokogiri +0 -3
  117. data/test/views/layout2.rabl +0 -3
  118. data/test/views/layout2.radius +0 -2
  119. data/test/views/layout2.slim +0 -3
  120. data/test/views/layout2.str +0 -2
  121. data/test/views/layout2.test +0 -1
  122. data/test/views/layout2.wlang +0 -2
  123. data/test/views/nested.str +0 -1
  124. data/test/views/utf8.erb +0 -2
  125. data/test/wlang_test.rb +0 -87
  126. data/test/yajl_test.rb +0 -86
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Sinatra
2
2
 
3
+ [![Build Status](https://secure.travis-ci.org/sinatra/sinatra.svg)](http://travis-ci.org/sinatra/sinatra)
4
+
3
5
  Sinatra is a [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) for
4
6
  quickly creating web applications in Ruby with minimal effort:
5
7
 
@@ -73,6 +75,9 @@ pick up if available.
73
75
  * [Filters](#filters)
74
76
  * [Helpers](#helpers)
75
77
  * [Using Sessions](#using-sessions)
78
+ * [Session Secret Security](#session-secret-security)
79
+ * [Session Config](#session-config)
80
+ * [Choosing Your Own Session Middleware](#choosing-your-own-session-middleware)
76
81
  * [Halting](#halting)
77
82
  * [Passing](#passing)
78
83
  * [Triggering Another Route](#triggering-another-route)
@@ -113,8 +118,6 @@ pick up if available.
113
118
  * [Requirement](#requirement)
114
119
  * [The Bleeding Edge](#the-bleeding-edge)
115
120
  * [With Bundler](#with-bundler)
116
- * [Roll Your Own](#roll-your-own)
117
- * [Install Globally](#install-globally)
118
121
  * [Versioning](#versioning)
119
122
  * [Further Reading](#further-reading)
120
123
 
@@ -160,6 +163,14 @@ end
160
163
  Routes are matched in the order they are defined. The first route that
161
164
  matches the request is invoked.
162
165
 
166
+ Routes with trailing slashes are different from the ones without:
167
+
168
+ ```ruby
169
+ get '/foo' do
170
+ # Does not match "GET /foo/"
171
+ end
172
+ ```
173
+
163
174
  Route patterns may include named parameters, accessible via the
164
175
  `params` hash:
165
176
 
@@ -208,7 +219,7 @@ end
208
219
  Route matching with Regular Expressions:
209
220
 
210
221
  ```ruby
211
- get /\A\/hello\/([\w]+)\z/ do
222
+ get /\/hello\/([\w]+)/ do
212
223
  "Hello, #{params['captures'].first}!"
213
224
  end
214
225
  ```
@@ -241,8 +252,23 @@ get '/posts' do
241
252
  end
242
253
  ```
243
254
 
244
- By the way, unless you disable the path traversal attack protection (see below),
245
- the request path might be modified before matching against your routes.
255
+ By the way, unless you disable the path traversal attack protection (see
256
+ below), the request path might be modified before matching against your
257
+ routes.
258
+
259
+ You may customize the Mustermann options used for a given route by passing in a
260
+ `:mustermann_opts` hash:
261
+
262
+ ```ruby
263
+ get '\A/posts\z', :mustermann_opts => { :type => :regexp, :check_anchors => false } do
264
+ # matches /posts exactly, with explicit anchoring
265
+ "If you match an anchored pattern clap your hands!"
266
+ end
267
+ ```
268
+
269
+ It looks like a [condition](#conditions), but it isn't one! These options will
270
+ be merged into the global `:mustermann_opts` hash described
271
+ [below](#available-settings).
246
272
 
247
273
  ## Conditions
248
274
 
@@ -311,10 +337,10 @@ end
311
337
 
312
338
  ## Return Values
313
339
 
314
- The return value of a route block determines at least the response body passed
315
- on to the HTTP client, or at least the next middleware in the Rack stack.
316
- Most commonly, this is a string, as in the above examples. But other values are
317
- also accepted.
340
+ The return value of a route block determines at least the response body
341
+ passed on to the HTTP client, or at least the next middleware in the
342
+ Rack stack. Most commonly, this is a string, as in the above examples.
343
+ But other values are also accepted.
318
344
 
319
345
  You can return any object that would either be a valid Rack response, Rack
320
346
  body object or HTTP status code:
@@ -339,14 +365,14 @@ end
339
365
  get('/') { Stream.new }
340
366
  ```
341
367
 
342
- You can also use the `stream` helper method (described below) to reduce boiler
343
- plate and embed the streaming logic in the route.
368
+ You can also use the `stream` helper method (described below) to reduce
369
+ boiler plate and embed the streaming logic in the route.
344
370
 
345
371
  ## Custom Route Matchers
346
372
 
347
- As shown above, Sinatra ships with built-in support for using String patterns
348
- and regular expressions as route matches. However, it does not stop there. You
349
- can easily define your own matchers:
373
+ As shown above, Sinatra ships with built-in support for using String
374
+ patterns and regular expressions as route matches. However, it does not
375
+ stop there. You can easily define your own matchers:
350
376
 
351
377
  ```ruby
352
378
  class AllButPattern
@@ -384,7 +410,7 @@ end
384
410
  Or, using negative look ahead:
385
411
 
386
412
  ```ruby
387
- get %r{^(?!/index$)} do
413
+ get %r{(?!/index)} do
388
414
  # ...
389
415
  end
390
416
  ```
@@ -494,15 +520,17 @@ Available Options:
494
520
 
495
521
  <dt>scope</dt>
496
522
  <dd>
497
- Scope to render template under. Defaults to the application instance. If you
498
- change this, instance variables and helper methods will not be available.
523
+ Scope to render template under. Defaults to the application
524
+ instance. If you change this, instance variables and helper methods
525
+ will not be available.
499
526
  </dd>
500
527
 
501
528
  <dt>layout_engine</dt>
502
529
  <dd>
503
- Template engine to use for rendering the layout. Useful for languages that
504
- do not support layouts otherwise. Defaults to the engine used for the
505
- template. Example: <tt>set :rdoc, :layout_engine => :erb</tt>
530
+ Template engine to use for rendering the layout. Useful for
531
+ languages that do not support layouts otherwise. Defaults to the
532
+ engine used for the template. Example: <tt>set :rdoc, :layout_engine
533
+ => :erb</tt>
506
534
  </dd>
507
535
 
508
536
  <dt>layout_options</dt>
@@ -512,19 +540,19 @@ Available Options:
512
540
  </dd>
513
541
  </dl>
514
542
 
515
- Templates are assumed to be located directly under the `./views` directory. To
516
- use a different views directory:
543
+ Templates are assumed to be located directly under the `./views`
544
+ directory. To use a different views directory:
517
545
 
518
546
  ```ruby
519
547
  set :views, settings.root + '/templates'
520
548
  ```
521
549
 
522
550
 
523
- One important thing to remember is that you always have to reference templates
524
- with symbols, even if they're in a subdirectory (in this case, use:
525
- `:'subdir/template'` or `'subdir/template'.to_sym`). You must use a symbol
526
- because otherwise rendering methods will render any strings passed to them
527
- directly.
551
+ One important thing to remember is that you always have to reference
552
+ templates with symbols, even if they're in a subdirectory (in this case,
553
+ use: `:'subdir/template'` or `'subdir/template'.to_sym`). You must use a
554
+ symbol because otherwise rendering methods will render any strings
555
+ passed to them directly.
528
556
 
529
557
  ### Literal Templates
530
558
 
@@ -534,7 +562,15 @@ get '/' do
534
562
  end
535
563
  ```
536
564
 
537
- Renders the template string.
565
+ Renders the template string. You can optionally specify `:path` and
566
+ `:line` for a clearer backtrace if there is a filesystem path or line
567
+ associated with that string:
568
+
569
+ ```ruby
570
+ get '/' do
571
+ haml '%div.title Hello World', :path => 'examples/file.haml', :line => 3
572
+ end
573
+ ```
538
574
 
539
575
  ### Available Template Languages
540
576
 
@@ -718,7 +754,7 @@ template, you almost always want to pass locals to it.
718
754
  </tr>
719
755
  </table>
720
756
 
721
- It is not possible to call methods from markdown, nor to pass locals to it.
757
+ It is not possible to call methods from Markdown, nor to pass locals to it.
722
758
  You therefore will usually use it in combination with another rendering
723
759
  engine:
724
760
 
@@ -726,7 +762,8 @@ engine:
726
762
  erb :overview, :locals => { :text => markdown(:introduction) }
727
763
  ```
728
764
 
729
- Note that you may also call the `markdown` method from within other templates:
765
+ Note that you may also call the `markdown` method from within other
766
+ templates:
730
767
 
731
768
  ```ruby
732
769
  %h1 Hello From Haml!
@@ -754,8 +791,9 @@ template than for the layout by passing the `:layout_engine` option.
754
791
  </tr>
755
792
  </table>
756
793
 
757
- It is not possible to call methods from textile, nor to pass locals to it. You
758
- therefore will usually use it in combination with another rendering engine:
794
+ It is not possible to call methods from Textile, nor to pass locals to
795
+ it. You therefore will usually use it in combination with another
796
+ rendering engine:
759
797
 
760
798
  ```ruby
761
799
  erb :overview, :locals => { :text => textile(:introduction) }
@@ -789,7 +827,7 @@ template than for the layout by passing the `:layout_engine` option.
789
827
  </tr>
790
828
  </table>
791
829
 
792
- It is not possible to call methods from rdoc, nor to pass locals to it. You
830
+ It is not possible to call methods from RDoc, nor to pass locals to it. You
793
831
  therefore will usually use it in combination with another rendering engine:
794
832
 
795
833
  ```ruby
@@ -844,8 +882,8 @@ almost always want to pass locals to it.
844
882
  </tr>
845
883
  </table>
846
884
 
847
- Since you cannot call Ruby methods directly from a Radius template, you almost
848
- always want to pass locals to it.
885
+ Since you cannot call Ruby methods directly from a Radius template, you
886
+ almost always want to pass locals to it.
849
887
 
850
888
  #### Markaby Templates
851
889
 
@@ -917,7 +955,7 @@ It also takes a block for inline templates (see example).
917
955
  </tr>
918
956
  </table>
919
957
 
920
- It is not possible to call methods from creole, nor to pass locals to it. You
958
+ It is not possible to call methods from Creole, nor to pass locals to it. You
921
959
  therefore will usually use it in combination with another rendering engine:
922
960
 
923
961
  ```ruby
@@ -952,15 +990,16 @@ template than for the layout by passing the `:layout_engine` option.
952
990
  </tr>
953
991
  </table>
954
992
 
955
- It is not possible to call methods from MediaWiki markup, nor to pass locals to
956
- it. You therefore will usually use it in combination with another rendering
957
- engine:
993
+ It is not possible to call methods from MediaWiki markup, nor to pass
994
+ locals to it. You therefore will usually use it in combination with
995
+ another rendering engine:
958
996
 
959
997
  ```ruby
960
998
  erb :overview, :locals => { :text => mediawiki(:introduction) }
961
999
  ```
962
1000
 
963
- Note that you may also call the `mediawiki` method from within other templates:
1001
+ Note that you may also call the `mediawiki` method from within other
1002
+ templates:
964
1003
 
965
1004
  ```ruby
966
1005
  %h1 Hello From Haml!
@@ -1090,8 +1129,9 @@ present(resource);
1090
1129
  </tr>
1091
1130
  </table>
1092
1131
 
1093
- Since calling ruby methods is not idiomatic in WLang, you almost always want to
1094
- pass locals to it. Layouts written in WLang and `yield` are supported, though.
1132
+ Since calling ruby methods is not idiomatic in WLang, you almost always
1133
+ want to pass locals to it. Layouts written in WLang and `yield` are
1134
+ supported, though.
1095
1135
 
1096
1136
  ### Accessing Variables in Templates
1097
1137
 
@@ -1131,7 +1171,8 @@ end
1131
1171
 
1132
1172
  This code is mostly equivalent to `erb :index, :layout => :post`.
1133
1173
 
1134
- Passing blocks to rendering methods is most useful for creating nested layouts:
1174
+ Passing blocks to rendering methods is most useful for creating nested
1175
+ layouts:
1135
1176
 
1136
1177
  ```ruby
1137
1178
  erb :main_layout, :layout => false do
@@ -1254,8 +1295,8 @@ end
1254
1295
 
1255
1296
  ## Filters
1256
1297
 
1257
- Before filters are evaluated before each request within the same
1258
- context as the routes will be and can modify the request and response. Instance
1298
+ Before filters are evaluated before each request within the same context
1299
+ as the routes will be and can modify the request and response. Instance
1259
1300
  variables set in filters are accessible by routes and templates:
1260
1301
 
1261
1302
  ```ruby
@@ -1270,9 +1311,10 @@ get '/foo/*' do
1270
1311
  end
1271
1312
  ```
1272
1313
 
1273
- After filters are evaluated after each request within the same context as the
1274
- routes will be and can also modify the request and response. Instance variables
1275
- set in before filters and routes are accessible by after filters:
1314
+ After filters are evaluated after each request within the same context
1315
+ as the routes will be and can also modify the request and response.
1316
+ Instance variables set in before filters and routes are accessible by
1317
+ after filters:
1276
1318
 
1277
1319
  ```ruby
1278
1320
  after do
@@ -1280,9 +1322,9 @@ after do
1280
1322
  end
1281
1323
  ```
1282
1324
 
1283
- Note: Unless you use the `body` method rather than just returning a String from
1284
- the routes, the body will not yet be available in the after filter, since it is
1285
- generated later on.
1325
+ Note: Unless you use the `body` method rather than just returning a
1326
+ String from the routes, the body will not yet be available in the after
1327
+ filter, since it is generated later on.
1286
1328
 
1287
1329
  Filters optionally take a pattern, causing them to be evaluated only if the
1288
1330
  request path matches that pattern:
@@ -1359,35 +1401,83 @@ get '/:value' do
1359
1401
  end
1360
1402
  ```
1361
1403
 
1362
- Note that `enable :sessions` actually stores all data in a cookie. This
1363
- might not always be what you want (storing lots of data will increase your
1364
- traffic, for instance). You can use any Rack session middleware: in order to
1365
- do so, do **not** call `enable :sessions`, but instead pull in your
1366
- middleware of choice as you would any other middleware:
1404
+ #### Session Secret Security
1367
1405
 
1368
- ```ruby
1369
- use Rack::Session::Pool, :expire_after => 2592000
1406
+ To improve security, the session data in the cookie is signed with a session
1407
+ secret using `HMAC-SHA1`. This session secret should optimally be a
1408
+ cryptographically secure random value of an appropriate length which for
1409
+ `HMAC-SHA1` is greater than or equal to 64 bytes (512 bits, 128 hex
1410
+ characters). You would be advised not to use a secret that is less than 32
1411
+ bytes of randomness (256 bits, 64 hex characters). It is therefore **very
1412
+ important** that you don't just make the secret up, but instead use a secure
1413
+ random number generator to create it. Humans are extremely bad at generating
1414
+ random values.
1370
1415
 
1371
- get '/' do
1372
- "value = " << session[:value].inspect
1373
- end
1416
+ By default, a 32 byte secure random session secret is generated for you by
1417
+ Sinatra, but it will change with every restart of your application. If you
1418
+ have multiple instances of your application, and you let Sinatra generate the
1419
+ key, each instance would then have a different session key which is probably
1420
+ not what you want.
1374
1421
 
1375
- get '/:value' do
1376
- session['value'] = params['value']
1377
- end
1422
+ For better security and usability it's
1423
+ [recommended](https://12factor.net/config) that you generate a secure random
1424
+ secret and store it in an environment variable on each host running your
1425
+ application so that all of your application instances will share the same
1426
+ secret. You should periodically rotate this session secret to a new value.
1427
+ Here are some examples of how you might create a 64 byte secret and set it:
1428
+
1429
+ **Session Secret Generation**
1430
+
1431
+ ```text
1432
+ $ ruby -e "require 'securerandom'; puts SecureRandom.hex(64)"
1433
+ 99ae8af...snip...ec0f262ac
1378
1434
  ```
1379
1435
 
1380
- To improve security, the session data in the cookie is signed with a session
1381
- secret. A random secret is generated for you by Sinatra. However, since this
1382
- secret will change with every start of your application, you might want to
1383
- set the secret yourself, so all your application instances share it:
1436
+ **Session Secret Generation (Bonus Points)**
1437
+
1438
+ Use the [sysrandom gem](https://github.com/cryptosphere/sysrandom) to prefer
1439
+ use of system RNG facilities to generate random values instead of
1440
+ userspace `OpenSSL` which MRI Ruby currently defaults to:
1441
+
1442
+ ```text
1443
+ $ gem install sysrandom
1444
+ Building native extensions. This could take a while...
1445
+ Successfully installed sysrandom-1.x
1446
+ 1 gem installed
1447
+
1448
+ $ ruby -e "require 'sysrandom/securerandom'; puts SecureRandom.hex(64)"
1449
+ 99ae8af...snip...ec0f262ac
1450
+ ```
1451
+
1452
+ **Session Secret Environment Variable**
1453
+
1454
+ Set a `SESSION_SECRET` environment variable for Sinatra to the value you
1455
+ generated. Make this value persistent across reboots of your host. Since the
1456
+ method for doing this will vary across systems this is for illustrative
1457
+ purposes only:
1458
+
1459
+ ```bash
1460
+ # echo "export SESSION_SECRET=99ae8af...snip...ec0f262ac" >> ~/.bashrc
1461
+ ```
1462
+
1463
+ **Session Secret App Config**
1464
+
1465
+ Setup your app config to fail-safe to a secure random secret
1466
+ if the `SESSION_SECRET` environment variable is not available.
1467
+
1468
+ For bonus points use the [sysrandom
1469
+ gem](https://github.com/cryptosphere/sysrandom) here as well:
1384
1470
 
1385
1471
  ```ruby
1386
- set :session_secret, 'super secret'
1472
+ require 'securerandom'
1473
+ # -or- require 'sysrandom/securerandom'
1474
+ set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex(64) }
1387
1475
  ```
1388
1476
 
1389
- If you want to configure it further, you may also store a hash with options in
1390
- the `sessions` setting:
1477
+ #### Session Config
1478
+
1479
+ If you want to configure it further, you may also store a hash with options
1480
+ in the `sessions` setting:
1391
1481
 
1392
1482
  ```ruby
1393
1483
  set :sessions, :domain => 'foo.com'
@@ -1400,6 +1490,41 @@ domain with a *.* like this instead:
1400
1490
  set :sessions, :domain => '.foo.com'
1401
1491
  ```
1402
1492
 
1493
+ #### Choosing Your Own Session Middleware
1494
+
1495
+ Note that `enable :sessions` actually stores all data in a cookie. This
1496
+ might not always be what you want (storing lots of data will increase your
1497
+ traffic, for instance). You can use any Rack session middleware in order to
1498
+ do so, one of the following methods can be used:
1499
+
1500
+ ```ruby
1501
+ enable :sessions
1502
+ set :session_store, Rack::Session::Pool
1503
+ ```
1504
+
1505
+ Or to set up sessions with a hash of options:
1506
+
1507
+ ```ruby
1508
+ set :sessions, :expire_after => 2592000
1509
+ set :session_store, Rack::Session::Pool
1510
+ ```
1511
+
1512
+ Another option is to **not** call `enable :sessions`, but instead pull in
1513
+ your middleware of choice as you would any other middleware.
1514
+
1515
+ It is important to note that when using this method, session based
1516
+ protection **will not be enabled by default**.
1517
+
1518
+ The Rack middleware to do that will also need to be added:
1519
+
1520
+ ```ruby
1521
+ use Rack::Session::Pool, :expire_after => 2592000
1522
+ use Rack::Protection::RemoteToken
1523
+ use Rack::Protection::SessionHijacking
1524
+ ```
1525
+
1526
+ See 'Configuring attack protection' for more information.
1527
+
1403
1528
  ### Halting
1404
1529
 
1405
1530
  To immediately stop a request within a filter or route use:
@@ -1458,8 +1583,8 @@ matching route. If no matching route is found, a 404 is returned.
1458
1583
 
1459
1584
  ### Triggering Another Route
1460
1585
 
1461
- Sometimes `pass` is not what you want, instead you would like to get the result
1462
- of calling another route. Simply use `call` to achieve this:
1586
+ Sometimes `pass` is not what you want, instead you would like to get the
1587
+ result of calling another route. Simply use `call` to achieve this:
1463
1588
 
1464
1589
  ```ruby
1465
1590
  get '/foo' do
@@ -1472,21 +1597,22 @@ get '/bar' do
1472
1597
  end
1473
1598
  ```
1474
1599
 
1475
- Note that in the example above, you would ease testing and increase performance
1476
- by simply moving `"bar"` into a helper used by both `/foo` and `/bar`.
1600
+ Note that in the example above, you would ease testing and increase
1601
+ performance by simply moving `"bar"` into a helper used by both `/foo` and
1602
+ `/bar`.
1477
1603
 
1478
- If you want the request to be sent to the same application instance rather than
1479
- a duplicate, use `call!` instead of `call`.
1604
+ If you want the request to be sent to the same application instance rather
1605
+ than a duplicate, use `call!` instead of `call`.
1480
1606
 
1481
1607
  Check out the Rack specification if you want to learn more about `call`.
1482
1608
 
1483
1609
  ### Setting Body, Status Code and Headers
1484
1610
 
1485
- It is possible and recommended to set the status code and response body with the
1486
- return value of the route block. However, in some scenarios you might want to
1487
- set the body at an arbitrary point in the execution flow. You can do so with the
1488
- `body` helper method. If you do so, you can use that method from there on to
1489
- access the body:
1611
+ It is possible and recommended to set the status code and response body with
1612
+ the return value of the route block. However, in some scenarios you might
1613
+ want to set the body at an arbitrary point in the execution flow. You can do
1614
+ so with the `body` helper method. If you do so, you can use that method from
1615
+ there on to access the body:
1490
1616
 
1491
1617
  ```ruby
1492
1618
  get '/foo' do
@@ -1537,15 +1663,15 @@ end
1537
1663
 
1538
1664
  This allows you to implement streaming APIs,
1539
1665
  [Server Sent Events](https://w3c.github.io/eventsource/), and can be used as
1540
- the basis for [WebSockets](https://en.wikipedia.org/wiki/WebSocket). It can also be
1541
- used to increase throughput if some but not all content depends on a slow
1542
- resource.
1666
+ the basis for [WebSockets](https://en.wikipedia.org/wiki/WebSocket). It can
1667
+ also be used to increase throughput if some but not all content depends on a
1668
+ slow resource.
1543
1669
 
1544
- Note that the streaming behavior, especially the number of concurrent requests,
1545
- highly depends on the web server used to serve the application. Some servers
1546
- might not even support streaming at all. If the server does not support
1547
- streaming, the body will be sent all at once after the block passed to `stream`
1548
- finishes executing. Streaming does not work at all with Shotgun.
1670
+ Note that the streaming behavior, especially the number of concurrent
1671
+ requests, highly depends on the web server used to serve the application.
1672
+ Some servers might not even support streaming at all. If the server does not
1673
+ support streaming, the body will be sent all at once after the block passed
1674
+ to `stream` finishes executing. Streaming does not work at all with Shotgun.
1549
1675
 
1550
1676
  If the optional parameter is set to `keep_open`, it will not call `close` on
1551
1677
  the stream object, allowing you to close it at any later point in the
@@ -1581,6 +1707,10 @@ post '/:message' do
1581
1707
  end
1582
1708
  ```
1583
1709
 
1710
+ It's also possible for the client to close the connection when trying to
1711
+ write to the socket. Because of this, it's recommended to check
1712
+ `out.closed?` before trying to write.
1713
+
1584
1714
  ### Logging
1585
1715
 
1586
1716
  In the request scope, the `logger` helper exposes a `Logger` instance:
@@ -1596,8 +1726,8 @@ This logger will automatically take your Rack handler's logging settings into
1596
1726
  account. If logging is disabled, this method will return a dummy object, so
1597
1727
  you do not have to worry about it in your routes and filters.
1598
1728
 
1599
- Note that logging is only enabled for `Sinatra::Application` by default, so if
1600
- you inherit from `Sinatra::Base`, you probably want to enable it yourself:
1729
+ Note that logging is only enabled for `Sinatra::Application` by default, so
1730
+ if you inherit from `Sinatra::Base`, you probably want to enable it yourself:
1601
1731
 
1602
1732
  ```ruby
1603
1733
  class MyApp < Sinatra::Base
@@ -1749,8 +1879,9 @@ etag @article.sha1, :weak
1749
1879
  ```
1750
1880
 
1751
1881
  These helpers will not do any caching for you, but rather feed the necessary
1752
- information to your cache. If you are looking for a quick reverse-proxy caching
1753
- solution, try [rack-cache](https://github.com/rtomayko/rack-cache):
1882
+ information to your cache. If you are looking for a quick
1883
+ reverse-proxy caching solution, try
1884
+ [rack-cache](https://github.com/rtomayko/rack-cache):
1754
1885
 
1755
1886
  ```ruby
1756
1887
  require "rack/cache"
@@ -1768,12 +1899,13 @@ end
1768
1899
  Use the `:static_cache_control` setting (see below) to add
1769
1900
  `Cache-Control` header info to static files.
1770
1901
 
1771
- According to RFC 2616, your application should behave differently if the If-Match
1772
- or If-None-Match header is set to `*`, depending on whether the resource
1773
- requested is already in existence. Sinatra assumes resources for safe (like get)
1774
- and idempotent (like put) requests are already in existence, whereas other
1775
- resources (for instance post requests) are treated as new resources. You
1776
- can change this behavior by passing in a `:new_resource` option:
1902
+ According to RFC 2616, your application should behave differently if the
1903
+ If-Match or If-None-Match header is set to `*`, depending on whether the
1904
+ resource requested is already in existence. Sinatra assumes resources for
1905
+ safe (like get) and idempotent (like put) requests are already in existence,
1906
+ whereas other resources (for instance post requests) are treated as new
1907
+ resources. You can change this behavior by passing in a `:new_resource`
1908
+ option:
1777
1909
 
1778
1910
  ```ruby
1779
1911
  get '/create' do
@@ -1810,8 +1942,8 @@ The options are:
1810
1942
 
1811
1943
  <dl>
1812
1944
  <dt>filename</dt>
1813
- <dd>File name to be used in the response, defaults to the real file name.</dd>
1814
-
1945
+ <dd>File name to be used in the response,
1946
+ defaults to the real file name.</dd>
1815
1947
  <dt>last_modified</dt>
1816
1948
  <dd>Value for Last-Modified header, defaults to the file's mtime.</dd>
1817
1949
 
@@ -1830,18 +1962,17 @@ The options are:
1830
1962
 
1831
1963
  <dt>status</dt>
1832
1964
  <dd>
1833
- Status code to be sent. Useful when sending a static file as an error page.
1834
-
1835
- If supported by the Rack handler, other means than streaming from the Ruby
1836
- process will be used. If you use this helper method, Sinatra will
1837
- automatically handle range requests.
1965
+ Status code to be sent. Useful when sending a static file as an error
1966
+ page. If supported by the Rack handler, other means than streaming
1967
+ from the Ruby process will be used. If you use this helper method,
1968
+ Sinatra will automatically handle range requests.
1838
1969
  </dd>
1839
1970
  </dl>
1840
1971
 
1841
1972
  ### Accessing the Request Object
1842
1973
 
1843
- The incoming request object can be accessed from request level (filter, routes,
1844
- error handlers) through the `request` method:
1974
+ The incoming request object can be accessed from request level (filter,
1975
+ routes, error handlers) through the `request` method:
1845
1976
 
1846
1977
  ```ruby
1847
1978
  # app running on http://example.com/example
@@ -1898,8 +2029,8 @@ end
1898
2029
 
1899
2030
  ### Attachments
1900
2031
 
1901
- You can use the `attachment` helper to tell the browser the response should be
1902
- stored on disk rather than displayed in the browser:
2032
+ You can use the `attachment` helper to tell the browser the response should
2033
+ be stored on disk rather than displayed in the browser:
1903
2034
 
1904
2035
  ```ruby
1905
2036
  get '/' do
@@ -1919,19 +2050,20 @@ end
1919
2050
 
1920
2051
  ### Dealing with Date and Time
1921
2052
 
1922
- Sinatra offers a `time_for` helper method that generates a Time object from the
1923
- given value. It is also able to convert `DateTime`, `Date` and similar classes:
2053
+ Sinatra offers a `time_for` helper method that generates a Time object from
2054
+ the given value. It is also able to convert `DateTime`, `Date` and similar
2055
+ classes:
1924
2056
 
1925
2057
  ```ruby
1926
2058
  get '/' do
1927
- pass if Time.now > time_for('Dec 23, 2012')
2059
+ pass if Time.now > time_for('Dec 23, 2016')
1928
2060
  "still time"
1929
2061
  end
1930
2062
  ```
1931
2063
 
1932
- This method is used internally by `expires`, `last_modified` and akin. You can
1933
- therefore easily extend the behavior of those methods by overriding `time_for`
1934
- in your application:
2064
+ This method is used internally by `expires`, `last_modified` and akin. You
2065
+ can therefore easily extend the behavior of those methods by overriding
2066
+ `time_for` in your application:
1935
2067
 
1936
2068
  ```ruby
1937
2069
  helpers do
@@ -1961,9 +2093,9 @@ find_template settings.views, 'foo', Tilt[:haml] do |file|
1961
2093
  end
1962
2094
  ```
1963
2095
 
1964
- This is not really useful. But it is useful that you can actually override this
1965
- method to hook in your own lookup mechanism. For instance, if you want to be
1966
- able to use more than one view directory:
2096
+ This is not really useful. But it is useful that you can actually override
2097
+ this method to hook in your own lookup mechanism. For instance, if you want
2098
+ to be able to use more than one view directory:
1967
2099
 
1968
2100
  ```ruby
1969
2101
  set :views, ['views', 'templates']
@@ -1992,11 +2124,11 @@ end
1992
2124
  You can also easily wrap this up in an extension and share with others!
1993
2125
 
1994
2126
  Note that `find_template` does not check if the file really exists but
1995
- rather calls the given block for all possible paths. This is not a performance
1996
- issue, since `render` will use `break` as soon as a file is found. Also,
1997
- template locations (and content) will be cached if you are not running in
1998
- development mode. You should keep that in mind if you write a really crazy
1999
- method.
2127
+ rather calls the given block for all possible paths. This is not a
2128
+ performance issue, since `render` will use `break` as soon as a file is
2129
+ found. Also, template locations (and content) will be cached if you are not
2130
+ running in development mode. You should keep that in mind if you write a
2131
+ really crazy method.
2000
2132
 
2001
2133
  ## Configuration
2002
2134
 
@@ -2021,7 +2153,7 @@ configure do
2021
2153
  end
2022
2154
  ```
2023
2155
 
2024
- Run only when the environment (`RACK_ENV` environment variable) is set to
2156
+ Run only when the environment (`APP_ENV` environment variable) is set to
2025
2157
  `:production`:
2026
2158
 
2027
2159
  ```ruby
@@ -2055,10 +2187,10 @@ end
2055
2187
  ### Configuring attack protection
2056
2188
 
2057
2189
  Sinatra is using
2058
- [Rack::Protection](https://github.com/sinatra/rack-protection#readme) to defend
2059
- your application against common, opportunistic attacks. You can easily disable
2060
- this behavior (which will open up your application to tons of common
2061
- vulnerabilities):
2190
+ [Rack::Protection](https://github.com/sinatra/rack-protection#readme) to
2191
+ defend your application against common, opportunistic attacks. You can
2192
+ easily disable this behavior (which will open up your application to tons
2193
+ of common vulnerabilities):
2062
2194
 
2063
2195
  ```ruby
2064
2196
  disable :protection
@@ -2076,12 +2208,12 @@ set :protection, :except => [:path_traversal, :session_hijacking]
2076
2208
  ```
2077
2209
 
2078
2210
  By default, Sinatra will only set up session based protection if `:sessions`
2079
- has been enabled. Sometimes you want to set up sessions on your own, though. In
2080
- that case you can get it to set up session based protections by passing the
2081
- `:session` option:
2211
+ have been enabled. See 'Using Sessions'. Sometimes you may want to set up
2212
+ sessions "outside" of the Sinatra app, such as in the config.ru or with a
2213
+ separate `Rack::Builder` instance. In that case you can still set up session
2214
+ based protection by passing the `:session` option:
2082
2215
 
2083
2216
  ```ruby
2084
- use Rack::Session::Pool
2085
2217
  set :protection, :session => true
2086
2218
  ```
2087
2219
 
@@ -2089,191 +2221,218 @@ set :protection, :session => true
2089
2221
 
2090
2222
  <dl>
2091
2223
  <dt>absolute_redirects</dt>
2092
- <dd>
2093
- If disabled, Sinatra will allow relative redirects, however, Sinatra will no
2094
- longer conform with RFC 2616 (HTTP 1.1), which only allows absolute redirects.
2095
- </dd>
2096
- <dd>
2097
- Enable if your app is running behind a reverse proxy that has not been set up
2098
- properly. Note that the <tt>url</tt> helper will still produce absolute URLs, unless you
2099
- pass in <tt>false</tt> as the second parameter.
2100
- </dd>
2101
- <dd>Disabled by default.</dd>
2224
+ <dd>
2225
+ If disabled, Sinatra will allow relative redirects, however, Sinatra
2226
+ will no longer conform with RFC 2616 (HTTP 1.1), which only allows
2227
+ absolute redirects.
2228
+ </dd>
2229
+ <dd>
2230
+ Enable if your app is running behind a reverse proxy that has not been
2231
+ set up properly. Note that the <tt>url</tt> helper will still produce
2232
+ absolute URLs, unless you pass in <tt>false</tt> as the second
2233
+ parameter.
2234
+ </dd>
2235
+ <dd>Disabled by default.</dd>
2102
2236
 
2103
2237
  <dt>add_charset</dt>
2104
- <dd>
2105
- Mime types the <tt>content_type</tt> helper will automatically add the charset info to.
2106
- You should add to it rather than overriding this option:
2107
- <tt>settings.add_charset << "application/foobar"</tt>
2108
- </dd>
2238
+ <dd>
2239
+ Mime types the <tt>content_type</tt> helper will automatically add the
2240
+ charset info to. You should add to it rather than overriding this
2241
+ option: <tt>settings.add_charset << "application/foobar"</tt>
2242
+ </dd>
2109
2243
 
2110
2244
  <dt>app_file</dt>
2111
- <dd>
2112
- Path to the main application file, used to detect project root, views and public
2113
- folder and inline templates.
2114
- </dd>
2245
+ <dd>
2246
+ Path to the main application file, used to detect project root, views
2247
+ and public folder and inline templates.
2248
+ </dd>
2115
2249
 
2116
2250
  <dt>bind</dt>
2117
- <dd>IP address to bind to (default: <tt>0.0.0.0</tt> <em>or</em>
2118
- <tt>localhost</tt> if your `environment` is set to development). Only used
2119
- for built-in server.</dd>
2251
+ <dd>
2252
+ IP address to bind to (default: <tt>0.0.0.0</tt> <em>or</em>
2253
+ <tt>localhost</tt> if your `environment` is set to development). Only
2254
+ used for built-in server.
2255
+ </dd>
2120
2256
 
2121
2257
  <dt>default_encoding</dt>
2122
- <dd>Encoding to assume if unknown (defaults to <tt>"utf-8"</tt>).</dd>
2258
+ <dd>Encoding to assume if unknown (defaults to <tt>"utf-8"</tt>).</dd>
2123
2259
 
2124
2260
  <dt>dump_errors</dt>
2125
- <dd>Display errors in the log.</dd>
2261
+ <dd>Display errors in the log.</dd>
2126
2262
 
2127
2263
  <dt>environment</dt>
2128
- <dd>
2129
- Current environment. Defaults to <tt>ENV['RACK_ENV']</tt>, or
2130
- <tt>"development"</tt> if not available.
2131
- </dd>
2264
+ <dd>
2265
+ Current environment. Defaults to <tt>ENV['APP_ENV']</tt>, or
2266
+ <tt>"development"</tt> if not available.
2267
+ </dd>
2132
2268
 
2133
2269
  <dt>logging</dt>
2134
- <dd>Use the logger.</dd>
2270
+ <dd>Use the logger.</dd>
2135
2271
 
2136
2272
  <dt>lock</dt>
2137
- <dd>
2138
- Places a lock around every request, only running processing on request
2139
- per Ruby process concurrently.
2140
- </dd>
2141
- <dd>Enabled if your app is not thread-safe. Disabled by default.</dd>
2273
+ <dd>
2274
+ Places a lock around every request, only running processing on request
2275
+ per Ruby process concurrently.
2276
+ </dd>
2277
+ <dd>Enabled if your app is not thread-safe. Disabled by default.</dd>
2142
2278
 
2143
2279
  <dt>method_override</dt>
2280
+ <dd>
2281
+ Use <tt>_method</tt> magic to allow put/delete forms in browsers that
2282
+ don't support it.
2283
+ </dd>
2284
+
2285
+ <dt>mustermann_opts</dt>
2144
2286
  <dd>
2145
- Use <tt>_method</tt> magic to allow put/delete forms in browsers that
2146
- don't support it.
2287
+ A default hash of options to pass to Mustermann.new when compiling routing
2288
+ paths.
2147
2289
  </dd>
2148
2290
 
2149
2291
  <dt>port</dt>
2150
- <dd>Port to listen on. Only used for built-in server.</dd>
2292
+ <dd>Port to listen on. Only used for built-in server.</dd>
2151
2293
 
2152
2294
  <dt>prefixed_redirects</dt>
2153
- <dd>
2154
- Whether or not to insert <tt>request.script_name</tt> into redirects if no
2155
- absolute path is given. That way <tt>redirect '/foo'</tt> would behave like
2156
- <tt>redirect to('/foo')</tt>. Disabled by default.
2157
- </dd>
2295
+ <dd>
2296
+ Whether or not to insert <tt>request.script_name</tt> into redirects
2297
+ if no absolute path is given. That way <tt>redirect '/foo'</tt> would
2298
+ behave like <tt>redirect to('/foo')</tt>. Disabled by default.
2299
+ </dd>
2158
2300
 
2159
2301
  <dt>protection</dt>
2160
- <dd>Whether or not to enable web attack protections. See protection section
2161
- above.</dd>
2302
+ <dd>
2303
+ Whether or not to enable web attack protections. See protection section
2304
+ above.
2305
+ </dd>
2162
2306
 
2163
2307
  <dt>public_dir</dt>
2164
- <dd>Alias for <tt>public_folder</tt>. See below.</dd>
2308
+ <dd>Alias for <tt>public_folder</tt>. See below.</dd>
2165
2309
 
2166
2310
  <dt>public_folder</dt>
2167
- <dd>
2168
- Path to the folder public files are served from. Only used if static
2169
- file serving is enabled (see <tt>static</tt> setting below). Inferred from
2170
- <tt>app_file</tt> setting if not set.
2171
- </dd>
2311
+ <dd>
2312
+ Path to the folder public files are served from. Only used if static
2313
+ file serving is enabled (see <tt>static</tt> setting below). Inferred
2314
+ from <tt>app_file</tt> setting if not set.
2315
+ </dd>
2316
+
2317
+ <dt>quiet</dt>
2318
+ <dd>
2319
+ Disables logs generated by Sinatra's start and stop commands.
2320
+ <tt>false</tt> by default.
2321
+ </dd>
2172
2322
 
2173
2323
  <dt>reload_templates</dt>
2174
- <dd>
2175
- Whether or not to reload templates between requests. Enabled in development
2176
- mode.
2177
- </dd>
2324
+ <dd>
2325
+ Whether or not to reload templates between requests. Enabled in
2326
+ development mode.
2327
+ </dd>
2178
2328
 
2179
2329
  <dt>root</dt>
2180
- <dd>
2181
- Path to project root folder. Inferred from <tt>app_file</tt> setting if not
2182
- set.
2183
- </dd>
2330
+ <dd>
2331
+ Path to project root folder. Inferred from <tt>app_file</tt> setting
2332
+ if not set.
2333
+ </dd>
2184
2334
 
2185
2335
  <dt>raise_errors</dt>
2186
- <dd>
2187
- Raise exceptions (will stop application). Enabled by default when
2188
- <tt>environment</tt> is set to <tt>"test"</tt>, disabled otherwise.
2189
- </dd>
2336
+ <dd>
2337
+ Raise exceptions (will stop application). Enabled by default when
2338
+ <tt>environment</tt> is set to <tt>"test"</tt>, disabled otherwise.
2339
+ </dd>
2190
2340
 
2191
2341
  <dt>run</dt>
2192
- <dd>
2193
- If enabled, Sinatra will handle starting the web server. Do not
2194
- enable if using rackup or other means.
2195
- </dd>
2342
+ <dd>
2343
+ If enabled, Sinatra will handle starting the web server. Do not
2344
+ enable if using rackup or other means.
2345
+ </dd>
2196
2346
 
2197
2347
  <dt>running</dt>
2198
- <dd>Is the built-in server running now? Do not change this setting!</dd>
2348
+ <dd>Is the built-in server running now? Do not change this setting!</dd>
2199
2349
 
2200
2350
  <dt>server</dt>
2201
- <dd>
2202
- Server or list of servers to use for built-in server. Order indicates
2203
- priority, default depends on Ruby implementation.
2204
- </dd>
2351
+ <dd>
2352
+ Server or list of servers to use for built-in server. Order indicates
2353
+ priority, default depends on Ruby implementation.
2354
+ </dd>
2205
2355
 
2206
2356
  <dt>sessions</dt>
2207
- <dd>
2208
- Enable cookie-based sessions support using <tt>Rack::Session::Cookie</tt>.
2209
- See 'Using Sessions' section for more information.
2210
- </dd>
2357
+ <dd>
2358
+ Enable cookie-based sessions support using
2359
+ <tt>Rack::Session::Cookie</tt>. See 'Using Sessions' section for more
2360
+ information.
2361
+ </dd>
2362
+
2363
+ <dt>session_store</dt>
2364
+ <dd>
2365
+ The Rack session middleware used. Defaults to
2366
+ <tt>Rack::Session::Cookie</tt>. See 'Using Sessions' section for more
2367
+ information.
2368
+ </dd>
2211
2369
 
2212
2370
  <dt>show_exceptions</dt>
2213
- <dd>
2214
- Show a stack trace in the browser when an exception happens. Enabled by
2215
- default when <tt>environment</tt> is set to <tt>"development"</tt>,
2216
- disabled otherwise.
2217
- </dd>
2218
- <dd>
2219
- Can also be set to <tt>:after_handler</tt> to trigger app-specified error
2220
- handling before showing a stack trace in the browser.
2221
- </dd>
2371
+ <dd>
2372
+ Show a stack trace in the browser when an exception happens. Enabled by
2373
+ default when <tt>environment</tt> is set to <tt>"development"</tt>,
2374
+ disabled otherwise.
2375
+ </dd>
2376
+ <dd>
2377
+ Can also be set to <tt>:after_handler</tt> to trigger app-specified
2378
+ error handling before showing a stack trace in the browser.
2379
+ </dd>
2222
2380
 
2223
2381
  <dt>static</dt>
2224
- <dd>Whether Sinatra should handle serving static files.</dd>
2225
- <dd>Disable when using a server able to do this on its own.</dd>
2226
- <dd>Disabling will boost performance.</dd>
2227
- <dd>
2228
- Enabled by default in classic style, disabled for modular apps.
2229
- </dd>
2382
+ <dd>Whether Sinatra should handle serving static files.</dd>
2383
+ <dd>Disable when using a server able to do this on its own.</dd>
2384
+ <dd>Disabling will boost performance.</dd>
2385
+ <dd>
2386
+ Enabled by default in classic style, disabled for modular apps.
2387
+ </dd>
2230
2388
 
2231
2389
  <dt>static_cache_control</dt>
2232
- <dd>
2233
- When Sinatra is serving static files, set this to add <tt>Cache-Control</tt>
2234
- headers to the responses. Uses the <tt>cache_control</tt> helper. Disabled
2235
- by default.
2236
- </dd>
2237
- <dd>
2238
- Use an explicit array when setting multiple values:
2239
- <tt>set :static_cache_control, [:public, :max_age => 300]</tt>
2240
- </dd>
2390
+ <dd>
2391
+ When Sinatra is serving static files, set this to add
2392
+ <tt>Cache-Control</tt> headers to the responses. Uses the
2393
+ <tt>cache_control</tt> helper. Disabled by default.
2394
+ </dd>
2395
+ <dd>
2396
+ Use an explicit array when setting multiple values:
2397
+ <tt>set :static_cache_control, [:public, :max_age => 300]</tt>
2398
+ </dd>
2241
2399
 
2242
2400
  <dt>threaded</dt>
2243
- <dd>
2244
- If set to <tt>true</tt>, will tell Thin to use <tt>EventMachine.defer</tt>
2245
- for processing the request.
2246
- </dd>
2401
+ <dd>
2402
+ If set to <tt>true</tt>, will tell Thin to use
2403
+ <tt>EventMachine.defer</tt> for processing the request.
2404
+ </dd>
2247
2405
 
2248
2406
  <dt>traps</dt>
2249
- <dd>Whether Sinatra should handle system signals.</dd>
2407
+ <dd>Whether Sinatra should handle system signals.</dd>
2250
2408
 
2251
2409
  <dt>views</dt>
2252
- <dd>
2253
- Path to the views folder. Inferred from <tt>app_file</tt> setting if
2254
- not set.
2255
- </dd>
2410
+ <dd>
2411
+ Path to the views folder. Inferred from <tt>app_file</tt> setting if
2412
+ not set.
2413
+ </dd>
2256
2414
 
2257
2415
  <dt>x_cascade</dt>
2258
- <dd>
2259
- Whether or not to set the X-Cascade header if no route matches.
2260
- Defaults to <tt>true</tt>.
2261
- </dd>
2416
+ <dd>
2417
+ Whether or not to set the X-Cascade header if no route matches.
2418
+ Defaults to <tt>true</tt>.
2419
+ </dd>
2262
2420
  </dl>
2263
2421
 
2264
2422
  ## Environments
2265
2423
 
2266
- There are three predefined `environments`: `"development"`, `"production"` and
2267
- `"test"`. Environments can be set through the `RACK_ENV` environment variable.
2268
- The default value is `"development"`. In the `"development"` environment all
2269
- templates are reloaded between requests, and special `not_found` and `error`
2270
- handlers display stack traces in your browser. In the `"production"` and
2271
- `"test"` environments, templates are cached by default.
2424
+ There are three predefined `environments`: `"development"`,
2425
+ `"production"` and `"test"`. Environments can be set through the
2426
+ `APP_ENV` environment variable. The default value is `"development"`.
2427
+ In the `"development"` environment all templates are reloaded between
2428
+ requests, and special `not_found` and `error` handlers display stack
2429
+ traces in your browser. In the `"production"` and `"test"` environments,
2430
+ templates are cached by default.
2272
2431
 
2273
- To run different environments, set the `RACK_ENV` environment variable:
2432
+ To run different environments, set the `APP_ENV` environment variable:
2274
2433
 
2275
2434
  ```shell
2276
- RACK_ENV=production ruby my_app.rb
2435
+ APP_ENV=production ruby my_app.rb
2277
2436
  ```
2278
2437
 
2279
2438
  You can use predefined methods: `development?`, `test?` and `production?` to
@@ -2291,9 +2450,9 @@ end
2291
2450
 
2292
2451
  ## Error Handling
2293
2452
 
2294
- Error handlers run within the same context as routes and before filters, which
2295
- means you get all the goodies it has to offer, like `haml`,
2296
- `erb`, `halt`, etc.
2453
+ Error handlers run within the same context as routes and before filters,
2454
+ which means you get all the goodies it has to offer, like `haml`, `erb`,
2455
+ `halt`, etc.
2297
2456
 
2298
2457
  ### Not Found
2299
2458
 
@@ -2373,10 +2532,11 @@ and additional debugging information in your browser.
2373
2532
  ## Rack Middleware
2374
2533
 
2375
2534
  Sinatra rides on [Rack](http://rack.github.io/), a minimal standard
2376
- interface for Ruby web frameworks. One of Rack's most interesting capabilities
2377
- for application developers is support for "middleware" -- components that sit
2378
- between the server and your application monitoring and/or manipulating the
2379
- HTTP request/response to provide various types of common functionality.
2535
+ interface for Ruby web frameworks. One of Rack's most interesting
2536
+ capabilities for application developers is support for "middleware" --
2537
+ components that sit between the server and your application monitoring
2538
+ and/or manipulating the HTTP request/response to provide various types
2539
+ of common functionality.
2380
2540
 
2381
2541
  Sinatra makes building Rack middleware pipelines a cinch via a top-level
2382
2542
  `use` method:
@@ -2416,7 +2576,8 @@ or in the [Rack wiki](https://github.com/rack/rack/wiki/List-of-Middleware).
2416
2576
 
2417
2577
  ## Testing
2418
2578
 
2419
- Sinatra tests can be written using any Rack-based testing library or framework.
2579
+ Sinatra tests can be written using any Rack-based testing library or
2580
+ framework.
2420
2581
  [Rack::Test](http://www.rubydoc.info/github/brynary/rack-test/master/frames)
2421
2582
  is recommended:
2422
2583
 
@@ -2442,7 +2603,7 @@ class MyAppTest < Minitest::Test
2442
2603
  assert_equal 'Hello Frank!', last_response.body
2443
2604
  end
2444
2605
 
2445
- def test_with_rack_env
2606
+ def test_with_user_agent
2446
2607
  get '/', {}, 'HTTP_USER_AGENT' => 'Songbird'
2447
2608
  assert_equal "You're using Songbird!", last_response.body
2448
2609
  end
@@ -2475,9 +2636,9 @@ class MyApp < Sinatra::Base
2475
2636
  end
2476
2637
  ```
2477
2638
 
2478
- The methods available to `Sinatra::Base` subclasses are exactly the same as
2479
- those available via the top-level DSL. Most top-level apps can be converted to
2480
- `Sinatra::Base` components with two modifications:
2639
+ The methods available to `Sinatra::Base` subclasses are exactly the same
2640
+ as those available via the top-level DSL. Most top-level apps can be
2641
+ converted to `Sinatra::Base` components with two modifications:
2481
2642
 
2482
2643
  * Your file should require `sinatra/base` instead of `sinatra`;
2483
2644
  otherwise, all of Sinatra's DSL methods are imported into the main
@@ -2486,12 +2647,11 @@ those available via the top-level DSL. Most top-level apps can be converted to
2486
2647
  of `Sinatra::Base`.
2487
2648
 
2488
2649
  `Sinatra::Base` is a blank slate. Most options are disabled by default,
2489
- including the built-in server. See
2490
- [Configuring Settings](http://www.sinatrarb.com/configuration.html)
2491
- for details on available options and their behavior. If you want
2492
- behavior more similar to when you define your app at the top level (also
2493
- known as Classic style), you
2494
- can subclass `Sinatra::Application`.
2650
+ including the built-in server. See [Configuring
2651
+ Settings](http://www.sinatrarb.com/configuration.html) for details on
2652
+ available options and their behavior. If you want behavior more similar
2653
+ to when you define your app at the top level (also known as Classic
2654
+ style), you can subclass `Sinatra::Application`:
2495
2655
 
2496
2656
  ```ruby
2497
2657
  require 'sinatra/base'
@@ -2505,16 +2665,17 @@ end
2505
2665
 
2506
2666
  ### Modular vs. Classic Style
2507
2667
 
2508
- Contrary to common belief, there is nothing wrong with the classic style. If it
2509
- suits your application, you do not have to switch to a modular application.
2668
+ Contrary to common belief, there is nothing wrong with the classic
2669
+ style. If it suits your application, you do not have to switch to a
2670
+ modular application.
2510
2671
 
2511
- The main disadvantage of using the classic style rather than the modular style
2512
- is that you will only have one Sinatra application per Ruby process. If you
2513
- plan to use more than one, switch to the modular style. There is no reason you
2514
- cannot mix the modular and the classic styles.
2672
+ The main disadvantage of using the classic style rather than the modular
2673
+ style is that you will only have one Sinatra application per Ruby
2674
+ process. If you plan to use more than one, switch to the modular style.
2675
+ There is no reason you cannot mix the modular and the classic styles.
2515
2676
 
2516
- If switching from one style to the other, you should be aware of slightly
2517
- different default settings:
2677
+ If switching from one style to the other, you should be aware of
2678
+ slightly different default settings:
2518
2679
 
2519
2680
  <table>
2520
2681
  <tr>
@@ -2569,8 +2730,8 @@ different default settings:
2569
2730
 
2570
2731
  ### Serving a Modular Application
2571
2732
 
2572
- There are two common options for starting a modular app, actively starting with
2573
- `run!`:
2733
+ There are two common options for starting a modular app, actively
2734
+ starting with `run!`:
2574
2735
 
2575
2736
  ```ruby
2576
2737
  # my_app.rb
@@ -2633,16 +2794,16 @@ A `config.ru` file is recommended if:
2633
2794
  * You want to use more than one subclass of `Sinatra::Base`.
2634
2795
  * You want to use Sinatra only for middleware, and not as an endpoint.
2635
2796
 
2636
- **There is no need to switch to a `config.ru` simply because you switched to
2637
- the modular style, and you don't have to use the modular style for running with
2638
- a `config.ru`.**
2797
+ **There is no need to switch to a `config.ru` simply because you
2798
+ switched to the modular style, and you don't have to use the modular
2799
+ style for running with a `config.ru`.**
2639
2800
 
2640
2801
  ### Using Sinatra as Middleware
2641
2802
 
2642
- Not only is Sinatra able to use other Rack middleware, any Sinatra application
2643
- can in turn be added in front of any Rack endpoint as middleware itself. This
2644
- endpoint could be another Sinatra application, or any other Rack-based
2645
- application (Rails/Ramaze/Camping/...):
2803
+ Not only is Sinatra able to use other Rack middleware, any Sinatra
2804
+ application can in turn be added in front of any Rack endpoint as
2805
+ middleware itself. This endpoint could be another Sinatra application,
2806
+ or any other Rack-based application (Rails/Hanami/Roda/...):
2646
2807
 
2647
2808
  ```ruby
2648
2809
  require 'sinatra/base'
@@ -2731,9 +2892,9 @@ available.
2731
2892
  Every Sinatra application corresponds to a subclass of `Sinatra::Base`.
2732
2893
  If you are using the top-level DSL (`require 'sinatra'`), then this
2733
2894
  class is `Sinatra::Application`, otherwise it is the subclass you
2734
- created explicitly. At class level you have methods like `get` or `before`, but
2735
- you cannot access the `request` or `session` objects, as there is only a
2736
- single application class for all requests.
2895
+ created explicitly. At class level you have methods like `get` or
2896
+ `before`, but you cannot access the `request` or `session` objects, as
2897
+ there is only a single application class for all requests.
2737
2898
 
2738
2899
  Options created via `set` are methods at class level:
2739
2900
 
@@ -2817,7 +2978,7 @@ being [extending the main object](https://github.com/sinatra/sinatra/blob/ca0636
2817
2978
  Sinatra applications can be run directly:
2818
2979
 
2819
2980
  ```shell
2820
- ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s HANDLER]
2981
+ ruby myapp.rb [-h] [-x] [-q] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s HANDLER]
2821
2982
  ```
2822
2983
 
2823
2984
  Options are:
@@ -2828,6 +2989,7 @@ Options are:
2828
2989
  -o # set the host (default is 0.0.0.0)
2829
2990
  -e # set the environment (default is development)
2830
2991
  -s # specify rack server/handler (default is thin)
2992
+ -q # turn on quiet mode for server (default is off)
2831
2993
  -x # turn on the mutex lock (default is off)
2832
2994
  ```
2833
2995
 
@@ -2871,31 +3033,10 @@ thin --threaded start
2871
3033
 
2872
3034
  The following Ruby versions are officially supported:
2873
3035
  <dl>
2874
- <dt>Ruby 1.8.7</dt>
2875
- <dd>
2876
- 1.8.7 is fully supported, however, if nothing is keeping you from it, we
2877
- recommend upgrading or switching to JRuby or Rubinius. Support for 1.8.7
2878
- will not be dropped before Sinatra 2.0. Ruby 1.8.6 is no longer supported.
2879
- </dd>
2880
-
2881
- <dt>Ruby 1.9.2</dt>
3036
+ <dt>Ruby 2.2</dt>
2882
3037
  <dd>
2883
- 1.9.2 is fully supported. Do not use 1.9.2p0, as it is known to cause
2884
- segmentation faults when running Sinatra. Official support will continue
2885
- at least until the release of Sinatra 1.5.
2886
- </dd>
2887
-
2888
- <dt>Ruby 1.9.3</dt>
2889
- <dd>
2890
- 1.9.3 is fully supported and recommended. Please note that switching to 1.9.3
2891
- from an earlier version will invalidate all sessions. 1.9.3 will be supported
2892
- until the release of Sinatra 2.0.
2893
- </dd>
2894
-
2895
- <dt>Ruby 2.x</dt>
2896
- <dd>
2897
- 2.x is fully supported and recommended. There are currently no plans to drop
2898
- official support for it.
3038
+ 2.2 is fully supported and recommended. There are currently no plans to
3039
+ drop official support for it.
2899
3040
  </dd>
2900
3041
 
2901
3042
  <dt>Rubinius</dt>
@@ -2912,6 +3053,8 @@ The following Ruby versions are officially supported:
2912
3053
  </dd>
2913
3054
  </dl>
2914
3055
 
3056
+ Versions of Ruby prior to 2.2.2 are no longer supported as of Sinatra 2.0.
3057
+
2915
3058
  We also keep an eye on upcoming Ruby versions.
2916
3059
 
2917
3060
  The following Ruby implementations are not officially supported but still are
@@ -2925,9 +3068,9 @@ known to run Sinatra:
2925
3068
  Not being officially supported means if things only break there and not on a
2926
3069
  supported platform, we assume it's not our issue but theirs.
2927
3070
 
2928
- We also run our CI against ruby-head (future releases of MRI), but we can't
2929
- guarantee anything, since it is constantly moving. Expect upcoming 2.x releases
2930
- to be fully supported.
3071
+ We also run our CI against ruby-head (future releases of MRI), but we
3072
+ can't guarantee anything, since it is constantly moving. Expect upcoming
3073
+ 2.x releases to be fully supported.
2931
3074
 
2932
3075
  Sinatra should work on any operating system supported by the chosen Ruby
2933
3076
  implementation.
@@ -2935,12 +3078,13 @@ implementation.
2935
3078
  If you run MacRuby, you should `gem install control_tower`.
2936
3079
 
2937
3080
  Sinatra currently doesn't run on Cardinal, SmallRuby, BlueRuby or any
2938
- Ruby version prior to 1.8.7.
3081
+ Ruby version prior to 2.2.
2939
3082
 
2940
3083
  ## The Bleeding Edge
2941
3084
 
2942
- If you would like to use Sinatra's latest bleeding-edge code, feel free to run your
2943
- application against the master branch, it should be rather stable.
3085
+ If you would like to use Sinatra's latest bleeding-edge code, feel free
3086
+ to run your application against the master branch, it should be rather
3087
+ stable.
2944
3088
 
2945
3089
  We also push out prerelease gems from time to time, so you can do a
2946
3090
 
@@ -2965,16 +3109,15 @@ Then, in your project directory, create a `Gemfile`:
2965
3109
 
2966
3110
  ```ruby
2967
3111
  source 'https://rubygems.org'
2968
- gem 'sinatra', :github => "sinatra/sinatra"
3112
+ gem 'sinatra', :github => 'sinatra/sinatra'
2969
3113
 
2970
3114
  # other dependencies
2971
3115
  gem 'haml' # for instance, if you use haml
2972
- gem 'activerecord', '~> 3.0' # maybe you also need ActiveRecord 3.x
2973
3116
  ```
2974
3117
 
2975
- Note that you will have to list all your application's dependencies in the `Gemfile`.
2976
- Sinatra's direct dependencies (Rack and Tilt) will, however, be automatically
2977
- fetched and added by Bundler.
3118
+ Note that you will have to list all your application's dependencies in
3119
+ the `Gemfile`. Sinatra's direct dependencies (Rack and Tilt) will,
3120
+ however, be automatically fetched and added by Bundler.
2978
3121
 
2979
3122
  Now you can run your app like this:
2980
3123
 
@@ -2982,41 +3125,6 @@ Now you can run your app like this:
2982
3125
  bundle exec ruby myapp.rb
2983
3126
  ```
2984
3127
 
2985
- ### Roll Your Own
2986
-
2987
- Create a local clone and run your app with the `sinatra/lib` directory
2988
- on the `$LOAD_PATH`:
2989
-
2990
- ```shell
2991
- cd myapp
2992
- git clone git://github.com/sinatra/sinatra.git
2993
- ruby -I sinatra/lib myapp.rb
2994
- ```
2995
-
2996
- To update the Sinatra sources in the future:
2997
-
2998
- ```shell
2999
- cd myapp/sinatra
3000
- git pull
3001
- ```
3002
-
3003
- ### Install Globally
3004
-
3005
- You can build the gem on your own:
3006
-
3007
- ```shell
3008
- git clone git://github.com/sinatra/sinatra.git
3009
- cd sinatra
3010
- rake sinatra.gemspec
3011
- rake install
3012
- ```
3013
-
3014
- If you install gems as root, the last step should be:
3015
-
3016
- ```shell
3017
- sudo rake install
3018
- ```
3019
-
3020
3128
  ## Versioning
3021
3129
 
3022
3130
  Sinatra follows [Semantic Versioning](http://semver.org/), both SemVer and