diy_rails 0.1.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 (155) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +8 -0
  3. data/Gemfile.lock +21 -0
  4. data/README.md +29 -0
  5. data/Rakefile +4 -0
  6. data/lib/diy_rails/version.rb +5 -0
  7. data/lib/diy_rails.rb +8 -0
  8. data/sig/diy_rails.rbs +4 -0
  9. data/vendor/bundle/ruby/3.0.0/bin/rackup +29 -0
  10. data/vendor/bundle/ruby/3.0.0/bin/rake +29 -0
  11. data/vendor/bundle/ruby/3.0.0/cache/rack-2.2.4.gem +0 -0
  12. data/vendor/bundle/ruby/3.0.0/cache/rake-13.0.6.gem +0 -0
  13. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/CHANGELOG.md +708 -0
  14. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/CONTRIBUTING.md +136 -0
  15. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/MIT-LICENSE +20 -0
  16. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/README.rdoc +306 -0
  17. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/Rakefile +130 -0
  18. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/SPEC.rdoc +288 -0
  19. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/bin/rackup +5 -0
  20. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/contrib/rack.png +0 -0
  21. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/contrib/rack.svg +150 -0
  22. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/contrib/rack_logo.svg +164 -0
  23. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/contrib/rdoc.css +412 -0
  24. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/example/lobster.ru +6 -0
  25. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/example/protectedlobster.rb +16 -0
  26. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/example/protectedlobster.ru +10 -0
  27. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/abstract/handler.rb +39 -0
  28. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/abstract/request.rb +47 -0
  29. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/basic.rb +61 -0
  30. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/digest/md5.rb +131 -0
  31. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/digest/nonce.rb +54 -0
  32. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/digest/params.rb +54 -0
  33. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/digest/request.rb +43 -0
  34. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/body_proxy.rb +45 -0
  35. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/builder.rb +257 -0
  36. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/cascade.rb +68 -0
  37. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/chunked.rb +117 -0
  38. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/common_logger.rb +83 -0
  39. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/conditional_get.rb +83 -0
  40. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/config.rb +22 -0
  41. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/content_length.rb +38 -0
  42. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/content_type.rb +30 -0
  43. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/core_ext/regexp.rb +14 -0
  44. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/deflater.rb +144 -0
  45. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/directory.rb +199 -0
  46. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/etag.rb +77 -0
  47. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/events.rb +153 -0
  48. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/file.rb +7 -0
  49. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/files.rb +218 -0
  50. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/cgi.rb +59 -0
  51. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/fastcgi.rb +100 -0
  52. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/lsws.rb +61 -0
  53. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/scgi.rb +71 -0
  54. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/thin.rb +36 -0
  55. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler/webrick.rb +129 -0
  56. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/handler.rb +104 -0
  57. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/head.rb +25 -0
  58. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/lint.rb +806 -0
  59. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/lobster.rb +70 -0
  60. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/lock.rb +32 -0
  61. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/logger.rb +20 -0
  62. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/media_type.rb +43 -0
  63. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/method_override.rb +52 -0
  64. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/mime.rb +685 -0
  65. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/mock.rb +273 -0
  66. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/multipart/generator.rb +97 -0
  67. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/multipart/parser.rb +365 -0
  68. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/multipart/uploaded_file.rb +41 -0
  69. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/multipart.rb +64 -0
  70. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/null_logger.rb +39 -0
  71. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/query_parser.rb +221 -0
  72. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/recursive.rb +64 -0
  73. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/reloader.rb +114 -0
  74. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/request.rb +659 -0
  75. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/response.rb +318 -0
  76. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/rewindable_input.rb +94 -0
  77. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/runtime.rb +34 -0
  78. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/sendfile.rb +162 -0
  79. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/server.rb +466 -0
  80. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/session/abstract/id.rb +523 -0
  81. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/session/cookie.rb +203 -0
  82. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/session/memcache.rb +10 -0
  83. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/session/pool.rb +85 -0
  84. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/show_exceptions.rb +390 -0
  85. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/show_status.rb +113 -0
  86. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/static.rb +187 -0
  87. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/tempfile_reaper.rb +22 -0
  88. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/urlmap.rb +97 -0
  89. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/utils.rb +616 -0
  90. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/version.rb +29 -0
  91. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack.rb +141 -0
  92. data/vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/rack.gemspec +46 -0
  93. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/History.rdoc +2403 -0
  94. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/MIT-LICENSE +21 -0
  95. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/README.rdoc +155 -0
  96. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/command_line_usage.rdoc +158 -0
  97. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/example/Rakefile1 +38 -0
  98. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/example/Rakefile2 +35 -0
  99. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/example/a.c +6 -0
  100. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/example/b.c +6 -0
  101. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/example/main.c +11 -0
  102. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/glossary.rdoc +42 -0
  103. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/jamis.rb +592 -0
  104. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/proto_rake.rdoc +127 -0
  105. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/rake.1 +156 -0
  106. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/rakefile.rdoc +622 -0
  107. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/doc/rational.rdoc +151 -0
  108. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/exe/rake +27 -0
  109. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/application.rb +831 -0
  110. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/backtrace.rb +24 -0
  111. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/clean.rb +78 -0
  112. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/cloneable.rb +17 -0
  113. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/cpu_counter.rb +107 -0
  114. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/default_loader.rb +15 -0
  115. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/dsl_definition.rb +195 -0
  116. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/early_time.rb +22 -0
  117. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/ext/core.rb +26 -0
  118. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/ext/string.rb +176 -0
  119. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/file_creation_task.rb +25 -0
  120. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/file_list.rb +435 -0
  121. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/file_task.rb +54 -0
  122. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/file_utils.rb +134 -0
  123. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/file_utils_ext.rb +134 -0
  124. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/invocation_chain.rb +57 -0
  125. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/invocation_exception_mixin.rb +17 -0
  126. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/late_time.rb +18 -0
  127. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/linked_list.rb +112 -0
  128. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/loaders/makefile.rb +54 -0
  129. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/multi_task.rb +14 -0
  130. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/name_space.rb +38 -0
  131. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/packagetask.rb +222 -0
  132. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/phony.rb +16 -0
  133. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/private_reader.rb +21 -0
  134. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/promise.rb +100 -0
  135. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/pseudo_status.rb +30 -0
  136. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/rake_module.rb +67 -0
  137. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/rake_test_loader.rb +27 -0
  138. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/rule_recursion_overflow_error.rb +20 -0
  139. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/scope.rb +43 -0
  140. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/task.rb +434 -0
  141. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/task_argument_error.rb +8 -0
  142. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/task_arguments.rb +109 -0
  143. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/task_manager.rb +331 -0
  144. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/tasklib.rb +12 -0
  145. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/testtask.rb +189 -0
  146. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/thread_history_display.rb +49 -0
  147. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/thread_pool.rb +163 -0
  148. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/trace_output.rb +23 -0
  149. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/version.rb +10 -0
  150. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake/win32.rb +51 -0
  151. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/lib/rake.rb +71 -0
  152. data/vendor/bundle/ruby/3.0.0/gems/rake-13.0.6/rake.gemspec +100 -0
  153. data/vendor/bundle/ruby/3.0.0/specifications/rack-2.2.4.gemspec +41 -0
  154. data/vendor/bundle/ruby/3.0.0/specifications/rake-13.0.6.gemspec +26 -0
  155. metadata +210 -0
