shopify-cli 1.4.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) 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 +3 -2
  5. data/.github/workflows/release.yml +0 -1
  6. data/.github/workflows/triage.yml +22 -0
  7. data/.rubocop.yml +21 -7
  8. data/.rubocop_todo.yml +2 -15
  9. data/.travis.yml +1 -1
  10. data/CHANGELOG.md +28 -0
  11. data/Gemfile +12 -11
  12. data/Gemfile.lock +17 -14
  13. data/README.md +39 -7
  14. data/RELEASING.md +5 -13
  15. data/Rakefile +32 -28
  16. data/bin/load_shopify.rb +6 -6
  17. data/bin/shopify +2 -2
  18. data/dev.yml +2 -2
  19. data/docs/_config.yml +1 -18
  20. data/docs/app/node/commands/index.md +2 -80
  21. data/docs/app/node/index.md +2 -33
  22. data/docs/app/rails/commands/index.md +2 -78
  23. data/docs/app/rails/index.md +2 -34
  24. data/docs/core/index.md +2 -84
  25. data/docs/getting-started/index.md +2 -25
  26. data/docs/getting-started/install/index.md +1 -118
  27. data/docs/getting-started/migrate/index.md +2 -94
  28. data/docs/getting-started/uninstall/index.md +2 -35
  29. data/docs/getting-started/upgrade/index.md +2 -39
  30. data/docs/help/start-app/index.md +2 -4
  31. data/docs/index.md +2 -24
  32. data/ext/shopify-cli/extconf.rb +7 -7
  33. data/install.sh +1 -1
  34. data/lib/docgen/markdown.rb +12 -12
  35. data/lib/graphql/fetch_specifications.graphql +14 -0
  36. data/lib/{project_types/extension/graphql → graphql}/get_app_by_api_key.graphql +0 -0
  37. data/lib/project_types/extension/cli.rb +54 -47
  38. data/lib/project_types/extension/commands/build.rb +3 -3
  39. data/lib/project_types/extension/commands/create.rb +17 -10
  40. data/lib/project_types/extension/commands/extension_command.rb +12 -5
  41. data/lib/project_types/extension/commands/push.rb +8 -8
  42. data/lib/project_types/extension/commands/register.rb +19 -30
  43. data/lib/project_types/extension/commands/serve.rb +31 -3
  44. data/lib/project_types/extension/commands/tunnel.rb +12 -12
  45. data/lib/project_types/extension/extension_project.rb +8 -4
  46. data/lib/project_types/extension/extension_project_keys.rb +4 -4
  47. data/lib/project_types/extension/features/argo.rb +117 -0
  48. data/lib/project_types/extension/features/argo_config.rb +5 -5
  49. data/lib/project_types/extension/features/argo_dependencies.rb +5 -5
  50. data/lib/project_types/extension/features/argo_setup.rb +2 -2
  51. data/lib/project_types/extension/features/argo_setup_steps.rb +4 -4
  52. data/lib/project_types/extension/forms/create.rb +28 -34
  53. data/lib/project_types/extension/forms/questions/ask_app.rb +53 -0
  54. data/lib/project_types/extension/forms/questions/ask_name.rb +40 -0
  55. data/lib/project_types/extension/forms/questions/ask_type.rb +47 -0
  56. data/lib/project_types/extension/messages/messages.rb +55 -52
  57. data/lib/project_types/extension/models/lazy_specification_handler.rb +12 -0
  58. data/lib/project_types/extension/models/specification.rb +37 -0
  59. data/lib/project_types/extension/models/specification_handlers/checkout_post_purchase.rb +19 -0
  60. data/lib/project_types/extension/models/specification_handlers/default.rb +67 -0
  61. data/lib/project_types/extension/models/specifications.rb +88 -0
  62. data/lib/project_types/extension/tasks/configure_features.rb +52 -0
  63. data/lib/project_types/extension/tasks/converters/app_converter.rb +6 -6
  64. data/lib/project_types/extension/tasks/converters/registration_converter.rb +6 -6
  65. data/lib/project_types/extension/tasks/converters/validation_error_converter.rb +4 -4
  66. data/lib/project_types/extension/tasks/converters/version_converter.rb +7 -7
  67. data/lib/project_types/extension/tasks/create_extension.rb +4 -4
  68. data/lib/project_types/extension/tasks/fetch_specifications.rb +18 -0
  69. data/lib/project_types/extension/tasks/get_app.rb +4 -4
  70. data/lib/project_types/extension/tasks/get_apps.rb +3 -3
  71. data/lib/project_types/extension/tasks/update_draft.rb +4 -4
  72. data/lib/project_types/extension/tasks/user_errors.rb +4 -4
  73. data/lib/project_types/node/cli.rb +19 -16
  74. data/lib/project_types/node/commands/connect.rb +15 -0
  75. data/lib/project_types/node/commands/create.rb +44 -41
  76. data/lib/project_types/node/commands/deploy.rb +4 -4
  77. data/lib/project_types/node/commands/deploy/heroku.rb +24 -24
  78. data/lib/project_types/node/commands/generate.rb +9 -18
  79. data/lib/project_types/node/commands/open.rb +2 -2
  80. data/lib/project_types/node/commands/populate.rb +6 -6
  81. data/lib/project_types/node/commands/populate/customer.rb +5 -5
  82. data/lib/project_types/node/commands/populate/draft_order.rb +5 -5
  83. data/lib/project_types/node/commands/populate/product.rb +5 -5
  84. data/lib/project_types/node/commands/serve.rb +9 -9
  85. data/lib/project_types/node/commands/tunnel.rb +7 -7
  86. data/lib/project_types/node/forms/create.rb +17 -8
  87. data/lib/project_types/node/messages/messages.rb +20 -53
  88. data/lib/project_types/rails/cli.rb +21 -18
  89. data/lib/project_types/rails/commands/connect.rb +15 -0
  90. data/lib/project_types/rails/commands/create.rb +58 -57
  91. data/lib/project_types/rails/commands/deploy.rb +4 -4
  92. data/lib/project_types/rails/commands/deploy/heroku.rb +30 -30
  93. data/lib/project_types/rails/commands/generate.rb +7 -7
  94. data/lib/project_types/rails/commands/generate/webhook.rb +6 -6
  95. data/lib/project_types/rails/commands/open.rb +2 -2
  96. data/lib/project_types/rails/commands/populate.rb +6 -6
  97. data/lib/project_types/rails/commands/populate/customer.rb +5 -5
  98. data/lib/project_types/rails/commands/populate/draft_order.rb +5 -5
  99. data/lib/project_types/rails/commands/populate/product.rb +5 -5
  100. data/lib/project_types/rails/commands/serve.rb +11 -11
  101. data/lib/project_types/rails/commands/tunnel.rb +7 -7
  102. data/lib/project_types/rails/forms/create.rb +35 -25
  103. data/lib/project_types/rails/gem.rb +24 -24
  104. data/lib/project_types/rails/messages/messages.rb +13 -9
  105. data/lib/project_types/rails/ruby.rb +2 -2
  106. data/lib/project_types/script/cli.rb +44 -38
  107. data/lib/project_types/script/commands/create.rb +15 -10
  108. data/lib/project_types/script/commands/disable.rb +3 -3
  109. data/lib/project_types/script/commands/enable.rb +19 -9
  110. data/lib/project_types/script/commands/push.rb +10 -17
  111. data/lib/project_types/script/config/extension_points.yml +30 -12
  112. data/lib/project_types/script/errors.rb +22 -0
  113. data/lib/project_types/script/forms/create.rb +29 -5
  114. data/lib/project_types/script/graphql/app_script_update_or_create.graphql +12 -1
  115. data/lib/project_types/script/layers/application/build_script.rb +19 -19
  116. data/lib/project_types/script/layers/application/create_script.rb +45 -12
  117. data/lib/project_types/script/layers/application/disable_script.rb +2 -2
  118. data/lib/project_types/script/layers/application/enable_script.rb +2 -2
  119. data/lib/project_types/script/layers/application/extension_points.rb +24 -0
  120. data/lib/project_types/script/layers/application/project_dependencies.rb +4 -4
  121. data/lib/project_types/script/layers/application/push_script.rb +15 -18
  122. data/lib/project_types/script/layers/domain/config_ui.rb +16 -0
  123. data/lib/project_types/script/layers/domain/errors.rb +23 -0
  124. data/lib/project_types/script/layers/domain/extension_point.rb +67 -7
  125. data/lib/project_types/script/layers/domain/metadata.rb +55 -0
  126. data/lib/project_types/script/layers/domain/push_package.rb +29 -6
  127. data/lib/project_types/script/layers/infrastructure/assemblyscript_project_creator.rb +45 -55
  128. data/lib/project_types/script/layers/infrastructure/assemblyscript_task_runner.rb +41 -45
  129. data/lib/project_types/script/layers/infrastructure/config_ui_repository.rb +46 -0
  130. data/lib/project_types/script/layers/infrastructure/errors.rb +32 -5
  131. data/lib/project_types/script/layers/infrastructure/extension_point_repository.rb +12 -6
  132. data/lib/project_types/script/layers/infrastructure/project_creator.rb +2 -1
  133. data/lib/project_types/script/layers/infrastructure/push_package_repository.rb +20 -13
  134. data/lib/project_types/script/layers/infrastructure/rust_project_creator.rb +72 -0
  135. data/lib/project_types/script/layers/infrastructure/rust_task_runner.rb +59 -0
  136. data/lib/project_types/script/layers/infrastructure/script_service.rb +39 -17
  137. data/lib/project_types/script/layers/infrastructure/task_runner.rb +4 -3
  138. data/lib/project_types/script/messages/messages.rb +76 -10
  139. data/lib/project_types/script/script_project.rb +26 -16
  140. data/lib/project_types/script/ui/error_handler.rb +135 -50
  141. data/lib/project_types/script/ui/printing_spinner.rb +1 -1
  142. data/lib/project_types/script/ui/strict_spinner.rb +1 -1
  143. data/lib/project_types/theme/cli.rb +40 -0
  144. data/lib/project_types/theme/commands/connect.rb +54 -0
  145. data/lib/project_types/theme/commands/create.rb +48 -0
  146. data/lib/project_types/theme/commands/deploy.rb +38 -0
  147. data/lib/project_types/theme/commands/generate.rb +20 -0
  148. data/lib/project_types/theme/commands/generate/env.rb +79 -0
  149. data/lib/project_types/theme/commands/push.rb +55 -0
  150. data/lib/project_types/theme/commands/serve.rb +31 -0
  151. data/lib/project_types/theme/forms/connect.rb +34 -0
  152. data/lib/project_types/theme/forms/create.rb +22 -0
  153. data/lib/project_types/theme/messages/messages.rb +147 -0
  154. data/lib/project_types/theme/tasks/ensure_themekit_installed.rb +78 -0
  155. data/lib/project_types/theme/themekit.rb +113 -0
  156. data/lib/rubygems_plugin.rb +3 -3
  157. data/lib/shopify-cli/admin_api.rb +52 -12
  158. data/lib/shopify-cli/admin_api/populate_resource_command.rb +17 -17
  159. data/lib/shopify-cli/admin_api/schema.rb +3 -3
  160. data/lib/shopify-cli/api.rb +36 -31
  161. data/lib/shopify-cli/command.rb +1 -1
  162. data/lib/shopify-cli/commands.rb +9 -9
  163. data/lib/shopify-cli/commands/config.rb +28 -28
  164. data/lib/shopify-cli/commands/connect.rb +35 -18
  165. data/lib/shopify-cli/commands/create.rb +5 -5
  166. data/lib/shopify-cli/commands/help.rb +6 -6
  167. data/lib/shopify-cli/commands/logout.rb +3 -3
  168. data/lib/shopify-cli/commands/system.rb +33 -33
  169. data/lib/shopify-cli/commands/version.rb +2 -2
  170. data/lib/shopify-cli/context.rb +43 -22
  171. data/lib/shopify-cli/core.rb +4 -4
  172. data/lib/shopify-cli/core/entry_point.rb +5 -5
  173. data/lib/shopify-cli/core/executor.rb +1 -1
  174. data/lib/shopify-cli/core/help_resolver.rb +2 -2
  175. data/lib/shopify-cli/core/monorail.rb +17 -16
  176. data/lib/shopify-cli/db.rb +2 -2
  177. data/lib/shopify-cli/feature.rb +1 -3
  178. data/lib/shopify-cli/form.rb +1 -1
  179. data/lib/shopify-cli/git.rb +17 -17
  180. data/lib/shopify-cli/helpers.rb +1 -1
  181. data/lib/shopify-cli/helpers/haikunator.rb +1 -1
  182. data/lib/shopify-cli/heroku.rb +28 -28
  183. data/lib/shopify-cli/http_request.rb +21 -9
  184. data/lib/shopify-cli/js_deps.rb +13 -13
  185. data/lib/shopify-cli/js_system.rb +5 -5
  186. data/lib/shopify-cli/lazy_delegator.rb +55 -0
  187. data/lib/shopify-cli/messages/messages.rb +21 -10
  188. data/lib/shopify-cli/method_object.rb +104 -0
  189. data/lib/shopify-cli/oauth.rb +25 -25
  190. data/lib/shopify-cli/oauth/servlet.rb +9 -9
  191. data/lib/shopify-cli/options.rb +3 -3
  192. data/lib/shopify-cli/packager.rb +24 -24
  193. data/lib/shopify-cli/partners_api.rb +38 -16
  194. data/lib/shopify-cli/partners_api/organizations.rb +10 -10
  195. data/lib/shopify-cli/process_supervision.rb +8 -8
  196. data/lib/shopify-cli/project.rb +27 -23
  197. data/lib/shopify-cli/project_type.rb +21 -5
  198. data/lib/shopify-cli/resolve_constant.rb +25 -0
  199. data/lib/shopify-cli/resources.rb +1 -1
  200. data/lib/shopify-cli/resources/env_file.rb +9 -9
  201. data/lib/shopify-cli/result.rb +432 -0
  202. data/lib/shopify-cli/shopifolk.rb +35 -18
  203. data/lib/shopify-cli/sub_command.rb +1 -1
  204. data/lib/shopify-cli/task.rb +9 -1
  205. data/lib/shopify-cli/tasks.rb +7 -7
  206. data/lib/shopify-cli/tasks/create_api_client.rb +13 -4
  207. data/lib/shopify-cli/tasks/ensure_dev_store.rb +12 -12
  208. data/lib/shopify-cli/tasks/ensure_env.rb +18 -15
  209. data/lib/shopify-cli/tasks/ensure_loopback_url.rb +4 -4
  210. data/lib/shopify-cli/tasks/select_org_and_shop.rb +28 -24
  211. data/lib/shopify-cli/tasks/update_dashboard_urls.rb +10 -10
  212. data/lib/shopify-cli/transform_data_structure.rb +86 -0
  213. data/lib/shopify-cli/tunnel.rb +36 -30
  214. data/lib/shopify-cli/version.rb +1 -1
  215. data/lib/shopify_cli.rb +57 -52
  216. data/shopify-cli.gemspec +6 -6
  217. data/shopify.fish +1 -1
  218. data/shopify.sh +1 -1
  219. data/vendor/deps/cli-kit/REVISION +1 -1
  220. data/vendor/deps/cli-kit/lib/cli/kit/logger.rb +2 -2
  221. data/vendor/deps/cli-kit/lib/cli/kit/system.rb +3 -3
  222. data/vendor/deps/cli-ui/REVISION +1 -1
  223. data/vendor/deps/cli-ui/lib/cli/ui.rb +26 -22
  224. data/vendor/deps/cli-ui/lib/cli/ui/ansi.rb +4 -6
  225. data/vendor/deps/cli-ui/lib/cli/ui/frame.rb +3 -3
  226. data/vendor/deps/cli-ui/lib/cli/ui/frame/frame_stack.rb +8 -9
  227. data/vendor/deps/cli-ui/lib/cli/ui/frame/frame_style.rb +1 -1
  228. data/vendor/deps/cli-ui/lib/cli/ui/glyph.rb +1 -0
  229. data/vendor/deps/cli-ui/lib/cli/ui/printer.rb +15 -3
  230. data/vendor/deps/cli-ui/lib/cli/ui/prompt/interactive_options.rb +4 -11
  231. data/vendor/deps/cli-ui/lib/cli/ui/spinner.rb +3 -5
  232. data/vendor/deps/cli-ui/lib/cli/ui/terminal.rb +10 -10
  233. data/vendor/deps/cli-ui/lib/cli/ui/version.rb +1 -1
  234. data/vendor/deps/cli-ui/lib/cli/ui/wrap.rb +56 -0
  235. data/vendor/deps/webrick/.gitignore +9 -0
  236. data/vendor/deps/webrick/Gemfile +3 -0
  237. data/vendor/deps/webrick/LICENSE.txt +22 -0
  238. data/vendor/deps/webrick/README.md +61 -0
  239. data/vendor/deps/webrick/Rakefile +10 -0
  240. data/vendor/deps/webrick/lib/webrick.rb +232 -0
  241. data/vendor/deps/webrick/lib/webrick/accesslog.rb +157 -0
  242. data/vendor/deps/webrick/lib/webrick/cgi.rb +313 -0
  243. data/vendor/deps/webrick/lib/webrick/compat.rb +36 -0
  244. data/vendor/deps/webrick/lib/webrick/config.rb +158 -0
  245. data/vendor/deps/webrick/lib/webrick/cookie.rb +172 -0
  246. data/vendor/deps/webrick/lib/webrick/htmlutils.rb +30 -0
  247. data/vendor/deps/webrick/lib/webrick/httpauth.rb +96 -0
  248. data/vendor/deps/webrick/lib/webrick/httpauth/authenticator.rb +117 -0
  249. data/vendor/deps/webrick/lib/webrick/httpauth/basicauth.rb +116 -0
  250. data/vendor/deps/webrick/lib/webrick/httpauth/digestauth.rb +395 -0
  251. data/vendor/deps/webrick/lib/webrick/httpauth/htdigest.rb +132 -0
  252. data/vendor/deps/webrick/lib/webrick/httpauth/htgroup.rb +97 -0
  253. data/vendor/deps/webrick/lib/webrick/httpauth/htpasswd.rb +158 -0
  254. data/vendor/deps/webrick/lib/webrick/httpauth/userdb.rb +53 -0
  255. data/vendor/deps/webrick/lib/webrick/httpproxy.rb +354 -0
  256. data/vendor/deps/webrick/lib/webrick/httprequest.rb +636 -0
  257. data/vendor/deps/webrick/lib/webrick/httpresponse.rb +564 -0
  258. data/vendor/deps/webrick/lib/webrick/https.rb +152 -0
  259. data/vendor/deps/webrick/lib/webrick/httpserver.rb +294 -0
  260. data/vendor/deps/webrick/lib/webrick/httpservlet.rb +23 -0
  261. data/vendor/deps/webrick/lib/webrick/httpservlet/abstract.rb +152 -0
  262. data/vendor/deps/webrick/lib/webrick/httpservlet/cgi_runner.rb +47 -0
  263. data/vendor/deps/webrick/lib/webrick/httpservlet/cgihandler.rb +126 -0
  264. data/vendor/deps/webrick/lib/webrick/httpservlet/erbhandler.rb +88 -0
  265. data/vendor/deps/webrick/lib/webrick/httpservlet/filehandler.rb +552 -0
  266. data/vendor/deps/webrick/lib/webrick/httpservlet/prochandler.rb +47 -0
  267. data/vendor/deps/webrick/lib/webrick/httpstatus.rb +194 -0
  268. data/vendor/deps/webrick/lib/webrick/httputils.rb +512 -0
  269. data/vendor/deps/webrick/lib/webrick/httpversion.rb +76 -0
  270. data/vendor/deps/webrick/lib/webrick/log.rb +156 -0
  271. data/vendor/deps/webrick/lib/webrick/server.rb +381 -0
  272. data/vendor/deps/webrick/lib/webrick/ssl.rb +215 -0
  273. data/vendor/deps/webrick/lib/webrick/utils.rb +265 -0
  274. data/vendor/deps/webrick/lib/webrick/version.rb +18 -0
  275. data/vendor/deps/webrick/webrick.gemspec +74 -0
  276. data/vendor/gen/template/bin/update-deps +9 -9
  277. metadata +83 -29
  278. data/docs/Gemfile +0 -5
  279. data/docs/Gemfile.lock +0 -258
  280. data/docs/_data/nav.yml +0 -35
  281. data/docs/_includes/footer.html +0 -15
  282. data/docs/_includes/head.html +0 -19
  283. data/docs/_includes/sidebar_nav.html +0 -22
  284. data/docs/_includes/toc.html +0 -112
  285. data/docs/_layouts/default.html +0 -79
  286. data/docs/css/docs.css +0 -157
  287. data/docs/images/header.png +0 -0
  288. data/docs/installing-ruby.md +0 -28
  289. data/lib/project_types/extension/features/argo/admin.rb +0 -20
  290. data/lib/project_types/extension/features/argo/base.rb +0 -129
  291. data/lib/project_types/extension/features/argo/checkout.rb +0 -20
  292. data/lib/project_types/extension/forms/register.rb +0 -47
  293. data/lib/project_types/extension/models/type.rb +0 -81
  294. data/lib/project_types/extension/models/types/checkout_post_purchase.rb +0 -23
  295. data/lib/project_types/extension/models/types/product_subscription.rb +0 -24
  296. data/lib/project_types/node/commands/generate/billing.rb +0 -39
  297. data/lib/project_types/node/commands/generate/page.rb +0 -59
  298. data/lib/project_types/node/commands/generate/webhook.rb +0 -37
  299. data/lib/project_types/script/layers/domain/script.rb +0 -18
  300. data/lib/project_types/script/layers/infrastructure/assemblyscript_tsconfig.rb +0 -38
  301. data/lib/project_types/script/layers/infrastructure/script_repository.rb +0 -59
  302. data/lib/project_types/script/templates/ts/as-pect.config.js +0 -27
  303. data/lib/project_types/script/templates/ts/as-pect.d.ts +0 -1
