datadog-json_logger 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba2bce053c01ef4787e60b782e1796f050d0d1462af869de7f09594a18bb1476
4
- data.tar.gz: 980509d54903433e0a4e755c3a2d73e96a192ad8a732bf1c119dc675846f8563
3
+ metadata.gz: 7120de0085ceee8323c41cc80a7743e111818bb30662d9dbd7f1be4c4feb4a6c
4
+ data.tar.gz: 4fa8c5501f35a9794ec44763ac4a7f41ae2cc80b0f72c1326497f0bdaabcafe9
5
5
  SHA512:
6
- metadata.gz: 89da523671f52049de515a7299ab7f664d03925d777c16648de762859807c88f0fbdf3a20ed1ce438fcb4fdb9c6b459cf6674e7057a2daa62dadc65cde9b7580
7
- data.tar.gz: e75b0d5ae7c83847ebd3aeca11611f05f11a5ac4ce7edfc7cb768e7fe4b91d6451d3d5dffd2f932faf38eb7985b8e8752419b0e09e1c8309caf55fcce826bacc
6
+ metadata.gz: 5d7549654631f0ea56c684d81607c49fd3fa0d65e8c60073e511cbc2169d13c89bb29d0d50e9b4aef44ab62213cb859b4b612faa0b23fd2ccedb4a2eaabf29d0
7
+ data.tar.gz: d463571ab78f6baf7a44640db5b0df3d4049fa9ddd05f734e74398b2530fdeadda4d6b53498aa82ab79dcbe8b4fdeadcae5d95f47b79a8cf4f12da2cfa1d24f2
data/.rubocop.yml CHANGED
@@ -28,6 +28,8 @@ Metrics/MethodLength:
28
28
 
29
29
  Metrics/BlockLength:
30
30
  Max: 100
31
+ Exclude:
32
+ - 'spec/**/*'
31
33
 
32
34
  Metrics/ParameterLists:
33
35
  Max: 6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,35 @@
1
- ## [Unreleased]
1
+ ## v0.1.5 (2024-02-02)
2
2
 
3
- ## [0.0.1] - 2023-11-20
3
+ ### New feature:
4
4
 
