ruby_wolf 0.3.3 → 0.4.0

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: 698a7c16587b3bd2d1359bd4e6392666346c29af
4
- data.tar.gz: 7dd6c7e7936651dd78cdf0480ff50915a863630f
3
+ metadata.gz: 03a1ec8b4a81dec0caa042067573f74ea59b294d
4
+ data.tar.gz: bfb6eded8e2f5e1a36b1f38ef6cd19f4a5aafbba
5
5
  SHA512:
6
- metadata.gz: 728ca882ea7fda050bc126ec75cce59f08c48e66ee48de32c6932235d3390cdafbfb9340bc8bbcc06660279cc1dc0ef8485b0a832800b291f41b3aaf67d832bf
7
- data.tar.gz: 33a07bd9e0597fac9fb58cbf03632383b1a4beaae64ca9efca69431936e1e902df457fd38612ed7b5136a406019d1a2ea231db8dee5f1da2413ac1b6d11e125f
6
+ metadata.gz: 5f532b43a8b05fe7b53468f2a81cfceb97fda03f05e16b18e2abf14c3d9b38b3fb26fb77f6791cea9ac0410f3403d980d7cf9cbd2a58a96b96e563b00c6bb328
7
+ data.tar.gz: 8f8c7aec4adb099866f76e89592bf97b35a47143b69bc3e7b9aa6f80d1569e848f8842970dd73234142a3d5ceaa5e192de785732a9069f414948661bb654b0bd
@@ -2,28 +2,36 @@ require 'http/parser'
2
2
 
3
3
  module RubyWolf
4
4
  class Handler
5
-
6
- attr_reader :app, :env, :connection, :response, :callback, :body, :headers, :status
5
+ attr_reader :app, :env, :connection, :response
7
6
 
8
7
  def initialize(app, connection, &callback)
9
8
  @app = app
10
- @connection = connection
11
- @callback = callback
12
9
  @env = {}
10
+ @connection = connection
13
11
  @response = ''
12
+
13
+ @callback = callback
14
14
  end
15
15
 
16
16
  def process
17
17
  prepare_rack_env
18
- generate_response
19
- callback.call(response) if callback
20
- response
18
+ log_request
19
+ process_request
20
+ compose_response
21
+ log_response
22
+
23
+ @callback.call(response) if @callback
24
+ @response
21
25
  end
22
26
 
23
27
  private
24
28
 
25
29
  def prepare_rack_env
