sinatra 2.0.0 → 2.2.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.
Potentially problematic release.
This version of sinatra might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/AUTHORS.md +1 -0
- data/CHANGELOG.md +258 -37
- data/CONTRIBUTING.md +7 -7
- data/Gemfile +15 -6
- data/MAINTENANCE.md +2 -15
- data/README.de.md +22 -22
- data/README.es.md +772 -362
- data/README.fr.md +188 -91
- data/README.hu.md +3 -3
- data/README.ja.md +84 -54
- data/README.ko.md +7 -7
- data/README.malayalam.md +3141 -0
- data/README.md +165 -113
- data/README.pt-br.md +2366 -339
- data/README.pt-pt.md +3 -3
- data/README.ru.md +835 -564
- data/README.zh.md +83 -21
- data/Rakefile +10 -7
- data/VERSION +1 -0
- data/examples/chat.rb +2 -1
- data/examples/rainbows.conf +3 -0
- data/examples/rainbows.rb +20 -0
- data/examples/stream.ru +4 -4
- data/lib/sinatra/base.rb +160 -123
- data/lib/sinatra/indifferent_hash.rb +79 -15
- data/lib/sinatra/main.rb +30 -11
- data/lib/sinatra/show_exceptions.rb +8 -11
- data/lib/sinatra/version.rb +1 -1
- data/sinatra.gemspec +25 -7
- metadata +20 -22
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Sinatra
|
2
2
|
|
3
|
-
[](https://badge.fury.io/rb/sinatra)
|
4
|
+
[](https://travis-ci.org/sinatra/sinatra)
|
5
|
+
[](https://dependabot.com/compatibility-score.html?dependency-name=sinatra&package-manager=bundler&version-scheme=semver)
|
4
6
|
|
5
7
|
Sinatra is a [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) for
|
6
8
|
quickly creating web applications in Ruby with minimal effort:
|
@@ -28,7 +30,11 @@ ruby myapp.rb
|
|
28
30
|
|
29
31
|
View at: [http://localhost:4567](http://localhost:4567)
|
30
32
|
|
31
|
-
|
33
|
+
The code you changed will not take effect until you restart the server.
|
34
|
+
Please restart the server every time you change or use
|
35
|
+
[sinatra/reloader](http://www.sinatrarb.com/contrib/reloader).
|
36
|
+
|
37
|
+
It is recommended to also run `gem install puma`, which Sinatra will
|
32
38
|
pick up if available.
|
33
39
|
|
34
40
|
## Table of Contents
|
@@ -75,9 +81,9 @@ pick up if available.
|
|
75
81
|
* [Filters](#filters)
|
76
82
|
* [Helpers](#helpers)
|
77
83
|
* [Using Sessions](#using-sessions)
|
78
|
-
|
79
|
-
|
80
|
-
|
84
|
+
* [Session Secret Security](#session-secret-security)
|
85
|
+
* [Session Config](#session-config)
|
86
|
+
* [Choosing Your Own Session Middleware](#choosing-your-own-session-middleware)
|
81
87
|
* [Halting](#halting)
|
82
88
|
* [Passing](#passing)
|
83
89
|
* [Triggering Another Route](#triggering-another-route)
|
@@ -253,11 +259,11 @@ end
|
|
253
259
|
```
|
254
260
|
|
255
261
|
By the way, unless you disable the path traversal attack protection (see
|
256
|
-
below), the request path might be modified before
|
257
|
-
routes.
|
262
|
+
[below](#configuring-attack-protection)), the request path might be modified before
|
263
|
+
matching against your routes.
|
258
264
|
|
259
|
-
You may customize the Mustermann
|
260
|
-
`:mustermann_opts` hash:
|
265
|
+
You may customize the [Mustermann](https://github.com/sinatra/mustermann#readme)
|
266
|
+
options used for a given route by passing in a `:mustermann_opts` hash:
|
261
267
|
|
262
268
|
```ruby
|
263
269
|
get '\A/posts\z', :mustermann_opts => { :type => :regexp, :check_anchors => false } do
|
@@ -338,20 +344,20 @@ end
|
|
338
344
|
## Return Values
|
339
345
|
|
340
346
|
The return value of a route block determines at least the response body
|
341
|
-
passed on to the HTTP client
|
347
|
+
passed on to the HTTP client or at least the next middleware in the
|
342
348
|
Rack stack. Most commonly, this is a string, as in the above examples.
|
343
349
|
But other values are also accepted.
|
344
350
|
|
345
|
-
You can return
|
351
|
+
You can return an object that would either be a valid Rack response, Rack
|
346
352
|
body object or HTTP status code:
|
347
353
|
|
348
|
-
* An Array with three elements: `[status (
|
354
|
+
* An Array with three elements: `[status (Integer), headers (Hash), response
|
349
355
|
body (responds to #each)]`
|
350
|
-
* An Array with two elements: `[status (
|
356
|
+
* An Array with two elements: `[status (Integer), response body (responds to
|
351
357
|
#each)]`
|
352
358
|
* An object that responds to `#each` and passes nothing but strings to
|
353
359
|
the given block
|
354
|
-
* A
|
360
|
+
* A Integer representing the status code
|
355
361
|
|
356
362
|
That way we can, for instance, easily implement a streaming example:
|
357
363
|
|
@@ -365,8 +371,8 @@ end
|
|
365
371
|
get('/') { Stream.new }
|
366
372
|
```
|
367
373
|
|
368
|
-
You can also use the `stream` helper method (described below) to reduce
|
369
|
-
|
374
|
+
You can also use the `stream` helper method ([described below](#streaming-responses)) to reduce
|
375
|
+
boilerplate and embed the streaming logic in the route.
|
370
376
|
|
371
377
|
## Custom Route Matchers
|
372
378
|
|
@@ -421,14 +427,14 @@ Static files are served from the `./public` directory. You can specify
|
|
421
427
|
a different location by setting the `:public_folder` option:
|
422
428
|
|
423
429
|
```ruby
|
424
|
-
set :public_folder,
|
430
|
+
set :public_folder, __dir__ + '/static'
|
425
431
|
```
|
426
432
|
|
427
433
|
Note that the public directory name is not included in the URL. A file
|
428
434
|
`./public/css/style.css` is made available as
|
429
435
|
`http://example.com/css/style.css`.
|
430
436
|
|
431
|
-
Use the `:static_cache_control` setting (see below) to add
|
437
|
+
Use the `:static_cache_control` setting (see [below](#cache-control)) to add
|
432
438
|
`Cache-Control` header info.
|
433
439
|
|
434
440
|
## Views / Templates
|
@@ -605,13 +611,15 @@ get('/') { markdown :index }
|
|
605
611
|
<tr>
|
606
612
|
<td>Dependency</td>
|
607
613
|
<td>
|
608
|
-
<a href="
|
614
|
+
<a href="https://github.com/jeremyevans/erubi" title="erubi">erubi</a>
|
615
|
+
or <a href="http://www.kuwata-lab.com/erubis/" title="erubis">erubis</a>
|
609
616
|
or erb (included in Ruby)
|
610
617
|
</td>
|
611
618
|
</tr>
|
612
619
|
<tr>
|
613
620
|
<td>File Extensions</td>
|
614
|
-
<td><tt>.erb</tt>, <tt>.rhtml</tt> or <tt>.
|
621
|
+
<td><tt>.erb</tt>, <tt>.rhtml</tt> or <tt>.erubi</tt> (Erubi only)
|
622
|
+
or <tt>.erubis</tt> (Erubis only)</td>
|
615
623
|
</tr>
|
616
624
|
<tr>
|
617
625
|
<td>Example</td>
|
@@ -638,7 +646,7 @@ get('/') { markdown :index }
|
|
638
646
|
</tr>
|
639
647
|
</table>
|
640
648
|
|
641
|
-
It also takes a block for inline templates (see example).
|
649
|
+
It also takes a block for inline templates (see [example](#inline-templates)).
|
642
650
|
|
643
651
|
#### Nokogiri Templates
|
644
652
|
|
@@ -657,14 +665,14 @@ It also takes a block for inline templates (see example).
|
|
657
665
|
</tr>
|
658
666
|
</table>
|
659
667
|
|
660
|
-
It also takes a block for inline templates (see example).
|
668
|
+
It also takes a block for inline templates (see [example](#inline-templates)).
|
661
669
|
|
662
670
|
#### Sass Templates
|
663
671
|
|
664
672
|
<table>
|
665
673
|
<tr>
|
666
674
|
<td>Dependency</td>
|
667
|
-
<td><a href="
|
675
|
+
<td><a href="https://sass-lang.com/" title="sass">sass</a></td>
|
668
676
|
</tr>
|
669
677
|
<tr>
|
670
678
|
<td>File Extension</td>
|
@@ -681,7 +689,7 @@ It also takes a block for inline templates (see example).
|
|
681
689
|
<table>
|
682
690
|
<tr>
|
683
691
|
<td>Dependency</td>
|
684
|
-
<td><a href="
|
692
|
+
<td><a href="https://sass-lang.com/" title="sass">sass</a></td>
|
685
693
|
</tr>
|
686
694
|
<tr>
|
687
695
|
<td>File Extension</td>
|
@@ -715,7 +723,7 @@ It also takes a block for inline templates (see example).
|
|
715
723
|
<table>
|
716
724
|
<tr>
|
717
725
|
<td>Dependency</td>
|
718
|
-
<td><a href="
|
726
|
+
<td><a href="https://shopify.github.io/liquid/" title="liquid">liquid</a></td>
|
719
727
|
</tr>
|
720
728
|
<tr>
|
721
729
|
<td>File Extension</td>
|
@@ -739,9 +747,11 @@ template, you almost always want to pass locals to it.
|
|
739
747
|
Anyone of:
|
740
748
|
<a href="https://github.com/davidfstr/rdiscount" title="RDiscount">RDiscount</a>,
|
741
749
|
<a href="https://github.com/vmg/redcarpet" title="RedCarpet">RedCarpet</a>,
|
742
|
-
<a href="
|
743
|
-
<a href="
|
750
|
+
<a href="https://github.com/ged/bluecloth" title="BlueCloth">BlueCloth</a>,
|
751
|
+
<a href="https://kramdown.gettalong.org/" title="kramdown">kramdown</a>,
|
744
752
|
<a href="https://github.com/bhollis/maruku" title="maruku">maruku</a>
|
753
|
+
<a href="https://github.com/gjtorikian/commonmarker" title="commonmarker">commonmarker</a>
|
754
|
+
<a href="https://github.com/alphabetum/pandoc-ruby" title="pandoc">pandoc</a>
|
745
755
|
</td>
|
746
756
|
</tr>
|
747
757
|
<tr>
|
@@ -890,7 +900,7 @@ almost always want to pass locals to it.
|
|
890
900
|
<table>
|
891
901
|
<tr>
|
892
902
|
<td>Dependency</td>
|
893
|
-
<td><a href="
|
903
|
+
<td><a href="https://markaby.github.io/" title="Markaby">Markaby</a></td>
|
894
904
|
</tr>
|
895
905
|
<tr>
|
896
906
|
<td>File Extension</td>
|
@@ -902,7 +912,7 @@ almost always want to pass locals to it.
|
|
902
912
|
</tr>
|
903
913
|
</table>
|
904
914
|
|
905
|
-
It also takes a block for inline templates (see example).
|
915
|
+
It also takes a block for inline templates (see [example](#inline-templates)).
|
906
916
|
|
907
917
|
#### RABL Templates
|
908
918
|
|
@@ -1019,7 +1029,7 @@ template than for the layout by passing the `:layout_engine` option.
|
|
1019
1029
|
<a href="https://github.com/josh/ruby-coffee-script" title="Ruby CoffeeScript">
|
1020
1030
|
CoffeeScript
|
1021
1031
|
</a> and a
|
1022
|
-
<a href="https://github.com/sstephenson/execjs
|
1032
|
+
<a href="https://github.com/sstephenson/execjs" title="ExecJS">
|
1023
1033
|
way to execute javascript
|
1024
1034
|
</a>
|
1025
1035
|
</td>
|
@@ -1043,7 +1053,7 @@ template than for the layout by passing the `:layout_engine` option.
|
|
1043
1053
|
<a href="https://github.com/forgecrafted/ruby-stylus" title="Ruby Stylus">
|
1044
1054
|
Stylus
|
1045
1055
|
</a> and a
|
1046
|
-
<a href="https://github.com/sstephenson/execjs
|
1056
|
+
<a href="https://github.com/sstephenson/execjs" title="ExecJS">
|
1047
1057
|
way to execute javascript
|
1048
1058
|
</a>
|
1049
1059
|
</td>
|
@@ -1117,7 +1127,7 @@ present(resource);
|
|
1117
1127
|
<table>
|
1118
1128
|
<tr>
|
1119
1129
|
<td>Dependency</td>
|
1120
|
-
<td><a href="https://github.com/blambeau/wlang
|
1130
|
+
<td><a href="https://github.com/blambeau/wlang" title="WLang">WLang</a></td>
|
1121
1131
|
</tr>
|
1122
1132
|
<tr>
|
1123
1133
|
<td>File Extension</td>
|
@@ -1191,7 +1201,7 @@ end
|
|
1191
1201
|
```
|
1192
1202
|
|
1193
1203
|
Currently, the following rendering methods accept a block: `erb`, `haml`,
|
1194
|
-
`liquid`, `slim `, `wlang`. Also the general `render` method accepts a block.
|
1204
|
+
`liquid`, `slim `, `wlang`. Also, the general `render` method accepts a block.
|
1195
1205
|
|
1196
1206
|
### Inline Templates
|
1197
1207
|
|
@@ -1214,7 +1224,7 @@ __END__
|
|
1214
1224
|
%div.title Hello world.
|
1215
1225
|
```
|
1216
1226
|
|
1217
|
-
NOTE: Inline templates defined in the source file that requires
|
1227
|
+
NOTE: Inline templates defined in the source file that requires Sinatra are
|
1218
1228
|
automatically loaded. Call `enable :inline_templates` explicitly if you
|
1219
1229
|
have inline templates in other source files.
|
1220
1230
|
|
@@ -1273,8 +1283,8 @@ get '/' do
|
|
1273
1283
|
end
|
1274
1284
|
```
|
1275
1285
|
|
1276
|
-
Renders `./views/index.myat`.
|
1277
|
-
|
1286
|
+
Renders `./views/index.myat`. Learn more about
|
1287
|
+
[Tilt](https://github.com/rtomayko/tilt#readme).
|
1278
1288
|
|
1279
1289
|
### Using Custom Logic for Template Lookup
|
1280
1290
|
|
@@ -1283,7 +1293,7 @@ own `#find_template` method:
|
|
1283
1293
|
|
1284
1294
|
```ruby
|
1285
1295
|
configure do
|
1286
|
-
set :views [ './views/a', './views/b' ]
|
1296
|
+
set :views, [ './views/a', './views/b' ]
|
1287
1297
|
end
|
1288
1298
|
|
1289
1299
|
def find_template(views, name, engine, &block)
|
@@ -1424,7 +1434,7 @@ For better security and usability it's
|
|
1424
1434
|
secret and store it in an environment variable on each host running your
|
1425
1435
|
application so that all of your application instances will share the same
|
1426
1436
|
secret. You should periodically rotate this session secret to a new value.
|
1427
|
-
Here are some examples of how you might create a 64
|
1437
|
+
Here are some examples of how you might create a 64-byte secret and set it:
|
1428
1438
|
|
1429
1439
|
**Session Secret Generation**
|
1430
1440
|
|
@@ -1435,8 +1445,8 @@ $ ruby -e "require 'securerandom'; puts SecureRandom.hex(64)"
|
|
1435
1445
|
|
1436
1446
|
**Session Secret Generation (Bonus Points)**
|
1437
1447
|
|
1438
|
-
Use the [sysrandom gem](https://github.com/cryptosphere/sysrandom) to
|
1439
|
-
use
|
1448
|
+
Use the [sysrandom gem](https://github.com/cryptosphere/sysrandom#readme) to
|
1449
|
+
use the system RNG facilities to generate random values instead of
|
1440
1450
|
userspace `OpenSSL` which MRI Ruby currently defaults to:
|
1441
1451
|
|
1442
1452
|
```text
|
@@ -1462,11 +1472,11 @@ purposes only:
|
|
1462
1472
|
|
1463
1473
|
**Session Secret App Config**
|
1464
1474
|
|
1465
|
-
|
1475
|
+
Set up your app config to fail-safe to a secure random secret
|
1466
1476
|
if the `SESSION_SECRET` environment variable is not available.
|
1467
1477
|
|
1468
1478
|
For bonus points use the [sysrandom
|
1469
|
-
gem](https://github.com/cryptosphere/sysrandom) here as well:
|
1479
|
+
gem](https://github.com/cryptosphere/sysrandom#readme) here as well:
|
1470
1480
|
|
1471
1481
|
```ruby
|
1472
1482
|
require 'securerandom'
|
@@ -1523,7 +1533,7 @@ use Rack::Protection::RemoteToken
|
|
1523
1533
|
use Rack::Protection::SessionHijacking
|
1524
1534
|
```
|
1525
1535
|
|
1526
|
-
See 'Configuring attack protection' for more information.
|
1536
|
+
See '[Configuring attack protection](#configuring-attack-protection)' for more information.
|
1527
1537
|
|
1528
1538
|
### Halting
|
1529
1539
|
|
@@ -1583,7 +1593,7 @@ matching route. If no matching route is found, a 404 is returned.
|
|
1583
1593
|
|
1584
1594
|
### Triggering Another Route
|
1585
1595
|
|
1586
|
-
Sometimes `pass` is not what you want, instead you would like to get the
|
1596
|
+
Sometimes `pass` is not what you want, instead, you would like to get the
|
1587
1597
|
result of calling another route. Simply use `call` to achieve this:
|
1588
1598
|
|
1589
1599
|
```ruby
|
@@ -1606,13 +1616,13 @@ than a duplicate, use `call!` instead of `call`.
|
|
1606
1616
|
|
1607
1617
|
Check out the Rack specification if you want to learn more about `call`.
|
1608
1618
|
|
1609
|
-
### Setting Body, Status Code and Headers
|
1619
|
+
### Setting Body, Status Code, and Headers
|
1610
1620
|
|
1611
1621
|
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
|
1622
|
+
the return value of the route block. However, in some scenarios, you might
|
1613
1623
|
want to set the body at an arbitrary point in the execution flow. You can do
|
1614
1624
|
so with the `body` helper method. If you do so, you can use that method from
|
1615
|
-
|
1625
|
+
thereon to access the body:
|
1616
1626
|
|
1617
1627
|
```ruby
|
1618
1628
|
get '/foo' do
|
@@ -1625,7 +1635,7 @@ end
|
|
1625
1635
|
```
|
1626
1636
|
|
1627
1637
|
It is also possible to pass a block to `body`, which will be executed by the
|
1628
|
-
Rack handler (this can be used to implement streaming, see "Return Values").
|
1638
|
+
Rack handler (this can be used to implement streaming, [see "Return Values"](#return-values)).
|
1629
1639
|
|
1630
1640
|
Similar to the body, you can also set the status code and headers:
|
1631
1641
|
|
@@ -1634,8 +1644,8 @@ get '/foo' do
|
|
1634
1644
|
status 418
|
1635
1645
|
headers \
|
1636
1646
|
"Allow" => "BREW, POST, GET, PROPFIND, WHEN",
|
1637
|
-
"Refresh" => "Refresh: 20;
|
1638
|
-
body "I'm a
|
1647
|
+
"Refresh" => "Refresh: 20; https://ietf.org/rfc/rfc2324.txt"
|
1648
|
+
body "I'm a teapot!"
|
1639
1649
|
end
|
1640
1650
|
```
|
1641
1651
|
|
@@ -1668,43 +1678,59 @@ also be used to increase throughput if some but not all content depends on a
|
|
1668
1678
|
slow resource.
|
1669
1679
|
|
1670
1680
|
Note that the streaming behavior, especially the number of concurrent
|
1671
|
-
requests, highly depends on the
|
1681
|
+
requests, highly depends on the webserver used to serve the application.
|
1672
1682
|
Some servers might not even support streaming at all. If the server does not
|
1673
1683
|
support streaming, the body will be sent all at once after the block passed
|
1674
1684
|
to `stream` finishes executing. Streaming does not work at all with Shotgun.
|
1675
1685
|
|
1676
1686
|
If the optional parameter is set to `keep_open`, it will not call `close` on
|
1677
1687
|
the stream object, allowing you to close it at any later point in the
|
1678
|
-
execution flow. This only works on evented servers, like
|
1688
|
+
execution flow. This only works on evented servers, like Rainbows.
|
1679
1689
|
Other servers will still close the stream:
|
1680
1690
|
|
1681
1691
|
```ruby
|
1682
|
-
#
|
1683
|
-
|
1684
|
-
set :server, :thin
|
1685
|
-
connections = []
|
1692
|
+
# config.ru
|
1693
|
+
require 'sinatra/base'
|
1686
1694
|
|
1687
|
-
|
1688
|
-
|
1689
|
-
|
1690
|
-
|
1691
|
-
#
|
1692
|
-
|
1695
|
+
class App < Sinatra::Base
|
1696
|
+
connections = []
|
1697
|
+
|
1698
|
+
get '/subscribe', provides: 'text/event-stream' do
|
1699
|
+
# register a client's interest in server events
|
1700
|
+
stream(:keep_open) do |out|
|
1701
|
+
connections << out
|
1702
|
+
# purge dead connections
|
1703
|
+
connections.reject!(&:closed?)
|
1704
|
+
end
|
1693
1705
|
end
|
1694
|
-
end
|
1695
1706
|
|
1696
|
-
post '
|
1697
|
-
|
1698
|
-
|
1699
|
-
|
1707
|
+
post '/' do
|
1708
|
+
connections.each do |out|
|
1709
|
+
# notify client that a new message has arrived
|
1710
|
+
out << "data: #{params[:msg]}\n\n"
|
1711
|
+
|
1712
|
+
# indicate client to connect again
|
1713
|
+
out.close
|
1714
|
+
end
|
1700
1715
|
|
1701
|
-
#
|
1702
|
-
out.close
|
1716
|
+
204 # response without entity body
|
1703
1717
|
end
|
1718
|
+
end
|
1719
|
+
|
1720
|
+
run App
|
1721
|
+
```
|
1704
1722
|
|
1705
|
-
|
1706
|
-
|
1723
|
+
```ruby
|
1724
|
+
# rainbows.conf
|
1725
|
+
Rainbows! do
|
1726
|
+
use :EventMachine
|
1707
1727
|
end
|
1728
|
+
````
|
1729
|
+
|
1730
|
+
Run:
|
1731
|
+
|
1732
|
+
```shell
|
1733
|
+
rainbows -c rainbows.conf
|
1708
1734
|
```
|
1709
1735
|
|
1710
1736
|
It's also possible for the client to close the connection when trying to
|
@@ -1737,7 +1763,7 @@ class MyApp < Sinatra::Base
|
|
1737
1763
|
end
|
1738
1764
|
```
|
1739
1765
|
|
1740
|
-
To avoid any logging middleware to be set up, set the `logging`
|
1766
|
+
To avoid any logging middleware to be set up, set the `logging` option to
|
1741
1767
|
`nil`. However, keep in mind that `logger` will in that case return `nil`. A
|
1742
1768
|
common use case is when you want to set your own logger. Sinatra will use
|
1743
1769
|
whatever it will find in `env['rack.logger']`.
|
@@ -1771,9 +1797,9 @@ Haml:
|
|
1771
1797
|
%a{:href => url('/foo')} foo
|
1772
1798
|
```
|
1773
1799
|
|
1774
|
-
It takes reverse proxies and Rack routers into account
|
1800
|
+
It takes reverse proxies and Rack routers into account - if present.
|
1775
1801
|
|
1776
|
-
This method is also aliased to `to` (see below for an example).
|
1802
|
+
This method is also aliased to `to` (see [below](#browser-redirect) for an example).
|
1777
1803
|
|
1778
1804
|
### Browser Redirect
|
1779
1805
|
|
@@ -1881,7 +1907,7 @@ etag @article.sha1, :weak
|
|
1881
1907
|
These helpers will not do any caching for you, but rather feed the necessary
|
1882
1908
|
information to your cache. If you are looking for a quick
|
1883
1909
|
reverse-proxy caching solution, try
|
1884
|
-
[rack-cache](https://github.com/rtomayko/rack-cache):
|
1910
|
+
[rack-cache](https://github.com/rtomayko/rack-cache#readme):
|
1885
1911
|
|
1886
1912
|
```ruby
|
1887
1913
|
require "rack/cache"
|
@@ -1896,7 +1922,7 @@ get '/' do
|
|
1896
1922
|
end
|
1897
1923
|
```
|
1898
1924
|
|
1899
|
-
Use the `:static_cache_control` setting (see below) to add
|
1925
|
+
Use the `:static_cache_control` setting (see [below](#cache-control)) to add
|
1900
1926
|
`Cache-Control` header info to static files.
|
1901
1927
|
|
1902
1928
|
According to RFC 2616, your application should behave differently if the
|
@@ -2121,7 +2147,7 @@ helpers do
|
|
2121
2147
|
end
|
2122
2148
|
```
|
2123
2149
|
|
2124
|
-
You can also easily wrap this up in an extension and share with others!
|
2150
|
+
You can also easily wrap this up in an extension and share it with others!
|
2125
2151
|
|
2126
2152
|
Note that `find_template` does not check if the file really exists but
|
2127
2153
|
rather calls the given block for all possible paths. This is not a
|
@@ -2187,7 +2213,7 @@ end
|
|
2187
2213
|
### Configuring attack protection
|
2188
2214
|
|
2189
2215
|
Sinatra is using
|
2190
|
-
[Rack::Protection](https://github.com/sinatra/rack-protection#readme) to
|
2216
|
+
[Rack::Protection](https://github.com/sinatra/sinatra/tree/master/rack-protection#readme) to
|
2191
2217
|
defend your application against common, opportunistic attacks. You can
|
2192
2218
|
easily disable this behavior (which will open up your application to tons
|
2193
2219
|
of common vulnerabilities):
|
@@ -2208,9 +2234,9 @@ set :protection, :except => [:path_traversal, :session_hijacking]
|
|
2208
2234
|
```
|
2209
2235
|
|
2210
2236
|
By default, Sinatra will only set up session based protection if `:sessions`
|
2211
|
-
have been enabled. See 'Using Sessions'. Sometimes you may want to set up
|
2237
|
+
have been enabled. See '[Using Sessions](#using-sessions)'. Sometimes you may want to set up
|
2212
2238
|
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
|
2239
|
+
separate `Rack::Builder` instance. In that case, you can still set up session
|
2214
2240
|
based protection by passing the `:session` option:
|
2215
2241
|
|
2216
2242
|
```ruby
|
@@ -2254,11 +2280,20 @@ set :protection, :session => true
|
|
2254
2280
|
used for built-in server.
|
2255
2281
|
</dd>
|
2256
2282
|
|
2283
|
+
<dt>default_content_type</dt>
|
2284
|
+
<dd>
|
2285
|
+
Content-Type to assume if unknown (defaults to <tt>"text/html"</tt>). Set
|
2286
|
+
to <tt>nil</tt> to not set a default Content-Type on every response; when
|
2287
|
+
configured so, you must set the Content-Type manually when emitting content
|
2288
|
+
or the user-agent will have to sniff it (or, if <tt>nosniff</tt> is enabled
|
2289
|
+
in Rack::Protection::XSSHeader, assume <tt>application/octet-stream</tt>).
|
2290
|
+
</dd>
|
2291
|
+
|
2257
2292
|
<dt>default_encoding</dt>
|
2258
2293
|
<dd>Encoding to assume if unknown (defaults to <tt>"utf-8"</tt>).</dd>
|
2259
2294
|
|
2260
2295
|
<dt>dump_errors</dt>
|
2261
|
-
<dd>Display errors in the log.</dd>
|
2296
|
+
<dd>Display errors in the log. Enabled by default unless environment is "test".</dd>
|
2262
2297
|
|
2263
2298
|
<dt>environment</dt>
|
2264
2299
|
<dd>
|
@@ -2353,6 +2388,16 @@ set :protection, :session => true
|
|
2353
2388
|
priority, default depends on Ruby implementation.
|
2354
2389
|
</dd>
|
2355
2390
|
|
2391
|
+
<dt>server_settings</dt>
|
2392
|
+
<dd>
|
2393
|
+
If you are using a WEBrick web server, presumably for your development
|
2394
|
+
environment, you can pass a hash of options to <tt>server_settings</tt>,
|
2395
|
+
such as <tt>SSLEnable</tt> or <tt>SSLVerifyClient</tt>. However, web
|
2396
|
+
servers such as Puma do not support this, so you can set
|
2397
|
+
<tt>server_settings</tt> by defining it as a method when you call
|
2398
|
+
<tt>configure</tt>.
|
2399
|
+
</dd>
|
2400
|
+
|
2356
2401
|
<dt>sessions</dt>
|
2357
2402
|
<dd>
|
2358
2403
|
Enable cookie-based sessions support using
|
@@ -2399,7 +2444,7 @@ set :protection, :session => true
|
|
2399
2444
|
|
2400
2445
|
<dt>threaded</dt>
|
2401
2446
|
<dd>
|
2402
|
-
If set to <tt>true</tt>, will tell
|
2447
|
+
If set to <tt>true</tt>, will tell server to use
|
2403
2448
|
<tt>EventMachine.defer</tt> for processing the request.
|
2404
2449
|
</dd>
|
2405
2450
|
|
@@ -2531,7 +2576,7 @@ and additional debugging information in your browser.
|
|
2531
2576
|
|
2532
2577
|
## Rack Middleware
|
2533
2578
|
|
2534
|
-
Sinatra rides on [Rack](
|
2579
|
+
Sinatra rides on [Rack](https://rack.github.io/), a minimal standard
|
2535
2580
|
interface for Ruby web frameworks. One of Rack's most interesting
|
2536
2581
|
capabilities for application developers is support for "middleware" --
|
2537
2582
|
components that sit between the server and your application monitoring
|
@@ -2571,7 +2616,7 @@ typically don't have to `use` them explicitly.
|
|
2571
2616
|
|
2572
2617
|
You can find useful middleware in
|
2573
2618
|
[rack](https://github.com/rack/rack/tree/master/lib/rack),
|
2574
|
-
[rack-contrib](https://github.com/rack/rack-contrib#
|
2619
|
+
[rack-contrib](https://github.com/rack/rack-contrib#readme),
|
2575
2620
|
or in the [Rack wiki](https://github.com/rack/rack/wiki/List-of-Middleware).
|
2576
2621
|
|
2577
2622
|
## Testing
|
@@ -2672,7 +2717,7 @@ modular application.
|
|
2672
2717
|
The main disadvantage of using the classic style rather than the modular
|
2673
2718
|
style is that you will only have one Sinatra application per Ruby
|
2674
2719
|
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
|
2720
|
+
There is no reason you cannot mix the modular and classic styles.
|
2676
2721
|
|
2677
2722
|
If switching from one style to the other, you should be aware of
|
2678
2723
|
slightly different default settings:
|
@@ -2801,7 +2846,7 @@ style for running with a `config.ru`.**
|
|
2801
2846
|
### Using Sinatra as Middleware
|
2802
2847
|
|
2803
2848
|
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
|
2849
|
+
application can, in turn, be added in front of any Rack endpoint as
|
2805
2850
|
middleware itself. This endpoint could be another Sinatra application,
|
2806
2851
|
or any other Rack-based application (Rails/Hanami/Roda/...):
|
2807
2852
|
|
@@ -2892,7 +2937,7 @@ available.
|
|
2892
2937
|
Every Sinatra application corresponds to a subclass of `Sinatra::Base`.
|
2893
2938
|
If you are using the top-level DSL (`require 'sinatra'`), then this
|
2894
2939
|
class is `Sinatra::Application`, otherwise it is the subclass you
|
2895
|
-
created explicitly. At class level you have methods like `get` or
|
2940
|
+
created explicitly. At the class level, you have methods like `get` or
|
2896
2941
|
`before`, but you cannot access the `request` or `session` objects, as
|
2897
2942
|
there is only a single application class for all requests.
|
2898
2943
|
|
@@ -2915,7 +2960,7 @@ You have the application scope binding inside:
|
|
2915
2960
|
* Your application class body
|
2916
2961
|
* Methods defined by extensions
|
2917
2962
|
* The block passed to `helpers`
|
2918
|
-
* Procs/blocks used as value for `set`
|
2963
|
+
* Procs/blocks used as a value for `set`
|
2919
2964
|
* The block passed to `Sinatra.new`
|
2920
2965
|
|
2921
2966
|
You can reach the scope object (the class) like this:
|
@@ -2966,7 +3011,7 @@ do not share variables/state with the class scope (read: you have a different
|
|
2966
3011
|
|
2967
3012
|
You have the delegate scope binding inside:
|
2968
3013
|
|
2969
|
-
* The top
|
3014
|
+
* The top-level binding, if you did `require "sinatra"`
|
2970
3015
|
* An object extended with the `Sinatra::Delegator` mixin
|
2971
3016
|
|
2972
3017
|
Have a look at the code for yourself: here's the
|
@@ -2988,25 +3033,27 @@ Options are:
|
|
2988
3033
|
-p # set the port (default is 4567)
|
2989
3034
|
-o # set the host (default is 0.0.0.0)
|
2990
3035
|
-e # set the environment (default is development)
|
2991
|
-
-s # specify rack server/handler (default is
|
3036
|
+
-s # specify rack server/handler (default is puma)
|
2992
3037
|
-q # turn on quiet mode for server (default is off)
|
2993
3038
|
-x # turn on the mutex lock (default is off)
|
2994
3039
|
```
|
2995
3040
|
|
2996
3041
|
### Multi-threading
|
2997
3042
|
|
2998
|
-
_Paraphrasing from
|
3043
|
+
_Paraphrasing from
|
3044
|
+
[this StackOverflow answer](https://stackoverflow.com/a/6282999/5245129)
|
3045
|
+
by Konstantin_
|
2999
3046
|
|
3000
|
-
Sinatra doesn't impose any concurrency model
|
3001
|
-
underlying Rack handler (server) like
|
3047
|
+
Sinatra doesn't impose any concurrency model but leaves that to the
|
3048
|
+
underlying Rack handler (server) like Puma or WEBrick. Sinatra
|
3002
3049
|
itself is thread-safe, so there won't be any problem if the Rack handler
|
3003
3050
|
uses a threaded model of concurrency. This would mean that when starting
|
3004
3051
|
the server, you'd have to specify the correct invocation method for the
|
3005
3052
|
specific Rack handler. The following example is a demonstration of how
|
3006
|
-
to start a multi-threaded
|
3053
|
+
to start a multi-threaded Rainbows server:
|
3007
3054
|
|
3008
3055
|
```ruby
|
3009
|
-
#
|
3056
|
+
# config.ru
|
3010
3057
|
|
3011
3058
|
require 'sinatra/base'
|
3012
3059
|
|
@@ -3016,26 +3063,31 @@ class App < Sinatra::Base
|
|
3016
3063
|
end
|
3017
3064
|
end
|
3018
3065
|
|
3019
|
-
App
|
3066
|
+
run App
|
3067
|
+
```
|
3068
|
+
|
3069
|
+
```ruby
|
3070
|
+
# rainbows.conf
|
3020
3071
|
|
3072
|
+
# Rainbows configurator is based on Unicorn.
|
3073
|
+
Rainbows! do
|
3074
|
+
use :ThreadSpawn
|
3075
|
+
end
|
3021
3076
|
```
|
3022
3077
|
|
3023
3078
|
To start the server, the command would be:
|
3024
3079
|
|
3025
3080
|
```shell
|
3026
|
-
|
3081
|
+
rainbows -c rainbows.conf
|
3027
3082
|
```
|
3028
3083
|
|
3029
|
-
|
3030
|
-
[so-answer]: http://stackoverflow.com/questions/6278817/is-sinatra-multi-threaded/6282999#6282999)
|
3031
|
-
|
3032
3084
|
## Requirement
|
3033
3085
|
|
3034
3086
|
The following Ruby versions are officially supported:
|
3035
3087
|
<dl>
|
3036
|
-
<dt>Ruby 2.
|
3088
|
+
<dt>Ruby 2.3</dt>
|
3037
3089
|
<dd>
|
3038
|
-
2.
|
3090
|
+
2.3 is fully supported and recommended. There are currently no plans to
|
3039
3091
|
drop official support for it.
|
3040
3092
|
</dd>
|
3041
3093
|
|
@@ -3053,7 +3105,7 @@ The following Ruby versions are officially supported:
|
|
3053
3105
|
</dd>
|
3054
3106
|
</dl>
|
3055
3107
|
|
3056
|
-
Versions of Ruby
|
3108
|
+
Versions of Ruby before 2.3 are no longer supported as of Sinatra 2.1.0.
|
3057
3109
|
|
3058
3110
|
We also keep an eye on upcoming Ruby versions.
|
3059
3111
|
|
@@ -3097,7 +3149,7 @@ to get some of the latest features.
|
|
3097
3149
|
### With Bundler
|
3098
3150
|
|
3099
3151
|
If you want to run your application with the latest Sinatra, using
|
3100
|
-
[Bundler](
|
3152
|
+
[Bundler](https://bundler.io) is the recommended way.
|
3101
3153
|
|
3102
3154
|
First, install bundler, if you haven't:
|
3103
3155
|
|
@@ -3127,7 +3179,7 @@ bundle exec ruby myapp.rb
|
|
3127
3179
|
|
3128
3180
|
## Versioning
|
3129
3181
|
|
3130
|
-
Sinatra follows [Semantic Versioning](
|
3182
|
+
Sinatra follows [Semantic Versioning](https://semver.org/), both SemVer and
|
3131
3183
|
SemVerTag.
|
3132
3184
|
|
3133
3185
|
## Further Reading
|
@@ -3138,14 +3190,14 @@ SemVerTag.
|
|
3138
3190
|
help? Have a patch?
|
3139
3191
|
* [Issue tracker](https://github.com/sinatra/sinatra/issues)
|
3140
3192
|
* [Twitter](https://twitter.com/sinatra)
|
3141
|
-
* [Mailing List](
|
3142
|
-
* IRC: [#sinatra](irc://chat.freenode.net/#sinatra) on
|
3143
|
-
* [Sinatra & Friends](https://sinatrarb.slack.com) on Slack
|
3144
|
-
[
|
3145
|
-
* [Sinatra Book](https://github.com/sinatra/sinatra-book
|
3146
|
-
* [Sinatra Recipes](http://recipes.sinatrarb.com/) Community
|
3147
|
-
|
3193
|
+
* [Mailing List](https://groups.google.com/forum/#!forum/sinatrarb)
|
3194
|
+
* IRC: [#sinatra](irc://chat.freenode.net/#sinatra) on [Freenode](https://freenode.net)
|
3195
|
+
* [Sinatra & Friends](https://sinatrarb.slack.com) on Slack
|
3196
|
+
([get an invite](https://sinatra-slack.herokuapp.com/))
|
3197
|
+
* [Sinatra Book](https://github.com/sinatra/sinatra-book) - Cookbook Tutorial
|
3198
|
+
* [Sinatra Recipes](http://recipes.sinatrarb.com/) - Community contributed
|
3199
|
+
recipes
|
3148
3200
|
* API documentation for the [latest release](http://www.rubydoc.info/gems/sinatra)
|
3149
3201
|
or the [current HEAD](http://www.rubydoc.info/github/sinatra/sinatra) on
|
3150
|
-
http://www.rubydoc.info/
|
3202
|
+
[RubyDoc](http://www.rubydoc.info/)
|
3151
3203
|
* [CI server](https://travis-ci.org/sinatra/sinatra)
|