roda 3.94.0 → 3.96.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 692e6feff78ea9a3c6562026fb6d5ea0e4b1f26cf76d9c283e3482296b4253f0
4
- data.tar.gz: 330f62ed0603dea2ef640a424c0d1286a312b90b65ca4087cdd6772f1206c151
3
+ metadata.gz: 5cd2f1819c84a5977f70830dd6188e00a2e93ba0002a7ab13108a3ac70b8fad4
4
+ data.tar.gz: 2c9ee53f9fc84598e40b33f6381e14727d568baf7f3bfb07b371565a2e439a92
5
5
  SHA512:
6
- metadata.gz: b87645bffa98a056fed1fc31b032a58be23a99ef202d4f91bb9f9d10f1d62e73ebe4ae4a5008fbe978d7abdb1768b22010cf61ab1d37b677718db9477d891842
7
- data.tar.gz: f2572f750dfcf59d1d563983c32d53246f5260d94124336332a8ae6c696f1ff4b18b269043e41105e10462ed4c5d4d1e66039291c60a80303b4a0a94fe602c0d
6
+ metadata.gz: 74520bc003b12e9098b05fdbe22aaa4945d0d9964827ea1e1ec26c669b9f58dff3374cad123cde2bd16de87148f4d11754ed980f79ba40d12ba413ecf83a801c
7
+ data.tar.gz: 0a57700f09fb3a61b091dd9c6c4150637ac5f0f6c86d888c4426ab6e4f4f70dc809aa134368d943f41d47421d4b441048c5ef35ac33ccb47fe90095d6fa640bb
@@ -0,0 +1,59 @@
1
+ # frozen-string-literal: true
2
+
3
+ #
4
+ class Roda
5
+ module RodaPlugins
6
+ # The redirect_path plugin builds on top of the path plugin,
7
+ # and allows the +r.redirect+ method to be passed a non-string
8
+ # object that will be passed to +path+, and redirect to the
9
+ # result of +path+.
10
+ #
11
+ # In the second argument, you can provide a suffix to the
12
+ # generated path. However, in this case you cannot provide a
13
+ # non-default redirect status in the same call).
14
+ #
15
+ # Example:
16
+ #
17
+ # Foo = Struct.new(:id)
18
+ # foo = Foo.new(1)
19
+ #
20
+ # plugin :path
21
+ # path Foo do |foo|
22
+ # "/foo/#{foo.id}"
23
+ # end
24
+ #
25
+ # route do |r|
26
+ # r.get "example" do
27
+ # # redirects to /foo/1
28
+ # r.redirect(foo)
29
+ # end
30
+ #
31
+ # r.get "suffix-example" do
32
+ # # redirects to /foo/1/status
33
+ # r.redirect(foo, "/status")
34
+ # end
35
+ # end
36
+ module RedirectPath
37
+ def self.load_dependencies(app)
38
+ app.plugin :path
39
+ end
40
+
41
+ module RequestMethods
42
+ def redirect(path=default_redirect_path, status=default_redirect_status)
43
+ if String === path
44
+ super
45
+ else
46
+ path = scope.path(path)
47
+ if status.is_a?(String)
48
+ super(path + status)
49
+ else
50
+ super
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ register_plugin(:redirect_path, RedirectPath)
58
+ end
59
+ end
@@ -0,0 +1,79 @@
1
+ # frozen-string-literal: true
2
+
3
+ #
4
+ class Roda
5
+ module RodaPlugins
6
+ # The response_content_type extension adds response.content_type
7
+ # and response.content_type= methods for getting and setting the
8
+ # response content-type.
9
+ #
10
+ # When setting the content-type, you can pass either a string, which
11
+ # is used directly:
12
+ #
13
+ # response.content_type = "text/html"
14
+ #
15
+ # Or, if you have registered mime types when loading the plugin:
16
+ #
17
+ # plugin :response_content_type, mime_types: {
18
+ # plain: "text/plain",
19
+ # html: "text/html",
20
+ # pdf: "application/pdf"
21
+ # }
22
+ #
23
+ # You can use a symbol:
24
+ #
25
+ # response.content_type = :html
26
+ #
27
+ # If you would like to load all mime types supported by rack/mime,
28
+ # you can use the <tt>mime_types: :from_rack_mime</tt> option:
29
+ #
30
+ # plugin :response_content_type, mime_types: :from_rack_mime
31
+ #
32
+ # Note that you are unlikely to be using all of these mime types,
33
+ # so doing this will likely result in unnecessary memory usage. It
34
+ # is recommended to use a hash with only the mime types your
35
+ # application actually uses.
36
+ #
37
+ # To prevent silent failures, if you attempt to set the response
38
+ # type with a symbol, and the symbol is not recognized, a KeyError
39
+ # is raised.
40
+ module ResponseContentType
41
+ def self.configure(app, opts=OPTS)
42
+ if mime_types = opts[:mime_types]
43
+ mime_types = if mime_types == :from_rack_mime
44
+ require "rack/mime"
45
+ h = {}
46
+ Rack::Mime::MIME_TYPES.each do |k, v|
47
+ h[k.slice(1,100).to_sym] = v
48
+ end
49
+ h
50
+ else
51
+ mime_types.dup
52
+ end
53
+ app.opts[:repsonse_content_types] = mime_types.freeze
54
+ else
55
+ app.opts[:repsonse_content_types] ||= {}
56
+ end
57
+ end
58
+
59
+ module ResponseMethods
60
+ # Return the content-type of the response. Will be nil if it has
61
+ # not yet been explicitly set.
62
+ def content_type
63
+ @headers[RodaResponseHeaders::CONTENT_TYPE]
64
+ end
65
+
66
+ # Set the content-type of the response. If given a string,
67
+ # it is used directly. If given a symbol, looks up the mime
68
+ # type with the given file extension. If the symbol is not
69
+ # a recognized mime type, raises KeyError.
70
+ def content_type=(mime_type)
71
+ mime_type = roda_class.opts[:repsonse_content_types].fetch(mime_type) if mime_type.is_a?(Symbol)
72
+ @headers[RodaResponseHeaders::CONTENT_TYPE] = mime_type
73
+ end
74
+ end
75
+ end
76
+
77
+ register_plugin(:response_content_type, ResponseContentType)
78
+ end
79
+ end
@@ -273,7 +273,29 @@ class Roda
273
273
  cookie = Hash[opts[:cookie_options]]