26
- @env = ENV.to_h.merge(
30
+ @connection.headers.each do |key, value|
31
+ @env[rack_key(key)] = value
32
+ end
33
+
34
+ @env = @env.merge(
27
35
  'rack.version' => ::Rack::VERSION,
28
36
  'rack.errors' => STDERR,
29
37
  'rack.multithread' => false,
@@ -31,69 +39,66 @@ module RubyWolf
31
39
  'rack.runonce' => true,
32
40
  'rack.url_scheme' => ENV['HTTPS'] ? 'https' : 'http',
33
41
 
34
- 'REQUEST_METHOD' => connection.method,
35
- 'REQUEST_PATH' => connection.path,
36
- 'PATH_INFO' => connection.path,
37
- 'QUERY_STRING' => connection.query,
42
+ 'REQUEST_METHOD' => @connection.method,
43
+ 'REQUEST_PATH' => @connection.path,
44
+ 'PATH_INFO' => @connection.path,
45
+ 'QUERY_STRING' => @connection.query,
38
46
 
39
47
  'SERVER_PROTOCOL' => 'HTTP/1.1',
40
48
  'SERVER_NAME' => 'Ruby Wolf',
41
49
  'HTTP_VERSION' => 'HTTP/1.1',
42
- 'HTTP_HOST' => connection.headers['Host'],
43
- 'HTTP_USER_AGENT' => connection.headers['User-Agent'],
44
- 'HTTP_ACCEPT' => connection.headers['Accept'],
45
- 'CONTENT_LENGTH' => connection.headers['Content-Length'],
46
- 'CONTENT_TYPE' => connection.headers['Content-Type'],
47
50
 
48
- 'rack.input' => StringIO.new(connection.read_chunk)
51
+ 'rack.input' => StringIO.new(@connection.read_chunk)
49
52
  )
50
- log_request
51
53
  end
52
54
 
53
- def generate_response
54
- @status, @headers, @body = app.call(env)
55
- compose_response
56
- log_response
55
+ def process_request
56
+ @status, @headers, @body = @app.call(env)
57
+ rescue => e
58
+ message = "Error while processing the request: #{e.message}\n#{e.backtrace.join("\n")}"
59
+ @status = 500
60
+ @body = [message]
61
+ @headers = [
62
+ ['Content-Length', message.bytesize],
63
+ ['Content-Type', 'text/plain; charset=utf-8']
64
+ ]
65
+ RubyWolf.logger.error(message)
57
66
  end
58
67
 
59
68
  def compose_response
60
- @response += "HTTP/1.1 #{status} #{RubyWolf::CRLF}"
61
- headers.each do |key, value|
69
+ @response += "HTTP/1.1 #{@status} #{RubyWolf::CRLF}"
70
+ @headers.each do |key, value|
62
71
  @response += "#{key}: #{value}#{RubyWolf::CRLF}"
63
72
  end
64
73
 
65
74
  @response += RubyWolf::CRLF
66
- body.each do |part|
75
+ @body.each do |part|
67
76
  @response += part
68
77
  end
69
78
  ensure
70
- body.close if body.respond_to? :close
79
+ @body.close if @body.respond_to? :close
71
80
  end
72
81
 
73
82
  private
74
83
 
75
84
  def log_request
76
- RubyWolf.logger.info(
77
- [
78
- 'HTTP/1.1',
79
- connection.method,
80
- request_path
81
- ].join(' ')
82
- )
85
+ RubyWolf.logger.info("HTTP/1.1 #{@connection.method} #{request_path}")
83
86
  end
84
87
 
85
88
  def log_response
86
- RubyWolf.logger.info(
87
- "Response HTTP/1.1 #{status}"
88
- )
89
+ RubyWolf.logger.info("Response HTTP/1.1 #{@status}")
89
90
  end
90
91
 
91
92
  def request_path
92
- if connection.query
93
- "#{connection.path}?#{connection.query}"
93
+ if @connection.query
94
+ "#{@connection.path}?#{@connection.query}"
94
95
  else
95
- connection.path
96
+ @connection.path
96
97
  end
97
98
  end
99
+
100
+ def rack_key(key)
101
+ "HTTP_#{key.upcase.gsub(/[^0-9A-Z]/, '_')}"
102
+ end
98
103
  end
99
104
  end
@@ -2,31 +2,39 @@ require 'logger'
2
2
 
3
3
  module RubyWolf
4
4
  class Logger < ::Logger
5
- def info(contents = "")
5
+ def initialize(*args)
6
+ super(*args)
7
+ @formatter = proc do |severity, datetime, _progname, msg|
8
+ date_format = datetime.strftime('%Y-%m-%d %H:%M:%S')
9
+ "[#{severity.to_s[0]}] [#{date_format}] #{msg}\n"
10
+ end
11
+ end
12
+
13
+ def info(contents = '')
6
14
  pre_process(contents) do |content|
7
15
  super(content)
8
16
  end
9
17
  end
10
18
 
11
- def warn(contents = "")
19
+ def warn(contents = '')
12
20
  pre_process(contents) do |content|
13
21
  super(content)
14
22
  end
15
23
  end
16
24
 
17
- def debug(contents = "")
25
+ def debug(contents = '')
18
26
  pre_process(contents) do |content|
19
27
  super(content)
20
28
  end
21
29
  end
22
30
 
23
- def error(contents = "")
31
+ def error(contents = '')
24
32
  pre_process(contents) do |content|
25
33
  super(content)
26
34
  end
27
35
  end
28
36
 
29
- def fatal(contents = "")
37
+ def fatal(contents = '')
30
38
  pre_process(contents) do |content|
31
39
  super(content)
32
40
  end
@@ -1,3 +1,3 @@
1
1
  module RubyWolf
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_wolf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nguyễn Quang Minh