midori.rb 0.7.1 → 0.8.0

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: 2adfa1f5075c05406725691e2e254c7ac1e0f3ff9aba2e8787d9567323681654
4
- data.tar.gz: f6f22a1df53862dbc956f53b188a7e4b023c6ef85e58f879c4fcdaefb0539e38
3
+ metadata.gz: edd8000aa37d5c07d175259326fe4b04cb6abe08f544d9ba3d9abd97d95ee07e
4
+ data.tar.gz: b99490a6e749c18d6cbb780141f970f23fc5bddf33b55575b7f79938135a9f88
5
5
  SHA512:
6
- metadata.gz: ab4f42932ef1851b8529c6eaa3d3a10d8a21cdd97e3702e94717a0bb8076ab981cd352d275e700f93203f4c790b75993b066e7c59f6b2c4fa6aa39f5479bd210
7
- data.tar.gz: cab7f16b86eaa3e532ab5d40265e057c7b998d4122d002db14690e78e243191c2888795a77364a45bcd3112f6524ef194e71eb239b44026831a4ce789dd5381b
6
+ metadata.gz: d186e5e12777db3855033f394eead2c927a127b5bfd9f06f784d72eec4bc560047517ae6e1e98303330cfa763b9196e0068611b7d18ec9897ea151e215267597
7
+ data.tar.gz: 2fcef7288f3f5358643ccd677ca628a40c3c06e4d8bceb0f4f0de411524da556f0fb449e881ac9b55d4350a7a1357704690baa54517e126ebce33e596aa6095a
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ $LOAD_PATH.unshift File.expand_path("./lib", __dir__)
3
+
4
+ require 'evt'
5
+ require 'midori'
6
+
7
+ Fiber.set_scheduler Evt::Scheduler.new
8
+
9
+ class HelloWorldAPI < Midori::API
10
+ get '/' do
11
+ 'Ohayou Midori'
12
+ end
13
+ end
14
+
15
+ Fiber.schedule do
16
+ Midori::Runner.new(HelloWorldAPI).start
17
+ end
@@ -1,11 +1,11 @@
1
1
  require 'cgi'
2
+ require 'evt'
2
3
  require 'digest/sha1'
3
4
  require 'stringio'
4
5
  require 'fiber'
5
6
  require 'logger'
6
7
  require 'mizu'
7
8
  require 'mustermann'
8
- require 'murasaki'
9
9
  require 'socket'
10
10
 
11
11
  require_relative 'midori/core_ext/configurable'
@@ -13,19 +13,16 @@ class Midori::Connection
13
13
  @registered = false
14
14
  @socket = socket[0]
15
15
  @peer_addr = socket[1].ip_unpack
16
- @monitor = nil
17
16
  @close_flag = false
18
17
  @buffer = ''
19
- listen(@socket)
20
18
  end
21
19
 
22
20
  # Register events of connection
23
21
  # @param [Array] socket raw socket
24
- def listen(socket)
25
- EventLoop.register(socket, :rw) do |monitor|
26
- @monitor = monitor
27
- receive_data(monitor) if monitor.readable?
28
- if monitor.writable?
22
+ def listen
23
+ Fiber.schedule do
24
+ until @socket.closed?
25
+ receive_data(@socket)
29
26
  if !@buffer.empty?
30
27
  send_buffer
31
28
  elsif @close_flag
@@ -46,23 +43,13 @@ class Midori::Connection
46
43
  # Send buffer immediately
47
44
  # @return [nil] nil
48
45
  private def send_buffer
49
- if @monitor.writable?
50
- written = @socket.write_nonblock(@buffer)
51
- @buffer = @buffer.byteslice(written..-1)
52
- end
53
- nil
54
- rescue IO::EAGAINWaitWritable => _e
55
- # :nocov:
56
- # Unknown Reason Resource Conflict
46
+ @socket.write(@buffer) unless @socket.closed?
57
47
  nil
58
- # :nocov:
59
48
  end
60
49
 
61
50
  # Close the connection
62
51
  # @return [nil] nil
63
52
  def close_connection
64
- EventLoop.remove_timer(@keep_alive_timer) unless @keep_alive_timer.nil? # Be sure to remove timer for memory safety
65
- EventLoop.deregister @socket
66
53
  @socket.close
67
54
  nil
68
55
  end
@@ -26,6 +26,8 @@ class Midori::Request
26
26
  @body_parsed = false
27
27
  @is_websocket = false
28
28
  @is_eventsource = false
29
+ @ip = ''
30
+ @port = 0
29
31
  @parser = Mizu::Parser.new
30
32
  @params = {}
31
33
  @query_params = Hash.new(Array.new)
@@ -74,6 +76,7 @@ class Midori::Request
74
76
  if @parsed
75
77
  @body += data
76
78
  else
79
+ return nil if data.nil?
77
80
  @parser << data
78
81
  @body += data[@parser.offset..-1] if @parsed
79
82
  end
@@ -28,20 +28,22 @@ class Midori::Runner
28
28
  # Start the Midori server
29
29
  # @note This is an async method, but no callback
30
30
  def start
31
- return false if running? || EventLoop.running?
31
+ return false if running?
32
32
  @logger.info "Midori #{Midori::VERSION} is now running on #{bind}:#{port}".blue
33
33
  init_socket