274
274
  cookie[:value] = cookie_value
275
275
  cookie[:secure] = true if !cookie.has_key?(:secure) && ssl?
276
+
277
+ before_size = if (set_cookie_before = headers[RodaResponseHeaders::SET_COOKIE]).is_a?(String)
278
+ set_cookie_before.bytesize
279
+ else
280
+ 0
281
+ end
282
+
276
283
  Rack::Utils.set_cookie_header!(headers, opts[:key], cookie)
284
+
285
+ cookie_size = case set_cookie_after = headers[RodaResponseHeaders::SET_COOKIE]
286
+ when String
287
+ # Rack < 3 always takes this branch, combines cookies into string, subtract previous size
288
+ # Rack 3+ takes this branch if this is the first cookie set, in which case before size is 0
289
+ set_cookie_after.bytesize - before_size
290
+ else # when Array
291
+ # Rack 3+ takes branch if this is not the first cookie set, and last element of the array
292
+ # is most recently added cookie
293
+ set_cookie_after.last.bytesize
294
+ end
295
+
296
+ if cookie_size >= 4096
297
+ raise CookieTooLarge, "attempted to create cookie larger than 4096 bytes (bytes: #{cookie_size})"
298
+ end
277
299
  end
278
300
 
279
301
  if env[SESSION_DELETE_RACK_COOKIE]
@@ -500,7 +522,7 @@ class Roda
500
522
  data = Base64_.urlsafe_encode64(data)
501
523
 
502
524
  if data.bytesize >= 4096
