em-midori 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/midori/connection.rb +1 -1
- data/lib/midori/core_ext/event_loop.rb +1 -0
- data/lib/midori/request.rb +35 -8
- data/lib/midori/server.rb +6 -7
- data/lib/midori/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94ba1f04f2fa2a1c67fb02452a7e876a4539055a
|
4
|
+
data.tar.gz: f0f7755bfc1d858f8b96a59bd4d85448063b0e18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '019ee08cb1214e64468b04d134399815237c792470916af7590293c321b2400b76a9190c73a163625529d542c768ec6a9e953a0b8192b032d6fa12a86fbf5581'
|
7
|
+
data.tar.gz: eb8310f650a6963f3ffd54c4544e7d6e9ad850dde10f89f00f87761d13c0a18f0eaa606cdd9800b2abc01f4bd07d73818be643f111ade90ea05137a63bb7de81
|
data/lib/midori/connection.rb
CHANGED
data/lib/midori/request.rb
CHANGED
@@ -13,20 +13,28 @@
|
|
13
13
|
class Midori::Request
|
14
14
|
attr_accessor :ip, :port,
|
15
15
|
:protocol, :method, :path, :query_string,
|
16
|
-
:header, :body, :parsed, :params
|
16
|
+
:header, :body, :parsed, :body_parsed, :params
|
17
17
|
|
18
18
|
# Init Request
|
19
19
|
def initialize
|
20
20
|
@parsed = false
|
21
|
+
@body_parsed = false
|
21
22
|
@is_websocket = false
|
22
23
|
@is_eventsource = false
|
23
24
|
@parser = Http::Parser.new
|
24
25
|
@params = {}
|
26
|
+
@body = ''
|
25
27
|
@parser.on_headers_complete = proc do
|
26
28
|
@protocol = @parser.http_version
|
27
29
|
@method = @parser.http_method
|
28
30
|
@path = @parser.request_url
|
29
31
|
@header = @parser.headers
|
32
|
+
|
33
|
+
@query_string = @path.match(/\?(.*?)$/)
|
34
|
+
@query_string = @query_string[1] unless @query_string.nil?
|
35
|
+
@path.gsub!(/\?(.*?)$/, '')
|
36
|
+
@method = @method.to_sym
|
37
|
+
@parsed = true
|
30
38
|
:stop
|
31
39
|
end
|
32
40
|
end
|
@@ -35,12 +43,25 @@ class Midori::Request
|
|
35
43
|
# @param [String] data
|
36
44
|
# @return [nil] nil
|
37
45
|
def parse(data)
|
38
|
-
|
39
|
-
@
|
40
|
-
|
41
|
-
|
42
|
-
|
46
|
+
# Call parser if header not parsed
|
47
|
+
if @parsed
|
48
|
+
@body += data
|
49
|
+
else
|
50
|
+
offset = @parser << data
|
51
|
+
@body += data[offset..-1] if @parsed
|
52
|
+
end
|
53
|
+
|
54
|
+
# Set body parsed if body reaches content length
|
55
|
+
if (@header['Content-Length'].to_i || 0) <= @body.bytesize
|
56
|
+
@body_parsed = true
|
57
|
+
pre_proceed
|
58
|
+
end
|
59
|
+
nil
|
60
|
+
end
|
43
61
|
|
62
|
+
# Preproceed the request after parsed
|
63
|
+
# @return [nil] nil
|
64
|
+
def pre_proceed
|
44
65
|
# Deal with WebSocket
|
45
66
|
if @header['Upgrade'] == 'websocket' && @header['Connection'] == 'Upgrade'
|
46
67
|
@method = :WEBSOCKET
|
@@ -54,15 +75,21 @@ class Midori::Request
|
|
54
75
|
end
|
55
76
|
|
56
77
|
@method = @method.to_sym
|
57
|
-
|
78
|
+
nil
|
58
79
|
end
|
59
80
|
|
60
|
-
# Syntactic sugar for whether a request is parsed
|
81
|
+
# Syntactic sugar for whether a request header is parsed
|
61
82
|
# @return [Boolean] parsed or not
|
62
83
|
def parsed?
|
63
84
|
@parsed
|
64
85
|
end
|
65
86
|
|
87
|
+
# Syntactic sugar for whether a request body is parsed
|
88
|
+
# @return [Boolean] parsed or not
|
89
|
+
def body_parsed?
|
90
|
+
@body_parsed
|
91
|
+
end
|
92
|
+
|
66
93
|
# Syntactic sugar for whether a request is a websocket request
|
67
94
|
# @return [Boolean] websocket or not
|
68
95
|
def websocket?
|
data/lib/midori/server.rb
CHANGED
@@ -34,26 +34,25 @@ module Midori::Server
|
|
34
34
|
@request.ip = ip
|
35
35
|
@request.port = port
|
36
36
|
data = monitor.io.read_nonblock(16_384)
|
37
|
-
if @request.parsed?
|
37
|
+
if @request.parsed? && @request.body_parsed?
|
38
38
|
websocket_request(StringIO.new(data))
|
39
39
|
else
|
40
|
-
|
40
|
+
@request.parse(data)
|
41
|
+
receive_new_request if @request.parsed && @request.body_parsed?
|
41
42
|
end
|
42
43
|
now_time = Time.now
|
43
44
|
@logger.info "#{@request.ip} - - \"#{@request.method} #{@request.path} HTTP/#{@request.protocol.join('.')}\" #{@response.status} #{(now_time.to_f - start_time.to_f).round(6)}".green
|
44
|
-
rescue
|
45
|
+
rescue => e
|
45
46
|
close_connection
|
46
|
-
@logger.warn "#{@request.ip} - -
|
47
|
+
@logger.warn "#{@request.ip} - - #{e.backtrace.join("\n")}".yellow
|
47
48
|
end
|
48
49
|
end)
|
49
50
|
end.call
|
50
51
|
end
|
51
52
|
|
52
53
|
# Logic of receiving new request
|
53
|
-
|
54
|
-
def receive_new_request(data)
|
54
|
+
def receive_new_request
|
55
55
|
begin
|
56
|
-
@request.parse(data)
|
57
56
|
@response = @api.receive(request, self)
|
58
57
|
call_event(:open) if @request.websocket?
|
59
58
|
rescue Midori::Exception::NotFound => e
|
data/lib/midori/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-midori
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- HeckPsi Lab
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nio4r
|