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 +4 -4
- data/example.rb +17 -0
- data/lib/midori.rb +1 -1
- data/lib/midori/connection.rb +5 -18
- data/lib/midori/request.rb +3 -0
- data/lib/midori/runner.rb +10 -10
- data/lib/midori/server.rb +27 -25
- data/lib/midori/version.rb +1 -1
- metadata +10 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edd8000aa37d5c07d175259326fe4b04cb6abe08f544d9ba3d9abd97d95ee07e
|
4
|
+
data.tar.gz: b99490a6e749c18d6cbb780141f970f23fc5bddf33b55575b7f79938135a9f88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d186e5e12777db3855033f394eead2c927a127b5bfd9f06f784d72eec4bc560047517ae6e1e98303330cfa763b9196e0068611b7d18ec9897ea151e215267597
|
7
|
+
data.tar.gz: 2fcef7288f3f5358643ccd677ca628a40c3c06e4d8bceb0f4f0de411524da556f0fb449e881ac9b55d4350a7a1357704690baa54517e126ebce33e596aa6095a
|
data/example.rb
ADDED
@@ -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
|
data/lib/midori.rb
CHANGED
data/lib/midori/connection.rb
CHANGED
@@ -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
|
25
|
-
|
26
|
-
@
|
27
|
-
|
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
|
-
|
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
|
data/lib/midori/request.rb
CHANGED
@@ -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
|
data/lib/midori/runner.rb
CHANGED
@@ -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?
|
31
|
+
return false if running?
|
32
32
|
@logger.info "Midori #{Midori::VERSION} is now running on #{bind}:#{port}".blue
|
33
33
|
init_socket
|
34
|
-
|
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
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
data/lib/midori/server.rb
CHANGED
@@ -28,29 +28,32 @@ module Midori::Server
|
|
28
28
|
end
|
29
29
|
|
30
30
|
# Logic of receiving data
|
31
|
-
# @param [
|
32
|
-
def receive_data(
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
data =
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
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
|
-
|
123
|
-
|
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
|
data/lib/midori/version.rb
CHANGED
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.
|
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:
|
11
|
+
date: 2020-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: evt
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
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.
|
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:
|
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.
|
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: []
|