shopify-cli 1.3.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +2 -2
  3. data/.github/CONTRIBUTING.md +9 -1
  4. data/.github/PULL_REQUEST_TEMPLATE.md +10 -1
  5. data/.github/workflows/release.yml +61 -0
  6. data/.github/workflows/triage.yml +22 -0
  7. data/.gitignore +0 -1
  8. data/.rubocop.yml +61 -8
  9. data/.rubocop_todo.yml +11 -0
  10. data/.travis.yml +1 -0
  11. data/CHANGELOG.md +30 -0
  12. data/Gemfile +3 -2
  13. data/Gemfile.lock +39 -37
  14. data/README.md +39 -7
  15. data/RELEASING.md +19 -29
  16. data/Rakefile +2 -0
  17. data/dev.yml +2 -2
  18. data/docs/_config.yml +1 -18
  19. data/docs/app/node/commands/index.md +2 -80
  20. data/docs/app/node/index.md +2 -33
  21. data/docs/app/rails/commands/index.md +2 -78
  22. data/docs/app/rails/index.md +2 -34
  23. data/docs/core/index.md +2 -84
  24. data/docs/getting-started/index.md +2 -25
  25. data/docs/getting-started/install/index.md +1 -118
  26. data/docs/getting-started/migrate/index.md +2 -94
  27. data/docs/getting-started/uninstall/index.md +2 -35
  28. data/docs/getting-started/upgrade/index.md +2 -39
  29. data/docs/help/start-app/index.md +2 -4
  30. data/docs/index.md +2 -24
  31. data/install.sh +1 -1
  32. data/lib/project_types/extension/cli.rb +21 -11
  33. data/lib/project_types/extension/commands/extension_command.rb +2 -2
  34. data/lib/project_types/extension/features/argo.rb +117 -0
  35. data/lib/project_types/extension/forms/create.rb +2 -2
  36. data/lib/project_types/extension/models/specification.rb +35 -0
  37. data/lib/project_types/extension/models/specification_handlers/checkout_post_purchase.rb +19 -0
  38. data/lib/project_types/extension/models/specification_handlers/default.rb +67 -0
  39. data/lib/project_types/extension/models/specifications.rb +77 -0
  40. data/lib/project_types/extension/tasks/configure_features.rb +52 -0
  41. data/lib/project_types/extension/tasks/fetch_specifications.rb +38 -0
  42. data/lib/project_types/node/cli.rb +4 -1
  43. data/lib/project_types/node/commands/connect.rb +15 -0
  44. data/lib/project_types/node/commands/create.rb +10 -4
  45. data/lib/project_types/node/commands/generate.rb +2 -11
  46. data/lib/project_types/node/messages/messages.rb +16 -50
  47. data/lib/project_types/rails/cli.rb +4 -1
  48. data/lib/project_types/rails/commands/connect.rb +15 -0
  49. data/lib/project_types/rails/commands/create.rb +15 -12
  50. data/lib/project_types/rails/forms/create.rb +1 -1
  51. data/lib/project_types/rails/gem.rb +1 -1
  52. data/lib/project_types/rails/messages/messages.rb +8 -5
  53. data/lib/project_types/script/cli.rb +9 -5
  54. data/lib/project_types/script/commands/create.rb +6 -4
  55. data/lib/project_types/script/commands/enable.rb +12 -4
  56. data/lib/project_types/script/commands/push.rb +5 -13
  57. data/lib/project_types/script/config/extension_points.yml +17 -12
  58. data/lib/project_types/script/errors.rb +21 -0
  59. data/lib/project_types/script/forms/create.rb +26 -2
  60. data/lib/project_types/script/graphql/app_script_update_or_create.graphql +10 -1
  61. data/lib/project_types/script/layers/application/build_script.rb +18 -17
  62. data/lib/project_types/script/layers/application/create_script.rb +12 -10
  63. data/lib/project_types/script/layers/application/extension_points.rb +24 -0
  64. data/lib/project_types/script/layers/application/push_script.rb +18 -16
  65. data/lib/project_types/script/layers/domain/errors.rb +7 -0
  66. data/lib/project_types/script/layers/domain/extension_point.rb +62 -7
  67. data/lib/project_types/script/layers/domain/metadata.rb +55 -0
  68. data/lib/project_types/script/layers/domain/push_package.rb +25 -6
  69. data/lib/project_types/script/layers/infrastructure/assemblyscript_project_creator.rb +17 -52
  70. data/lib/project_types/script/layers/infrastructure/assemblyscript_task_runner.rb +42 -11
  71. data/lib/project_types/script/layers/infrastructure/errors.rb +16 -0
  72. data/lib/project_types/script/layers/infrastructure/extension_point_repository.rb +10 -4
  73. data/lib/project_types/script/layers/infrastructure/project_creator.rb +2 -1
  74. data/lib/project_types/script/layers/infrastructure/push_package_repository.rb +25 -13
  75. data/lib/project_types/script/layers/infrastructure/rust_project_creator.rb +72 -0
  76. data/lib/project_types/script/layers/infrastructure/rust_task_runner.rb +59 -0
  77. data/lib/project_types/script/layers/infrastructure/script_service.rb +9 -1
  78. data/lib/project_types/script/layers/infrastructure/task_runner.rb +4 -3
  79. data/lib/project_types/script/messages/messages.rb +55 -4
  80. data/lib/project_types/script/script_project.rb +25 -16
  81. data/lib/project_types/script/ui/error_handler.rb +59 -1
  82. data/lib/project_types/theme/cli.rb +40 -0
  83. data/lib/project_types/theme/commands/connect.rb +54 -0
  84. data/lib/project_types/theme/commands/create.rb +48 -0
  85. data/lib/project_types/theme/commands/deploy.rb +38 -0
  86. data/lib/project_types/theme/commands/generate.rb +20 -0
  87. data/lib/project_types/theme/commands/generate/env.rb +79 -0
  88. data/lib/project_types/theme/commands/push.rb +55 -0
  89. data/lib/project_types/theme/commands/serve.rb +31 -0
  90. data/lib/project_types/theme/forms/connect.rb +34 -0
  91. data/lib/project_types/theme/forms/create.rb +22 -0
  92. data/lib/project_types/theme/messages/messages.rb +147 -0
  93. data/lib/project_types/theme/tasks/ensure_themekit_installed.rb +78 -0
  94. data/lib/project_types/theme/themekit.rb +113 -0
  95. data/lib/shopify-cli/admin_api.rb +42 -2
  96. data/lib/shopify-cli/api.rb +34 -33
  97. data/lib/shopify-cli/commands/config.rb +24 -0
  98. data/lib/shopify-cli/commands/connect.rb +32 -15
  99. data/lib/shopify-cli/commands/system.rb +10 -1
  100. data/lib/shopify-cli/context.rb +23 -2
  101. data/lib/shopify-cli/core/entry_point.rb +1 -1
  102. data/lib/shopify-cli/core/monorail.rb +6 -4
  103. data/lib/shopify-cli/feature.rb +0 -2
  104. data/lib/shopify-cli/http_request.rb +27 -0
  105. data/lib/shopify-cli/js_deps.rb +1 -1
  106. data/lib/shopify-cli/messages/messages.rb +31 -7
  107. data/lib/shopify-cli/method_object.rb +104 -0
  108. data/lib/shopify-cli/partners_api.rb +25 -3
  109. data/lib/shopify-cli/process_supervision.rb +1 -1
  110. data/lib/shopify-cli/project.rb +12 -8
  111. data/lib/shopify-cli/project_type.rb +18 -2
  112. data/lib/shopify-cli/resolve_constant.rb +25 -0
  113. data/lib/shopify-cli/result.rb +432 -0
  114. data/lib/shopify-cli/shopifolk.rb +87 -0
  115. data/lib/shopify-cli/task.rb +8 -0
  116. data/lib/shopify-cli/tasks/create_api_client.rb +13 -2
  117. data/lib/shopify-cli/tasks/ensure_env.rb +3 -0
  118. data/lib/shopify-cli/tasks/select_org_and_shop.rb +10 -5
  119. data/lib/shopify-cli/tunnel.rb +8 -2
  120. data/lib/shopify-cli/version.rb +1 -1
  121. data/lib/shopify_cli.rb +5 -1
  122. data/shopify.fish +1 -1
  123. data/shopify.sh +1 -1
  124. data/vendor/deps/cli-kit/REVISION +1 -1
  125. data/vendor/deps/cli-kit/lib/cli/kit/logger.rb +2 -2
  126. data/vendor/deps/cli-kit/lib/cli/kit/system.rb +3 -3
  127. data/vendor/deps/cli-ui/REVISION +1 -1
  128. data/vendor/deps/cli-ui/lib/cli/ui.rb +26 -22
  129. data/vendor/deps/cli-ui/lib/cli/ui/ansi.rb +4 -6
  130. data/vendor/deps/cli-ui/lib/cli/ui/frame.rb +3 -3
  131. data/vendor/deps/cli-ui/lib/cli/ui/frame/frame_stack.rb +8 -9
  132. data/vendor/deps/cli-ui/lib/cli/ui/frame/frame_style.rb +1 -1
  133. data/vendor/deps/cli-ui/lib/cli/ui/glyph.rb +1 -0
  134. data/vendor/deps/cli-ui/lib/cli/ui/printer.rb +15 -3
  135. data/vendor/deps/cli-ui/lib/cli/ui/prompt/interactive_options.rb +4 -11
  136. data/vendor/deps/cli-ui/lib/cli/ui/spinner.rb +3 -5
  137. data/vendor/deps/cli-ui/lib/cli/ui/terminal.rb +10 -10
  138. data/vendor/deps/cli-ui/lib/cli/ui/version.rb +1 -1
  139. data/vendor/deps/cli-ui/lib/cli/ui/wrap.rb +56 -0
  140. data/vendor/deps/webrick/.gitignore +9 -0
  141. data/vendor/deps/webrick/Gemfile +3 -0
  142. data/vendor/deps/webrick/LICENSE.txt +22 -0
  143. data/vendor/deps/webrick/README.md +61 -0
  144. data/vendor/deps/webrick/Rakefile +10 -0
  145. data/vendor/deps/webrick/lib/webrick.rb +232 -0
  146. data/vendor/deps/webrick/lib/webrick/accesslog.rb +157 -0
  147. data/vendor/deps/webrick/lib/webrick/cgi.rb +313 -0
  148. data/vendor/deps/webrick/lib/webrick/compat.rb +36 -0
  149. data/vendor/deps/webrick/lib/webrick/config.rb +158 -0
  150. data/vendor/deps/webrick/lib/webrick/cookie.rb +172 -0
  151. data/vendor/deps/webrick/lib/webrick/htmlutils.rb +30 -0
  152. data/vendor/deps/webrick/lib/webrick/httpauth.rb +96 -0
  153. data/vendor/deps/webrick/lib/webrick/httpauth/authenticator.rb +117 -0
  154. data/vendor/deps/webrick/lib/webrick/httpauth/basicauth.rb +116 -0
  155. data/vendor/deps/webrick/lib/webrick/httpauth/digestauth.rb +395 -0
  156. data/vendor/deps/webrick/lib/webrick/httpauth/htdigest.rb +132 -0
  157. data/vendor/deps/webrick/lib/webrick/httpauth/htgroup.rb +97 -0
  158. data/vendor/deps/webrick/lib/webrick/httpauth/htpasswd.rb +158 -0
  159. data/vendor/deps/webrick/lib/webrick/httpauth/userdb.rb +53 -0
  160. data/vendor/deps/webrick/lib/webrick/httpproxy.rb +354 -0
  161. data/vendor/deps/webrick/lib/webrick/httprequest.rb +636 -0
  162. data/vendor/deps/webrick/lib/webrick/httpresponse.rb +564 -0
  163. data/vendor/deps/webrick/lib/webrick/https.rb +152 -0
  164. data/vendor/deps/webrick/lib/webrick/httpserver.rb +294 -0
  165. data/vendor/deps/webrick/lib/webrick/httpservlet.rb +23 -0
  166. data/vendor/deps/webrick/lib/webrick/httpservlet/abstract.rb +152 -0
  167. data/vendor/deps/webrick/lib/webrick/httpservlet/cgi_runner.rb +47 -0
  168. data/vendor/deps/webrick/lib/webrick/httpservlet/cgihandler.rb +126 -0
  169. data/vendor/deps/webrick/lib/webrick/httpservlet/erbhandler.rb +88 -0
  170. data/vendor/deps/webrick/lib/webrick/httpservlet/filehandler.rb +552 -0
  171. data/vendor/deps/webrick/lib/webrick/httpservlet/prochandler.rb +47 -0
  172. data/vendor/deps/webrick/lib/webrick/httpstatus.rb +194 -0
  173. data/vendor/deps/webrick/lib/webrick/httputils.rb +512 -0
  174. data/vendor/deps/webrick/lib/webrick/httpversion.rb +76 -0
  175. data/vendor/deps/webrick/lib/webrick/log.rb +156 -0
  176. data/vendor/deps/webrick/lib/webrick/server.rb +381 -0
  177. data/vendor/deps/webrick/lib/webrick/ssl.rb +215 -0
  178. data/vendor/deps/webrick/lib/webrick/utils.rb +265 -0
  179. data/vendor/deps/webrick/lib/webrick/version.rb +18 -0
  180. data/vendor/deps/webrick/webrick.gemspec +74 -0
  181. metadata +77 -27
  182. data/docs/Gemfile +0 -5
  183. data/docs/Gemfile.lock +0 -258
  184. data/docs/_data/nav.yml +0 -35
  185. data/docs/_includes/footer.html +0 -15
  186. data/docs/_includes/head.html +0 -19
  187. data/docs/_includes/sidebar_nav.html +0 -22
  188. data/docs/_includes/toc.html +0 -112
  189. data/docs/_layouts/default.html +0 -79
  190. data/docs/css/docs.css +0 -157
  191. data/docs/images/header.png +0 -0
  192. data/docs/installing-ruby.md +0 -28
  193. data/lib/project_types/extension/features/argo/admin.rb +0 -20
  194. data/lib/project_types/extension/features/argo/base.rb +0 -129
  195. data/lib/project_types/extension/features/argo/checkout.rb +0 -20
  196. data/lib/project_types/extension/models/type.rb +0 -81
  197. data/lib/project_types/extension/models/types/checkout_post_purchase.rb +0 -23
  198. data/lib/project_types/extension/models/types/product_subscription.rb +0 -24
  199. data/lib/project_types/node/commands/generate/billing.rb +0 -39
  200. data/lib/project_types/node/commands/generate/page.rb +0 -59
  201. data/lib/project_types/node/commands/generate/webhook.rb +0 -37
  202. data/lib/project_types/script/layers/domain/script.rb +0 -18
  203. data/lib/project_types/script/layers/infrastructure/assemblyscript_tsconfig.rb +0 -38
  204. data/lib/project_types/script/layers/infrastructure/script_repository.rb +0 -59
  205. data/lib/project_types/script/templates/ts/as-pect.config.js +0 -27
  206. data/lib/project_types/script/templates/ts/as-pect.d.ts +0 -1
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # prochandler.rb -- ProcHandler Class
4
+ #
5
+ # Author: IPR -- Internet Programming with Ruby -- writers
6
+ # Copyright (c) 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
7
+ # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
8
+ # reserved.
9
+ #
10
+ # $IPR: prochandler.rb,v 1.7 2002/09/21 12:23:42 gotoyuzo Exp $
11
+
12
+ require_relative 'abstract'
13
+
14
+ module WEBrick
15
+ module HTTPServlet
16
+
17
+ ##
18
+ # Mounts a proc at a path that accepts a request and response.
19
+ #
20
+ # Instead of mounting this servlet with WEBrick::HTTPServer#mount use
21
+ # WEBrick::HTTPServer#mount_proc:
22
+ #
23
+ # server.mount_proc '/' do |req, res|
24
+ # res.body = 'it worked!'
25
+ # res.status = 200
26
+ # end
27
+
28
+ class ProcHandler < AbstractServlet
29
+ # :stopdoc:
30
+ def get_instance(server, *options)
31
+ self
32
+ end
33
+
34
+ def initialize(proc)
35
+ @proc = proc
36
+ end
37
+
38
+ def do_GET(request, response)
39
+ @proc.call(request, response)
40
+ end
41
+
42
+ alias do_POST do_GET
43
+ # :startdoc:
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,194 @@
1
+ # frozen_string_literal: true
2
+ #--
3
+ # httpstatus.rb -- HTTPStatus Class
4
+ #
5
+ # Author: IPR -- Internet Programming with Ruby -- writers
6
+ # Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
7
+ # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
8
+ # reserved.
9
+ #
10
+ # $IPR: httpstatus.rb,v 1.11 2003/03/24 20:18:55 gotoyuzo Exp $
11
+
12
+ require_relative 'accesslog'
13
+
14
+ module WEBrick
15
+
16
+ ##
17
+ # This module is used to manager HTTP status codes.
18
+ #
19
+ # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for more
20
+ # information.
21
+ module HTTPStatus
22
+
23
+ ##
24
+ # Root of the HTTP status class hierarchy
25
+ class Status < StandardError
26
+ class << self
27
+ attr_reader :code, :reason_phrase # :nodoc:
28
+ end
29
+
30
+ # Returns the HTTP status code
31
+ def code() self::class::code end
32
+
33
+ # Returns the HTTP status description
34
+ def reason_phrase() self::class::reason_phrase end
35
+
36
+ alias to_i code # :nodoc:
37
+ end
38
+
39
+ # Root of the HTTP info statuses
40
+ class Info < Status; end
41
+ # Root of the HTTP success statuses
42
+ class Success < Status; end
43
+ # Root of the HTTP redirect statuses
44
+ class Redirect < Status; end
45
+ # Root of the HTTP error statuses
46
+ class Error < Status; end
47
+ # Root of the HTTP client error statuses
48
+ class ClientError < Error; end
49
+ # Root of the HTTP server error statuses
50
+ class ServerError < Error; end
51
+
52
+ class EOFError < StandardError; end
53
+
54
+ # HTTP status codes and descriptions
55
+ StatusMessage = { # :nodoc:
56
+ 100 => 'Continue',
57
+ 101 => 'Switching Protocols',
58
+ 200 => 'OK',
59
+ 201 => 'Created',
60
+ 202 => 'Accepted',
61
+ 203 => 'Non-Authoritative Information',
62
+ 204 => 'No Content',
63
+ 205 => 'Reset Content',
64
+ 206 => 'Partial Content',
65
+ 207 => 'Multi-Status',
66
+ 300 => 'Multiple Choices',
67
+ 301 => 'Moved Permanently',
68
+ 302 => 'Found',
69
+ 303 => 'See Other',
70
+ 304 => 'Not Modified',
71
+ 305 => 'Use Proxy',
72
+ 307 => 'Temporary Redirect',
73
+ 400 => 'Bad Request',
74
+ 401 => 'Unauthorized',
75
+ 402 => 'Payment Required',
76
+ 403 => 'Forbidden',
77
+ 404 => 'Not Found',
78
+ 405 => 'Method Not Allowed',
79
+ 406 => 'Not Acceptable',
80
+ 407 => 'Proxy Authentication Required',
81
+ 408 => 'Request Timeout',
82
+ 409 => 'Conflict',
83
+ 410 => 'Gone',
84
+ 411 => 'Length Required',
85
+ 412 => 'Precondition Failed',
86
+ 413 => 'Request Entity Too Large',
87
+ 414 => 'Request-URI Too Large',
88
+ 415 => 'Unsupported Media Type',
89
+ 416 => 'Request Range Not Satisfiable',
90
+ 417 => 'Expectation Failed',
91
+ 422 => 'Unprocessable Entity',
92
+ 423 => 'Locked',
93
+ 424 => 'Failed Dependency',
94
+ 426 => 'Upgrade Required',
95
+ 428 => 'Precondition Required',
96
+ 429 => 'Too Many Requests',
97
+ 431 => 'Request Header Fields Too Large',
98
+ 451 => 'Unavailable For Legal Reasons',
99
+ 500 => 'Internal Server Error',
100
+ 501 => 'Not Implemented',
101
+ 502 => 'Bad Gateway',
102
+ 503 => 'Service Unavailable',
103
+ 504 => 'Gateway Timeout',
104
+ 505 => 'HTTP Version Not Supported',
105
+ 507 => 'Insufficient Storage',
106
+ 511 => 'Network Authentication Required',
107
+ }
108
+
109
+ # Maps a status code to the corresponding Status class
110
+ CodeToError = {} # :nodoc:
111
+
112
+ # Creates a status or error class for each status code and
113
+ # populates the CodeToError map.
114
+ StatusMessage.each{|code, message|
115
+ message.freeze
116
+ var_name = message.gsub(/[ \-]/,'_').upcase
117
+ err_name = message.gsub(/[ \-]/,'')
118
+
119
+ case code
120
+ when 100...200; parent = Info
121
+ when 200...300; parent = Success
122
+ when 300...400; parent = Redirect
123
+ when 400...500; parent = ClientError
124
+ when 500...600; parent = ServerError
125
+ end
126
+
127
+ const_set("RC_#{var_name}", code)
128
+ err_class = Class.new(parent)
129
+ err_class.instance_variable_set(:@code, code)
130
+ err_class.instance_variable_set(:@reason_phrase, message)
131
+ const_set(err_name, err_class)
132
+ CodeToError[code] = err_class
133
+ }
134
+
135
+ ##
136
+ # Returns the description corresponding to the HTTP status +code+
137
+ #
138
+ # WEBrick::HTTPStatus.reason_phrase 404
139
+ # => "Not Found"
140
+ def reason_phrase(code)
141
+ StatusMessage[code.to_i]
142
+ end
143
+
144
+ ##
145
+ # Is +code+ an informational status?
146
+ def info?(code)
147
+ code.to_i >= 100 and code.to_i < 200
148
+ end
149
+
150
+ ##
151
+ # Is +code+ a successful status?
152
+ def success?(code)
153
+ code.to_i >= 200 and code.to_i < 300
154
+ end
155
+
156
+ ##
157
+ # Is +code+ a redirection status?
158
+ def redirect?(code)
159
+ code.to_i >= 300 and code.to_i < 400
160
+ end
161
+
162
+ ##
163
+ # Is +code+ an error status?
164
+ def error?(code)
165
+ code.to_i >= 400 and code.to_i < 600
166
+ end
167
+
168
+ ##
169
+ # Is +code+ a client error status?
170
+ def client_error?(code)
171
+ code.to_i >= 400 and code.to_i < 500
172
+ end
173
+
174
+ ##
175
+ # Is +code+ a server error status?
176
+ def server_error?(code)
177
+ code.to_i >= 500 and code.to_i < 600
178
+ end
179
+
180
+ ##
181
+ # Returns the status class corresponding to +code+
182
+ #
183
+ # WEBrick::HTTPStatus[302]
184
+ # => WEBrick::HTTPStatus::NotFound
185
+ #
186
+ def self.[](code)
187
+ CodeToError[code]
188
+ end
189
+
190
+ module_function :reason_phrase
191
+ module_function :info?, :success?, :redirect?, :error?
192
+ module_function :client_error?, :server_error?
193
+ end
194
+ end
@@ -0,0 +1,512 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # httputils.rb -- HTTPUtils Module
4
+ #
5
+ # Author: IPR -- Internet Programming with Ruby -- writers
6
+ # Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
7
+ # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
8
+ # reserved.
9
+ #
10
+ # $IPR: httputils.rb,v 1.34 2003/06/05 21:34:08 gotoyuzo Exp $
11
+
12
+ require 'socket'
13
+ require 'tempfile'
14
+
15
+ module WEBrick
16
+ CR = "\x0d" # :nodoc:
17
+ LF = "\x0a" # :nodoc:
18
+ CRLF = "\x0d\x0a" # :nodoc:
19
+
20
+ ##
21
+ # HTTPUtils provides utility methods for working with the HTTP protocol.
22
+ #
23
+ # This module is generally used internally by WEBrick
24
+
25
+ module HTTPUtils
26
+
27
+ ##
28
+ # Normalizes a request path. Raises an exception if the path cannot be
29
+ # normalized.
30
+
31
+ def normalize_path(path)
32
+ raise "abnormal path `#{path}'" if path[0] != ?/
33
+ ret = path.dup
34
+
35
+ ret.gsub!(%r{/+}o, '/') # // => /
36
+ while ret.sub!(%r'/\.(?:/|\Z)', '/'); end # /. => /
37
+ while ret.sub!(%r'/(?!\.\./)[^/]+/\.\.(?:/|\Z)', '/'); end # /foo/.. => /foo
38
+
39
+ raise "abnormal path `#{path}'" if %r{/\.\.(/|\Z)} =~ ret
40
+ ret
41
+ end
42
+ module_function :normalize_path
43
+
44
+ ##
45
+ # Default mime types
46
+
47
+ DefaultMimeTypes = {
48
+ "ai" => "application/postscript",
49
+ "asc" => "text/plain",
50
+ "avi" => "video/x-msvideo",
51
+ "bin" => "application/octet-stream",
52
+ "bmp" => "image/bmp",
53
+ "class" => "application/octet-stream",
54
+ "cer" => "application/pkix-cert",
55
+ "crl" => "application/pkix-crl",
56
+ "crt" => "application/x-x509-ca-cert",
57
+ #"crl" => "application/x-pkcs7-crl",
58
+ "css" => "text/css",
59
+ "dms" => "application/octet-stream",
60
+ "doc" => "application/msword",
61
+ "dvi" => "application/x-dvi",
62
+ "eps" => "application/postscript",
63
+ "etx" => "text/x-setext",
64
+ "exe" => "application/octet-stream",
65
+ "gif" => "image/gif",
66
+ "htm" => "text/html",
67
+ "html" => "text/html",
68
+ "jpe" => "image/jpeg",
69
+ "jpeg" => "image/jpeg",
70
+ "jpg" => "image/jpeg",
71
+ "js" => "application/javascript",
72
+ "json" => "application/json",
73
+ "lha" => "application/octet-stream",
74
+ "lzh" => "application/octet-stream",
75
+ "mjs" => "application/javascript",
76
+ "mov" => "video/quicktime",
77
+ "mpe" => "video/mpeg",
78
+ "mpeg" => "video/mpeg",
79
+ "mpg" => "video/mpeg",
80
+ "pbm" => "image/x-portable-bitmap",
81
+ "pdf" => "application/pdf",
82
+ "pgm" => "image/x-portable-graymap",
83
+ "png" => "image/png",
84
+ "pnm" => "image/x-portable-anymap",
85
+ "ppm" => "image/x-portable-pixmap",
86
+ "ppt" => "application/vnd.ms-powerpoint",
87
+ "ps" => "application/postscript",
88
+ "qt" => "video/quicktime",
89
+ "ras" => "image/x-cmu-raster",
90
+ "rb" => "text/plain",
91
+ "rd" => "text/plain",
92
+ "rtf" => "application/rtf",
93
+ "sgm" => "text/sgml",
94
+ "sgml" => "text/sgml",
95
+ "svg" => "image/svg+xml",
96
+ "tif" => "image/tiff",
97
+ "tiff" => "image/tiff",
98
+ "txt" => "text/plain",
99
+ "wasm" => "application/wasm",
100
+ "xbm" => "image/x-xbitmap",
101
+ "xhtml" => "text/html",
102
+ "xls" => "application/vnd.ms-excel",
103
+ "xml" => "text/xml",
104
+ "xpm" => "image/x-xpixmap",
105
+ "xwd" => "image/x-xwindowdump",
106
+ "zip" => "application/zip",
107
+ }
108
+
109
+ ##
110
+ # Loads Apache-compatible mime.types in +file+.
111
+
112
+ def load_mime_types(file)
113
+ # note: +file+ may be a "| command" for now; some people may
114
+ # rely on this, but currently we do not use this method by default.
115
+ open(file){ |io|
116
+ hash = Hash.new
117
+ io.each{ |line|
118
+ next if /^#/ =~ line
119
+ line.chomp!
120
+ mimetype, ext0 = line.split(/\s+/, 2)
121
+ next unless ext0
122
+ next if ext0.empty?
123
+ ext0.split(/\s+/).each{ |ext| hash[ext] = mimetype }
124
+ }
125
+ hash
126
+ }
127
+ end
128
+ module_function :load_mime_types
129
+
130
+ ##
131
+ # Returns the mime type of +filename+ from the list in +mime_tab+. If no
132
+ # mime type was found application/octet-stream is returned.
133
+
134
+ def mime_type(filename, mime_tab)
135
+ suffix1 = (/\.(\w+)$/ =~ filename && $1.downcase)
136
+ suffix2 = (/\.(\w+)\.[\w\-]+$/ =~ filename && $1.downcase)
137
+ mime_tab[suffix1] || mime_tab[suffix2] || "application/octet-stream"
138
+ end
139
+ module_function :mime_type
140
+
141
+ ##
142
+ # Parses an HTTP header +raw+ into a hash of header fields with an Array
143
+ # of values.
144
+
145
+ def parse_header(raw)
146
+ header = Hash.new([].freeze)
147
+ field = nil
148
+ raw.each_line{|line|
149
+ case line
150
+ when /^([A-Za-z0-9!\#$%&'*+\-.^_`|~]+):\s*(.*?)\s*\z/om
151
+ field, value = $1, $2
152
+ field.downcase!
153
+ header[field] = [] unless header.has_key?(field)
154
+ header[field] << value
155
+ when /^\s+(.*?)\s*\z/om
156
+ value = $1
157
+ unless field
158
+ raise HTTPStatus::BadRequest, "bad header '#{line}'."
159
+ end
160
+ header[field][-1] << " " << value
161
+ else
162
+ raise HTTPStatus::BadRequest, "bad header '#{line}'."
163
+ end
164
+ }
165
+ header.each{|key, values|
166
+ values.each(&:strip!)
167
+ }
168
+ header
169
+ end
170
+ module_function :parse_header
171
+
172
+ ##
173
+ # Splits a header value +str+ according to HTTP specification.
174
+
175
+ def split_header_value(str)
176
+ str.scan(%r'\G((?:"(?:\\.|[^"])+?"|[^",]+)+)
177
+ (?:,\s*|\Z)'xn).flatten
178
+ end
179
+ module_function :split_header_value
180
+
181
+ ##
182
+ # Parses a Range header value +ranges_specifier+
183
+
184
+ def parse_range_header(ranges_specifier)
185
+ if /^bytes=(.*)/ =~ ranges_specifier
186
+ byte_range_set = split_header_value($1)
187
+ byte_range_set.collect{|range_spec|
188
+ case range_spec
189
+ when /^(\d+)-(\d+)/ then $1.to_i .. $2.to_i
190
+ when /^(\d+)-/ then $1.to_i .. -1
191
+ when /^-(\d+)/ then -($1.to_i) .. -1
192
+ else return nil
193
+ end
194
+ }
195
+ end
196
+ end
197
+ module_function :parse_range_header
198
+
199
+ ##
200
+ # Parses q values in +value+ as used in Accept headers.
201
+
202
+ def parse_qvalues(value)
203
+ tmp = []
204
+ if value
205
+ parts = value.split(/,\s*/)
206
+ parts.each {|part|
207
+ if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
208
+ val = m[1]
209
+ q = (m[2] or 1).to_f
210
+ tmp.push([val, q])
211
+ end
212
+ }
213
+ tmp = tmp.sort_by{|val, q| -q}
214
+ tmp.collect!{|val, q| val}
215
+ end
216
+ return tmp
217
+ end
218
+ module_function :parse_qvalues
219
+
220
+ ##
221
+ # Removes quotes and escapes from +str+
222
+
223
+ def dequote(str)
224
+ ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
225
+ ret.gsub!(/\\(.)/, "\\1")
226
+ ret
227
+ end
228
+ module_function :dequote
229
+
230
+ ##
231
+ # Quotes and escapes quotes in +str+
232
+
233
+ def quote(str)
234
+ +'"' << str.gsub(/[\\\"]/o, "\\\1") << '"'
235
+ end
236
+ module_function :quote
237
+
238
+ ##
239
+ # Stores multipart form data. FormData objects are created when
240
+ # WEBrick::HTTPUtils.parse_form_data is called.
241
+
242
+ class FormData < String
243
+ EmptyRawHeader = [].freeze # :nodoc:
244
+ EmptyHeader = {}.freeze # :nodoc:
245
+
246
+ ##
247
+ # The name of the form data part
248
+
249
+ attr_accessor :name
250
+
251
+ ##
252
+ # The filename of the form data part
253
+
254
+ attr_accessor :filename
255
+
256
+ attr_accessor :next_data # :nodoc:
257
+ protected :next_data
258
+
259
+ ##
260
+ # Creates a new FormData object.
261
+ #
262
+ # +args+ is an Array of form data entries. One FormData will be created
263
+ # for each entry.
264
+ #
265
+ # This is called by WEBrick::HTTPUtils.parse_form_data for you
266
+
267
+ def initialize(*args)
268
+ @name = @filename = @next_data = nil
269
+ if args.empty?
270
+ @raw_header = []
271
+ @header = nil
272
+ super("")
273
+ else
274
+ @raw_header = EmptyRawHeader
275
+ @header = EmptyHeader
276
+ super(args.shift)
277
+ unless args.empty?
278
+ @next_data = self.class.new(*args)
279
+ end
280
+ end
281
+ end
282
+
283
+ ##
284
+ # Retrieves the header at the first entry in +key+
285
+
286
+ def [](*key)
287
+ begin
288
+ @header[key[0].downcase].join(", ")
289
+ rescue StandardError, NameError
290
+ super
291
+ end
292
+ end
293
+
294
+ ##
295
+ # Adds +str+ to this FormData which may be the body, a header or a
296
+ # header entry.
297
+ #
298
+ # This is called by WEBrick::HTTPUtils.parse_form_data for you
299
+
300
+ def <<(str)
301
+ if @header
302
+ super
303
+ elsif str == CRLF
304
+ @header = HTTPUtils::parse_header(@raw_header.join)
305
+ if cd = self['content-disposition']
306
+ if /\s+name="(.*?)"/ =~ cd then @name = $1 end
307
+ if /\s+filename="(.*?)"/ =~ cd then @filename = $1 end
308
+ end
309
+ else
310
+ @raw_header << str
311
+ end
312
+ self
313
+ end
314
+
315
+ ##
316
+ # Adds +data+ at the end of the chain of entries
317
+ #
318
+ # This is called by WEBrick::HTTPUtils.parse_form_data for you.
319
+
320
+ def append_data(data)
321
+ tmp = self
322
+ while tmp
323
+ unless tmp.next_data
324
+ tmp.next_data = data
325
+ break
326
+ end
327
+ tmp = tmp.next_data
328
+ end
329
+ self
330
+ end
331
+
332
+ ##
333
+ # Yields each entry in this FormData
334
+
335
+ def each_data
336
+ tmp = self
337
+ while tmp
338
+ next_data = tmp.next_data
339
+ yield(tmp)
340
+ tmp = next_data
341
+ end
342
+ end
343
+
344
+ ##
345
+ # Returns all the FormData as an Array
346
+
347
+ def list
348
+ ret = []
349
+ each_data{|data|
350
+ ret << data.to_s
351
+ }
352
+ ret
353
+ end
354
+
355
+ ##
356
+ # A FormData will behave like an Array
357
+
358
+ alias :to_ary :list
359
+
360
+ ##
361
+ # This FormData's body
362
+
363
+ def to_s
364
+ String.new(self)
365
+ end
366
+ end
367
+
368
+ ##
369
+ # Parses the query component of a URI in +str+
370
+
371
+ def parse_query(str)
372
+ query = Hash.new
373
+ if str
374
+ str.split(/[&;]/).each{|x|
375
+ next if x.empty?
376
+ key, val = x.split(/=/,2)
377
+ key = unescape_form(key)
378
+ val = unescape_form(val.to_s)
379
+ val = FormData.new(val)
380
+ val.name = key
381
+ if query.has_key?(key)
382
+ query[key].append_data(val)
383
+ next
384
+ end
385
+ query[key] = val
386
+ }
387
+ end
388
+ query
389
+ end
390
+ module_function :parse_query
391
+
392
+ ##
393
+ # Parses form data in +io+ with the given +boundary+
394
+
395
+ def parse_form_data(io, boundary)
396
+ boundary_regexp = /\A--#{Regexp.quote(boundary)}(--)?#{CRLF}\z/
397
+ form_data = Hash.new
398
+ return form_data unless io
399
+ data = nil
400
+ io.each_line{|line|
401
+ if boundary_regexp =~ line
402
+ if data
403
+ data.chop!
404
+ key = data.name
405
+ if form_data.has_key?(key)
406
+ form_data[key].append_data(data)
407
+ else
408
+ form_data[key] = data
409
+ end
410
+ end
411
+ data = FormData.new
412
+ next
413
+ else
414
+ if data
415
+ data << line
416
+ end
417
+ end
418
+ }
419
+ return form_data
420
+ end
421
+ module_function :parse_form_data
422
+
423
+ #####
424
+
425
+ reserved = ';/?:@&=+$,'
426
+ num = '0123456789'
427
+ lowalpha = 'abcdefghijklmnopqrstuvwxyz'
428
+ upalpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
429
+ mark = '-_.!~*\'()'
430
+ unreserved = num + lowalpha + upalpha + mark
431
+ control = (0x0..0x1f).collect{|c| c.chr }.join + "\x7f"
432
+ space = " "
433
+ delims = '<>#%"'
434
+ unwise = '{}|\\^[]`'
435
+ nonascii = (0x80..0xff).collect{|c| c.chr }.join
436
+
437
+ module_function
438
+
439
+ # :stopdoc:
440
+
441
+ def _make_regex(str) /([#{Regexp.escape(str)}])/n end
442
+ def _make_regex!(str) /([^#{Regexp.escape(str)}])/n end
443
+ def _escape(str, regex)
444
+ str = str.b
445
+ str.gsub!(regex) {"%%%02X" % $1.ord}
446
+ # %-escaped string should contain US-ASCII only
447
+ str.force_encoding(Encoding::US_ASCII)
448
+ end
449
+ def _unescape(str, regex)
450
+ str = str.b
451
+ str.gsub!(regex) {$1.hex.chr}
452
+ # encoding of %-unescaped string is unknown
453
+ str
454
+ end
455
+
456
+ UNESCAPED = _make_regex(control+space+delims+unwise+nonascii)
457
+ UNESCAPED_FORM = _make_regex(reserved+control+delims+unwise+nonascii)
458
+ NONASCII = _make_regex(nonascii)
459
+ ESCAPED = /%([0-9a-fA-F]{2})/
460
+ UNESCAPED_PCHAR = _make_regex!(unreserved+":@&=+$,")
461
+
462
+ # :startdoc:
463
+
464
+ ##
465
+ # Escapes HTTP reserved and unwise characters in +str+
466
+
467
+ def escape(str)
468
+ _escape(str, UNESCAPED)
469
+ end
470
+
471
+ ##
472
+ # Unescapes HTTP reserved and unwise characters in +str+
473
+
474
+ def unescape(str)
475
+ _unescape(str, ESCAPED)
476
+ end
477
+
478
+ ##
479
+ # Escapes form reserved characters in +str+
480
+
481
+ def escape_form(str)
482
+ ret = _escape(str, UNESCAPED_FORM)
483
+ ret.gsub!(/ /, "+")
484
+ ret
485
+ end
486
+
487
+ ##
488
+ # Unescapes form reserved characters in +str+
489
+
490
+ def unescape_form(str)
491
+ _unescape(str.gsub(/\+/, " "), ESCAPED)
492
+ end
493
+
494
+ ##
495
+ # Escapes path +str+
496
+
497
+ def escape_path(str)
498
+ result = +""
499
+ str.scan(%r{/([^/]*)}).each{|i|
500
+ result << "/" << _escape(i[0], UNESCAPED_PCHAR)
501
+ }
502
+ return result
503
+ end
504
+
505
+ ##
506
+ # Escapes 8 bit characters in +str+
507
+
508
+ def escape8bit(str)
509
+ _escape(str, NONASCII)
510
+ end
511
+ end
512
+ end