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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e4238511fe308c3529be24fbad1cd44b3e436cc
4
- data.tar.gz: 9a01e07793ec3b2f72fd2e43f2609aba482798dd
3
+ metadata.gz: 94ba1f04f2fa2a1c67fb02452a7e876a4539055a
4
+ data.tar.gz: f0f7755bfc1d858f8b96a59bd4d85448063b0e18
5
5
  SHA512:
6
- metadata.gz: 336b392c12a61aafa8538d3d6cad03e7c7c1559432faf4dc879d4dfc16dd0bff2a69822a20e52f79f2fc1fb7198d6bf05111cccf4f57aecf866673ec1f94be4e
7
- data.tar.gz: 4714ff8b26be8dcf63fea4f8386cc7c1d0ed200f64058dd9707f945470bc34e024c1b2c2e22ed3c9b0252f4153bfe65324f2ad6b914ebac709da431e32f95768
6
+ metadata.gz: '019ee08cb1214e64468b04d134399815237c792470916af7590293c321b2400b76a9190c73a163625529d542c768ec6a9e953a0b8192b032d6fa12a86fbf5581'
7
+ data.tar.gz: eb8310f650a6963f3ffd54c4544e7d6e9ad850dde10f89f00f87761d13c0a18f0eaa606cdd9800b2abc01f4bd07d73818be643f111ade90ea05137a63bb7de81
@@ -27,7 +27,7 @@ class Midori::Connection
27
27
  receive_data(monitor)
28
28
  end
29
29
  if monitor.writable?
30
- if !@data.empty?
30
+ if !@data == ''
31
31
  # :nocov:
32
32
  # Leave for corner cases
33
33
  monitor.io.write_nonblock(@data)
@@ -31,6 +31,7 @@ module EventLoop
31
31
  # @yield what to run when io callbacks
32
32
  # @return [nil] nil
33
33
  def register(io, interest=(:rw), &callback)
34
+ config if @selector.nil?
34
35
  if @queue[io.to_i].nil?
35
36
  @queue[io.to_i] = Array.new
36
37
  register_raw(io, interest, callback)
@@ -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
- offset = @parser << data
39
- @body = data[offset..-1]
40
- @query_string = @path.match(/\?(.*?)$/)
41
- @query_string = @query_string[1] unless @query_string.nil?
42
- @path.gsub!(/\?(.*?)$/, '')
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
- @parsed = true
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
- receive_new_request(data)
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} - - Reached an EOF Error".yellow
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
- # @param [String] data raw data
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
@@ -1,5 +1,5 @@
1
1
  # Midori Module
2
2
  module Midori
3
3
  # Current Version Code
4
- VERSION = '0.2.2'.freeze
4
+ VERSION = '0.2.3'.freeze
5
5
  end
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.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-05-12 00:00:00.000000000 Z
11
+ date: 2017-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nio4r