34
- async_fiber(Fiber.new do
34
+ Fiber.schedule do
35
35
  @logger.info 'Midori is booting...'.blue
36
36
  @before.call
37
37
  @logger.info 'Midori is serving...'.blue
38
- EventLoop.register(@server, :r) do |monitor|
39
- socket = monitor.io.accept_nonblock
40
- connection = Midori::Connection.new(socket)
41
- connection.server_initialize(@api, @logger)
38
+ Fiber.schedule do
39
+ loop do
40
+ socket = @server.accept
41
+ connection = Midori::Connection.new(socket)
42
+ connection.server_initialize(@api, @logger)
43
+ connection.listen
44
+ end
42
45
  end
43
- end)
44
- EventLoop.start
46
+ end
45
47
  nil
46
48
  end
47
49
 
@@ -63,10 +65,8 @@ class Midori::Runner
63
65
  def stop
64
66
  if running?
65
67
  @logger.info 'Stopping Midori'.blue
66
- EventLoop.deregister @server
67
68
  @server.close
68
69
  @server = nil
69
- EventLoop.stop
70
70
  true
71
71
  else
72
72
  @logger.error 'Midori Server has NOT been started'.red
@@ -28,29 +28,32 @@ module Midori::Server
28
28
  end
29
29
 
30
30
  # Logic of receiving data
31
- # @param [NIO::Monitor] monitor the socket able to read
32
- def receive_data(monitor)
33
- async_fiber(Fiber.new do
34
- begin
35
- @request.ip, @request.port = @peer_addr
36
- data = monitor.io.read_nonblock(16_384)
37
- if @request.parsed? && @request.body_parsed?
38
- websocket_request(StringIO.new(data))
39
- else
40
- @request.parse(data)
41
- receive_new_request if @request.parsed && @request.body_parsed?
42
- end
43
- rescue EOFError, Errno::ENOTCONN => _e
44
- close_connection
45
- # Ignore client's disconnection
46
- rescue => e
47
- # :nocov:
48
- # Leave for corner cases
49
- close_connection
50
- @logger.warn "#{@request.ip} - - #{e.class} #{e.backtrace.join("\n")}".yellow
51
- # :nocov:
31
+ # @param [Scoket] the socket able to read
32
+ def receive_data(socket)
33
+ begin
34
+ @request.ip, @request.port = @peer_addr
35
+ if @request.parsed? && !(@request.body_parsed?)
36
+ data = socket.read @request.body.bytesize - @request.header['Content-Length']
37
+ else
38
+ data = socket.readline
39
+ end
40
+
41
+ if @request.websocket?
42
+ websocket_request(StringIO.new(data))
43
+ else
44
+ @request.parse(data)
45
+ receive_new_request if @request.parsed? && @request.body_parsed?
52
46
  end
53
- end)
47
+ rescue EOFError, Errno::ENOTCONN => _e
48
+ close_connection
49
+ # Ignore client's disconnection
50
+ rescue => e
51
+ # :nocov:
52
+ # Leave for corner cases
53
+ close_connection
54
+ @logger.warn "#{@request.ip} - - #{e.class} #{e.backtrace.join("\n")}".yellow
55
+ # :nocov:
56
+ end
54
57
  end
55
58
 
56
59
  # Logic of receiving new request
@@ -119,11 +122,10 @@ module Midori::Server
119
122
  end
120
123
  # Add timeout for keep-alive
121
124
  @keep_alive_count += 1
122
- EventLoop.remove_timer(@keep_alive_timer) unless @keep_alive_timer.nil?
123
- @keep_alive_timer = EventLoop::Timer.new(Midori::Configure.keep_alive_timeout) do
125
+ Fiber.schedule do
126
+ sleep Midori::Configure.keep_alive_timeout
124
127
  close_connection
125
128
  end
126
- EventLoop.add_timer(@keep_alive_timer)
127
129
  # Reset request
128
130
  @request.reset!
129
131
  end
@@ -1,5 +1,5 @@
1
1
  # Midori Module
2
2
  module Midori
3
3
  # Current Version Code
4
- VERSION = '0.7.1'.freeze
4
+ VERSION = '0.8.0'.freeze
5
5
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midori.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HeckPsi Lab
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-30 00:00:00.000000000 Z
11
+ date: 2020-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: murasaki
14
+ name: evt
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.2.2
19
+ version: 0.3.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.2.2
26
+ version: 0.3.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: mustermann
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -78,6 +78,7 @@ files:
78
78
  - ".editorconfig"
79
79
  - ".github/ISSUE_TEMPLATE.md"
80
80
  - LICENSE
81
+ - example.rb
81
82
  - ext/midori/extconf.rb
82
83
  - ext/midori/websocket.c
83
84
  - lib/midori.rb
@@ -125,15 +126,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
126
  requirements:
126
127
  - - ">="
127
128
  - !ruby/object:Gem::Version
128
- version: 2.3.6
129
+ version: 3.0.0.rc1
129
130
  required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  requirements:
131
132
  - - ">="
132
133
  - !ruby/object:Gem::Version
133
134
  version: '0'
134
135
  requirements: []
135
- rubygems_version: 3.0.1
136
- signing_key:
136
+ rubygems_version: 3.2.2
137
+ signing_key:
137
138
  specification_version: 4
138
139
  summary: High performance ruby web framework
139
140
  test_files: []