sinatra 2.0.0 → 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 +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.zh.md
CHANGED
@@ -239,6 +239,17 @@ end
|
|
239
239
|
```
|
240
240
|
顺便一提,除非你禁用了路径遍历攻击防护(见下文),请求路径可能在匹配路由前发生改变。
|
241
241
|
|
242
|
+
你也可以通过`:mustermann_opt`选项定义[Mustermann](https://github.com/sinatra/mustermann)来匹配路由。
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
get '\A/posts\z', :mustermann_opts => { :type => :regexp, :check_anchors => false } do
|
246
|
+
# matches /posts exactly, with explicit anchoring
|
247
|
+
"If you match an anchored pattern clap your hands!"
|
248
|
+
end
|
249
|
+
```
|
250
|
+
|
251
|
+
它看起来像一个[条件](https://github.com/sinatra/sinatra/blob/master/README.zh.md#%E6%9D%A1%E4%BB%B6),但实际不是!这些选项将被合并到全局的`mustermann_opts`。
|
252
|
+
|
242
253
|
### 条件
|
243
254
|
|
244
255
|
路由可以包含各种匹配条件,比如 user agent:
|
@@ -313,8 +324,8 @@ Rack 堆栈中的下一个中间件。大多数情况下,返回值是一个字
|
|
313
324
|
|
314
325
|
你可以返回任何对象,该对象要么是一个合理的 Rack 响应,要么是一个 Rack body 对象,要么是 HTTP 状态码:
|
315
326
|
|
316
|
-
* 一个包含三个元素的数组: `[状态 (
|
317
|
-
* 一个包含两个元素的数组: `[状态 (
|
327
|
+
* 一个包含三个元素的数组: `[状态 (Integer), 响应首部 (Hash), 响应主体 (可以响应 #each 方法)]`
|
328
|
+
* 一个包含两个元素的数组: `[状态 (Integer), 响应主体 (可以响应 #each 方法)]`
|
318
329
|
* 一个响应 `#each` 方法,只传回字符串的对象
|
319
330
|
* 一个代表状态码的数字
|
320
331
|
|
@@ -382,7 +393,7 @@ end
|
|
382
393
|
静态文件从 `./public` 目录提供服务。可以通过设置`:public_folder` 选项设定一个不同的位置:
|
383
394
|
|
384
395
|
```ruby
|
385
|
-
set :public_folder,
|
396
|
+
set :public_folder, __dir__ + '/static'
|
386
397
|
```
|
387
398
|
|
388
399
|
请注意 public 目录名并没有包含在 URL 中。文件 `./public/css/style.css` 可以通过
|
@@ -649,7 +660,7 @@ get('/') { markdown :index }
|
|
649
660
|
<table>
|
650
661
|
<tr>
|
651
662
|
<td>依赖项</td>
|
652
|
-
<td><a href="
|
663
|
+
<td><a href="https://shopify.github.io/liquid/" title="liquid">liquid</a></td>
|
653
664
|
</tr>
|
654
665
|
<tr>
|
655
666
|
<td>文件扩展名</td>
|
@@ -672,7 +683,7 @@ get('/') { markdown :index }
|
|
672
683
|
下列任一:
|
673
684
|
<a href="https://github.com/davidfstr/rdiscount" title="RDiscount">RDiscount</a>,
|
674
685
|
<a href="https://github.com/vmg/redcarpet" title="RedCarpet">RedCarpet</a>,
|
675
|
-
<a href="
|
686
|
+
<a href="https://github.com/ged/bluecloth" title="bluecloth">BlueCloth</a>,
|
676
687
|
<a href="http://kramdown.gettalong.org/" title="kramdown">kramdown</a>,
|
677
688
|
<a href="https://github.com/bhollis/maruku" title="maruku">maruku</a>
|
678
689
|
</td>
|
@@ -1302,33 +1313,59 @@ get '/:value' do
|
|
1302
1313
|
end
|
1303
1314
|
```
|
1304
1315
|
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1316
|
+
#### 会话加密
|
1317
|
+
|
1318
|
+
为提高安全性,cookie 中的会话数据使用`HMAC-SHA1`进行加密。会话加密的最佳实践应当是像`HMAC-SHA1`这样生成大于或等于64字节 (512 bits, 128 hex characters)的随机值。应当避免使用少于32字节(256 bits, 64 hex characters)的随机值。应当使用生成器来创建安全的密钥,而不是拍脑袋决定。
|
1319
|
+
|
1320
|
+
默认情况下,Sinatra会生成一个32字节的密钥,但随着应用程序的每次重新启动,它都会发生改变。如果有多个应用程序的实例,使用Sinatra生成密钥,每个实例将有不同的密钥,这可能不是您想要的。
|
1321
|
+
|
1322
|
+
为了更好的安全性和可用性,[建议](https://12factor.net/config)生成安全的随机密钥,并将其存储在运行应用程序的每个主机上的环境变量中,以便所有应用程序实例都将共享相同的密钥。并且应该定期更新会话密钥。下面是一些创建64比特密钥的例子:
|
1323
|
+
|
1324
|
+
#### 生成密钥
|
1309
1325
|
|
1310
1326
|
```ruby
|
1311
|
-
|
1327
|
+
$ ruby -e "require 'securerandom'; puts SecureRandom.hex(64)"
|
1328
|
+
99ae8af...snip...ec0f262ac
|
1329
|
+
```
|
1312
1330
|
|
1313
|
-
|
1314
|
-
"value = " << session['value'].inspect
|
1315
|
-
end
|
1331
|
+
#### 生成密钥(小贴士)
|
1316
1332
|
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1333
|
+
MRI Ruby目前认为[sysrandom gem](https://github.com/cryptosphere/sysrandom)使用系统的随机数生成器要比用户态的`OpenSSL`好。
|
1334
|
+
|
1335
|
+
```ruby
|
1336
|
+
$ gem install sysrandom
|
1337
|
+
Building native extensions. This could take a while...
|
1338
|
+
Successfully installed sysrandom-1.x
|
1339
|
+
1 gem installed
|
1340
|
+
|
1341
|
+
$ ruby -e "require 'sysrandom/securerandom'; puts SecureRandom.hex(64)"
|
1342
|
+
99ae8af...snip...ec0f262ac
|
1320
1343
|
```
|
1321
1344
|
|
1322
|
-
|
1323
|
-
然而,每次启动应用时,该密码都会变化,你也可以自己设置该密码,以便所有的应用实例共享:
|
1345
|
+
#### 从环境变量使用密钥
|
1324
1346
|
|
1347
|
+
将Sinatra的SESSION_SECRET环境变量设置为生成的值。在主机的重新启动之间保存这个值。由于这样做的方法会因系统而异,仅供说明之用:
|
1325
1348
|
```
|
1326
|
-
|
1349
|
+
# echo "export SESSION_SECRET=99ae8af...snip...ec0f262ac" >> ~/.bashrc
|
1327
1350
|
```
|
1328
1351
|
|
1329
|
-
|
1352
|
+
#### 应用的密钥配置
|
1330
1353
|
|
1354
|
+
如果SESSION SECRET环境变量不可用,将把应用的随机密钥设置为不安全的。
|
1355
|
+
|
1356
|
+
关于[sysrandom gem](https://github.com/cryptosphere/sysrandom)的更多用法:
|
1357
|
+
|
1358
|
+
```ruby
|
1359
|
+
require 'securerandom'
|
1360
|
+
# -or- require 'sysrandom/securerandom'
|
1361
|
+
set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex(64) }
|
1331
1362
|
```
|
1363
|
+
|
1364
|
+
#### 会话配置
|
1365
|
+
|
1366
|
+
如果你想进一步配置会话,可以在设置 `sessions` 时提供一个选项 hash 作为第二个参数:
|
1367
|
+
|
1368
|
+
```ruby
|
1332
1369
|
set :sessions, :domain => 'foo.com'
|
1333
1370
|
```
|
1334
1371
|
|
@@ -1338,6 +1375,31 @@ set :sessions, :domain => 'foo.com'
|
|
1338
1375
|
set :sessions, :domain => '.foo.com'
|
1339
1376
|
```
|
1340
1377
|
|
1378
|
+
#### 选择你自己的会话中间件
|
1379
|
+
|
1380
|
+
请注意 `enable :sessions` 实际将所有的数据保存在一个 cookie 中。
|
1381
|
+
这可能并不总是你想要的(cookie 中存储大量的数据会增加你的流量)。
|
1382
|
+
你可以使用任何 Rack session 中间件:要达到此目的,**不要**使用 `enable :sessions`,
|
1383
|
+
而是按照自己的需要引入想使用的中间件:
|
1384
|
+
|
1385
|
+
```ruby
|
1386
|
+
enable :sessions
|
1387
|
+
set :session_store, Rack::Session::Pool
|
1388
|
+
```
|
1389
|
+
|
1390
|
+
另一种选择是不要调用enable:sessions,而是像你想要的其他中间件一样加入你的中间件。
|
1391
|
+
|
1392
|
+
重要的是要注意,使用此方法时,默认情况下不会启用基于会话的保护。
|
1393
|
+
|
1394
|
+
还需要添加Rack中间件:
|
1395
|
+
|
1396
|
+
```ruby
|
1397
|
+
use Rack::Session::Pool, :expire_after => 2592000
|
1398
|
+
use Rack::Protection::RemoteToken
|
1399
|
+
use Rack::Protection::SessionHijacking
|
1400
|
+
```
|
1401
|
+
更多[安全防护配置](https://github.com/sinatra/sinatra#configuring-attack-protection)的信息。
|
1402
|
+
|
1341
1403
|
### 中断请求
|
1342
1404
|
|
1343
1405
|
要想在过滤器或路由中立即中断一个请求:
|
@@ -1965,7 +2027,7 @@ end
|
|
1965
2027
|
|
1966
2028
|
### 配置攻击防护
|
1967
2029
|
|
1968
|
-
Sinatra 使用 [Rack::Protection](https://github.com/sinatra/rack-protection#readme)
|
2030
|
+
Sinatra 使用 [Rack::Protection](https://github.com/sinatra/sinatra/tree/master/rack-protection#readme)
|
1969
2031
|
来抵御常见的攻击。你可以轻易地禁用该行为(但这会大大增加应用被攻击的概率)。
|
1970
2032
|
|
1971
2033
|
```ruby
|
data/Rakefile
CHANGED
@@ -30,7 +30,7 @@ end
|
|
30
30
|
|
31
31
|
Rake::TestTask.new(:test) do |t|
|
32
32
|
t.test_files = FileList['test/*_test.rb']
|
33
|
-
t.ruby_opts = ['-rubygems'] if defined? Gem
|
33
|
+
t.ruby_opts = ['-r rubygems'] if defined? Gem
|
34
34
|
t.ruby_opts << '-I.'
|
35
35
|
t.warning = true
|
36
36
|
end
|
@@ -41,7 +41,7 @@ Rake::TestTask.new(:"test:core") do |t|
|
|
41
41
|
readme request response result route_added_hook
|
42
42
|
routing server settings sinatra static templates]
|
43
43
|
t.test_files = core_tests.map {|n| "test/#{n}_test.rb"}
|
44
|
-
t.ruby_opts = ["-rubygems"] if defined? Gem
|
44
|
+
t.ruby_opts = ["-r rubygems"] if defined? Gem
|
45
45
|
t.ruby_opts << "-I."
|
46
46
|
t.warning = true
|
47
47
|
end
|
@@ -202,11 +202,14 @@ if defined?(Gem)
|
|
202
202
|
|
203
203
|
desc "Commits the version to github repository"
|
204
204
|
task :commit_version do
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
205
|
+
%w[
|
206
|
+
lib/sinatra
|
207
|
+
sinatra-contrib/lib/sinatra/contrib
|
208
|
+
rack-protection/lib/rack/protection
|
209
|
+
].each do |path|
|
210
|
+
path = File.join(path, 'version.rb')
|
211
|
+
File.write(path, File.read(path).sub(/VERSION = '(.+?)'/, "VERSION = '#{source_version}'"))
|
212
|
+
end
|
210
213
|
|
211
214
|
sh <<-SH
|
212
215
|
git commit --allow-empty -a -m '#{source_version} release' &&
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.0
|
data/examples/chat.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rainbows'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
module Handler
|
5
|
+
class Rainbows
|
6
|
+
def self.run(app, **options)
|
7
|
+
rainbows_options = {
|
8
|
+
listeners: ["#{options[:Host]}:#{options[:Port]}"],
|
9
|
+
worker_processes: 1,
|
10
|
+
timeout: 30,
|
11
|
+
config_file: ::File.expand_path('rainbows.conf', __dir__),
|
12
|
+
}
|
13
|
+
|
14
|
+
::Rainbows::HttpServer.new(app, rainbows_options).start.join
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
register :rainbows, ::Rack::Handler::Rainbows
|
19
|
+
end
|
20
|
+
end
|
data/examples/stream.ru
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
#
|
3
3
|
# run *one* of these:
|
4
4
|
#
|
5
|
-
# rackup -s mongrel stream.ru
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
5
|
+
# rackup -s mongrel stream.ru # gem install mongrel
|
6
|
+
# unicorn stream.ru # gem install unicorn
|
7
|
+
# puma stream.ru # gem install puma
|
8
|
+
# rainbows -c rainbows.conf stream.ru # gem install rainbows eventmachine
|
9
9
|
|
10
10
|
require 'sinatra/base'
|
11
11
|
|