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,806 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Rack
6
+ # Rack::Lint validates your application and the requests and
7
+ # responses according to the Rack spec.
8
+
9
+ class Lint
10
+ def initialize(app)
11
+ @app = app
12
+ @content_length = nil
13
+ end
14
+
15
+ # :stopdoc:
16
+
17
+ class LintError < RuntimeError; end
18
+ module Assertion
19
+ def assert(message)
20
+ unless yield
21
+ raise LintError, message
22
+ end
23
+ end
24
+ end
25
+ include Assertion
26
+
27
+ ## This specification aims to formalize the Rack protocol. You
28
+ ## can (and should) use Rack::Lint to enforce it.
29
+ ##
30
+ ## When you develop middleware, be sure to add a Lint before and
31
+ ## after to catch all mistakes.
32
+
33
+ ## = Rack applications
34
+
35
+ ## A Rack application is a Ruby object (not a class) that
36
+ ## responds to +call+.
37
+ def call(env = nil)
38
+ dup._call(env)
39
+ end
40
+
41
+ def _call(env)
42
+ ## It takes exactly one argument, the *environment*
43
+ assert("No env given") { env }
44
+ check_env env
45
+
46
+ env[RACK_INPUT] = InputWrapper.new(env[RACK_INPUT])
47
+ env[RACK_ERRORS] = ErrorWrapper.new(env[RACK_ERRORS])
48
+
49
+ ## and returns an Array of exactly three values:
50
+ ary = @app.call(env)
51
+ assert("response is not an Array, but #{ary.class}") {
52
+ ary.kind_of? Array
53
+ }
54
+ assert("response array has #{ary.size} elements instead of 3") {
55
+ ary.size == 3
56
+ }
57
+
58
+ status, headers, @body = ary
59
+ ## The *status*,
60
+ check_status status
61
+ ## the *headers*,
62
+ check_headers headers
63
+
64
+ hijack_proc = check_hijack_response headers, env
65
+ if hijack_proc && headers.is_a?(Hash)
66
+ headers[RACK_HIJACK] = hijack_proc
67
+ end
68
+
69
+ ## and the *body*.
70
+ check_content_type status, headers
71
+ check_content_length status, headers
72
+ @head_request = env[REQUEST_METHOD] == HEAD
73
+ [status, headers, self]
74
+ end
75
+
76
+ ## == The Environment
77
+ def check_env(env)
78
+ ## The environment must be an unfrozen instance of Hash that includes
79
+ ## CGI-like headers. The application is free to modify the
80
+ ## environment.
81
+ assert("env #{env.inspect} is not a Hash, but #{env.class}") {
82
+ env.kind_of? Hash
83
+ }
84
+ assert("env should not be frozen, but is") {
85
+ !env.frozen?
86
+ }
87
+
88
+ ##
89
+ ## The environment is required to include these variables
90
+ ## (adopted from PEP333), except when they'd be empty, but see
91
+ ## below.
92
+
93
+ ## <tt>REQUEST_METHOD</tt>:: The HTTP request method, such as
94
+ ## "GET" or "POST". This cannot ever
95
+ ## be an empty string, and so is
96
+ ## always required.
97
+
98
+ ## <tt>SCRIPT_NAME</tt>:: The initial portion of the request
99
+ ## URL's "path" that corresponds to the
100
+ ## application object, so that the
101
+ ## application knows its virtual
102
+ ## "location". This may be an empty
103
+ ## string, if the application corresponds
104
+ ## to the "root" of the server.
105
+
106
+ ## <tt>PATH_INFO</tt>:: The remainder of the request URL's
107
+ ## "path", designating the virtual
108
+ ## "location" of the request's target
109
+ ## within the application. This may be an
110
+ ## empty string, if the request URL targets
111
+ ## the application root and does not have a
112
+ ## trailing slash. This value may be
113
+ ## percent-encoded when originating from
114
+ ## a URL.
115
+
116
+ ## <tt>QUERY_STRING</tt>:: The portion of the request URL that
117
+ ## follows the <tt>?</tt>, if any. May be
118
+ ## empty, but is always required!
119
+
120
+ ## <tt>SERVER_NAME</tt>:: When combined with <tt>SCRIPT_NAME</tt> and
121
+ ## <tt>PATH_INFO</tt>, these variables can be
122
+ ## used to complete the URL. Note, however,
123
+ ## that <tt>HTTP_HOST</tt>, if present,
124
+ ## should be used in preference to
125
+ ## <tt>SERVER_NAME</tt> for reconstructing
126
+ ## the request URL.
127
+ ## <tt>SERVER_NAME</tt> can never be an empty
128
+ ## string, and so is always required.
129
+
130
+ ## <tt>SERVER_PORT</tt>:: An optional +Integer+ which is the port the
131
+ ## server is running on. Should be specified if
132
+ ## the server is running on a non-standard port.
133
+
134
+ ## <tt>HTTP_</tt> Variables:: Variables corresponding to the
135
+ ## client-supplied HTTP request
136
+ ## headers (i.e., variables whose
137
+ ## names begin with <tt>HTTP_</tt>). The
138
+ ## presence or absence of these
139
+ ## variables should correspond with
140
+ ## the presence or absence of the
141
+ ## appropriate HTTP header in the
142
+ ## request. See
143
+ ## {RFC3875 section 4.1.18}[https://tools.ietf.org/html/rfc3875#section-4.1.18]
144
+ ## for specific behavior.
145
+
146
+ ## In addition to this, the Rack environment must include these
147
+ ## Rack-specific variables:
148
+
149
+ ## <tt>rack.version</tt>:: The Array representing this version of Rack
150
+ ## See Rack::VERSION, that corresponds to
151
+ ## the version of this SPEC.
152
+
153
+ ## <tt>rack.url_scheme</tt>:: +http+ or +https+, depending on the
154
+ ## request URL.
155
+
156
+ ## <tt>rack.input</tt>:: See below, the input stream.
157
+
158
+ ## <tt>rack.errors</tt>:: See below, the error stream.
159
+
160
+ ## <tt>rack.multithread</tt>:: true if the application object may be
161
+ ## simultaneously invoked by another thread
162
+ ## in the same process, false otherwise.
163
+
164
+ ## <tt>rack.multiprocess</tt>:: true if an equivalent application object
165
+ ## may be simultaneously invoked by another
166
+ ## process, false otherwise.
167
+
168
+ ## <tt>rack.run_once</tt>:: true if the server expects
169
+ ## (but does not guarantee!) that the
170
+ ## application will only be invoked this one
171
+ ## time during the life of its containing
172
+ ## process. Normally, this will only be true
173
+ ## for a server based on CGI
174
+ ## (or something similar).
175
+
176
+ ## <tt>rack.hijack?</tt>:: present and true if the server supports
177
+ ## connection hijacking. See below, hijacking.
178
+
179
+ ## <tt>rack.hijack</tt>:: an object responding to #call that must be
180
+ ## called at least once before using
181
+ ## rack.hijack_io.
182
+ ## It is recommended #call return rack.hijack_io
183
+ ## as well as setting it in env if necessary.
184
+
185
+ ## <tt>rack.hijack_io</tt>:: if rack.hijack? is true, and rack.hijack
186
+ ## has received #call, this will contain
187
+ ## an object resembling an IO. See hijacking.
188
+
189
+ ## Additional environment specifications have approved to
190
+ ## standardized middleware APIs. None of these are required to
191
+ ## be implemented by the server.
192
+
193
+ ## <tt>rack.session</tt>:: A hash like interface for storing
194
+ ## request session data.
195
+ ## The store must implement:
196
+ if session = env[RACK_SESSION]
197
+ ## store(key, value) (aliased as []=);
198
+ assert("session #{session.inspect} must respond to store and []=") {
199
+ session.respond_to?(:store) && session.respond_to?(:[]=)
200
+ }
201
+
202
+ ## fetch(key, default = nil) (aliased as []);
203
+ assert("session #{session.inspect} must respond to fetch and []") {
204
+ session.respond_to?(:fetch) && session.respond_to?(:[])
205
+ }
206
+
207
+ ## delete(key);
208
+ assert("session #{session.inspect} must respond to delete") {
209
+ session.respond_to?(:delete)
210
+ }
211
+
212
+ ## clear;
213
+ assert("session #{session.inspect} must respond to clear") {
214
+ session.respond_to?(:clear)
215
+ }
216
+
217
+ ## to_hash (returning unfrozen Hash instance);
218
+ assert("session #{session.inspect} must respond to to_hash and return unfrozen Hash instance") {
219
+ session.respond_to?(:to_hash) && session.to_hash.kind_of?(Hash) && !session.to_hash.frozen?
220
+ }
221
+ end
222
+
223
+ ## <tt>rack.logger</tt>:: A common object interface for logging messages.
224
+ ## The object must implement:
225
+ if logger = env[RACK_LOGGER]
226
+ ## info(message, &block)
227
+ assert("logger #{logger.inspect} must respond to info") {
228
+ logger.respond_to?(:info)
229
+ }
230
+
231
+ ## debug(message, &block)
232
+ assert("logger #{logger.inspect} must respond to debug") {
233
+ logger.respond_to?(:debug)
234
+ }
235
+
236
+ ## warn(message, &block)
237
+ assert("logger #{logger.inspect} must respond to warn") {
238
+ logger.respond_to?(:warn)
239
+ }
240
+
241
+ ## error(message, &block)
242
+ assert("logger #{logger.inspect} must respond to error") {
243
+ logger.respond_to?(:error)
244
+ }
245
+
246
+ ## fatal(message, &block)
247
+ assert("logger #{logger.inspect} must respond to fatal") {
248
+ logger.respond_to?(:fatal)
249
+ }
250
+ end
251
+
252
+ ## <tt>rack.multipart.buffer_size</tt>:: An Integer hint to the multipart parser as to what chunk size to use for reads and writes.
253
+ if bufsize = env[RACK_MULTIPART_BUFFER_SIZE]
254
+ assert("rack.multipart.buffer_size must be an Integer > 0 if specified") {
255
+ bufsize.is_a?(Integer) && bufsize > 0
256
+ }
257
+ end
258
+
259
+ ## <tt>rack.multipart.tempfile_factory</tt>:: An object responding to #call with two arguments, the filename and content_type given for the multipart form field, and returning an IO-like object that responds to #<< and optionally #rewind. This factory will be used to instantiate the tempfile for each multipart form file upload field, rather than the default class of Tempfile.
260
+ if tempfile_factory = env[RACK_MULTIPART_TEMPFILE_FACTORY]
261
+ assert("rack.multipart.tempfile_factory must respond to #call") { tempfile_factory.respond_to?(:call) }
262
+ env[RACK_MULTIPART_TEMPFILE_FACTORY] = lambda do |filename, content_type|
263
+ io = tempfile_factory.call(filename, content_type)
264
+ assert("rack.multipart.tempfile_factory return value must respond to #<<") { io.respond_to?(:<<) }
265
+ io
266
+ end
267
+ end
268
+
269
+ ## The server or the application can store their own data in the
270
+ ## environment, too. The keys must contain at least one dot,
271
+ ## and should be prefixed uniquely. The prefix <tt>rack.</tt>
272
+ ## is reserved for use with the Rack core distribution and other
273
+ ## accepted specifications and must not be used otherwise.
274
+ ##
275
+
276
+ %w[REQUEST_METHOD SERVER_NAME QUERY_STRING
277
+ rack.version rack.input rack.errors
278
+ rack.multithread rack.multiprocess rack.run_once].each { |header|
279
+ assert("env missing required key #{header}") { env.include? header }
280
+ }
281
+
282
+ ## The <tt>SERVER_PORT</tt> must be an Integer if set.
283
+ assert("env[SERVER_PORT] is not an Integer") do
284
+ server_port = env["SERVER_PORT"]
285
+ server_port.nil? || (Integer(server_port) rescue false)
286
+ end
287
+
288
+ ## The <tt>SERVER_NAME</tt> must be a valid authority as defined by RFC7540.
289
+ assert("#{env[SERVER_NAME]} must be a valid authority") do
290
+ URI.parse("http://#{env[SERVER_NAME]}/") rescue false
291
+ end
292
+
293
+ ## The <tt>HTTP_HOST</tt> must be a valid authority as defined by RFC7540.
294
+ assert("#{env[HTTP_HOST]} must be a valid authority") do
295
+ URI.parse("http://#{env[HTTP_HOST]}/") rescue false
296
+ end
297
+
298
+ ## The environment must not contain the keys
299
+ ## <tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>
300
+ ## (use the versions without <tt>HTTP_</tt>).
301
+ %w[HTTP_CONTENT_TYPE HTTP_CONTENT_LENGTH].each { |header|
302
+ assert("env contains #{header}, must use #{header[5, -1]}") {
303
+ not env.include? header
304
+ }
305
+ }
306
+
307
+ ## The CGI keys (named without a period) must have String values.
308
+ ## If the string values for CGI keys contain non-ASCII characters,
309
+ ## they should use ASCII-8BIT encoding.
310
+ env.each { |key, value|
311
+ next if key.include? "." # Skip extensions
312
+ assert("env variable #{key} has non-string value #{value.inspect}") {
313
+ value.kind_of? String
314
+ }
315
+ next if value.encoding == Encoding::ASCII_8BIT
316
+ assert("env variable #{key} has value containing non-ASCII characters and has non-ASCII-8BIT encoding #{value.inspect} encoding: #{value.encoding}") {
317
+ value.b !~ /[\x80-\xff]/n
318
+ }
319
+ }
320
+
321
+ ## There are the following restrictions:
322
+
323
+ ## * <tt>rack.version</tt> must be an array of Integers.
324
+ assert("rack.version must be an Array, was #{env[RACK_VERSION].class}") {
325
+ env[RACK_VERSION].kind_of? Array
326
+ }
327
+ ## * <tt>rack.url_scheme</tt> must either be +http+ or +https+.
328
+ assert("rack.url_scheme unknown: #{env[RACK_URL_SCHEME].inspect}") {
329
+ %w[http https].include?(env[RACK_URL_SCHEME])
330
+ }
331
+
332
+ ## * There must be a valid input stream in <tt>rack.input</tt>.
333
+ check_input env[RACK_INPUT]
334
+ ## * There must be a valid error stream in <tt>rack.errors</tt>.
335
+ check_error env[RACK_ERRORS]
336
+ ## * There may be a valid hijack stream in <tt>rack.hijack_io</tt>
337
+ check_hijack env
338
+
339
+ ## * The <tt>REQUEST_METHOD</tt> must be a valid token.
340
+ assert("REQUEST_METHOD unknown: #{env[REQUEST_METHOD].dump}") {
341
+ env[REQUEST_METHOD] =~ /\A[0-9A-Za-z!\#$%&'*+.^_`|~-]+\z/
342
+ }
343
+
344
+ ## * The <tt>SCRIPT_NAME</tt>, if non-empty, must start with <tt>/</tt>
345
+ assert("SCRIPT_NAME must start with /") {
346
+ !env.include?(SCRIPT_NAME) ||
347
+ env[SCRIPT_NAME] == "" ||
348
+ env[SCRIPT_NAME] =~ /\A\//
349
+ }
350
+ ## * The <tt>PATH_INFO</tt>, if non-empty, must start with <tt>/</tt>
351
+ assert("PATH_INFO must start with /") {
352
+ !env.include?(PATH_INFO) ||
353
+ env[PATH_INFO] == "" ||
354
+ env[PATH_INFO] =~ /\A\//
355
+ }
356
+ ## * The <tt>CONTENT_LENGTH</tt>, if given, must consist of digits only.
357
+ assert("Invalid CONTENT_LENGTH: #{env["CONTENT_LENGTH"]}") {
358
+ !env.include?("CONTENT_LENGTH") || env["CONTENT_LENGTH"] =~ /\A\d+\z/
359
+ }
360
+
361
+ ## * One of <tt>SCRIPT_NAME</tt> or <tt>PATH_INFO</tt> must be
362
+ ## set. <tt>PATH_INFO</tt> should be <tt>/</tt> if
363
+ ## <tt>SCRIPT_NAME</tt> is empty.
364
+ assert("One of SCRIPT_NAME or PATH_INFO must be set (make PATH_INFO '/' if SCRIPT_NAME is empty)") {
365
+ env[SCRIPT_NAME] || env[PATH_INFO]
366
+ }
367
+ ## <tt>SCRIPT_NAME</tt> never should be <tt>/</tt>, but instead be empty.
368
+ assert("SCRIPT_NAME cannot be '/', make it '' and PATH_INFO '/'") {
369
+ env[SCRIPT_NAME] != "/"
370
+ }
371
+ end
372
+
373
+ ## === The Input Stream
374
+ ##
375
+ ## The input stream is an IO-like object which contains the raw HTTP
376
+ ## POST data.
377
+ def check_input(input)
378
+ ## When applicable, its external encoding must be "ASCII-8BIT" and it
379
+ ## must be opened in binary mode, for Ruby 1.9 compatibility.
380
+ assert("rack.input #{input} does not have ASCII-8BIT as its external encoding") {
381
+ input.external_encoding == Encoding::ASCII_8BIT
382
+ } if input.respond_to?(:external_encoding)
383
+ assert("rack.input #{input} is not opened in binary mode") {
384
+ input.binmode?
385
+ } if input.respond_to?(:binmode?)
386
+
387
+ ## The input stream must respond to +gets+, +each+, +read+ and +rewind+.
388
+ [:gets, :each, :read, :rewind].each { |method|
389
+ assert("rack.input #{input} does not respond to ##{method}") {
390
+ input.respond_to? method
391
+ }
392
+ }
393
+ end
394
+
395
+ class InputWrapper
396
+ include Assertion
397
+
398
+ def initialize(input)
399
+ @input = input
400
+ end
401
+
402
+ ## * +gets+ must be called without arguments and return a string,
403
+ ## or +nil+ on EOF.
404
+ def gets(*args)
405
+ assert("rack.input#gets called with arguments") { args.size == 0 }
406
+ v = @input.gets
407
+ assert("rack.input#gets didn't return a String") {
408
+ v.nil? or v.kind_of? String
409
+ }
410
+ v
411
+ end
412
+
413
+ ## * +read+ behaves like IO#read.
414
+ ## Its signature is <tt>read([length, [buffer]])</tt>.
415
+ ##
416
+ ## If given, +length+ must be a non-negative Integer (>= 0) or +nil+,
417
+ ## and +buffer+ must be a String and may not be nil.
418
+ ##
419
+ ## If +length+ is given and not nil, then this method reads at most
420
+ ## +length+ bytes from the input stream.
421
+ ##
422
+ ## If +length+ is not given or nil, then this method reads
423
+ ## all data until EOF.
424
+ ##
425
+ ## When EOF is reached, this method returns nil if +length+ is given
426
+ ## and not nil, or "" if +length+ is not given or is nil.
427
+ ##
428
+ ## If +buffer+ is given, then the read data will be placed
429
+ ## into +buffer+ instead of a newly created String object.
430
+ def read(*args)
431
+ assert("rack.input#read called with too many arguments") {
432
+ args.size <= 2
433
+ }
434
+ if args.size >= 1
435
+ assert("rack.input#read called with non-integer and non-nil length") {
436
+ args.first.kind_of?(Integer) || args.first.nil?
437
+ }
438
+ assert("rack.input#read called with a negative length") {
439
+ args.first.nil? || args.first >= 0
440
+ }
441
+ end
442
+ if args.size >= 2
443
+ assert("rack.input#read called with non-String buffer") {
444
+ args[1].kind_of?(String)
445
+ }
446
+ end
447
+
448
+ v = @input.read(*args)
449
+
450
+ assert("rack.input#read didn't return nil or a String") {
451
+ v.nil? or v.kind_of? String
452
+ }
453
+ if args[0].nil?
454
+ assert("rack.input#read(nil) returned nil on EOF") {
455
+ !v.nil?
456
+ }
457
+ end
458
+
459
+ v
460
+ end
461
+
462
+ ## * +each+ must be called without arguments and only yield Strings.
463
+ def each(*args)
464
+ assert("rack.input#each called with arguments") { args.size == 0 }
465
+ @input.each { |line|
466
+ assert("rack.input#each didn't yield a String") {
467
+ line.kind_of? String
468
+ }
469
+ yield line
470
+ }
471
+ end
472
+
473
+ ## * +rewind+ must be called without arguments. It rewinds the input
474
+ ## stream back to the beginning. It must not raise Errno::ESPIPE:
475
+ ## that is, it may not be a pipe or a socket. Therefore, handler
476
+ ## developers must buffer the input data into some rewindable object
477
+ ## if the underlying input stream is not rewindable.
478
+ def rewind(*args)
479
+ assert("rack.input#rewind called with arguments") { args.size == 0 }
480
+ assert("rack.input#rewind raised Errno::ESPIPE") {
481
+ begin
482
+ @input.rewind
483
+ true
484
+ rescue Errno::ESPIPE
485
+ false
486
+ end
487
+ }
488
+ end
489
+
490
+ ## * +close+ must never be called on the input stream.
491
+ def close(*args)
492
+ assert("rack.input#close must not be called") { false }
493
+ end
494
+ end
495
+
496
+ ## === The Error Stream
497
+ def check_error(error)
498
+ ## The error stream must respond to +puts+, +write+ and +flush+.
499
+ [:puts, :write, :flush].each { |method|
500
+ assert("rack.error #{error} does not respond to ##{method}") {
501
+ error.respond_to? method
502
+ }
503
+ }
504
+ end
505
+
506
+ class ErrorWrapper
507
+ include Assertion
508
+
509
+ def initialize(error)
510
+ @error = error
511
+ end
512
+
513
+ ## * +puts+ must be called with a single argument that responds to +to_s+.
514
+ def puts(str)
515
+ @error.puts str
516
+ end
517
+
518
+ ## * +write+ must be called with a single argument that is a String.
519
+ def write(str)
520
+ assert("rack.errors#write not called with a String") { str.kind_of? String }
521
+ @error.write str
522
+ end
523
+
524
+ ## * +flush+ must be called without arguments and must be called
525
+ ## in order to make the error appear for sure.
526
+ def flush
527
+ @error.flush
528
+ end
529
+
530
+ ## * +close+ must never be called on the error stream.
531
+ def close(*args)
532
+ assert("rack.errors#close must not be called") { false }
533
+ end
534
+ end
535
+
536
+ class HijackWrapper
537
+ include Assertion
538
+ extend Forwardable
539
+
540
+ REQUIRED_METHODS = [
541
+ :read, :write, :read_nonblock, :write_nonblock, :flush, :close,
542
+ :close_read, :close_write, :closed?
543
+ ]
544
+
545
+ def_delegators :@io, *REQUIRED_METHODS
546
+
547
+ def initialize(io)
548
+ @io = io
549
+ REQUIRED_METHODS.each do |meth|
550
+ assert("rack.hijack_io must respond to #{meth}") { io.respond_to? meth }
551
+ end
552
+ end
553
+ end
554
+
555
+ ## === Hijacking
556
+ #
557
+ # AUTHORS: n.b. The trailing whitespace between paragraphs is important and
558
+ # should not be removed. The whitespace creates paragraphs in the RDoc
559
+ # output.
560
+ #
561
+ ## ==== Request (before status)
562
+ def check_hijack(env)
563
+ if env[RACK_IS_HIJACK]
564
+ ## If rack.hijack? is true then rack.hijack must respond to #call.
565
+ original_hijack = env[RACK_HIJACK]
566
+ assert("rack.hijack must respond to call") { original_hijack.respond_to?(:call) }
567
+ env[RACK_HIJACK] = proc do
568
+ ## rack.hijack must return the io that will also be assigned (or is
569
+ ## already present, in rack.hijack_io.
570
+ io = original_hijack.call
571
+ HijackWrapper.new(io)
572
+ ##
573
+ ## rack.hijack_io must respond to:
574
+ ## <tt>read, write, read_nonblock, write_nonblock, flush, close,
575
+ ## close_read, close_write, closed?</tt>
576
+ ##
577
+ ## The semantics of these IO methods must be a best effort match to
578
+ ## those of a normal ruby IO or Socket object, using standard
579
+ ## arguments and raising standard exceptions. Servers are encouraged
580
+ ## to simply pass on real IO objects, although it is recognized that
581
+ ## this approach is not directly compatible with SPDY and HTTP 2.0.
582
+ ##
583
+ ## IO provided in rack.hijack_io should preference the
584
+ ## IO::WaitReadable and IO::WaitWritable APIs wherever supported.
585
+ ##
586
+ ## There is a deliberate lack of full specification around
587
+ ## rack.hijack_io, as semantics will change from server to server.
588
+ ## Users are encouraged to utilize this API with a knowledge of their
589
+ ## server choice, and servers may extend the functionality of
590
+ ## hijack_io to provide additional features to users. The purpose of
591
+ ## rack.hijack is for Rack to "get out of the way", as such, Rack only
592
+ ## provides the minimum of specification and support.
593
+ env[RACK_HIJACK_IO] = HijackWrapper.new(env[RACK_HIJACK_IO])
594
+ io
595
+ end
596
+ else
597
+ ##
598
+ ## If rack.hijack? is false, then rack.hijack should not be set.
599
+ assert("rack.hijack? is false, but rack.hijack is present") { env[RACK_HIJACK].nil? }
600
+ ##
601
+ ## If rack.hijack? is false, then rack.hijack_io should not be set.
602
+ assert("rack.hijack? is false, but rack.hijack_io is present") { env[RACK_HIJACK_IO].nil? }
603
+ end
604
+ end
605
+
606
+ ## ==== Response (after headers)
607
+ ## It is also possible to hijack a response after the status and headers
608
+ ## have been sent.
609
+ def check_hijack_response(headers, env)
610
+
611
+ # this check uses headers like a hash, but the spec only requires
612
+ # headers respond to #each
613
+ headers = Rack::Utils::HeaderHash[headers]
614
+
615
+ ## In order to do this, an application may set the special header
616
+ ## <tt>rack.hijack</tt> to an object that responds to <tt>call</tt>
617
+ ## accepting an argument that conforms to the <tt>rack.hijack_io</tt>
618
+ ## protocol.
619
+ ##
620
+ ## After the headers have been sent, and this hijack callback has been
621
+ ## called, the application is now responsible for the remaining lifecycle
622
+ ## of the IO. The application is also responsible for maintaining HTTP
623
+ ## semantics. Of specific note, in almost all cases in the current SPEC,
624
+ ## applications will have wanted to specify the header Connection:close in
625
+ ## HTTP/1.1, and not Connection:keep-alive, as there is no protocol for
626
+ ## returning hijacked sockets to the web server. For that purpose, use the
627
+ ## body streaming API instead (progressively yielding strings via each).
628
+ ##
629
+ ## Servers must ignore the <tt>body</tt> part of the response tuple when
630
+ ## the <tt>rack.hijack</tt> response API is in use.
631
+
632
+ if env[RACK_IS_HIJACK] && headers[RACK_HIJACK]
633
+ assert('rack.hijack header must respond to #call') {
634
+ headers[RACK_HIJACK].respond_to? :call
635
+ }
636
+ original_hijack = headers[RACK_HIJACK]
637
+ proc do |io|
638
+ original_hijack.call HijackWrapper.new(io)
639
+ end
640
+ else
641
+ ##
642
+ ## The special response header <tt>rack.hijack</tt> must only be set
643
+ ## if the request env has <tt>rack.hijack?</tt> <tt>true</tt>.
644
+ assert('rack.hijack header must not be present if server does not support hijacking') {
645
+ headers[RACK_HIJACK].nil?
646
+ }
647
+
648
+ nil
649
+ end
650
+ end
651
+ ## ==== Conventions
652
+ ## * Middleware should not use hijack unless it is handling the whole
653
+ ## response.
654
+ ## * Middleware may wrap the IO object for the response pattern.
655
+ ## * Middleware should not wrap the IO object for the request pattern. The
656
+ ## request pattern is intended to provide the hijacker with "raw tcp".
657
+
658
+ ## == The Response
659
+
660
+ ## === The Status
661
+ def check_status(status)
662
+ ## This is an HTTP status. When parsed as integer (+to_i+), it must be
663
+ ## greater than or equal to 100.
664
+ assert("Status must be >=100 seen as integer") { status.to_i >= 100 }
665
+ end
666
+
667
+ ## === The Headers
668
+ def check_headers(header)
669
+ ## The header must respond to +each+, and yield values of key and value.
670
+ assert("headers object should respond to #each, but doesn't (got #{header.class} as headers)") {
671
+ header.respond_to? :each
672
+ }
673
+
674
+ header.each { |key, value|
675
+ ## The header keys must be Strings.
676
+ assert("header key must be a string, was #{key.class}") {
677
+ key.kind_of? String
678
+ }
679
+
680
+ ## Special headers starting "rack." are for communicating with the
681
+ ## server, and must not be sent back to the client.
682
+ next if key =~ /^rack\..+$/
683
+
684
+ ## The header must not contain a +Status+ key.
685
+ assert("header must not contain Status") { key.downcase != "status" }
686
+ ## The header must conform to RFC7230 token specification, i.e. cannot
687
+ ## contain non-printable ASCII, DQUOTE or "(),/:;<=>?@[\]{}".
688
+ assert("invalid header name: #{key}") { key !~ /[\(\),\/:;<=>\?@\[\\\]{}[:cntrl:]]/ }
689
+
690
+ ## The values of the header must be Strings,
691
+ assert("a header value must be a String, but the value of " +
692
+ "'#{key}' is a #{value.class}") { value.kind_of? String }
693
+ ## consisting of lines (for multiple header values, e.g. multiple
694
+ ## <tt>Set-Cookie</tt> values) separated by "\\n".
695
+ value.split("\n").each { |item|
696
+ ## The lines must not contain characters below 037.
697
+ assert("invalid header value #{key}: #{item.inspect}") {
698
+ item !~ /[\000-\037]/
699
+ }
700
+ }
701
+ }
702
+ end
703
+
704
+ ## === The Content-Type
705
+ def check_content_type(status, headers)
706
+ headers.each { |key, value|
707
+ ## There must not be a <tt>Content-Type</tt>, when the +Status+ is 1xx,
708
+ ## 204 or 304.
709
+ if key.downcase == "content-type"
710
+ assert("Content-Type header found in #{status} response, not allowed") {
711
+ not Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.key? status.to_i
712
+ }
713
+ return
714
+ end
715
+ }
716
+ end
717
+
718
+ ## === The Content-Length
719
+ def check_content_length(status, headers)
720
+ headers.each { |key, value|
721
+ if key.downcase == 'content-length'
722
+ ## There must not be a <tt>Content-Length</tt> header when the
723
+ ## +Status+ is 1xx, 204 or 304.
724
+ assert("Content-Length header found in #{status} response, not allowed") {
725
+ not Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.key? status.to_i
726
+ }
727
+ @content_length = value
728
+ end
729
+ }
730
+ end
731
+
732
+ def verify_content_length(bytes)
733
+ if @head_request
734
+ assert("Response body was given for HEAD request, but should be empty") {
735
+ bytes == 0
736
+ }
737
+ elsif @content_length
738
+ assert("Content-Length header was #{@content_length}, but should be #{bytes}") {
739
+ @content_length == bytes.to_s
740
+ }
741
+ end
742
+ end
743
+
744
+ ## === The Body
745
+ def each
746
+ @closed = false
747
+ bytes = 0
748
+
749
+ ## The Body must respond to +each+
750
+ assert("Response body must respond to each") do
751
+ @body.respond_to?(:each)
752
+ end
753
+
754
+ @body.each { |part|
755
+ ## and must only yield String values.
756
+ assert("Body yielded non-string value #{part.inspect}") {
757
+ part.kind_of? String
758
+ }
759
+ bytes += part.bytesize
760
+ yield part
761
+ }
762
+ verify_content_length(bytes)
763
+
764
+ ##
765
+ ## The Body itself should not be an instance of String, as this will
766
+ ## break in Ruby 1.9.
767
+ ##
768
+ ## If the Body responds to +close+, it will be called after iteration. If
769
+ ## the body is replaced by a middleware after action, the original body
770
+ ## must be closed first, if it responds to close.
771
+ # XXX howto: assert("Body has not been closed") { @closed }
772
+
773
+
774
+ ##
775
+ ## If the Body responds to +to_path+, it must return a String
776
+ ## identifying the location of a file whose contents are identical
777
+ ## to that produced by calling +each+; this may be used by the
778
+ ## server as an alternative, possibly more efficient way to
779
+ ## transport the response.
780
+
781
+ if @body.respond_to?(:to_path)
782
+ assert("The file identified by body.to_path does not exist") {
783
+ ::File.exist? @body.to_path
784
+ }
785
+ end
786
+
787
+ ##
788
+ ## The Body commonly is an Array of Strings, the application
789
+ ## instance itself, or a File-like object.
790
+ end
791
+
792
+ def close
793
+ @closed = true
794
+ @body.close if @body.respond_to?(:close)
795
+ end
796
+
797
+ # :startdoc:
798
+
799
+ end
800
+ end
801
+
802
+ ## == Thanks
803
+ ## Some parts of this specification are adopted from PEP333: Python
804
+ ## Web Server Gateway Interface
805
+ ## v1.0 (http://www.python.org/dev/peps/pep-0333/). I'd like to thank
806
+ ## everyone involved in that effort.