5
- - Initial release
5
+ - Add better log message (#5)([`2e8e49e`](https://github.com/buyco/datadog-json-logger/commit/2e8e49e37903de85858a1ffa54f52ee8a145592c)) (by Eth3rnit3)
6
+
7
+ ## v0.1.4 (2023-12-19)
8
+
9
+ ### New feature:
10
+
11
+ - add option to raise error on request or not (#4)([`e98dea0`](https://github.com/buyco/datadog-json-logger/commit/e98dea065fba1623811749f551d2cc4505ea468e)) (by Eth3rnit3)
12
+
13
+ ## v0.1.3 (2023-12-11)
14
+
15
+ ### New feature:
16
+
17
+ - make error tracking compatible (#3)([`7397780`](https://github.com/buyco/datadog-json-logger/commit/7397780e48bdb829d1eda4e90834cb5a3f069183)) (by Eth3rnit3)
18
+ - add rbs (#2)([`3b08f16`](https://github.com/buyco/datadog-json-logger/commit/3b08f1667de534b1eb4598110b8421a326e2c346)) (by Eth3rnit3)
19
+ - add nexus workflow([`7107c69`](https://github.com/buyco/datadog-json-logger/commit/7107c6909e9c755ea29ad09c4b67f31c585666b1)) (by Eth3rnit3)
20
+
21
+ ## v0.1.0 (2023-11-22)
22
+
23
+ ### New feature:
24
+
25
+ - improve middleware and specs([`b7cc576`](https://github.com/buyco/datadog-json-logger/commit/b7cc576111a33192379472202e4a1a881a7167e5)) (by Eth3rnit3)
26
+ - improve formatter and specs([`7b0b9e1`](https://github.com/buyco/datadog-json-logger/commit/7b0b9e174e70872c7bf58e914502a7e70e52b020)) (by Eth3rnit3)
27
+ - rename and improve readme([`2b6a367`](https://github.com/buyco/datadog-json-logger/commit/2b6a367a3e474d4be1fd9a0908e9ff6f4cc9521c)) (by Eth3rnit3)
28
+ - improve setup([`ba11412`](https://github.com/buyco/datadog-json-logger/commit/ba114125fac37f0c6ba8a8129825a6c47591c252)) (by Eth3rnit3)
29
+ - improve specs([`776323c`](https://github.com/buyco/datadog-json-logger/commit/776323c7a8eb19087d9c7b68d844b1f0d9f638aa)) (by Eth3rnit3)
30
+ - add json logger and sinatra middleware([`8910067`](https://github.com/buyco/datadog-json-logger/commit/89100678d9aa3c2f81ac42bfeed46ac070c974a4)) (by Eth3rnit3)
31
+
32
+ ### Bugs fixed:
33
+
34
+ - use rack-test gem([`94080f0`](https://github.com/buyco/datadog-json-logger/commit/94080f0d4a117a5aae2ab09db46654ac7f7bcbe7)) (by Eth3rnit3)
35
+ - add missing rack gem for spec([`85f7881`](https://github.com/buyco/datadog-json-logger/commit/85f7881cf48e9946855095f854eb86028d0b2f1c)) (by Eth3rnit3)
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # Datadog::JSONLogger
2
2
 
3
- [![Rubocop and Rspec](https://github.com/Eth3rnit3/datadog-json_logger/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/Eth3rnit3/datadog-json_logger/actions/workflows/main.yml)
4
- [![Nexus upload](https://github.com/buyco/datadog-json-logger/actions/workflows/nexus.yml/badge.svg)](https://github.com/buyco/datadog-json-logger/actions/workflows/nexus.yml)
3
+ [![CodeQL](https://github.com/buyco/datadog-json-logger/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/buyco/datadog-json-logger/actions/workflows/github-code-scanning/codeql)
4
+ [![Rubocop and Rspec](https://github.com/buyco/datadog-json-logger/actions/workflows/main.yml/badge.svg)](https://github.com/buyco/datadog-json-logger/actions/workflows/main.yml)
5
+ [![Publish on RubyGems](https://github.com/buyco/datadog-json-logger/actions/workflows/gem-push.yml/badge.svg)](https://github.com/buyco/datadog-json-logger/actions/workflows/gem-push.yml)
5
6
 
6
7
  `Datadog::JSONLogger` is a Ruby gem designed to seamlessly integrate Ruby applications with Datadog's logging and tracing services. This gem allows your Ruby application to format its output as JSON, including necessary correlation IDs and other details for optimal Datadog functionality.
7
8
 
@@ -39,6 +40,13 @@ gem install datadog-json_logger
39
40
  # Example in Sinatra (app.rb)
40
41
  require 'datadog/json_logger'
41
42
 
43
+ Datadog::JSONLogger.configure do |config|
44
+ config.current_user = ->(env) { { email: env['warned'].user.email } } # Log user on each request
45
+ config.controller_key = "sinatra.controller_name"
46
+ config.resource_key = "sinatra.resource_name"
47
+ config.action_key = "sinatra.action_name"
48
+ end
49
+
42
50
  def logger
43
51
  @logger ||= Datadog::JSONLogger.new
44
52
  end
@@ -102,9 +110,9 @@ To install this gem onto your local machine, run `bundle exec rake install`.
102
110
 
103
111
  ## Contributing
104
112
 
105
- Bug reports and pull requests are welcome on GitHub at https://github.com/eth3rnit3/datadog-json_logger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/eth3rnit3/datadog-json_logger/blob/main/CODE_OF_CONDUCT.md).
113
+ Bug reports and pull requests are welcome on GitHub at https://github.com/buyco/datadog-json-logger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/buyco/datadog-json-logger/blob/main/CODE_OF_CONDUCT.md).
106
114
 
107
- 1. Fork the repository (https://github.com/eth3rnit3/datadog-json_logger/fork)
115
+ 1. Fork the repository (https://github.com/buyco/datadog-json-logger/fork)
108
116
  2. Create your feature branch (`git checkout -b feature/my_feature`)
109
117
  3. Commit your changes (`git commit -am 'Add a new feature'`)
110
118
  4. Push the branch (`git push origin feature/my_feature`)
@@ -116,4 +124,4 @@ This gem is available as open source under the terms of the [MIT License](https:
116
124
 
117
125
  ## Code of Conduct
118
126
 
119
- Everyone interacting in the Datadog::JSONLogger project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](https://github.com/eth3rnit3/datadog-json_logger/blob/main/CODE_OF_CONDUCT.md).
127
+ Everyone interacting in the Datadog::JSONLogger project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](https://github.com/buyco/datadog-json-logger/blob/main/CODE_OF_CONDUCT.md).
@@ -10,15 +10,22 @@ Gem::Specification.new do |spec|
10
10
 
11
11
  spec.summary = "Connect your ruby application to Datadog logging and tracing."
12
12
  spec.description = "This gem provides easy integration for connecting a ruby application to Datadog's logging and tracing services."
13
- spec.homepage = "https://github.com/eth3rnit3/datadog-json_logger"
13
+ spec.homepage = "https://github.com/buyco/datadog-json-logger"
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = ">= 3.0"
16
16
 
17
17
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
18
 
19
+ if ENV.fetch('GEM_PUSHER', 'default') == 'github'
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.pkg.github.com/buyco'
21
+ else
22
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
23
+ end
24
+
25
+
19
26
  spec.metadata["homepage_uri"] = spec.homepage
20
- spec.metadata["source_code_uri"] = "https://github.com/eth3rnit3/datadog-json_logger"
21
- spec.metadata["changelog_uri"] = "https://github.com/eth3rnit3/datadog-json_logger/blob/main/CHANGELOG.md"
27
+ spec.metadata["source_code_uri"] = "https://github.com/buyco/datadog-json-logger"
28
+ spec.metadata["changelog_uri"] = "https://github.com/buyco/datadog-json-logger/blob/main/CHANGELOG.md"
22
29
 
23
30
  # Specify which files should be added to the gem when it is released.
24
31
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ class Configuration
5
+ attr_accessor :current_user, :controller_key, :resource_key, :action_key
6
+
7
+ # @param current_user [Lambda] A lambda that returns the current user
8
+ # e.g. ->(env) { env["current_user"] }
9
+
10
+ # @return [Datadog::Configuration]
11
+ def initialize
12
+ @current_user = nil
13
+ @controller_key = "sinatra.controller_name"
14
+ @resource_key = "sinatra.resource_name"
15
+ @action_key = "sinatra.action_name"
16
+ end
17
+
18
+ def log_current_user?
19
+ @current_user ? true : false
20
+ end
21
+ end
22
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "logger"
4
+ require_relative "configuration"
4
5
  require_relative "loggers/json_formatter"
5
6
  require_relative "loggers/version"
6
7
 
@@ -8,11 +9,18 @@ module Datadog
8
9
  class Error < StandardError; end
9
10
 
10
11
  class JSONLogger < Logger
12
+ attr_accessor :config
13
+
11
14
  def initialize(output = default_output)
12
- super(output)
15
+ super
13
16
  @default_formatter = ::Datadog::Loggers::JSONFormatter
14
17
  end
15
18
 
19
+ def self.configure
20
+ self.config ||= Configuration.new
21
+ yield(config)
22
+ end
23
+
16
24
  private
17
25
 
18
26
  def default_output
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Datadog
4
4
  module Loggers
5
- VERSION = "0.1.4"
5
+ VERSION = "0.1.6"
6
6
  end
7
7
  end
@@ -51,20 +51,26 @@ module Datadog
51
51
  def log_request(request, env, status, headers, start_time, end_time)
52
52
  log_data = {
53
53
  request: true,
54
- request_ip: request.ip,
55
- method: request.request_method,
56
- controller: env["sinatra.controller_name"],
57
- action: env["sinatra.action_name"],
58
- path: request.path,
59
54
  params: parse_query(request.query_string),
60
55
  status: status,
61
56
  format: headers["Content-Type"],
62
- duration: calculate_duration(start_time, end_time)
57
+ duration: calculate_duration(start_time, end_time),
58
+ **aggregate_logs(request, env),
59
+ **current_user(env)
63
60
  }
64
61
 
62
+ log_data[:message] = message(log_data)
63
+
65
64
  logger.info(log_data)
66
65
  end
67
66
 
67
+ def message(log_data)
68
+ "Received #{log_data[:method]} request from #{log_data[:request_ip]} " \
69
+ "to #{log_data[:controller]}##{log_data[:action]} at #{log_data[:path]} " \
70
+ "with params #{log_data[:params].to_json}. Responded with status #{log_data[:status]} " \
71
+ "in #{log_data[:duration]}ms. Content-Type: #{log_data[:format]}."
72
+ end
73
+
68
74
  def calculate_duration(start_time, end_time)
69
75
  ((end_time - start_time) * 1000).round # Duration in milliseconds
70
76
  end
@@ -82,5 +88,29 @@ module Datadog
82
88
 
83
89
  raise(exception)
84
90
  end
91
+
92
+ def config
93
+ @logger.config
94
+ end
95
+
96
+ def aggregate_logs(request, env)
97
+ hash = {}
98
+ hash[:request_ip] = request.ip
99
+ hash[:method] = request.request_method
100
+ hash[:path] = request.path
101
+ hash[:controller] = env[config.controller_key.to_s] if config.controller_key
102
+ hash[:action] = env[config.action_key.to_s] if config.action_key
103
+ hash[:resource] = env[config.resource_key.to_s] if config.resource_key
104
+
105
+ hash
106
+ end
107
+
108
+ def current_user(env)
109
+ return {} unless config.log_current_user?
110
+
111
+ {
112
+ user: config.current_user.call(env)
113
+ }
114
+ end
85
115
  end
86
116
  end
data/sig/.keep ADDED
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datadog-json_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eth3rnit3
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-04 00:00:00.000000000 Z
11
+ date: 2024-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ddtrace
@@ -44,27 +44,23 @@ files:
44
44
  - Rakefile
45
45
  - Steepfile
46
46
  - datadog-json_logger.gemspec
47
+ - lib/datadog/configuration.rb
47
48
  - lib/datadog/json_logger.rb
48
49
  - lib/datadog/loggers/json_formatter.rb
49
50
  - lib/datadog/loggers/version.rb
50
51
  - lib/datadog/sinatra_middleware.rb
51
52
  - nexus_release.sh
52
- - sig/datadog/json_logger.rbs
53
- - sig/datadog/loggers/json_formatter.rbs
54
- - sig/datadog/loggers/version.rbs
55
- - sig/datadog/sinatra_middleware.rbs
56
- - sig/datadog/tracing.rbs
57
- - sig/rack/request.rbs
58
- homepage: https://github.com/eth3rnit3/datadog-json_logger
53
+ - sig/.keep
54
+ homepage: https://github.com/buyco/datadog-json-logger
59
55
  licenses:
60
56
  - MIT
61
57
  metadata:
62
58
  allowed_push_host: https://rubygems.org
63
- homepage_uri: https://github.com/eth3rnit3/datadog-json_logger
64
- source_code_uri: https://github.com/eth3rnit3/datadog-json_logger
65
- changelog_uri: https://github.com/eth3rnit3/datadog-json_logger/blob/main/CHANGELOG.md
59
+ homepage_uri: https://github.com/buyco/datadog-json-logger
60
+ source_code_uri: https://github.com/buyco/datadog-json-logger
61
+ changelog_uri: https://github.com/buyco/datadog-json-logger/blob/main/CHANGELOG.md
66
62
  rubygems_mfa_required: 'true'
67
- post_install_message:
63
+ post_install_message:
68
64
  rdoc_options: []
69
65
  require_paths:
70
66
  - lib
@@ -80,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
76
  version: '0'
81
77
  requirements: []
82
78
  rubygems_version: 3.4.10
83
- signing_key:
79
+ signing_key:
84
80
  specification_version: 4
85
81
  summary: Connect your ruby application to Datadog logging and tracing.
86
82
  test_files: []
@@ -1,14 +0,0 @@
1
- module Datadog
2
- class Error < StandardError
3
- end
4
-
5
- class JSONLogger < Logger
6
- @default_formatter: singleton(Loggers::JSONFormatter)
7
-
8
- def initialize: (?IO | StringIO | File | NilClass output) -> void
9
-
10
- private
11
-
12
- def default_output: () -> IO
13
- end
14
- end
@@ -1,29 +0,0 @@
1
- module Datadog
2
- module Loggers
3
- class JSONFormatter < Logger::Formatter
4
- def self.call: (String severity, DateTime datetime, String progname, untyped msg) ?{ (untyped) -> untyped } -> ::String
5
-
6
- def self.base_log_hash: (String severity, DateTime datetime, String progname) -> { dd: Hash[untyped, untyped], timestamp: String, severity: String, progname: String }
7
-
8
- def self.formatter_for: ((String | Hash[Symbol, untyped] | Exception) msg) -> untyped
9
-
10
- def self.correlation_hash: () -> { trace_id: Integer, span_id: Integer, env: String, service: String, version: String | Integer }
11
-
12
- module HashFormatter
13
- def self?.format: (Hash[Symbol, untyped] log_hash, Hash[untyped, untyped] msg) -> untyped
14
- end
15
-
16
- module ExceptionFormatter
17
- def self?.format: (Hash[Symbol, untyped] log_hash, Exception exception) -> untyped
18
- end
19
-
20
- module StringFormatter
21
- def self?.format: (Hash[Symbol, untyped] log_hash, String msg) -> untyped
22
- end
23
-
24
- module DefaultFormatter
25
- def self?.format: (Hash[Symbol, untyped] log_hash, untyped msg) -> untyped
26
- end
27
- end
28
- end
29
- end
@@ -1,5 +0,0 @@
1
- module Datadog
2
- module Loggers
3
- VERSION: String
4
- end
5
- end
@@ -1,32 +0,0 @@
1
- module Rack
2
- class CommonLogger
3
- def log: (untyped _env, untyped _status, untyped _response_headers, untyped _began_at) -> nil
4
- end
5
- end
6
-
7
- module Datadog
8
- class SinatraMiddleware
9
- @app: untyped
10
- @logger: Logger
11
- @raise_exceptions: bool
12
-
13
- attr_reader app: untyped
14
- attr_reader logger: Logger
15
-
16
- def initialize: (untyped app, Logger logger, ?Hash[Symbol, untyped] opt) -> void
17
-
18
- def call: (untyped env) -> untyped
19
-
20
- private
21
-
22
- def safely_process_request: (untyped env) -> untyped
23
-
24
- def log_request: (Rack::Request request, untyped env, String status, Hash[String, String] headers, Time start_time, Time end_time) -> untyped
25
-
26
- def calculate_duration: (Time start_time, Time end_time) -> Integer
27
-
28
- def parse_query: (String query_string) -> Hash[String, String]
29
-
30
- def handle_exception: (Exception exception) -> Exception
31
- end
32
- end
@@ -1,90 +0,0 @@
1
- module Datadog
2
- # Datadog APM tracing public API.
3
- #
4
- # The Datadog team ensures that public methods in this module
5
- # only receive backwards compatible changes, and breaking changes
6
- # will only occur in new major versions releases.
7
- # @public_api
8
- module Tracing
9
- # (see Datadog::Tracing::Tracer#trace)
10
- # @public_api
11
- def self.trace: (untyped name, ?continue_from: untyped?, **untyped span_options) { () -> untyped } -> untyped
12
-
13
- # (see Datadog::Tracing::Tracer#continue_trace!)
14
- # @public_api
15
- def self.continue_trace!: (untyped digest) { () -> untyped } -> untyped
16
-
17
- # The tracer's internal logger instance.
18
- # All tracing log output is handled by this object.
19
- #
20
- # The logger can be configured through {.configure},
21
- # through {Datadog::Core::Configuration::Settings::DSL::Logger} options.
22
- #
23
- # @!attribute [r] logger
24
- # @public_api
25
- def self.logger: () -> untyped
26
-
27
- # (see Datadog::Tracing::Tracer#active_trace)
28
- # @public_api
29
- def self.active_trace: () -> (nil | untyped)
30
-
31
- # (see Datadog::Tracing::Tracer#active_span)
32
- # @public_api
33
- def self.active_span: () -> (nil | untyped)
34
-
35
- # (see Datadog::Tracing::TraceSegment#keep!)
36
- # If no trace is active, no action is taken.
37
- # @public_api
38
- def self.keep!: () -> untyped
39
-
40
- # (see Datadog::Tracing::TraceSegment#reject!)
41
- # If no trace is active, no action is taken.
42
- # @public_api
43
- def self.reject!: () -> untyped
44
-
45
- # (see Datadog::Tracing::Tracer#active_correlation)
46
- # @public_api
47
- def self.correlation: () -> (nil | untyped)
48
-
49
- # Textual representation of {.correlation}, which can be
50
- # added to individual log lines in order to correlate them with the active
51
- # trace.
52
- #
53
- # Example:
54
- #
55
- # ```
56
- # MyLogger.log("#{Datadog::Tracing.log_correlation}] My message")
57
- # # dd.env=prod dd.service=auth dd.version=13.8 dd.trace_id=5458478252992251 dd.span_id=7117552347370098 My message
58
- # ```
59
- #
60
- # @return [String] correlation information
61
- # @public_api
62
- def self.log_correlation: () -> untyped
63
-
64
- # Gracefully shuts down the tracer.
65
- #
66
- # The public tracing API will still respond to method calls as usual
67
- # but might not internally perform the expected internal work after shutdown.
68
- #
69
- # This avoids errors being raised across the host application
70
- # during shutdown while allowing for the graceful decommission of resources.
71
- #
72
- # {.shutdown!} cannot be reversed.
73
- # @public_api
74
- def self.shutdown!: () -> (nil | untyped)
75
-
76
- # (see Datadog::Tracing::Pipeline.before_flush)
77
- def self.before_flush: (*untyped processors) { () -> untyped } -> untyped
78
-
79
- # Is the tracer collecting telemetry data in this process?
80
- # @return [Boolean] `true` if the tracer is collecting data in this process, otherwise `false`.
81
- def self.enabled?: () -> (false | untyped)
82
-
83
- private
84
-
85
- # DEV: components hosts both tracing and profiling inner objects today
86
- def self.components: () -> untyped
87
-
88
- def self.tracer: () -> untyped
89
- end
90
- end
data/sig/rack/request.rbs DELETED
@@ -1,362 +0,0 @@
1
- module Rack
2
- class Request
3
- self.@forwarded_priority: untyped
4
-
5
- self.@x_forwarded_proto_priority: untyped
6
-
7
- @env: untyped
8
-
9
- @params: untyped
10
-
11
- attr_accessor self.ip_filter: untyped
12
-
13
- # The priority when checking forwarded headers. The default
14
- # is <tt>[:forwarded, :x_forwarded]</tt>, which means, check the
15
- # +Forwarded+ header first, followed by the appropriate
16
- # <tt>X-Forwarded-*</tt> header. You can revert the priority by
17
- # reversing the priority, or remove checking of either
18
- # or both headers by removing elements from the array.
19
- #
20
- # This should be set as appropriate in your environment
21
- # based on what reverse proxies are in use. If you are not
22
- # using reverse proxies, you should probably use an empty
23
- # array.
24
- attr_accessor self.forwarded_priority: untyped
25
-
26
- # The priority when checking either the <tt>X-Forwarded-Proto</tt>
27
- # or <tt>X-Forwarded-Scheme</tt> header for the forwarded protocol.
28
- # The default is <tt>[:proto, :scheme]</tt>, to try the
29
- # <tt>X-Forwarded-Proto</tt> header before the
30
- # <tt>X-Forwarded-Scheme</tt> header. Rack 2 had behavior
31
- # similar to <tt>[:scheme, :proto]</tt>. You can remove either or
32
- # both of the entries in array to ignore that respective header.
33
- attr_accessor self.x_forwarded_proto_priority: untyped
34
-
35
- ALLOWED_SCHEMES: ::Array["https" | "http" | "wss" | "ws"]
36
-
37
- def initialize: (untyped env) -> void
38
-
39
- def params: () -> untyped
40
-
41
- def update_param: (untyped k, untyped v) -> untyped
42
-
43
- def delete_param: (untyped k) -> untyped
44
-
45
- module Env
46
- @env: untyped
47
-
48
- # The environment of the request.
49
- attr_reader env: untyped
50
-
51
- def initialize: (untyped env) -> void
52
-
53
- # Predicate method to test to see if `name` has been set as request
54
- # specific data
55
- def has_header?: (untyped name) -> untyped
56
-
57
- # Get a request specific value for `name`.
58
- def get_header: (untyped name) -> untyped
59
-
60
- # If a block is given, it yields to the block if the value hasn't been set
61
- # on the request.
62
- def fetch_header: (untyped name) { () -> untyped } -> untyped
63
-
64
- # Loops through each key / value pair in the request specific data.
65
- def each_header: () { () -> untyped } -> untyped
66
-
67
- # Set a request specific value for `name` to `v`
68
- def set_header: (untyped name, untyped v) -> untyped
69
-
70
- # Add a header that may have multiple values.
71
- #
72
- # Example:
73
- # request.add_header 'Accept', 'image/png'
74
- # request.add_header 'Accept', '*/*'
75
- #
76
- # assert_equal 'image/png,*/*', request.get_header('Accept')
77
- #
78
- # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
79
- def add_header: (untyped key, untyped v) -> untyped
80
-
81
- # Delete a request specific value for `name`.
82
- def delete_header: (untyped name) -> untyped
83
-
84
- def initialize_copy: (untyped other) -> untyped
85
- end
86
-
87
- module Helpers
88
- # The set of form-data media-types. Requests that do not indicate
89
- # one of the media types present in this list will not be eligible
90
- # for form-data / param parsing.
91
- FORM_DATA_MEDIA_TYPES: ::Array["application/x-www-form-urlencoded" | "multipart/form-data"]
92
-
93
- # The set of media-types. Requests that do not indicate
94
- # one of the media types present in this list will not be eligible
95
- # for param parsing like soap attachments or generic multiparts
96
- PARSEABLE_DATA_MEDIA_TYPES: ::Array["multipart/related" | "multipart/mixed"]
97
-
98
- # Default ports depending on scheme. Used to decide whether or not
99
- # to include the port in a generated URI.
100
- DEFAULT_PORTS: ::Hash[::String, 80 | 443]
101
-
102
- # The address of the client which connected to the proxy.
103
- HTTP_X_FORWARDED_FOR: "HTTP_X_FORWARDED_FOR"
104
-
105
- # The contents of the host/:authority header sent to the proxy.
106
- HTTP_X_FORWARDED_HOST: "HTTP_X_FORWARDED_HOST"
107
-
108
- HTTP_FORWARDED: "HTTP_FORWARDED"
109
-
110
- # The value of the scheme sent to the proxy.
111
- HTTP_X_FORWARDED_SCHEME: "HTTP_X_FORWARDED_SCHEME"
112
-
113
- # The protocol used to connect to the proxy.
114
- HTTP_X_FORWARDED_PROTO: "HTTP_X_FORWARDED_PROTO"
115
-
116
- # The port used to connect to the proxy.
117
- HTTP_X_FORWARDED_PORT: "HTTP_X_FORWARDED_PORT"
118
-
119
- # Another way for specifying https scheme was used.
120
- HTTP_X_FORWARDED_SSL: "HTTP_X_FORWARDED_SSL"
121
-
122
- def body: () -> untyped
123
-
124
- def script_name: () -> untyped
125
-
126
- def script_name=: (untyped s) -> untyped
127
-
128
- def path_info: () -> untyped
129
-
130
- def path_info=: (untyped s) -> untyped
131
-
132
- def request_method: () -> untyped
133
-
134
- def query_string: () -> untyped
135
-
136
- def content_length: () -> untyped
137
-
138
- def logger: () -> untyped
139
-
140
- def user_agent: () -> untyped
141
-
142
- # the referer of the client
143
- def referer: () -> untyped
144
-
145
- alias referrer referer
146
-
147
- def session: () -> untyped
148
-
149
- def session_options: () -> untyped
150
-
151
- # Checks the HTTP request method (or verb) to see if it was of type DELETE
152
- def delete?: () -> untyped
153
-
154
- # Checks the HTTP request method (or verb) to see if it was of type GET
155
- def get?: () -> untyped
156
-
157
- # Checks the HTTP request method (or verb) to see if it was of type HEAD
158
- def head?: () -> untyped
159
-
160
- # Checks the HTTP request method (or verb) to see if it was of type OPTIONS
161
- def options?: () -> untyped
162
-
163
- # Checks the HTTP request method (or verb) to see if it was of type LINK
164
- def link?: () -> untyped
165
-
166
- # Checks the HTTP request method (or verb) to see if it was of type PATCH
167
- def patch?: () -> untyped
168
-
169
- # Checks the HTTP request method (or verb) to see if it was of type POST
170
- def post?: () -> untyped
171
-
172
- # Checks the HTTP request method (or verb) to see if it was of type PUT
173
- def put?: () -> untyped
174
-
175
- # Checks the HTTP request method (or verb) to see if it was of type TRACE
176
- def trace?: () -> untyped
177
-
178
- # Checks the HTTP request method (or verb) to see if it was of type UNLINK
179
- def unlink?: () -> untyped
180
-
181
- def scheme: () -> ("https" | "https" | untyped)
182
-
183
- # The authority of the incoming request as defined by RFC3976.
184
- # https://tools.ietf.org/html/rfc3986#section-3.2
185
- #
186
- # In HTTP/1, this is the `host` header.
187
- # In HTTP/2, this is the `:authority` pseudo-header.
188
- def authority: () -> untyped
189
-
190
- # The authority as defined by the `SERVER_NAME` and `SERVER_PORT`
191
- # variables.
192
- def server_authority: () -> (nil | untyped)
193
-
194
- def server_name: () -> untyped
195
-
196
- def server_port: () -> untyped
197
-
198
- def cookies: () -> untyped
199
-
200
- def content_type: () -> untyped
201
-
202
- def xhr?: () -> untyped
203
-
204
- # The `HTTP_HOST` header.
205
- def host_authority: () -> untyped
206
-
207
- def host_with_port: (?untyped authority) -> untyped
208
-
209
- # Returns a formatted host, suitable for being used in a URI.
210
- def host: () -> untyped
211
-
212
- # Returns an address suitable for being to resolve to an address.
213
- # In the case of a domain name or IPv4 address, the result is the same
214
- # as +host+. In the case of IPv6 or future address formats, the square
215
- # brackets are removed.
216
- def hostname: () -> untyped
217
-
218
- def port: () -> untyped
219
-
220
- def forwarded_for: () -> (untyped | nil)
221
-
222
- def forwarded_port: () -> (untyped | nil)
223
-
224
- def forwarded_authority: () -> (untyped | nil)
225
-
226
- def ssl?: () -> untyped
227
-
228
- def ip: () -> untyped
229
-
230
- # The media type (type/subtype) portion of the CONTENT_TYPE header
231
- # without any media type parameters. e.g., when CONTENT_TYPE is
232
- # "text/plain;charset=utf-8", the media-type is "text/plain".
233
- #
234
- # For more information on the use of media types in HTTP, see:
235
- # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
236
- def media_type: () -> untyped
237
-
238
- # The media type parameters provided in CONTENT_TYPE as a Hash, or
239
- # an empty Hash if no CONTENT_TYPE or media-type parameters were
240
- # provided. e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",
241
- # this method responds with the following Hash:
242
- # { 'charset' => 'utf-8' }
243
- def media_type_params: () -> untyped
244
-
245
- # The character set of the request body if a "charset" media type
246
- # parameter was given, or nil if no "charset" was specified. Note
247
- # that, per RFC2616, text/* media types that specify no explicit
248
- # charset are to be considered ISO-8859-1.
249
- def content_charset: () -> untyped
250
-
251
- # Determine whether the request body contains form-data by checking
252
- # the request content-type for one of the media-types:
253
- # "application/x-www-form-urlencoded" or "multipart/form-data". The
254
- # list of form-data media types can be modified through the
255
- # +FORM_DATA_MEDIA_TYPES+ array.
256
- #
257
- # A request body is also assumed to contain form-data when no
258
- # content-type header is provided and the request_method is POST.
259
- def form_data?: () -> untyped
260
-
261
- # Determine whether the request body contains data by checking
262
- # the request media_type against registered parse-data media-types
263
- def parseable_data?: () -> untyped
264
-
265
- # Returns the data received in the query string.
266
- def GET: () -> untyped
267
-
268
- # Returns the data received in the request body.
269
- #
270
- # This method support both application/x-www-form-urlencoded and
271
- # multipart/form-data.
272
- def POST: () -> untyped
273
-
274
- # The union of GET and POST data.
275
- #
276
- # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.
277
- def params: () -> untyped
278
-
279
- # Destructively update a parameter, whether it's in GET and/or POST. Returns nil.
280
- #
281
- # The parameter is updated wherever it was previous defined, so GET, POST, or both. If it wasn't previously defined, it's inserted into GET.
282
- #
283
- # <tt>env['rack.input']</tt> is not touched.
284
- def update_param: (untyped k, untyped v) -> (nil | untyped)
285
-
286
- # Destructively delete a parameter, whether it's in GET or POST. Returns the value of the deleted parameter.
287
- #
288
- # If the parameter is in both GET and POST, the POST value takes precedence since that's how #params works.
289
- #
290
- # <tt>env['rack.input']</tt> is not touched.
291
- def delete_param: (untyped k) -> untyped
292
-
293
- def base_url: () -> ::String
294
-
295
- # Tries to return a remake of the original request URL as a string.
296
- def url: () -> untyped
297
-
298
- def path: () -> untyped
299
-
300
- def fullpath: () -> (untyped | ::String)
301
-
302
- def accept_encoding: () -> untyped
303
-
304
- def accept_language: () -> untyped
305
-
306
- def trusted_proxy?: (untyped ip) -> untyped
307
-
308
- # shortcut for <tt>request.params[key]</tt>
309
- def []: (untyped key) -> untyped
310
-
311
- # shortcut for <tt>request.params[key] = value</tt>
312
- #
313
- # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.
314
- def []=: (untyped key, untyped value) -> untyped
315
-
316
- # like Hash#values_at
317
- def values_at: (*untyped keys) -> untyped
318
-
319
- private
320
-
321
- def default_session: () -> ::Hash[untyped, untyped]
322
-
323
- # Assist with compatibility when processing `X-Forwarded-For`.
324
- def wrap_ipv6: (untyped host) -> (::String | untyped)
325
-
326
- def parse_http_accept_header: (untyped header) -> untyped
327
-
328
- # Get an array of values set in the RFC 7239 `Forwarded` request header.
329
- def get_http_forwarded: (untyped token) -> untyped
330
-
331
- def query_parser: () -> untyped
332
-
333
- def parse_query: (untyped qs, ?::String d) -> untyped
334
-
335
- def parse_multipart: () -> untyped
336
-
337
- def expand_param_pairs: (untyped pairs, ?untyped query_parser) -> untyped
338
-
339
- def split_header: (untyped value) -> (untyped | ::Array[untyped])
340
-
341
- AUTHORITY: ::Regexp
342
-
343
- def split_authority: (untyped authority) -> ::Array[untyped]
344
-
345
- def reject_trusted_ip_addresses: (untyped ip_addresses) -> untyped
346
-
347
- FORWARDED_SCHEME_HEADERS: { proto: untyped, scheme: untyped }
348
-
349
- def forwarded_scheme: () -> (untyped | nil)
350
-
351
- def allowed_scheme: (untyped header) -> (untyped | nil)
352
-
353
- def forwarded_priority: () -> untyped
354
-
355
- def x_forwarded_proto_priority: () -> untyped
356
- end
357
-
358
- include Env
359
-
360
- include Helpers
361
- end
362
- end