sinatra 2.0.8 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sinatra might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +105 -2
- data/Gemfile +5 -4
- data/MAINTENANCE.md +2 -15
- data/README.de.md +16 -16
- data/README.es.md +62 -33
- data/README.fr.md +182 -85
- data/README.hu.md +3 -3
- data/README.ja.md +62 -32
- data/README.ko.md +1 -1
- data/README.md +93 -60
- data/README.pt-br.md +61 -61
- data/README.pt-pt.md +3 -3
- data/README.ru.md +3 -3
- data/README.zh.md +1 -1
- data/VERSION +1 -1
- 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 +109 -68
- data/lib/sinatra/indifferent_hash.rb +27 -9
- data/lib/sinatra/main.rb +5 -5
- data/lib/sinatra/show_exceptions.rb +2 -37
- data/lib/sinatra/version.rb +1 -1
- data/sinatra.gemspec +4 -10
- metadata +10 -21
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Sinatra
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/sinatra.svg)](
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/sinatra.svg)](https://badge.fury.io/rb/sinatra)
|
4
4
|
[![Build Status](https://secure.travis-ci.org/sinatra/sinatra.svg)](https://travis-ci.org/sinatra/sinatra)
|
5
5
|
[![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=sinatra&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=sinatra&package-manager=bundler&version-scheme=semver)
|
6
6
|
|
@@ -34,7 +34,7 @@ The code you changed will not take effect until you restart the server.
|
|
34
34
|
Please restart the server every time you change or use
|
35
35
|
[sinatra/reloader](http://www.sinatrarb.com/contrib/reloader).
|
36
36
|
|
37
|
-
It is recommended to also run `gem install
|
37
|
+
It is recommended to also run `gem install puma`, which Sinatra will
|
38
38
|
pick up if available.
|
39
39
|
|
40
40
|
## Table of Contents
|
@@ -344,11 +344,11 @@ end
|
|
344
344
|
## Return Values
|
345
345
|
|
346
346
|
The return value of a route block determines at least the response body
|
347
|
-
passed on to the HTTP client
|
347
|
+
passed on to the HTTP client or at least the next middleware in the
|
348
348
|
Rack stack. Most commonly, this is a string, as in the above examples.
|
349
349
|
But other values are also accepted.
|
350
350
|
|
351
|
-
You can return
|
351
|
+
You can return an object that would either be a valid Rack response, Rack
|
352
352
|
body object or HTTP status code:
|
353
353
|
|
354
354
|
* An Array with three elements: `[status (Integer), headers (Hash), response
|
@@ -372,7 +372,7 @@ get('/') { Stream.new }
|
|
372
372
|
```
|
373
373
|
|
374
374
|
You can also use the `stream` helper method ([described below](#streaming-responses)) to reduce
|
375
|
-
|
375
|
+
boilerplate and embed the streaming logic in the route.
|
376
376
|
|
377
377
|
## Custom Route Matchers
|
378
378
|
|
@@ -427,7 +427,7 @@ Static files are served from the `./public` directory. You can specify
|
|
427
427
|
a different location by setting the `:public_folder` option:
|
428
428
|
|
429
429
|
```ruby
|
430
|
-
set :public_folder,
|
430
|
+
set :public_folder, __dir__ + '/static'
|
431
431
|
```
|
432
432
|
|
433
433
|
Note that the public directory name is not included in the URL. A file
|
@@ -1201,7 +1201,7 @@ end
|
|
1201
1201
|
```
|
1202
1202
|
|
1203
1203
|
Currently, the following rendering methods accept a block: `erb`, `haml`,
|
1204
|
-
`liquid`, `slim `, `wlang`. Also the general `render` method accepts a block.
|
1204
|
+
`liquid`, `slim `, `wlang`. Also, the general `render` method accepts a block.
|
1205
1205
|
|
1206
1206
|
### Inline Templates
|
1207
1207
|
|
@@ -1224,7 +1224,7 @@ __END__
|
|
1224
1224
|
%div.title Hello world.
|
1225
1225
|
```
|
1226
1226
|
|
1227
|
-
NOTE: Inline templates defined in the source file that requires
|
1227
|
+
NOTE: Inline templates defined in the source file that requires Sinatra are
|
1228
1228
|
automatically loaded. Call `enable :inline_templates` explicitly if you
|
1229
1229
|
have inline templates in other source files.
|
1230
1230
|
|
@@ -1293,7 +1293,7 @@ own `#find_template` method:
|
|
1293
1293
|
|
1294
1294
|
```ruby
|
1295
1295
|
configure do
|
1296
|
-
set :views [ './views/a', './views/b' ]
|
1296
|
+
set :views, [ './views/a', './views/b' ]
|
1297
1297
|
end
|
1298
1298
|
|
1299
1299
|
def find_template(views, name, engine, &block)
|
@@ -1434,7 +1434,7 @@ For better security and usability it's
|
|
1434
1434
|
secret and store it in an environment variable on each host running your
|
1435
1435
|
application so that all of your application instances will share the same
|
1436
1436
|
secret. You should periodically rotate this session secret to a new value.
|
1437
|
-
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:
|
1438
1438
|
|
1439
1439
|
**Session Secret Generation**
|
1440
1440
|
|
@@ -1446,7 +1446,7 @@ $ ruby -e "require 'securerandom'; puts SecureRandom.hex(64)"
|
|
1446
1446
|
**Session Secret Generation (Bonus Points)**
|
1447
1447
|
|
1448
1448
|
Use the [sysrandom gem](https://github.com/cryptosphere/sysrandom#readme) to
|
1449
|
-
|
1449
|
+
use the system RNG facilities to generate random values instead of
|
1450
1450
|
userspace `OpenSSL` which MRI Ruby currently defaults to:
|
1451
1451
|
|
1452
1452
|
```text
|
@@ -1472,7 +1472,7 @@ purposes only:
|
|
1472
1472
|
|
1473
1473
|
**Session Secret App Config**
|
1474
1474
|
|
1475
|
-
|
1475
|
+
Set up your app config to fail-safe to a secure random secret
|
1476
1476
|
if the `SESSION_SECRET` environment variable is not available.
|
1477
1477
|
|
1478
1478
|
For bonus points use the [sysrandom
|
@@ -1593,7 +1593,7 @@ matching route. If no matching route is found, a 404 is returned.
|
|
1593
1593
|
|
1594
1594
|
### Triggering Another Route
|
1595
1595
|
|
1596
|
-
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
|
1597
1597
|
result of calling another route. Simply use `call` to achieve this:
|
1598
1598
|
|
1599
1599
|
```ruby
|
@@ -1616,13 +1616,13 @@ than a duplicate, use `call!` instead of `call`.
|
|
1616
1616
|
|
1617
1617
|
Check out the Rack specification if you want to learn more about `call`.
|
1618
1618
|
|
1619
|
-
### Setting Body, Status Code and Headers
|
1619
|
+
### Setting Body, Status Code, and Headers
|
1620
1620
|
|
1621
1621
|
It is possible and recommended to set the status code and response body with
|
1622
|
-
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
|
1623
1623
|
want to set the body at an arbitrary point in the execution flow. You can do
|
1624
1624
|
so with the `body` helper method. If you do so, you can use that method from
|
1625
|
-
|
1625
|
+
thereon to access the body:
|
1626
1626
|
|
1627
1627
|
```ruby
|
1628
1628
|
get '/foo' do
|
@@ -1645,7 +1645,7 @@ get '/foo' do
|
|
1645
1645
|
headers \
|
1646
1646
|
"Allow" => "BREW, POST, GET, PROPFIND, WHEN",
|
1647
1647
|
"Refresh" => "Refresh: 20; https://ietf.org/rfc/rfc2324.txt"
|
1648
|
-
body "I'm a
|
1648
|
+
body "I'm a teapot!"
|
1649
1649
|
end
|
1650
1650
|
```
|
1651
1651
|
|
@@ -1678,43 +1678,59 @@ also be used to increase throughput if some but not all content depends on a
|
|
1678
1678
|
slow resource.
|
1679
1679
|
|
1680
1680
|
Note that the streaming behavior, especially the number of concurrent
|
1681
|
-
requests, highly depends on the
|
1681
|
+
requests, highly depends on the webserver used to serve the application.
|
1682
1682
|
Some servers might not even support streaming at all. If the server does not
|
1683
1683
|
support streaming, the body will be sent all at once after the block passed
|
1684
1684
|
to `stream` finishes executing. Streaming does not work at all with Shotgun.
|
1685
1685
|
|
1686
1686
|
If the optional parameter is set to `keep_open`, it will not call `close` on
|
1687
1687
|
the stream object, allowing you to close it at any later point in the
|
1688
|
-
execution flow. This only works on evented servers, like
|
1688
|
+
execution flow. This only works on evented servers, like Rainbows.
|
1689
1689
|
Other servers will still close the stream:
|
1690
1690
|
|
1691
1691
|
```ruby
|
1692
|
-
#
|
1693
|
-
|
1694
|
-
set :server, :thin
|
1695
|
-
connections = []
|
1692
|
+
# config.ru
|
1693
|
+
require 'sinatra/base'
|
1696
1694
|
|
1697
|
-
|
1698
|
-
|
1699
|
-
|
1700
|
-
|
1701
|
-
#
|
1702
|
-
|
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
|
1703
1705
|
end
|
1704
|
-
end
|
1705
1706
|
|
1706
|
-
post '
|
1707
|
-
|
1708
|
-
|
1709
|
-
|
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
|
1710
1715
|
|
1711
|
-
#
|
1712
|
-
out.close
|
1716
|
+
204 # response without entity body
|
1713
1717
|
end
|
1718
|
+
end
|
1719
|
+
|
1720
|
+
run App
|
1721
|
+
```
|
1714
1722
|
|
1715
|
-
|
1716
|
-
|
1723
|
+
```ruby
|
1724
|
+
# rainbows.conf
|
1725
|
+
Rainbows! do
|
1726
|
+
use :EventMachine
|
1717
1727
|
end
|
1728
|
+
````
|
1729
|
+
|
1730
|
+
Run:
|
1731
|
+
|
1732
|
+
```shell
|
1733
|
+
rainbows -c rainbows.conf
|
1718
1734
|
```
|
1719
1735
|
|
1720
1736
|
It's also possible for the client to close the connection when trying to
|
@@ -1747,7 +1763,7 @@ class MyApp < Sinatra::Base
|
|
1747
1763
|
end
|
1748
1764
|
```
|
1749
1765
|
|
1750
|
-
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
|
1751
1767
|
`nil`. However, keep in mind that `logger` will in that case return `nil`. A
|
1752
1768
|
common use case is when you want to set your own logger. Sinatra will use
|
1753
1769
|
whatever it will find in `env['rack.logger']`.
|
@@ -1781,7 +1797,7 @@ Haml:
|
|
1781
1797
|
%a{:href => url('/foo')} foo
|
1782
1798
|
```
|
1783
1799
|
|
1784
|
-
It takes reverse proxies and Rack routers into account
|
1800
|
+
It takes reverse proxies and Rack routers into account - if present.
|
1785
1801
|
|
1786
1802
|
This method is also aliased to `to` (see [below](#browser-redirect) for an example).
|
1787
1803
|
|
@@ -2131,7 +2147,7 @@ helpers do
|
|
2131
2147
|
end
|
2132
2148
|
```
|
2133
2149
|
|
2134
|
-
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!
|
2135
2151
|
|
2136
2152
|
Note that `find_template` does not check if the file really exists but
|
2137
2153
|
rather calls the given block for all possible paths. This is not a
|
@@ -2220,7 +2236,7 @@ set :protection, :except => [:path_traversal, :session_hijacking]
|
|
2220
2236
|
By default, Sinatra will only set up session based protection if `:sessions`
|
2221
2237
|
have been enabled. See '[Using Sessions](#using-sessions)'. Sometimes you may want to set up
|
2222
2238
|
sessions "outside" of the Sinatra app, such as in the config.ru or with a
|
2223
|
-
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
|
2224
2240
|
based protection by passing the `:session` option:
|
2225
2241
|
|
2226
2242
|
```ruby
|
@@ -2264,11 +2280,20 @@ set :protection, :session => true
|
|
2264
2280
|
used for built-in server.
|
2265
2281
|
</dd>
|
2266
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
|
+
|
2267
2292
|
<dt>default_encoding</dt>
|
2268
2293
|
<dd>Encoding to assume if unknown (defaults to <tt>"utf-8"</tt>).</dd>
|
2269
2294
|
|
2270
2295
|
<dt>dump_errors</dt>
|
2271
|
-
<dd>Display errors in the log.</dd>
|
2296
|
+
<dd>Display errors in the log. Enabled by default unless environment is "test".</dd>
|
2272
2297
|
|
2273
2298
|
<dt>environment</dt>
|
2274
2299
|
<dd>
|
@@ -2368,7 +2393,7 @@ set :protection, :session => true
|
|
2368
2393
|
If you are using a WEBrick web server, presumably for your development
|
2369
2394
|
environment, you can pass a hash of options to <tt>server_settings</tt>,
|
2370
2395
|
such as <tt>SSLEnable</tt> or <tt>SSLVerifyClient</tt>. However, web
|
2371
|
-
servers such as Puma
|
2396
|
+
servers such as Puma do not support this, so you can set
|
2372
2397
|
<tt>server_settings</tt> by defining it as a method when you call
|
2373
2398
|
<tt>configure</tt>.
|
2374
2399
|
</dd>
|
@@ -2419,7 +2444,7 @@ set :protection, :session => true
|
|
2419
2444
|
|
2420
2445
|
<dt>threaded</dt>
|
2421
2446
|
<dd>
|
2422
|
-
If set to <tt>true</tt>, will tell
|
2447
|
+
If set to <tt>true</tt>, will tell server to use
|
2423
2448
|
<tt>EventMachine.defer</tt> for processing the request.
|
2424
2449
|
</dd>
|
2425
2450
|
|
@@ -2692,7 +2717,7 @@ modular application.
|
|
2692
2717
|
The main disadvantage of using the classic style rather than the modular
|
2693
2718
|
style is that you will only have one Sinatra application per Ruby
|
2694
2719
|
process. If you plan to use more than one, switch to the modular style.
|
2695
|
-
There is no reason you cannot mix the modular and
|
2720
|
+
There is no reason you cannot mix the modular and classic styles.
|
2696
2721
|
|
2697
2722
|
If switching from one style to the other, you should be aware of
|
2698
2723
|
slightly different default settings:
|
@@ -2821,7 +2846,7 @@ style for running with a `config.ru`.**
|
|
2821
2846
|
### Using Sinatra as Middleware
|
2822
2847
|
|
2823
2848
|
Not only is Sinatra able to use other Rack middleware, any Sinatra
|
2824
|
-
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
|
2825
2850
|
middleware itself. This endpoint could be another Sinatra application,
|
2826
2851
|
or any other Rack-based application (Rails/Hanami/Roda/...):
|
2827
2852
|
|
@@ -2912,7 +2937,7 @@ available.
|
|
2912
2937
|
Every Sinatra application corresponds to a subclass of `Sinatra::Base`.
|
2913
2938
|
If you are using the top-level DSL (`require 'sinatra'`), then this
|
2914
2939
|
class is `Sinatra::Application`, otherwise it is the subclass you
|
2915
|
-
created explicitly. At class level you have methods like `get` or
|
2940
|
+
created explicitly. At the class level, you have methods like `get` or
|
2916
2941
|
`before`, but you cannot access the `request` or `session` objects, as
|
2917
2942
|
there is only a single application class for all requests.
|
2918
2943
|
|
@@ -2935,7 +2960,7 @@ You have the application scope binding inside:
|
|
2935
2960
|
* Your application class body
|
2936
2961
|
* Methods defined by extensions
|
2937
2962
|
* The block passed to `helpers`
|
2938
|
-
* Procs/blocks used as value for `set`
|
2963
|
+
* Procs/blocks used as a value for `set`
|
2939
2964
|
* The block passed to `Sinatra.new`
|
2940
2965
|
|
2941
2966
|
You can reach the scope object (the class) like this:
|
@@ -2986,7 +3011,7 @@ do not share variables/state with the class scope (read: you have a different
|
|
2986
3011
|
|
2987
3012
|
You have the delegate scope binding inside:
|
2988
3013
|
|
2989
|
-
* The top
|
3014
|
+
* The top-level binding, if you did `require "sinatra"`
|
2990
3015
|
* An object extended with the `Sinatra::Delegator` mixin
|
2991
3016
|
|
2992
3017
|
Have a look at the code for yourself: here's the
|
@@ -3008,7 +3033,7 @@ Options are:
|
|
3008
3033
|
-p # set the port (default is 4567)
|
3009
3034
|
-o # set the host (default is 0.0.0.0)
|
3010
3035
|
-e # set the environment (default is development)
|
3011
|
-
-s # specify rack server/handler (default is
|
3036
|
+
-s # specify rack server/handler (default is puma)
|
3012
3037
|
-q # turn on quiet mode for server (default is off)
|
3013
3038
|
-x # turn on the mutex lock (default is off)
|
3014
3039
|
```
|
@@ -3019,16 +3044,16 @@ _Paraphrasing from
|
|
3019
3044
|
[this StackOverflow answer](https://stackoverflow.com/a/6282999/5245129)
|
3020
3045
|
by Konstantin_
|
3021
3046
|
|
3022
|
-
Sinatra doesn't impose any concurrency model
|
3023
|
-
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
|
3024
3049
|
itself is thread-safe, so there won't be any problem if the Rack handler
|
3025
3050
|
uses a threaded model of concurrency. This would mean that when starting
|
3026
3051
|
the server, you'd have to specify the correct invocation method for the
|
3027
3052
|
specific Rack handler. The following example is a demonstration of how
|
3028
|
-
to start a multi-threaded
|
3053
|
+
to start a multi-threaded Rainbows server:
|
3029
3054
|
|
3030
3055
|
```ruby
|
3031
|
-
#
|
3056
|
+
# config.ru
|
3032
3057
|
|
3033
3058
|
require 'sinatra/base'
|
3034
3059
|
|
@@ -3038,23 +3063,31 @@ class App < Sinatra::Base
|
|
3038
3063
|
end
|
3039
3064
|
end
|
3040
3065
|
|
3041
|
-
App
|
3066
|
+
run App
|
3067
|
+
```
|
3042
3068
|
|
3069
|
+
```ruby
|
3070
|
+
# rainbows.conf
|
3071
|
+
|
3072
|
+
# Rainbows configurator is based on Unicorn.
|
3073
|
+
Rainbows! do
|
3074
|
+
use :ThreadSpawn
|
3075
|
+
end
|
3043
3076
|
```
|
3044
3077
|
|
3045
3078
|
To start the server, the command would be:
|
3046
3079
|
|
3047
3080
|
```shell
|
3048
|
-
|
3081
|
+
rainbows -c rainbows.conf
|
3049
3082
|
```
|
3050
3083
|
|
3051
3084
|
## Requirement
|
3052
3085
|
|
3053
3086
|
The following Ruby versions are officially supported:
|
3054
3087
|
<dl>
|
3055
|
-
<dt>Ruby 2.
|
3088
|
+
<dt>Ruby 2.3</dt>
|
3056
3089
|
<dd>
|
3057
|
-
2.
|
3090
|
+
2.3 is fully supported and recommended. There are currently no plans to
|
3058
3091
|
drop official support for it.
|
3059
3092
|
</dd>
|
3060
3093
|
|
@@ -3072,7 +3105,7 @@ The following Ruby versions are officially supported:
|
|
3072
3105
|
</dd>
|
3073
3106
|
</dl>
|
3074
3107
|
|
3075
|
-
Versions of Ruby
|
3108
|
+
Versions of Ruby before 2.3 are no longer supported as of Sinatra 2.1.0.
|
3076
3109
|
|
3077
3110
|
We also keep an eye on upcoming Ruby versions.
|
3078
3111
|
|