@@ -0,0 +1,157 @@
1
+ # frozen_string_literal: true
2
+ #--
3
+ # accesslog.rb -- Access log handling utilities
4
+ #
5
+ # Author: IPR -- Internet Programming with Ruby -- writers
6
+ # Copyright (c) 2002 keita yamaguchi
7
+ # Copyright (c) 2002 Internet Programming with Ruby writers
8
+ #
9
+ # $IPR: accesslog.rb,v 1.1 2002/10/01 17:16:32 gotoyuzo Exp $
10
+
11
+ module WEBrick
12
+
13
+ ##
14
+ # AccessLog provides logging to various files in various formats.
15
+ #
16
+ # Multiple logs may be written to at the same time:
17
+ #
18
+ # access_log = [
19
+ # [$stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT],
20
+ # [$stderr, WEBrick::AccessLog::REFERER_LOG_FORMAT],
21
+ # ]
22
+ #
23
+ # server = WEBrick::HTTPServer.new :AccessLog => access_log
24
+ #
25
+ # Custom log formats may be defined. WEBrick::AccessLog provides a subset
26
+ # of the formatting from Apache's mod_log_config
27
+ # http://httpd.apache.org/docs/mod/mod_log_config.html#formats. See
28
+ # AccessLog::setup_params for a list of supported options
29
+
30
+ module AccessLog
31
+
32
+ ##
33
+ # Raised if a parameter such as %e, %i, %o or %n is used without fetching
34
+ # a specific field.
35
+
36
+ class AccessLogError < StandardError; end
37
+
38
+ ##
39
+ # The Common Log Format's time format
40
+
41
+ CLF_TIME_FORMAT = "[%d/%b/%Y:%H:%M:%S %Z]"
42
+
43
+ ##
44
+ # Common Log Format
45
+
46
+ COMMON_LOG_FORMAT = "%h %l %u %t \"%r\" %s %b"
47
+
48
+ ##
49
+ # Short alias for Common Log Format
50
+
51
+ CLF = COMMON_LOG_FORMAT
52
+
53
+ ##
54
+ # Referer Log Format
55
+
56
+ REFERER_LOG_FORMAT = "%{Referer}i -> %U"
57
+
58
+ ##
59
+ # User-Agent Log Format
60
+
61
+ AGENT_LOG_FORMAT = "%{User-Agent}i"
62
+
63
+ ##
64
+ # Combined Log Format
65
+
66
+ COMBINED_LOG_FORMAT = "#{CLF} \"%{Referer}i\" \"%{User-agent}i\""
67
+
68
+ module_function
69
+
70
+ # This format specification is a subset of mod_log_config of Apache:
71
+ #
72
+ # %a:: Remote IP address
73
+ # %b:: Total response size
74
+ # %e{variable}:: Given variable in ENV
75
+ # %f:: Response filename
76
+ # %h:: Remote host name
77
+ # %{header}i:: Given request header
78
+ # %l:: Remote logname, always "-"
79
+ # %m:: Request method
80
+ # %{attr}n:: Given request attribute from <tt>req.attributes</tt>
81
+ # %{header}o:: Given response header
82
+ # %p:: Server's request port
83
+ # %{format}p:: The canonical port of the server serving the request or the
84
+ # actual port or the client's actual port. Valid formats are
85
+ # canonical, local or remote.
86
+ # %q:: Request query string
87
+ # %r:: First line of the request
88
+ # %s:: Request status
89
+ # %t:: Time the request was received
90
+ # %T:: Time taken to process the request
91
+ # %u:: Remote user from auth
92
+ # %U:: Unparsed URI
93
+ # %%:: Literal %
94
+
95
+ def setup_params(config, req, res)
96
+ params = Hash.new("")
97
+ params["a"] = req.peeraddr[3]
98
+ params["b"] = res.sent_size
99
+ params["e"] = ENV
100
+ params["f"] = res.filename || ""
101
+ params["h"] = req.peeraddr[2]
102
+ params["i"] = req
103
+ params["l"] = "-"
104
+ params["m"] = req.request_method
105
+ params["n"] = req.attributes
106
+ params["o"] = res
107
+ params["p"] = req.port
108
+ params["q"] = req.query_string
109
+ params["r"] = req.request_line.sub(/\x0d?\x0a\z/o, '')
110
+ params["s"] = res.status # won't support "%>s"
111
+ params["t"] = req.request_time
112
+ params["T"] = Time.now - req.request_time
113
+ params["u"] = req.user || "-"
114
+ params["U"] = req.unparsed_uri
115
+ params["v"] = config[:ServerName]
116
+ params
117
+ end
118
+
119
+ ##
120
+ # Formats +params+ according to +format_string+ which is described in
121
+ # setup_params.
122
+
123
+ def format(format_string, params)
124
+ format_string.gsub(/\%(?:\{(.*?)\})?>?([a-zA-Z%])/){
125
+ param, spec = $1, $2
126
+ case spec[0]
127
+ when ?e, ?i, ?n, ?o
128
+ raise AccessLogError,
129
+ "parameter is required for \"#{spec}\"" unless param
130
+ (param = params[spec][param]) ? escape(param) : "-"
131
+ when ?t
132
+ params[spec].strftime(param || CLF_TIME_FORMAT)
133
+ when ?p
134
+ case param
135
+ when 'remote'
136
+ escape(params["i"].peeraddr[1].to_s)
137
+ else
138
+ escape(params["p"].to_s)
139
+ end
140
+ when ?%
141
+ "%"
142
+ else
143
+ escape(params[spec].to_s)
144
+ end
145
+ }
146
+ end
147
+
148
+ ##
149
+ # Escapes control characters in +data+
150
+
151
+ def escape(data)
152
+ data = data.gsub(/[[:cntrl:]\\]+/) {$&.dump[1...-1]}
153
+ data.untaint if RUBY_VERSION < '2.7'
154
+ data
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,313 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # cgi.rb -- Yet another CGI library
4
+ #
5
+ # Author: IPR -- Internet Programming with Ruby -- writers
6
+ # Copyright (c) 2003 Internet Programming with Ruby writers. All rights
7
+ # reserved.
8
+ #
9
+ # $Id$
10
+
11
+ require_relative "httprequest"
12
+ require_relative "httpresponse"
13
+ require_relative "config"
14
+ require "stringio"
15
+
16
+ module WEBrick
17
+
18
+ # A CGI library using WEBrick requests and responses.
19
+ #
20
+ # Example:
21
+ #
22
+ # class MyCGI < WEBrick::CGI
23
+ # def do_GET req, res
24
+ # res.body = 'it worked!'
25
+ # res.status = 200
26
+ # end
27
+ # end
28
+ #
29
+ # MyCGI.new.start
30
+
31
+ class CGI
32
+
33
+ # The CGI error exception class
34
+
35
+ CGIError = Class.new(StandardError)
36
+
37
+ ##
38
+ # The CGI configuration. This is based on WEBrick::Config::HTTP
39
+
40
+ attr_reader :config
41
+
42
+ ##
43
+ # The CGI logger
44
+
45
+ attr_reader :logger
46
+
47
+ ##
48
+ # Creates a new CGI interface.
49
+ #
50
+ # The first argument in +args+ is a configuration hash which would update
51
+ # WEBrick::Config::HTTP.
52
+ #
53
+ # Any remaining arguments are stored in the <code>@options</code> instance
54
+ # variable for use by a subclass.
55
+
56
+ def initialize(*args)
57
+ if defined?(MOD_RUBY)
58
+ unless ENV.has_key?("GATEWAY_INTERFACE")
59
+ Apache.request.setup_cgi_env
60
+ end
61
+ end
62
+ if %r{HTTP/(\d+\.\d+)} =~ ENV["SERVER_PROTOCOL"]
63
+ httpv = $1
64
+ end
65
+ @config = WEBrick::Config::HTTP.dup.update(
66
+ :ServerSoftware => ENV["SERVER_SOFTWARE"] || "null",
67
+ :HTTPVersion => HTTPVersion.new(httpv || "1.0"),
68
+ :RunOnCGI => true, # to detect if it runs on CGI.
69
+ :NPH => false # set true to run as NPH script.
70
+ )
71
+ if config = args.shift
72
+ @config.update(config)
73
+ end
74
+ @config[:Logger] ||= WEBrick::BasicLog.new($stderr)
75
+ @logger = @config[:Logger]
76
+ @options = args
77
+ end
78
+
79
+ ##
80
+ # Reads +key+ from the configuration
81
+
82
+ def [](key)
83
+ @config[key]
84
+ end
85
+
86
+ ##
87
+ # Starts the CGI process with the given environment +env+ and standard
88
+ # input and output +stdin+ and +stdout+.
89
+
90
+ def start(env=ENV, stdin=$stdin, stdout=$stdout)
91
+ sock = WEBrick::CGI::Socket.new(@config, env, stdin, stdout)
92
+ req = HTTPRequest.new(@config)
93
+ res = HTTPResponse.new(@config)
94
+ unless @config[:NPH] or defined?(MOD_RUBY)
95
+ def res.setup_header
96
+ unless @header["status"]
97
+ phrase = HTTPStatus::reason_phrase(@status)
98
+ @header["status"] = "#{@status} #{phrase}"
99
+ end
100
+ super
101
+ end
102
+ def res.status_line
103
+ ""
104
+ end
105
+ end
106
+
107
+ begin
108
+ req.parse(sock)
109
+ req.script_name = (env["SCRIPT_NAME"] || File.expand_path($0)).dup
110
+ req.path_info = (env["PATH_INFO"] || "").dup
111
+ req.query_string = env["QUERY_STRING"]
112
+ req.user = env["REMOTE_USER"]
113
+ res.request_method = req.request_method
114
+ res.request_uri = req.request_uri
115
+ res.request_http_version = req.http_version
116
+ res.keep_alive = req.keep_alive?
117
+ self.service(req, res)
118
+ rescue HTTPStatus::Error => ex
119
+ res.set_error(ex)
120
+ rescue HTTPStatus::Status => ex
121
+ res.status = ex.code
122
+ rescue Exception => ex
123
+ @logger.error(ex)
124
+ res.set_error(ex, true)
125
+ ensure
126
+ req.fixup
127
+ if defined?(MOD_RUBY)
128
+ res.setup_header
129
+ Apache.request.status_line = "#{res.status} #{res.reason_phrase}"
130
+ Apache.request.status = res.status
131
+ table = Apache.request.headers_out
132
+ res.header.each{|key, val|
133
+ case key
134
+ when /^content-encoding$/i
135
+ Apache::request.content_encoding = val
136
+ when /^content-type$/i
137
+ Apache::request.content_type = val
138
+ else
139
+ table[key] = val.to_s
140
+ end
141
+ }
142
+ res.cookies.each{|cookie|
143
+ table.add("Set-Cookie", cookie.to_s)
144
+ }
145
+ Apache.request.send_http_header
146
+ res.send_body(sock)
147
+ else
148
+ res.send_response(sock)
149
+ end
150
+ end
151
+ end
152
+
153
+ ##
154
+ # Services the request +req+ which will fill in the response +res+. See
155
+ # WEBrick::HTTPServlet::AbstractServlet#service for details.
156
+
157
+ def service(req, res)
158
+ method_name = "do_" + req.request_method.gsub(/-/, "_")
159
+ if respond_to?(method_name)
160
+ __send__(method_name, req, res)
161
+ else
162
+ raise HTTPStatus::MethodNotAllowed,
163
+ "unsupported method `#{req.request_method}'."
164
+ end
165
+ end
166
+
167
+ ##
168
+ # Provides HTTP socket emulation from the CGI environment
169
+
170
+ class Socket # :nodoc:
171
+ include Enumerable
172
+
173
+ private
174
+
175
+ def initialize(config, env, stdin, stdout)
176
+ @config = config
177
+ @env = env
178
+ @header_part = StringIO.new
179
+ @body_part = stdin
180
+ @out_port = stdout
181
+ @out_port.binmode
182
+
183
+ @server_addr = @env["SERVER_ADDR"] || "0.0.0.0"
184
+ @server_name = @env["SERVER_NAME"]
185
+ @server_port = @env["SERVER_PORT"]
186
+ @remote_addr = @env["REMOTE_ADDR"]
187
+ @remote_host = @env["REMOTE_HOST"] || @remote_addr
188
+ @remote_port = @env["REMOTE_PORT"] || 0
189
+
190
+ begin
191
+ @header_part << request_line << CRLF
192
+ setup_header
193
+ @header_part << CRLF
194
+ @header_part.rewind
195
+ rescue Exception
196
+ raise CGIError, "invalid CGI environment"
197
+ end
198
+ end
199
+
200
+ def request_line
201
+ meth = @env["REQUEST_METHOD"] || "GET"
202
+ unless url = @env["REQUEST_URI"]
203
+ url = (@env["SCRIPT_NAME"] || File.expand_path($0)).dup
204
+ url << @env["PATH_INFO"].to_s
205
+ url = WEBrick::HTTPUtils.escape_path(url)
206
+ if query_string = @env["QUERY_STRING"]
207
+ unless query_string.empty?
208
+ url << "?" << query_string
209
+ end
210
+ end
211
+ end
212
+ # we cannot get real HTTP version of client ;)
213
+ httpv = @config[:HTTPVersion]
214
+ return "#{meth} #{url} HTTP/#{httpv}"
215
+ end
216
+
217
+ def setup_header
218
+ @env.each{|key, value|
219
+ case key
220
+ when "CONTENT_TYPE", "CONTENT_LENGTH"
221
+ add_header(key.gsub(/_/, "-"), value)
222
+ when /^HTTP_(.*)/
223
+ add_header($1.gsub(/_/, "-"), value)
224
+ end
225
+ }
226
+ end
227
+
228
+ def add_header(hdrname, value)
229
+ unless value.empty?
230
+ @header_part << hdrname << ": " << value << CRLF
231
+ end
232
+ end
233
+
234
+ def input
235
+ @header_part.eof? ? @body_part : @header_part
236
+ end
237
+
238
+ public
239
+
240
+ def peeraddr
241
+ [nil, @remote_port, @remote_host, @remote_addr]
242
+ end
243
+
244
+ def addr
245
+ [nil, @server_port, @server_name, @server_addr]
246
+ end
247
+
248
+ def gets(eol=LF, size=nil)
249
+ input.gets(eol, size)
250
+ end
251
+
252
+ def read(size=nil)
253
+ input.read(size)
254
+ end
255
+
256
+ def each
257
+ input.each{|line| yield(line) }
258
+ end
259
+
260
+ def eof?
261
+ input.eof?
262
+ end
263
+
264
+ def <<(data)
265
+ @out_port << data
266
+ end
267
+
268
+ def write(data)
269
+ @out_port.write(data)
270
+ end
271
+
272
+ def cert
273
+ return nil unless defined?(OpenSSL)
274
+ if pem = @env["SSL_SERVER_CERT"]
275
+ OpenSSL::X509::Certificate.new(pem) unless pem.empty?
276
+ end
277
+ end
278
+
279
+ def peer_cert
280
+ return nil unless defined?(OpenSSL)
281
+ if pem = @env["SSL_CLIENT_CERT"]
282
+ OpenSSL::X509::Certificate.new(pem) unless pem.empty?
283
+ end
284
+ end
285
+
286
+ def peer_cert_chain
287
+ return nil unless defined?(OpenSSL)
288
+ if @env["SSL_CLIENT_CERT_CHAIN_0"]
289
+ keys = @env.keys
290
+ certs = keys.sort.collect{|k|
291
+ if /^SSL_CLIENT_CERT_CHAIN_\d+$/ =~ k
292
+ if pem = @env[k]
293
+ OpenSSL::X509::Certificate.new(pem) unless pem.empty?
294
+ end
295
+ end
296
+ }
297
+ certs.compact
298
+ end
299
+ end
300
+
301
+ def cipher
302
+ return nil unless defined?(OpenSSL)
303
+ if cipher = @env["SSL_CIPHER"]
304
+ ret = [ cipher ]
305
+ ret << @env["SSL_PROTOCOL"]
306
+ ret << @env["SSL_CIPHER_USEKEYSIZE"]
307
+ ret << @env["SSL_CIPHER_ALGKEYSIZE"]
308
+ ret
309
+ end
310
+ end
311
+ end
312
+ end
313
+ end