diy_rails 0.1.0 → 0.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.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +5 -2
  3. data/lib/diy_rails/array.rb +5 -0
  4. data/lib/diy_rails/routing.rb +10 -0
  5. data/lib/diy_rails/version.rb +1 -1
  6. data/lib/diy_rails.rb +25 -3
  7. metadata +28 -151
  8. data/vendor/bundle/ruby/3.0.0/bin/rackup +0 -29
  9. data/vendor/bundle/ruby/3.0.0/bin/rake +0 -29
  10. data/vendor/bundle/ruby/3.0.0/cache/rack-2.2.4.gem +0 -0
  11. data/vendor/bundle/ruby/3.0.0/cache/rake-13.0.6.gem +0 -0
  12. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/CHANGELOG.md +0 -708
  13. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/CONTRIBUTING.md +0 -136
  14. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/MIT-LICENSE +0 -20
  15. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/README.rdoc +0 -306
  16. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/Rakefile +0 -130
  17. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/SPEC.rdoc +0 -288
  18. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/bin/rackup +0 -5
  19. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/contrib/rack.png +0 -0
  20. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/contrib/rack.svg +0 -150
  21. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/contrib/rack_logo.svg +0 -164
  22. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/contrib/rdoc.css +0 -412
  23. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/example/lobster.ru +0 -6
  24. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/example/protectedlobster.rb +0 -16
  25. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/example/protectedlobster.ru +0 -10
  26. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/abstract/handler.rb +0 -39
  27. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/abstract/request.rb +0 -47
  28. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/basic.rb +0 -61
  29. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/digest/md5.rb +0 -131
  30. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/digest/nonce.rb +0 -54
  31. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/digest/params.rb +0 -54
  32. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/digest/request.rb +0 -43
  33. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/body_proxy.rb +0 -45
  34. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/builder.rb +0 -257
  35. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/cascade.rb +0 -68
  36. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/chunked.rb +0 -117
  37. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/common_logger.rb +0 -83
  38. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/conditional_get.rb +0 -83
  39. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/config.rb +0 -22
  40. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/content_length.rb +0 -38
  41. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/content_type.rb +0 -30
  42. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/core_ext/regexp.rb +0 -14
  43. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/deflater.rb +0 -144
  44. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/directory.rb +0 -199
  45. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/etag.rb +0 -77
  46. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/events.rb +0 -153
  47. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/file.rb +0 -7
  48. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/files.rb +0 -218
  49. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/cgi.rb +0 -59
  50. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/fastcgi.rb +0 -100
  51. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/lsws.rb +0 -61
  52. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/scgi.rb +0 -71
  53. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/thin.rb +0 -36
  54. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/webrick.rb +0 -129
  55. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler.rb +0 -104
  56. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/head.rb +0 -25
  57. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/lint.rb +0 -806
  58. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/lobster.rb +0 -70
  59. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/lock.rb +0 -32
  60. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/logger.rb +0 -20
  61. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/media_type.rb +0 -43
  62. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/method_override.rb +0 -52
  63. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/mime.rb +0 -685
  64. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/mock.rb +0 -273
  65. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/multipart/generator.rb +0 -97
  66. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/multipart/parser.rb +0 -365
  67. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/multipart/uploaded_file.rb +0 -41
  68. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/multipart.rb +0 -64
  69. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/null_logger.rb +0 -39
  70. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/query_parser.rb +0 -221
  71. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/recursive.rb +0 -64
  72. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/reloader.rb +0 -114
  73. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/request.rb +0 -659
  74. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/response.rb +0 -318
  75. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/rewindable_input.rb +0 -94
  76. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/runtime.rb +0 -34
  77. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/sendfile.rb +0 -162
  78. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/server.rb +0 -466
  79. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/session/abstract/id.rb +0 -523
  80. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/session/cookie.rb +0 -203
  81. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/session/memcache.rb +0 -10
  82. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/session/pool.rb +0 -85
  83. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/show_exceptions.rb +0 -390
  84. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/show_status.rb +0 -113
  85. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/static.rb +0 -187
  86. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/tempfile_reaper.rb +0 -22
  87. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/urlmap.rb +0 -97
  88. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/utils.rb +0 -616
  89. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/version.rb +0 -29
  90. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack.rb +0 -141
  91. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/rack.gemspec +0 -46
  92. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/History.rdoc +0 -2403
  93. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/MIT-LICENSE +0 -21
  94. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/README.rdoc +0 -155
  95. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/command_line_usage.rdoc +0 -158
  96. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/example/Rakefile1 +0 -38
  97. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/example/Rakefile2 +0 -35
  98. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/example/a.c +0 -6
  99. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/example/b.c +0 -6
  100. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/example/main.c +0 -11
  101. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/glossary.rdoc +0 -42
  102. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/jamis.rb +0 -592
  103. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/proto_rake.rdoc +0 -127
  104. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/rake.1 +0 -156
  105. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/rakefile.rdoc +0 -622
  106. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/rational.rdoc +0 -151
  107. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/exe/rake +0 -27
  108. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/application.rb +0 -831
  109. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/backtrace.rb +0 -24
  110. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/clean.rb +0 -78
  111. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/cloneable.rb +0 -17
  112. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/cpu_counter.rb +0 -107
  113. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/default_loader.rb +0 -15
  114. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/dsl_definition.rb +0 -195
  115. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/early_time.rb +0 -22
  116. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/ext/core.rb +0 -26
  117. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/ext/string.rb +0 -176
  118. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/file_creation_task.rb +0 -25
  119. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/file_list.rb +0 -435
  120. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/file_task.rb +0 -54
  121. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/file_utils.rb +0 -134
  122. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/file_utils_ext.rb +0 -134
  123. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/invocation_chain.rb +0 -57
  124. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/invocation_exception_mixin.rb +0 -17
  125. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/late_time.rb +0 -18
  126. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/linked_list.rb +0 -112
  127. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/loaders/makefile.rb +0 -54
  128. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/multi_task.rb +0 -14
  129. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/name_space.rb +0 -38
  130. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/packagetask.rb +0 -222
  131. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/phony.rb +0 -16
  132. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/private_reader.rb +0 -21
  133. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/promise.rb +0 -100
  134. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/pseudo_status.rb +0 -30
  135. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/rake_module.rb +0 -67
  136. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/rake_test_loader.rb +0 -27
  137. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/rule_recursion_overflow_error.rb +0 -20
  138. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/scope.rb +0 -43
  139. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/task.rb +0 -434
  140. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/task_argument_error.rb +0 -8
  141. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/task_arguments.rb +0 -109
  142. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/task_manager.rb +0 -331
  143. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/tasklib.rb +0 -12
  144. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/testtask.rb +0 -189
  145. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/thread_history_display.rb +0 -49
  146. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/thread_pool.rb +0 -163
  147. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/trace_output.rb +0 -23
  148. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/version.rb +0 -10
  149. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/win32.rb +0 -51
  150. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake.rb +0 -71
  151. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/rake.gemspec +0 -100
  152. data/vendor/bundle/ruby/3.0.0/specifications/rack-2.2.4.gemspec +0 -41
  153. data/vendor/bundle/ruby/3.0.0/specifications/rake-13.0.6.gemspec +0 -26
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'digest/md5'
4
- require 'base64'
5
-
6
- module Rack
7
- module Auth
8
- module Digest
9
- # Rack::Auth::Digest::Nonce is the default nonce generator for the
10
- # Rack::Auth::Digest::MD5 authentication handler.
11
- #
12
- # +private_key+ needs to set to a constant string.
13
- #
14
- # +time_limit+ can be optionally set to an integer (number of seconds),
15
- # to limit the validity of the generated nonces.
16
-
17
- class Nonce
18
-
19
- class << self
20
- attr_accessor :private_key, :time_limit
21
- end
22
-
23
- def self.parse(string)
24
- new(*Base64.decode64(string).split(' ', 2))
25
- end
26
-
27
- def initialize(timestamp = Time.now, given_digest = nil)
28
- @timestamp, @given_digest = timestamp.to_i, given_digest
29
- end
30
-
31
- def to_s
32
- Base64.encode64("#{@timestamp} #{digest}").strip
33
- end
34
-
35
- def digest
36
- ::Digest::MD5.hexdigest("#{@timestamp}:#{self.class.private_key}")
37
- end
38
-
39
- def valid?
40
- digest == @given_digest
41
- end
42
-
43
- def stale?
44
- !self.class.time_limit.nil? && (Time.now.to_i - @timestamp) > self.class.time_limit
45
- end
46
-
47
- def fresh?
48
- !stale?
49
- end
50
-
51
- end
52
- end
53
- end
54
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rack
4
- module Auth
5
- module Digest
6
- class Params < Hash
7
-
8
- def self.parse(str)
9
- Params[*split_header_value(str).map do |param|
10
- k, v = param.split('=', 2)
11
- [k, dequote(v)]
12
- end.flatten]
13
- end
14
-
15
- def self.dequote(str) # From WEBrick::HTTPUtils
16
- ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
17
- ret.gsub!(/\\(.)/, "\\1")
18
- ret
19
- end
20
-
21
- def self.split_header_value(str)
22
- str.scan(/\w+\=(?:"[^\"]+"|[^,]+)/n)
23
- end
24
-
25
- def initialize
26
- super()
27
-
28
- yield self if block_given?
29
- end
30
-
31
- def [](k)
32
- super k.to_s
33
- end
34
-
35
- def []=(k, v)
36
- super k.to_s, v.to_s
37
- end
38
-
39
- UNQUOTED = ['nc', 'stale']
40
-
41
- def to_s
42
- map do |k, v|
43
- "#{k}=#{(UNQUOTED.include?(k) ? v.to_s : quote(v))}"
44
- end.join(', ')
45
- end
46
-
47
- def quote(str) # From WEBrick::HTTPUtils
48
- '"' + str.gsub(/[\\\"]/o, "\\\1") + '"'
49
- end
50
-
51
- end
52
- end
53
- end
54
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../abstract/request'
4
- require_relative 'params'
5
- require_relative 'nonce'
6
-
7
- module Rack
8
- module Auth
9
- module Digest
10
- class Request < Auth::AbstractRequest
11
- def method
12
- @env[RACK_METHODOVERRIDE_ORIGINAL_METHOD] || @env[REQUEST_METHOD]
13
- end
14
-
15
- def digest?
16
- "digest" == scheme
17
- end
18
-
19
- def correct_uri?
20
- request.fullpath == uri
21
- end
22
-
23
- def nonce
24
- @nonce ||= Nonce.parse(params['nonce'])
25
- end
26
-
27
- def params
28
- @params ||= Params.parse(parts.last)
29
- end
30
-
31
- def respond_to?(sym, *)
32
- super or params.has_key? sym.to_s
33
- end
34
-
35
- def method_missing(sym, *args)
36
- return super unless params.has_key?(key = sym.to_s)
37
- return params[key] if args.size == 0
38
- raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
39
- end
40
- end
41
- end
42
- end
43
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rack
4
- # Proxy for response bodies allowing calling a block when
5
- # the response body is closed (after the response has been fully
6
- # sent to the client).
7
- class BodyProxy
8
- # Set the response body to wrap, and the block to call when the
9
- # response has been fully sent.
10
- def initialize(body, &block)
11
- @body = body
12
- @block = block
13
- @closed = false
14
- end
15
-
16
- # Return whether the wrapped body responds to the method.
17
- def respond_to_missing?(method_name, include_all = false)
18
- super or @body.respond_to?(method_name, include_all)
19
- end
20
-
21
- # If not already closed, close the wrapped body and
22
- # then call the block the proxy was initialized with.
23
- def close
24
- return if @closed
25
- @closed = true
26
- begin
27
- @body.close if @body.respond_to? :close
28
- ensure
29
- @block.call
30
- end
31
- end
32
-
33
- # Whether the proxy is closed. The proxy starts as not closed,
34
- # and becomes closed on the first call to close.
35
- def closed?
36
- @closed
37
- end
38
-
39
- # Delegate missing methods to the wrapped body.
40
- def method_missing(method_name, *args, &block)
41
- @body.__send__(method_name, *args, &block)
42
- end
43
- ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
44
- end
45
- end
@@ -1,257 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rack
4
- # Rack::Builder implements a small DSL to iteratively construct Rack
5
- # applications.
6
- #
7
- # Example:
8
- #
9
- # require 'rack/lobster'
10
- # app = Rack::Builder.new do
11
- # use Rack::CommonLogger
12
- # use Rack::ShowExceptions
13
- # map "/lobster" do
14
- # use Rack::Lint
15
- # run Rack::Lobster.new
16
- # end
17
- # end
18
- #
19
- # run app
20
- #
21
- # Or
22
- #
23
- # app = Rack::Builder.app do
24
- # use Rack::CommonLogger
25
- # run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['OK']] }
26
- # end
27
- #
28
- # run app
29
- #
30
- # +use+ adds middleware to the stack, +run+ dispatches to an application.
31
- # You can use +map+ to construct a Rack::URLMap in a convenient way.
32
-
33
- class Builder
34
-
35
- # https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-without-bom
36
- UTF_8_BOM = '\xef\xbb\xbf'
37
-
38
- # Parse the given config file to get a Rack application.
39
- #
40
- # If the config file ends in +.ru+, it is treated as a
41
- # rackup file and the contents will be treated as if
42
- # specified inside a Rack::Builder block, using the given
43
- # options.
44
- #
45
- # If the config file does not end in +.ru+, it is
46
- # required and Rack will use the basename of the file
47
- # to guess which constant will be the Rack application to run.
48
- # The options given will be ignored in this case.
49
- #
50
- # Examples:
51
- #
52
- # Rack::Builder.parse_file('config.ru')
53
- # # Rack application built using Rack::Builder.new
54
- #
55
- # Rack::Builder.parse_file('app.rb')
56
- # # requires app.rb, which can be anywhere in Ruby's
57
- # # load path. After requiring, assumes App constant
58
- # # contains Rack application
59
- #
60
- # Rack::Builder.parse_file('./my_app.rb')
61
- # # requires ./my_app.rb, which should be in the
62
- # # process's current directory. After requiring,
63
- # # assumes MyApp constant contains Rack application
64
- def self.parse_file(config, opts = Server::Options.new)
65
- if config.end_with?('.ru')
66
- return self.load_file(config, opts)
67
- else
68
- require config
69
- app = Object.const_get(::File.basename(config, '.rb').split('_').map(&:capitalize).join(''))
70
- return app, {}
71
- end
72
- end
73
-
74
- # Load the given file as a rackup file, treating the
75
- # contents as if specified inside a Rack::Builder block.
76
- #
77
- # Treats the first comment at the beginning of a line
78
- # that starts with a backslash as options similar to
79
- # options passed on a rackup command line.
80
- #
81
- # Ignores content in the file after +__END__+, so that
82
- # use of +__END__+ will not result in a syntax error.
83
- #
84
- # Example config.ru file:
85
- #
86
- # $ cat config.ru
87
- #
88
- # #\ -p 9393
89
- #
90
- # use Rack::ContentLength
91
- # require './app.rb'
92
- # run App
93
- def self.load_file(path, opts = Server::Options.new)
94
- options = {}
95
-
96
- cfgfile = ::File.read(path)
97
- cfgfile.slice!(/\A#{UTF_8_BOM}/) if cfgfile.encoding == Encoding::UTF_8
98
-
99
- if cfgfile[/^#\\(.*)/] && opts
100
- warn "Parsing options from the first comment line is deprecated!"
101
- options = opts.parse! $1.split(/\s+/)
102
- end
103
-
104
- cfgfile.sub!(/^__END__\n.*\Z/m, '')
105
- app = new_from_string cfgfile, path
106
-
107
- return app, options
108
- end
109
-
110
- # Evaluate the given +builder_script+ string in the context of
111
- # a Rack::Builder block, returning a Rack application.
112
- def self.new_from_string(builder_script, file = "(rackup)")
113
- # We want to build a variant of TOPLEVEL_BINDING with self as a Rack::Builder instance.
114
- # We cannot use instance_eval(String) as that would resolve constants differently.
115
- binding, builder = TOPLEVEL_BINDING.eval('Rack::Builder.new.instance_eval { [binding, self] }')
116
- eval builder_script, binding, file
117
- builder.to_app
118
- end
119
-
120
- # Initialize a new Rack::Builder instance. +default_app+ specifies the
121
- # default application if +run+ is not called later. If a block
122
- # is given, it is evaluted in the context of the instance.
123
- def initialize(default_app = nil, &block)
124
- @use, @map, @run, @warmup, @freeze_app = [], nil, default_app, nil, false
125
- instance_eval(&block) if block_given?
126
- end
127
-
128
- # Create a new Rack::Builder instance and return the Rack application
129
- # generated from it.
130
- def self.app(default_app = nil, &block)
131
- self.new(default_app, &block).to_app
132
- end
133
-
134
- # Specifies middleware to use in a stack.
135
- #
136
- # class Middleware
137
- # def initialize(app)
138
- # @app = app
139
- # end
140
- #
141
- # def call(env)
142
- # env["rack.some_header"] = "setting an example"
143
- # @app.call(env)
144
- # end
145
- # end
146
- #
147
- # use Middleware
148
- # run lambda { |env| [200, { "Content-Type" => "text/plain" }, ["OK"]] }
149
- #
150
- # All requests through to this application will first be processed by the middleware class.
151
- # The +call+ method in this example sets an additional environment key which then can be
152
- # referenced in the application if required.
153
- def use(middleware, *args, &block)
154
- if @map
155
- mapping, @map = @map, nil
156
- @use << proc { |app| generate_map(app, mapping) }
157
- end
158
- @use << proc { |app| middleware.new(app, *args, &block) }
159
- end
160
- ruby2_keywords(:use) if respond_to?(:ruby2_keywords, true)
161
-
162
- # Takes an argument that is an object that responds to #call and returns a Rack response.
163
- # The simplest form of this is a lambda object:
164
- #
165
- # run lambda { |env| [200, { "Content-Type" => "text/plain" }, ["OK"]] }
166
- #
167
- # However this could also be a class:
168
- #
169
- # class Heartbeat
170
- # def self.call(env)
171
- # [200, { "Content-Type" => "text/plain" }, ["OK"]]
172
- # end
173
- # end
174
- #
175
- # run Heartbeat
176
- def run(app)
177
- @run = app
178
- end
179
-
180
- # Takes a lambda or block that is used to warm-up the application. This block is called
181
- # before the Rack application is returned by to_app.
182
- #
183
- # warmup do |app|
184
- # client = Rack::MockRequest.new(app)
185
- # client.get('/')
186
- # end
187
- #
188
- # use SomeMiddleware
189
- # run MyApp
190
- def warmup(prc = nil, &block)
191
- @warmup = prc || block
192
- end
193
-
194
- # Creates a route within the application. Routes under the mapped path will be sent to
195
- # the Rack application specified by run inside the block. Other requests will be sent to the
196
- # default application specified by run outside the block.
197
- #
198
- # Rack::Builder.app do
199
- # map '/heartbeat' do
200
- # run Heartbeat
201
- # end
202
- # run App
203
- # end
204
- #
205
- # The +use+ method can also be used inside the block to specify middleware to run under a specific path:
206
- #
207
- # Rack::Builder.app do
208
- # map '/heartbeat' do
209
- # use Middleware
210
- # run Heartbeat
211
- # end
212
- # run App
213
- # end
214
- #
215
- # This example includes a piece of middleware which will run before +/heartbeat+ requests hit +Heartbeat+.
216
- #
217
- # Note that providing a +path+ of +/+ will ignore any default application given in a +run+ statement
218
- # outside the block.
219
- def map(path, &block)
220
- @map ||= {}
221
- @map[path] = block
222
- end
223
-
224
- # Freeze the app (set using run) and all middleware instances when building the application
225
- # in to_app.
226
- def freeze_app
227
- @freeze_app = true
228
- end
229
-
230
- # Return the Rack application generated by this instance.
231
- def to_app
232
- app = @map ? generate_map(@run, @map) : @run
233
- fail "missing run or map statement" unless app
234
- app.freeze if @freeze_app
235
- app = @use.reverse.inject(app) { |a, e| e[a].tap { |x| x.freeze if @freeze_app } }
236
- @warmup.call(app) if @warmup
237
- app
238
- end
239
-
240
- # Call the Rack application generated by this builder instance. Note that
241
- # this rebuilds the Rack application and runs the warmup code (if any)
242
- # every time it is called, so it should not be used if performance is important.
243
- def call(env)
244
- to_app.call(env)
245
- end
246
-
247
- private
248
-
249
- # Generate a URLMap instance by generating new Rack applications for each
250
- # map block in this instance.
251
- def generate_map(default_app, mapping)
252
- mapped = default_app ? { '/' => default_app } : {}
253
- mapping.each { |r, b| mapped[r] = self.class.new(default_app, &b).to_app }
254
- URLMap.new(mapped)
255
- end
256
- end
257
- end
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rack
4
- # Rack::Cascade tries a request on several apps, and returns the
5
- # first response that is not 404 or 405 (or in a list of configured
6
- # status codes). If all applications tried return one of the configured
7
- # status codes, return the last response.
8
-
9
- class Cascade
10
- # deprecated, no longer used
11
- NotFound = [404, { CONTENT_TYPE => "text/plain" }, []]
12
-
13
- # An array of applications to try in order.
14
- attr_reader :apps
15
-
16
- # Set the apps to send requests to, and what statuses result in
17
- # cascading. Arguments:
18
- #
19
- # apps: An enumerable of rack applications.
20
- # cascade_for: The statuses to use cascading for. If a response is received
21
- # from an app, the next app is tried.
22
- def initialize(apps, cascade_for = [404, 405])
23
- @apps = []
24
- apps.each { |app| add app }
25
-
26
- @cascade_for = {}
27
- [*cascade_for].each { |status| @cascade_for[status] = true }
28
- end
29
-
30
- # Call each app in order. If the responses uses a status that requires
31
- # cascading, try the next app. If all responses require cascading,
32
- # return the response from the last app.
33
- def call(env)
34
- return [404, { CONTENT_TYPE => "text/plain" }, []] if @apps.empty?
35
- result = nil
36
- last_body = nil
37
-
38
- @apps.each do |app|
39
- # The SPEC says that the body must be closed after it has been iterated
40
- # by the server, or if it is replaced by a middleware action. Cascade
41
- # replaces the body each time a cascade happens. It is assumed that nil
42
- # does not respond to close, otherwise the previous application body
43
- # will be closed. The final application body will not be closed, as it
44
- # will be passed to the server as a result.
45
- last_body.close if last_body.respond_to? :close
46
-
47
- result = app.call(env)
48
- return result unless @cascade_for.include?(result[0].to_i)
49
- last_body = result[2]
50
- end
51
-
52
- result
53
- end
54
-
55
- # Append an app to the list of apps to cascade. This app will
56
- # be tried last.
57
- def add(app)
58
- @apps << app
59
- end
60
-
61
- # Whether the given app is one of the apps to cascade to.
62
- def include?(app)
63
- @apps.include?(app)
64
- end
65
-
66
- alias_method :<<, :add
67
- end
68
- end
@@ -1,117 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rack
4
-
5
- # Middleware that applies chunked transfer encoding to response bodies
6
- # when the response does not include a Content-Length header.
7
- #
8
- # This supports the Trailer response header to allow the use of trailing
9
- # headers in the chunked encoding. However, using this requires you manually
10
- # specify a response body that supports a +trailers+ method. Example:
11
- #
12
- # [200, { 'Trailer' => 'Expires'}, ["Hello", "World"]]
13
- # # error raised
14
- #
15
- # body = ["Hello", "World"]
16
- # def body.trailers
17
- # { 'Expires' => Time.now.to_s }
18
- # end
19
- # [200, { 'Trailer' => 'Expires'}, body]
20
- # # No exception raised
21
- class Chunked
22
- include Rack::Utils
23
-
24
- # A body wrapper that emits chunked responses.
25
- class Body
26
- TERM = "\r\n"
27
- TAIL = "0#{TERM}"
28
-
29
- # Store the response body to be chunked.
30
- def initialize(body)
31
- @body = body
32
- end
33
-
34
- # For each element yielded by the response body, yield
35
- # the element in chunked encoding.
36
- def each(&block)
37
- term = TERM
38
- @body.each do |chunk|
39
- size = chunk.bytesize
40
- next if size == 0
41
-
42
- yield [size.to_s(16), term, chunk.b, term].join
43
- end
44
- yield TAIL
45
- yield_trailers(&block)
46
- yield term
47
- end
48
-
49
- # Close the response body if the response body supports it.
50
- def close
51
- @body.close if @body.respond_to?(:close)
52
- end
53
-
54
- private
55
-
56
- # Do nothing as this class does not support trailer headers.
57
- def yield_trailers
58
- end
59
- end
60
-
61
- # A body wrapper that emits chunked responses and also supports
62
- # sending Trailer headers. Note that the response body provided to
63
- # initialize must have a +trailers+ method that returns a hash
64
- # of trailer headers, and the rack response itself should have a
65
- # Trailer header listing the headers that the +trailers+ method
66
- # will return.
67
- class TrailerBody < Body
68
- private
69
-
70
- # Yield strings for each trailer header.
71
- def yield_trailers
72
- @body.trailers.each_pair do |k, v|
73
- yield "#{k}: #{v}\r\n"
74
- end
75
- end
76
- end
77
-
78
- def initialize(app)
79
- @app = app
80
- end
81
-
82
- # Whether the HTTP version supports chunked encoding (HTTP 1.1 does).
83
- def chunkable_version?(ver)
84
- case ver
85
- # pre-HTTP/1.0 (informally "HTTP/0.9") HTTP requests did not have
86
- # a version (nor response headers)
87
- when 'HTTP/1.0', nil, 'HTTP/0.9'
88
- false
89
- else
90
- true
91
- end
92
- end
93
-
94
- # If the rack app returns a response that should have a body,
95
- # but does not have Content-Length or Transfer-Encoding headers,
96
- # modify the response to use chunked Transfer-Encoding.
97
- def call(env)
98
- status, headers, body = @app.call(env)
99
- headers = HeaderHash[headers]
100
-
101
- if chunkable_version?(env[SERVER_PROTOCOL]) &&
102
- !STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) &&
103
- !headers[CONTENT_LENGTH] &&
104
- !headers[TRANSFER_ENCODING]
105
-
106
- headers[TRANSFER_ENCODING] = 'chunked'
107
- if headers['Trailer']
108
- body = TrailerBody.new(body)
109
- else
110
- body = Body.new(body)
111
- end
112
- end
113
-
114
- [status, headers, body]
115
- end
116
- end
117
- end