trace_header 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 914ab944715eda159c81340d84f4368a865ca512ec57d437c1545bfec58ec300
4
+ data.tar.gz: 7678cc5033c4e283fe10ebddc5d68af836e60b7402a4d3fb702dba78b9946047
5
+ SHA512:
6
+ metadata.gz: 4a214c910fa81a8ee6a2ef0b049cecaf589c92bc9f74ed1d3fd77500cd5b839d3813eefd12d104024baed952ec62af0c4c3f991ed542af260308f2997dd85502
7
+ data.tar.gz: cda2b1b11f265f691218c862e07be140ef1876ed3e55d40cefe1cbb0af23bc69bae4e18454c01ec6fad92cb99e57643529950ade306044bfc84d0bd42b994801
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in trace_header.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # TraceHeader
2
+
3
+ *PLEASE DO NOT USE THIS GEM IN YOUR PRODUCTION ENVIRONMENT*
4
+
5
+ Because This gem may change the behavior of your application.
6
+ Also, this doesn't work concurrently.
7
+
8
+ Use this gem only to learn about HTTP response headers and the Rack middleware.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'trace_header'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ WIP
25
+
26
+ ## Contributing
27
+
28
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shioimm/trace_header.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "trace_header"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,50 @@
1
+ module Rack
2
+ class TraceHeader
3
+ module Description
4
+ MAXIMUM_LENGTH = 50
5
+
6
+ def display(result)
7
+ puts description(result)
8
+ end
9
+
10
+ private
11
+ def description(result)
12
+ <<~"TEXT"
13
+ ----------------------------------------------------
14
+
15
+ TraceHeader printing...
16
+
17
+ [Target Middleware]
18
+ #{result.target_app.class}\n
19
+ [New Headers]
20
+ #{detailed_description(result.new_headers)}
21
+ [Changed Headers]
22
+ #{detailed_description(result.changed_headers)}
23
+ ----------------------------------------------------
24
+ TEXT
25
+ end
26
+
27
+ def detailed_description(headers)
28
+ if headers.empty?
29
+ " - Nothing added. -\n"
30
+ else
31
+ <<~"TEXT"
32
+ #{lined_description(headers).join("\n")}
33
+ TEXT
34
+ end
35
+ end
36
+
37
+ def lined_description(headers)
38
+ headers.flat_map do |header|
39
+ header.map { |field, value| " - #{field}: #{form(value)}" }
40
+ end
41
+ end
42
+
43
+ def form(text)
44
+ text.split(';').map do |str|
45
+ str.size > MAXIMUM_LENGTH ? str[0..MAXIMUM_LENGTH] + '...' : str
46
+ end.join(';')
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,51 @@
1
+ module Rack
2
+ class TraceHeader
3
+ class Result
4
+ attr_reader :target_app
5
+
6
+ def initialize(target_app, outputs)
7
+ @target_app = target_app
8
+ @target_hash = outputs.find { |output| output[:middleware].eql?(target_app.class) }
9
+ @inner_hash = outputs[outputs.index(@target_hash) + 1]
10
+ end
11
+
12
+ def new_headers
13
+ headers(new_fields)
14
+ end
15
+
16
+ def changed_headers
17
+ headers(changed_fields)
18
+ end
19
+
20
+ private
21
+
22
+ def common_fields
23
+ target_header.keys & prev_header.keys
24
+ end
25
+
26
+ def new_fields
27
+ target_header.keys - common_fields
28
+ end
29
+
30
+ def changed_fields
31
+ common_fields.select { |field| target_header[field] != prev_header[field] }
32
+ end
33
+
34
+ def target_header
35
+ @target_header ||= rack_app(@target_hash)[1]
36
+ end
37
+
38
+ def prev_header
39
+ @prev_header ||= rack_app(@inner_hash)[1]
40
+ end
41
+
42
+ def rack_app(hash)
43
+ hash[:app].call(hash[:env])
44
+ end
45
+
46
+ def headers(fields)
47
+ fields.map { |field| { field => target_header[field].to_s } }
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module TraceHeader
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,52 @@
1
+ require 'trace_header/version'
2
+ require 'active_support/core_ext'
3
+ require_relative 'trace_header/description'
4
+ require_relative 'trace_header/result'
5
+
6
+ module Rack
7
+ class TraceHeader
8
+ include Description
9
+
10
+ def initialize(app)
11
+ @app = app
12
+ @outputs = []
13
+ @fixed_app = nil
14
+ end
15
+
16
+ def call(env)
17
+ tracer.enable { @app.call(env) }
18
+ display(result)
19
+ @fixed_app
20
+ end
21
+
22
+ private
23
+ using Module.new {
24
+ refine TracePoint do
25
+ def called_rack_middleware?
26
+ method_id.eql?(:call) && binding.local_variable_defined?(:env)
27
+ end
28
+ end
29
+ }
30
+
31
+ def tracer
32
+ TracePoint.new(:call, :return) do |tp|
33
+ if tp.called_rack_middleware?
34
+ if tp.event.eql?(:call) \
35
+ && !@outputs.find { |input| input[:middleware].eql? tp.defined_class }
36
+ @outputs << { middleware: tp.defined_class,
37
+ app: tp.self.deep_dup,
38
+ env: tp.binding.local_variable_get(:env).deep_dup }
39
+ end
40
+
41
+ if tp.event.eql?(:return)
42
+ @fixed_app = tp.return_value
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def result
49
+ Rack::TraceHeader::Result.new(@app, @outputs)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "trace_header/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "trace_header"
8
+ spec.version = TraceHeader::VERSION
9
+ spec.authors = ["Misaki Shioi"]
10
+ spec.email = ["shioi.mm@gmail.com"]
11
+
12
+ spec.summary = %q{To inspect how the response headers would be changed by Rack middleware}
13
+ spec.description = %q{TraceHedaer is to inspect how the response headers would be changed by Rack middleware, and display them in your terminal.}
14
+ spec.homepage = "https://github.com/shioimm/trace_header"
15
+
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency 'active_support'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.17"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trace_header
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Misaki Shioi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_support
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: TraceHedaer is to inspect how the response headers would be changed by
56
+ Rack middleware, and display them in your terminal.
57
+ email:
58
+ - shioi.mm@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/trace_header.rb
70
+ - lib/trace_header/description.rb
71
+ - lib/trace_header/result.rb
72
+ - lib/trace_header/version.rb
73
+ - trace_header.gemspec
74
+ homepage: https://github.com/shioimm/trace_header
75
+ licenses: []
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.0.6
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: To inspect how the response headers would be changed by Rack middleware
96
+ test_files: []