susu 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0fa05c876bea2bb2aa92aae435330fc76f1ce079
4
- data.tar.gz: c31d3ec03fc35ddd7384181b31c2b0ea7d68ec9e
3
+ metadata.gz: 9a5669b11647a6b1411118fbb1343a4b808c811f
4
+ data.tar.gz: e4c22adabfe5977d60060875af8b628d0bb59c49
5
5
  SHA512:
6
- metadata.gz: 165efae4ffcce68a8dd5b3843b92e7fd60de9720126e67ebdc3888af61b33a0a442db524b9e03496b30c648340204d36ec2b056f9298db202abcfe254969dea7
7
- data.tar.gz: 8f205733ed09858647274e4686d0932ab2cf717f92f3f1e6348dabbf179772f01a2e74c4ef6a30526aa5dfce8cf8c432f78fa497c21d807c8dc3616b9e878bd1
6
+ metadata.gz: 174a610163af1c0b3a4a9787bbf8817944aa458817ca1752c085fac86663aefdfcd991387d00f2bdffaea8f6a9d6df823b817b86553502c28c00c5e33b5a1cd4
7
+ data.tar.gz: 272454ad89fee4f2af6e8778380f5a01a5c7c35428b2bfd8234272e71031f1d6822806d4356a758309f5d99e110cb2777bfd1410940bb0d7c59d0e5596110e62
@@ -0,0 +1,35 @@
1
+ if defined?(::HTTP::Client) && defined?(::HTTP::Connection)
2
+ module ::HTTP
3
+ class Client
4
+ request_method = respond_to?('make_request') ? 'make_request' : 'perform'
5
+ orig_request_method = "orig_#{request_method}"
6
+ alias_method(orig_request_method, request_method) unless method_defined?(orig_request_method)
7
+
8
+ define_method request_method do |req, options|
9
+ bm = Benchmark.realtime do
10
+ @response = send(orig_request_method, req, options)
11
+ end
12
+
13
+ headers = @response.headers
14
+ post_params = req.body
15
+ post_params = Rack::Utils.parse_nested_query post_params if post_params.class == String
16
+
17
+ Susu.log_all(
18
+ req.verb, req.uri, @response.code, bm, post_params, @response.body,
19
+ headers, headers['Content-Encoding'], headers['Content-Type']
20
+ )
21
+
22
+ @response
23
+ end
24
+ end
25
+
26
+ class Connection
27
+ alias_method(:orig_initialize, :initialize) unless method_defined?(:orig_initialize)
28
+
29
+ def initialize(req, options)
30
+ Susu.log_connection(req.uri.host, req.uri.port)
31
+ orig_initialize(req, options)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,52 @@
1
+ if defined?(::HTTPClient)
2
+ class HTTPClient
3
+ private
4
+ alias_method :orig_do_get_block, :do_get_block
5
+
6
+ def do_get_block(req, proxy, conn, &block)
7
+ retryable_response = nil
8
+ bm = Benchmark.realtime do
9
+ begin
10
+ orig_do_get_block(req, proxy, conn, &block)
11
+ rescue RetryableResponse => e
12
+ retryable_response = e
13
+ end
14
+ end
15
+
16
+ res = conn.pop
17
+ headers = res.headers
18
+ post_params = req.body
19
+ post_params = Rack::Utils.parse_nested_query post_params if post_params.class == String
20
+
21
+ Susu.log_all(
22
+ req.header.request_method, req.header.request_uri,
23
+ res.status_code, bm, post_params, res.body, headers,
24
+ headers['Content-Encoding'], headers['Content-Type']
25
+ )
26
+ conn.push(res)
27
+
28
+ raise retryable_response if retryable_response != nil
29
+ end
30
+
31
+ class Session
32
+ alias_method :orig_create_socket, :create_socket
33
+
34
+ # up to version 2.6, the method signature is `create_socket(site)`; after that,
35
+ # it's `create_socket(hort, port)`
36
+ if instance_method(:create_socket).arity == 1
37
+ def create_socket(site)
38
+ Susu.log_connection(site.host, site.port)
39
+ # end
40
+ orig_create_socket(site)
41
+ end
42
+
43
+ else
44
+ def create_socket(host, port)
45
+ Susu.log_connection(host, port)
46
+ # end
47
+ orig_create_socket(host,port)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,35 @@
1
+ module Net
2
+ class HTTP
3
+ alias_method(:orig_request, :request) unless method_defined?(:orig_request)
4
+ alias_method(:orig_connect, :connect) unless method_defined?(:orig_connect)
5
+
6
+ def request(req, body = nil, &block)
7
+
8
+ url = "http://#{@address}:#{@port}#{req.path}"
9
+
10
+ bm = Benchmark.realtime do
11
+ @response = orig_request(req, body, &block)
12
+ end
13
+ headers = @response.each_header.collect
14
+ post_params = req.body.nil? || req.body.size == 0 ? body : req.body
15
+ post_params = Rack::Utils.parse_nested_query post_params if post_params.class == String
16
+
17
+ if started?
18
+ Susu.log_all(
19
+ req.method, url, @response.code, bm, post_params,
20
+ @response.body, headers,
21
+ @response['Content-Encoding'], @response['Content-Type']
22
+ )
23
+ end
24
+
25
+ @response
26
+ end
27
+
28
+ def connect
29
+ Susu.log_connection(@address, @port) if !started?
30
+
31
+ orig_connect
32
+ end
33
+ end
34
+
35
+ end
data/lib/susu/susu.rb ADDED
@@ -0,0 +1,74 @@
1
+ require "net/http"
2
+ require "logger"
3
+ require "benchmark"
4
+ require "rack"
5
+
6
+ module Susu
7
+ LOG_PREFIX = "[SusuLog] "
8
+
9
+ class << self
10
+
11
+ def log(msg)
12
+ Rails.logger.debug(msg)
13
+ Rails.logger.flush
14
+ end
15
+
16
+ def log_connection(host, port = nil)
17
+ end
18
+
19
+ def log_all(method, uri, status, duration, post_params, response, headers, encoding, content_type)
20
+ status = Rack::Utils.status_code(status) unless status == /\d{3}/
21
+ duration = duration.to_f.round(6)
22
+ method = method.to_s.upcase
23
+ response_body = parse_body(response, encoding, content_type)
24
+ post_params = utf_encoded(post_params.to_s.dup)
25
+ # log("#{method.to_s.upcase} #{uri} completed with status code #{status} in #{seconds} seconds")
26
+ log("#{LOG_PREFIX}method=#{method} path=#{uri} status=#{status} duration=#{duration} post_params=#{post_params} response=#{response_body}")
27
+ end
28
+
29
+ private
30
+
31
+ def parse_body(body, encoding = nil, content_type=nil)
32
+ unless text_based?(content_type)
33
+ # log("Response: (not showing binary data)")
34
+ return "Response: (not showing binary data)"
35
+ end
36
+
37
+ if body.is_a?(Net::ReadAdapter)
38
+ # open-uri wraps the response in a Net::ReadAdapter that defers reading
39
+ # the content, so the reponse body is not available here.
40
+ # log("Response: (not available yet)")
41
+ return "Response: (not available yet)"
42
+ end
43
+
44
+ if encoding =~ /gzip/ && body && !body.empty?
45
+ sio = StringIO.new( body.to_s )
46
+ gz = Zlib::GzipReader.new( sio )
47
+ body = gz.read
48
+ end
49
+
50
+ data = utf_encoded(body.to_s, content_type)
51
+ return data
52
+ end
53
+
54
+ def utf_encoded(data, content_type=nil)
55
+ charset = content_type.to_s.scan(/; charset=(\S+)/).flatten.first || 'UTF-8'
56
+ data.force_encoding(charset) rescue data.force_encoding('UTF-8')
57
+ data.encode('UTF-8', :invalid => :replace, :undef => :replace)
58
+ end
59
+
60
+ def text_based?(content_type)
61
+ # This is a very naive way of determining if the content type is text-based; but
62
+ # it will allow application/json and the like without having to resort to more
63
+ # heavy-handed checks.
64
+ content_type =~ /^text/ ||
65
+ content_type =~ /^application/ && content_type != 'application/octet-stream'
66
+ end
67
+
68
+ def log_data_lines(data)
69
+ data.each_line.with_index do |line, row|
70
+ log("#{row + 1}: #{line.chomp}")
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module Susu
2
+ VERSION = "1.0.1"
3
+ end
data/lib/susu.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "susu/version"
2
+ require "susu/susu"
3
+ require "susu/http_lib/net_http"
4
+ require "susu/http_lib/httpclient"
5
+ require "susu/http_lib/http"
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: susu
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - hoptq
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2017-12-14 00:00:00.000000000 Z
12
12
  dependencies:
@@ -101,15 +101,14 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - CODE_OF_CONDUCT.md
105
- - Gemfile
106
- - LICENSE.txt
107
104
  - README.md
108
105
  - Rakefile
109
- - bin/console
110
- - bin/setup
111
- - test/susu_test.rb
112
- - test/test_helper.rb
106
+ - lib/susu.rb
107
+ - lib/susu/http_lib/http.rb
108
+ - lib/susu/http_lib/httpclient.rb
109
+ - lib/susu/http_lib/net_http.rb
110
+ - lib/susu/susu.rb
111
+ - lib/susu/version.rb
113
112
  homepage: ''
114
113
  licenses:
115
114
  - MIT
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at hoptq@topica.edu.vn. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile DELETED
@@ -1 +0,0 @@
1
- source "https://rubygems.org"
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2017 hoptq
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "susu"
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 DELETED
@@ -1,8 +0,0 @@
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
data/test/susu_test.rb DELETED
@@ -1,11 +0,0 @@
1
- require "test_helper"
2
-
3
- class SusuTest < Minitest::Test
4
- def test_that_it_has_a_version_number
5
- refute_nil ::Susu::VERSION
6
- end
7
-
8
- def test_it_does_something_useful
9
- assert false
10
- end
11
- end
data/test/test_helper.rb DELETED
@@ -1,4 +0,0 @@
1
- $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
- require "susu"
3
-
4
- require "minitest/autorun"