susu 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/susu/http_lib/http.rb +35 -0
- data/lib/susu/http_lib/httpclient.rb +52 -0
- data/lib/susu/http_lib/net_http.rb +35 -0
- data/lib/susu/susu.rb +74 -0
- data/lib/susu/version.rb +3 -0
- data/lib/susu.rb +5 -0
- metadata +8 -9
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -1
- data/LICENSE.txt +0 -21
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/test/susu_test.rb +0 -11
- data/test/test_helper.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a5669b11647a6b1411118fbb1343a4b808c811f
|
4
|
+
data.tar.gz: e4c22adabfe5977d60060875af8b628d0bb59c49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/susu/version.rb
ADDED
data/lib/susu.rb
ADDED
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.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hoptq
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
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
|
-
-
|
110
|
-
-
|
111
|
-
-
|
112
|
-
-
|
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
data/test/susu_test.rb
DELETED
data/test/test_helper.rb
DELETED