@@ -0,0 +1,659 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rack
4
+ # Rack::Request provides a convenient interface to a Rack
5
+ # environment. It is stateless, the environment +env+ passed to the
6
+ # constructor will be directly modified.
7
+ #
8
+ # req = Rack::Request.new(env)
9
+ # req.post?
10
+ # req.params["data"]
11
+
12
+ class Request
13
+ (require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4'
14
+
15
+ class << self
16
+ attr_accessor :ip_filter
17
+ end
18
+
19
+ self.ip_filter = lambda { |ip| /\A127\.0\.0\.1\Z|\A(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|\A::1\Z|\Afd[0-9a-f]{2}:.+|\Alocalhost\Z|\Aunix\Z|\Aunix:/i.match?(ip) }
20
+ ALLOWED_SCHEMES = %w(https http).freeze
21
+ SCHEME_WHITELIST = ALLOWED_SCHEMES
22
+ if Object.respond_to?(:deprecate_constant)
23
+ deprecate_constant :SCHEME_WHITELIST
24
+ end
25
+
26
+ def initialize(env)
27
+ @params = nil
28
+ super(env)
29
+ end
30
+
31
+ def params
32
+ @params ||= super
33
+ end
34
+
35
+ def update_param(k, v)
36
+ super
37
+ @params = nil
38
+ end
39
+
40
+ def delete_param(k)
41
+ v = super
42
+ @params = nil
43
+ v
44
+ end
45
+
46
+ module Env
47
+ # The environment of the request.
48
+ attr_reader :env
49
+
50
+ def initialize(env)
51
+ @env = env
52
+ super()
53
+ end
54
+
55
+ # Predicate method to test to see if `name` has been set as request
56
+ # specific data
57
+ def has_header?(name)
58
+ @env.key? name
59
+ end
60
+
61
+ # Get a request specific value for `name`.
62
+ def get_header(name)
63
+ @env[name]
64
+ end
65
+
66
+ # If a block is given, it yields to the block if the value hasn't been set
67
+ # on the request.
68
+ def fetch_header(name, &block)
69
+ @env.fetch(name, &block)
70
+ end
71
+
72
+ # Loops through each key / value pair in the request specific data.
73
+ def each_header(&block)
74
+ @env.each(&block)
75
+ end
76
+
77
+ # Set a request specific value for `name` to `v`
78
+ def set_header(name, v)
79
+ @env[name] = v
80
+ end
81
+
82
+ # Add a header that may have multiple values.
83
+ #
84
+ # Example:
85
+ # request.add_header 'Accept', 'image/png'
86
+ # request.add_header 'Accept', '*/*'
87
+ #
88
+ # assert_equal 'image/png,*/*', request.get_header('Accept')
89
+ #
90
+ # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
91
+ def add_header(key, v)
92
+ if v.nil?
93
+ get_header key
94
+ elsif has_header? key
95
+ set_header key, "#{get_header key},#{v}"
96
+ else
97
+ set_header key, v
98
+ end
99
+ end
100
+
101
+ # Delete a request specific value for `name`.
102
+ def delete_header(name)
103
+ @env.delete name
104
+ end
105
+
106
+ def initialize_copy(other)
107
+ @env = other.env.dup
108
+ end
109
+ end
110
+
111
+ module Helpers
112
+ # The set of form-data media-types. Requests that do not indicate
113
+ # one of the media types present in this list will not be eligible
114
+ # for form-data / param parsing.
115
+ FORM_DATA_MEDIA_TYPES = [
116
+ 'application/x-www-form-urlencoded',
117
+ 'multipart/form-data'
118
+ ]
119
+
120
+ # The set of media-types. Requests that do not indicate
121
+ # one of the media types present in this list will not be eligible
122
+ # for param parsing like soap attachments or generic multiparts
123
+ PARSEABLE_DATA_MEDIA_TYPES = [
124
+ 'multipart/related',
125
+ 'multipart/mixed'
126
+ ]
127
+
128
+ # Default ports depending on scheme. Used to decide whether or not
129
+ # to include the port in a generated URI.
130
+ DEFAULT_PORTS = { 'http' => 80, 'https' => 443, 'coffee' => 80 }
131
+
132
+ # The address of the client which connected to the proxy.
133
+ HTTP_X_FORWARDED_FOR = 'HTTP_X_FORWARDED_FOR'
134
+
135
+ # The contents of the host/:authority header sent to the proxy.
136
+ HTTP_X_FORWARDED_HOST = 'HTTP_X_FORWARDED_HOST'
137
+
138
+ # The value of the scheme sent to the proxy.
139
+ HTTP_X_FORWARDED_SCHEME = 'HTTP_X_FORWARDED_SCHEME'
140
+
141
+ # The protocol used to connect to the proxy.
142
+ HTTP_X_FORWARDED_PROTO = 'HTTP_X_FORWARDED_PROTO'
143
+
144
+ # The port used to connect to the proxy.
145
+ HTTP_X_FORWARDED_PORT = 'HTTP_X_FORWARDED_PORT'
146
+
147
+ # Another way for specifing https scheme was used.
148
+ HTTP_X_FORWARDED_SSL = 'HTTP_X_FORWARDED_SSL'
149
+
150
+ def body; get_header(RACK_INPUT) end
151
+ def script_name; get_header(SCRIPT_NAME).to_s end
152
+ def script_name=(s); set_header(SCRIPT_NAME, s.to_s) end
153
+
154
+ def path_info; get_header(PATH_INFO).to_s end
155
+ def path_info=(s); set_header(PATH_INFO, s.to_s) end
156
+
157
+ def request_method; get_header(REQUEST_METHOD) end
158
+ def query_string; get_header(QUERY_STRING).to_s end
159
+ def content_length; get_header('CONTENT_LENGTH') end
160
+ def logger; get_header(RACK_LOGGER) end
161
+ def user_agent; get_header('HTTP_USER_AGENT') end
162
+ def multithread?; get_header(RACK_MULTITHREAD) end
163
+
164
+ # the referer of the client
165
+ def referer; get_header('HTTP_REFERER') end
166
+ alias referrer referer
167
+
168
+ def session
169
+ fetch_header(RACK_SESSION) do |k|
170
+ set_header RACK_SESSION, default_session
171
+ end
172
+ end
173
+
174
+ def session_options
175
+ fetch_header(RACK_SESSION_OPTIONS) do |k|
176
+ set_header RACK_SESSION_OPTIONS, {}
177
+ end
178
+ end
179
+
180
+ # Checks the HTTP request method (or verb) to see if it was of type DELETE
181
+ def delete?; request_method == DELETE end
182
+
183
+ # Checks the HTTP request method (or verb) to see if it was of type GET
184
+ def get?; request_method == GET end
185
+
186
+ # Checks the HTTP request method (or verb) to see if it was of type HEAD
187
+ def head?; request_method == HEAD end
188
+
189
+ # Checks the HTTP request method (or verb) to see if it was of type OPTIONS
190
+ def options?; request_method == OPTIONS end
191
+
192
+ # Checks the HTTP request method (or verb) to see if it was of type LINK
193
+ def link?; request_method == LINK end
194
+
195
+ # Checks the HTTP request method (or verb) to see if it was of type PATCH
196
+ def patch?; request_method == PATCH end
197
+
198
+ # Checks the HTTP request method (or verb) to see if it was of type POST
199
+ def post?; request_method == POST end
200
+
201
+ # Checks the HTTP request method (or verb) to see if it was of type PUT
202
+ def put?; request_method == PUT end
203
+
204
+ # Checks the HTTP request method (or verb) to see if it was of type TRACE
205
+ def trace?; request_method == TRACE end
206
+
207
+ # Checks the HTTP request method (or verb) to see if it was of type UNLINK
208
+ def unlink?; request_method == UNLINK end
209
+
210
+ def scheme
211
+ if get_header(HTTPS) == 'on'
212
+ 'https'
213
+ elsif get_header(HTTP_X_FORWARDED_SSL) == 'on'
214
+ 'https'
215
+ elsif forwarded_scheme
216
+ forwarded_scheme
217
+ else
218
+ get_header(RACK_URL_SCHEME)
219
+ end
220
+ end
221
+
222
+ # The authority of the incoming request as defined by RFC3976.
223
+ # https://tools.ietf.org/html/rfc3986#section-3.2
224
+ #
225
+ # In HTTP/1, this is the `host` header.
226
+ # In HTTP/2, this is the `:authority` pseudo-header.
227
+ def authority
228
+ forwarded_authority || host_authority || server_authority
229
+ end
230
+
231
+ # The authority as defined by the `SERVER_NAME` and `SERVER_PORT`
232
+ # variables.
233
+ def server_authority
234
+ host = self.server_name
235
+ port = self.server_port
236
+
237
+ if host
238
+ if port
239
+ "#{host}:#{port}"
240
+ else
241
+ host
242
+ end
243
+ end
244
+ end
245
+
246
+ def server_name
247
+ get_header(SERVER_NAME)
248
+ end
249
+
250
+ def server_port
251
+ if port = get_header(SERVER_PORT)
252
+ Integer(port)
253
+ end
254
+ end
255
+
256
+ def cookies
257
+ hash = fetch_header(RACK_REQUEST_COOKIE_HASH) do |key|
258
+ set_header(key, {})
259
+ end
260
+
261
+ string = get_header(HTTP_COOKIE)
262
+
263
+ unless string == get_header(RACK_REQUEST_COOKIE_STRING)
264
+ hash.replace Utils.parse_cookies_header(string)
265
+ set_header(RACK_REQUEST_COOKIE_STRING, string)
266
+ end
267
+
268
+ hash
269
+ end
270
+
271
+ def content_type
272
+ content_type = get_header('CONTENT_TYPE')
273
+ content_type.nil? || content_type.empty? ? nil : content_type
274
+ end
275
+
276
+ def xhr?
277
+ get_header("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest"
278
+ end
279
+
280
+ # The `HTTP_HOST` header.
281
+ def host_authority
282
+ get_header(HTTP_HOST)
283
+ end
284
+
285
+ def host_with_port(authority = self.authority)
286
+ host, _, port = split_authority(authority)
287
+
288
+ if port == DEFAULT_PORTS[self.scheme]
289
+ host
290
+ else
291
+ authority
292
+ end
293
+ end
294
+
295
+ # Returns a formatted host, suitable for being used in a URI.
296
+ def host
297
+ split_authority(self.authority)[0]
298
+ end
299
+
300
+ # Returns an address suitable for being to resolve to an address.
301
+ # In the case of a domain name or IPv4 address, the result is the same
302
+ # as +host+. In the case of IPv6 or future address formats, the square
303
+ # brackets are removed.
304
+ def hostname
305
+ split_authority(self.authority)[1]
306
+ end
307
+
308
+ def port
309
+ if authority = self.authority
310
+ _, _, port = split_authority(self.authority)
311
+
312
+ if port
313
+ return port
314
+ end
315
+ end
316
+
317
+ if forwarded_port = self.forwarded_port
318
+ return forwarded_port.first
319
+ end
320
+
321
+ if scheme = self.scheme
322
+ if port = DEFAULT_PORTS[self.scheme]
323
+ return port
324
+ end
325
+ end
326
+
327
+ self.server_port
328
+ end
329
+
330
+ def forwarded_for
331
+ if value = get_header(HTTP_X_FORWARDED_FOR)
332
+ split_header(value).map do |authority|
333
+ split_authority(wrap_ipv6(authority))[1]
334
+ end
335
+ end
336
+ end
337
+
338
+ def forwarded_port
339
+ if value = get_header(HTTP_X_FORWARDED_PORT)
340
+ split_header(value).map(&:to_i)
341
+ end
342
+ end
343
+
344
+ def forwarded_authority
345
+ if value = get_header(HTTP_X_FORWARDED_HOST)
346
+ wrap_ipv6(split_header(value).first)
347
+ end
348
+ end
349
+
350
+ def ssl?
351
+ scheme == 'https' || scheme == 'wss'
352
+ end
353
+
354
+ def ip
355
+ remote_addresses = split_header(get_header('REMOTE_ADDR'))
356
+ external_addresses = reject_trusted_ip_addresses(remote_addresses)
357
+
358
+ unless external_addresses.empty?
359
+ return external_addresses.first
360
+ end
361
+
362
+ if forwarded_for = self.forwarded_for
363
+ unless forwarded_for.empty?
364
+ # The forwarded for addresses are ordered: client, proxy1, proxy2.
365
+ # So we reject all the trusted addresses (proxy*) and return the
366
+ # last client. Or if we trust everyone, we just return the first
367
+ # address.
368
+ return reject_trusted_ip_addresses(forwarded_for).last || forwarded_for.first
369
+ end
370
+ end
371
+
372
+ # If all the addresses are trusted, and we aren't forwarded, just return
373
+ # the first remote address, which represents the source of the request.
374
+ remote_addresses.first
375
+ end
376
+
377
+ # The media type (type/subtype) portion of the CONTENT_TYPE header
378
+ # without any media type parameters. e.g., when CONTENT_TYPE is
379
+ # "text/plain;charset=utf-8", the media-type is "text/plain".
380
+ #
381
+ # For more information on the use of media types in HTTP, see:
382
+ # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
383
+ def media_type
384
+ MediaType.type(content_type)
385
+ end
386
+
387
+ # The media type parameters provided in CONTENT_TYPE as a Hash, or
388
+ # an empty Hash if no CONTENT_TYPE or media-type parameters were
389
+ # provided. e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",
390
+ # this method responds with the following Hash:
391
+ # { 'charset' => 'utf-8' }
392
+ def media_type_params
393
+ MediaType.params(content_type)
394
+ end
395
+
396
+ # The character set of the request body if a "charset" media type
397
+ # parameter was given, or nil if no "charset" was specified. Note
398
+ # that, per RFC2616, text/* media types that specify no explicit
399
+ # charset are to be considered ISO-8859-1.
400
+ def content_charset
401
+ media_type_params['charset']
402
+ end
403
+
404
+ # Determine whether the request body contains form-data by checking
405
+ # the request Content-Type for one of the media-types:
406
+ # "application/x-www-form-urlencoded" or "multipart/form-data". The
407
+ # list of form-data media types can be modified through the
408
+ # +FORM_DATA_MEDIA_TYPES+ array.
409
+ #
410
+ # A request body is also assumed to contain form-data when no
411
+ # Content-Type header is provided and the request_method is POST.
412
+ def form_data?
413
+ type = media_type
414
+ meth = get_header(RACK_METHODOVERRIDE_ORIGINAL_METHOD) || get_header(REQUEST_METHOD)
415
+
416
+ (meth == POST && type.nil?) || FORM_DATA_MEDIA_TYPES.include?(type)
417
+ end
418
+
419
+ # Determine whether the request body contains data by checking
420
+ # the request media_type against registered parse-data media-types
421
+ def parseable_data?
422
+ PARSEABLE_DATA_MEDIA_TYPES.include?(media_type)
423
+ end
424
+
425
+ # Returns the data received in the query string.
426
+ def GET
427
+ if get_header(RACK_REQUEST_QUERY_STRING) == query_string
428
+ get_header(RACK_REQUEST_QUERY_HASH)
429
+ else
430
+ query_hash = parse_query(query_string, '&;')
431
+ set_header(RACK_REQUEST_QUERY_STRING, query_string)
432
+ set_header(RACK_REQUEST_QUERY_HASH, query_hash)
433
+ end
434
+ end
435
+
436
+ # Returns the data received in the request body.
437
+ #
438
+ # This method support both application/x-www-form-urlencoded and
439
+ # multipart/form-data.
440
+ def POST
441
+ if get_header(RACK_INPUT).nil?
442
+ raise "Missing rack.input"
443
+ elsif get_header(RACK_REQUEST_FORM_INPUT) == get_header(RACK_INPUT)
444
+ get_header(RACK_REQUEST_FORM_HASH)
445
+ elsif form_data? || parseable_data?
446
+ unless set_header(RACK_REQUEST_FORM_HASH, parse_multipart)
447
+ form_vars = get_header(RACK_INPUT).read
448
+
449
+ # Fix for Safari Ajax postings that always append \0
450
+ # form_vars.sub!(/\0\z/, '') # performance replacement:
451
+ form_vars.slice!(-1) if form_vars.end_with?("\0")
452
+
453
+ set_header RACK_REQUEST_FORM_VARS, form_vars
454
+ set_header RACK_REQUEST_FORM_HASH, parse_query(form_vars, '&')
455
+
456
+ get_header(RACK_INPUT).rewind
457
+ end
458
+ set_header RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT)
459
+ get_header RACK_REQUEST_FORM_HASH
460
+ else
461
+ {}
462
+ end
463
+ end
464
+
465
+ # The union of GET and POST data.
466
+ #
467
+ # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.
468
+ def params
469
+ self.GET.merge(self.POST)
470
+ end
471
+
472
+ # Destructively update a parameter, whether it's in GET and/or POST. Returns nil.
473
+ #
474
+ # The parameter is updated wherever it was previous defined, so GET, POST, or both. If it wasn't previously defined, it's inserted into GET.
475
+ #
476
+ # <tt>env['rack.input']</tt> is not touched.
477
+ def update_param(k, v)
478
+ found = false
479
+ if self.GET.has_key?(k)
480
+ found = true
481
+ self.GET[k] = v
482
+ end
483
+ if self.POST.has_key?(k)
484
+ found = true
485
+ self.POST[k] = v
486
+ end
487
+ unless found
488
+ self.GET[k] = v
489
+ end
490
+ end
491
+
492
+ # Destructively delete a parameter, whether it's in GET or POST. Returns the value of the deleted parameter.
493
+ #
494
+ # If the parameter is in both GET and POST, the POST value takes precedence since that's how #params works.
495
+ #
496
+ # <tt>env['rack.input']</tt> is not touched.
497
+ def delete_param(k)
498
+ post_value, get_value = self.POST.delete(k), self.GET.delete(k)
499
+ post_value || get_value
500
+ end
501
+
502
+ def base_url
503
+ "#{scheme}://#{host_with_port}"
504
+ end
505
+
506
+ # Tries to return a remake of the original request URL as a string.
507
+ def url
508
+ base_url + fullpath
509
+ end
510
+
511
+ def path
512
+ script_name + path_info
513
+ end
514
+
515
+ def fullpath
516
+ query_string.empty? ? path : "#{path}?#{query_string}"
517
+ end
518
+
519
+ def accept_encoding
520
+ parse_http_accept_header(get_header("HTTP_ACCEPT_ENCODING"))
521
+ end
522
+
523
+ def accept_language
524
+ parse_http_accept_header(get_header("HTTP_ACCEPT_LANGUAGE"))
525
+ end
526
+
527
+ def trusted_proxy?(ip)
528
+ Rack::Request.ip_filter.call(ip)
529
+ end
530
+
531
+ # shortcut for <tt>request.params[key]</tt>
532
+ def [](key)
533
+ if $VERBOSE
534
+ warn("Request#[] is deprecated and will be removed in a future version of Rack. Please use request.params[] instead")
535
+ end
536
+
537
+ params[key.to_s]
538
+ end
539
+
540
+ # shortcut for <tt>request.params[key] = value</tt>
541
+ #
542
+ # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.
543
+ def []=(key, value)
544
+ if $VERBOSE
545
+ warn("Request#[]= is deprecated and will be removed in a future version of Rack. Please use request.params[]= instead")
546
+ end
547
+
548
+ params[key.to_s] = value
549
+ end
550
+
551
+ # like Hash#values_at
552
+ def values_at(*keys)
553
+ keys.map { |key| params[key] }
554
+ end
555
+
556
+ private
557
+
558
+ def default_session; {}; end
559
+
560
+ # Assist with compatibility when processing `X-Forwarded-For`.
561
+ def wrap_ipv6(host)
562
+ # Even thought IPv6 addresses should be wrapped in square brackets,
563
+ # sometimes this is not done in various legacy/underspecified headers.
564
+ # So we try to fix this situation for compatibility reasons.
565
+
566
+ # Try to detect IPv6 addresses which aren't escaped yet:
567
+ if !host.start_with?('[') && host.count(':') > 1
568
+ "[#{host}]"
569
+ else
570
+ host
571
+ end
572
+ end
573
+
574
+ def parse_http_accept_header(header)
575
+ header.to_s.split(/\s*,\s*/).map do |part|
576
+ attribute, parameters = part.split(/\s*;\s*/, 2)
577
+ quality = 1.0
578
+ if parameters and /\Aq=([\d.]+)/ =~ parameters
579
+ quality = $1.to_f
580
+ end
581
+ [attribute, quality]
582
+ end
583
+ end
584
+
585
+ def query_parser
586
+ Utils.default_query_parser
587
+ end
588
+
589
+ def parse_query(qs, d = '&')
590
+ query_parser.parse_nested_query(qs, d)
591
+ end
592
+
593
+ def parse_multipart
594
+ Rack::Multipart.extract_multipart(self, query_parser)
595
+ end
596
+
597
+ def split_header(value)
598
+ value ? value.strip.split(/[,\s]+/) : []
599
+ end
600
+
601
+ AUTHORITY = /^
602
+ # The host:
603
+ (?<host>
604
+ # An IPv6 address:
605
+ (\[(?<ip6>.*)\])
606
+ |
607
+ # An IPv4 address:
608
+ (?<ip4>[\d\.]+)
609
+ |
610
+ # A hostname:
611
+ (?<name>[a-zA-Z0-9\.\-]+)
612
+ )
613
+ # The optional port:
614
+ (:(?<port>\d+))?
615
+ $/x
616
+
617
+ private_constant :AUTHORITY
618
+
619
+ def split_authority(authority)
620
+ if match = AUTHORITY.match(authority)
621
+ if address = match[:ip6]
622
+ return match[:host], address, match[:port]&.to_i
623
+ else
624
+ return match[:host], match[:host], match[:port]&.to_i
625
+ end
626
+ end
627
+
628
+ # Give up!
629
+ return authority, authority, nil
630
+ end
631
+
632
+ def reject_trusted_ip_addresses(ip_addresses)
633
+ ip_addresses.reject { |ip| trusted_proxy?(ip) }
634
+ end
635
+
636
+ def forwarded_scheme
637
+ allowed_scheme(get_header(HTTP_X_FORWARDED_SCHEME)) ||
638
+ allowed_scheme(extract_proto_header(get_header(HTTP_X_FORWARDED_PROTO)))
639
+ end
640
+
641
+ def allowed_scheme(header)
642
+ header if ALLOWED_SCHEMES.include?(header)
643
+ end
644
+
645
+ def extract_proto_header(header)
646
+ if header
647
+ if (comma_index = header.index(','))
648
+ header[0, comma_index]
649
+ else
650
+ header
651
+ end
652
+ end
653
+ end
654
+ end
655
+
656
+ include Env
657
+ include Helpers
658
+ end
659
+ end