503
- raise CookieTooLarge, "attempted to create cookie larger than 4096 bytes"
525
+ raise CookieTooLarge, "attempted to create cookie larger than 4096 bytes (bytes: #{data.bytesize})"
504
526
  end
505
527
 
506
528
  data
@@ -7,8 +7,8 @@ class Roda
7
7
  module RodaPlugins
8
8
  # The typecast_params plugin allows for type conversion of submitted parameters.
9
9
  # Submitted parameters should be considered untrusted input, and in standard use
10
- # with browsers, parameters are # submitted as strings (or a hash/array containing
11
- # strings). In most # cases it makes sense to explicitly convert the parameter to the
10
+ # with browsers, parameters are submitted as strings (or a hash/array containing
11
+ # strings). In most cases it makes sense to explicitly convert the parameter to the
12
12
  # desired type. While this can be done via manual conversion:
13
13
  #
14
14
  # val = request.params['key'].to_i
@@ -1155,11 +1155,26 @@ class Roda
1155
1155
  end
1156
1156
 
1157
1157
  module InstanceMethods
1158
- # Return and cache the instance of the Params class for the current request.
1158
+ # Return and cache the instance of the TypecastParams class wrapping access
1159
+ # to the request's params (merging query string params and body params).
1159
1160
  # Type conversion methods will be called on the result of this method.
1160
1161
  def typecast_params
1161
1162
  @_typecast_params ||= self.class::TypecastParams.new(@_request.params)
1162
1163
  end
1164
+
1165
+ # Return and cache the instance of the TypecastParams class wrapping
1166
+ # access to parameters in the request's query string.
1167
+ # Type conversion methods will be called on the result of this method.
1168
+ def typecast_query_params
1169
+ @_typecast_query_params ||= self.class::TypecastParams.new(@_request.GET)
1170
+ end
1171
+
1172
+ # Return and cache the instance of the TypecastParams class wrapping
1173
+ # access to parameters in the request's body.
1174
+ # Type conversion methods will be called on the result of this method.
1175
+ def typecast_body_params
1176
+ @_typecast_body_params ||= self.class::TypecastParams.new(@_request.POST)
1177
+ end
1163
1178
  end
1164
1179
  end
1165
1180
 
data/lib/roda/version.rb CHANGED
@@ -4,7 +4,7 @@ class Roda
4
4
  RodaMajorVersion = 3
5
5
 
6
6
  # The minor version of Roda, updated for new feature releases of Roda.
7
- RodaMinorVersion = 94
7
+ RodaMinorVersion = 96
8
8
 
9
9
  # The patch version of Roda, updated only for bug fixes from the last
10
10
  # feature release.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.94.0
4
+ version: 3.96.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
@@ -268,6 +268,7 @@ files:
268
268
  - lib/roda/plugins/r.rb
269
269
  - lib/roda/plugins/recheck_precompiled_assets.rb
270
270
  - lib/roda/plugins/redirect_http_to_https.rb
271
+ - lib/roda/plugins/redirect_path.rb
271
272
  - lib/roda/plugins/relative_path.rb
272
273
  - lib/roda/plugins/render.rb
273
274
  - lib/roda/plugins/render_coverage.rb
@@ -275,6 +276,7 @@ files:
275
276
  - lib/roda/plugins/render_locals.rb
276
277
  - lib/roda/plugins/request_aref.rb
277
278
  - lib/roda/plugins/request_headers.rb
279
+ - lib/roda/plugins/response_content_type.rb
278
280
  - lib/roda/plugins/response_request.rb
279
281
  - lib/roda/plugins/route_block_args.rb
280
282
  - lib/roda/plugins/route_csrf.rb
@@ -328,7 +330,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
328
330
  - !ruby/object:Gem::Version
329
331
  version: '0'
330
332
  requirements: []
331
- rubygems_version: 3.6.7
333
+ rubygems_version: 3.6.9
332
334
  specification_version: 4
333
335
  summary: Routing tree web toolkit
334
336
  test_files: []