midori.rb 0.4.4.1 → 0.5.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
  SHA1:
3
- metadata.gz: a8968a505d549238500796498dcafc2071f59297
4
- data.tar.gz: 9642b9824c45d88b1f18fa16d34d4e17f90f89c3
3
+ metadata.gz: 4b10de10fd9f845940ff8facc3b9442d68375a14
4
+ data.tar.gz: 90389f69f41d8cf55650a108b580a98b7a808ea5
5
5
  SHA512:
6
- metadata.gz: 49bb363d6c4a333d96a9154383d6024cb3c14a602902376a2575bb25fb3dc069c54586ac98c615034ee030d43dc8f3db611b47027cf2f7b9a2fcf20e67c5f445
7
- data.tar.gz: 70804512d3ad7d38c367b9992e958fb0f5c25a2ae0c7766a5a4f3d974f2588c1b36b5892ede6357e026b6dc7edeff724dfb1cec84fc63440b95a804c9d59a666
6
+ metadata.gz: 73cbcdb722891971a8e8ac238d8e0084ffc3000fa57efdf5e106df6c4d729b3fa99eadf7b0f5fa9ad03a746ed6eb5e95f1ba0cd11b61cd098ccacbb0ec1c3505
7
+ data.tar.gz: de462c5564b2bba6ef7c36f737d8291fef6e757d294dd100ee653ef5c3b55223d7c35003e1ddef4ce883fd242a948dcfd2bb208c5f78e7e663ba3879c1ce71de
@@ -9,4 +9,8 @@ class Midori::Configure
9
9
  set :port, 8080
10
10
  set :route_type, :sinatra
11
11
  set :before, proc {}
12
+ set :proxy, false
13
+ set :trust_real_ip, false
14
+ set :trusted_proxies, /\A127\.0\.0\.1\Z|\A(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|
15
+ \A::1\Z|\Afd[0-9a-f]{2}:.+|\Alocalhost\Z|\Aunix\Z|\Aunix:/ix
12
16
  end
@@ -0,0 +1,21 @@
1
+ ##
2
+ # Case Insensitive Hash
3
+ # Designed for HTTP Headers due to RFC 2.6 Chapter 4.2
4
+ # https://www.ietf.org/rfc/rfc2616.txt
5
+ class HTTPHeader < Hash
6
+ def [](key)
7
+ super _insensitive(key)
8
+ end
9
+
10
+ def []=(key, value)
11
+ super _insensitive(key), value
12
+ end
13
+
14
+ def key?(key)
15
+ super _insensitive(key)
16
+ end
17
+
18
+ protected def _insensitive(key)
19
+ key.downcase
20
+ end
21
+ end
@@ -2,25 +2,26 @@
2
2
  # Request class for midori
3
3
  # @attr [String] ip client ip address
4
4
  # @attr [Integer] port client port
5
+ # @attr [String] ip parsed ip address
5
6
  # @attr [String] protocol protocol version of HTTP request
6
7
  # @attr [Symbol] method HTTP method
7
8
  # @attr [String] path request path
8
9
  # @attr [Hash] query_params parameter parsed from query string
9
10
  # @attr [String | nil] query_string request query string
10
- # @attr [Hash] header request header
11
+ # @attr [HTTPHeader] header request header
11
12
  # @attr [String] body request body
12
13
  # @attr [Hash] cookie cookie hash coming from request
13
14
  # @attr [Boolean] parsed whether the request header parsed
14
15
  # @attr [Boolean] body_parsed whether the request body parsed
15
16
  # @attr [Hash] params params in the url
16
17
  class Midori::Request
17
- attr_accessor :ip, :port,
18
+ attr_accessor :ip, :port, :remote_ip,
18
19
  :protocol, :method, :path, :query_params, :query_string,
19
20
  :header, :body, :parsed, :body_parsed, :params, :cookie
20
21
 
21
22
  # Init Request
22
23
  def initialize
23
- @header = {}
24
+ @header = HTTPHeader.new
24
25
  @parsed = false
25
26
  @body_parsed = false
26
27
  @is_websocket = false
@@ -34,7 +35,10 @@ class Midori::Request
34
35
  @protocol = @parser.http_version
35
36
  @method = @parser.http_method
36
37
  @path = @parser.request_url
37
- @header = @parser.headers
38
+ # Turn header into case-insensitive due to RFC 2.6 Chapter 4.2
39
+ # https://www.ietf.org/rfc/rfc2616.txt
40
+ @parser.headers.each { |key, value| @header[key] = value }
41
+ @remote_ip = parse_ip || @ip # Detect client real IP with RFC 7239
38
42
 
39
43
  @query_string = @path.match(/\?(.*?)$/)
40
44
  unless @query_string.nil?
@@ -70,6 +74,23 @@ class Midori::Request
70
74
  nil
71
75
  end
72
76
 
77
+ # Get the real user IP from headers
78
+ # @return [String | nil] nil when not available, otherwise, return the real IP
79
+ # Modified from Rack
80
+ def parse_ip
81
+ # Do not parse anything if not behind proxy
82
+ return nil unless Midori::Configure.proxy
83
+ return @header['X-Real-IP'] if Midori::Configure.trust_real_ip
84
+ # Not enough infomation
85
+ return nil if @header['X-Forwarded-For'].nil?
86
+ forwarded_ips = @header['X-Forwarded-For'].split(', ')
87
+ # Spoofing check
88
+ trusted = forwarded_ips.reject do |ip|
89
+ ip =~ Midori::Configure.trusted_proxies
90
+ end
91
+ trusted.last
92
+ end
93
+
73
94
  # Preproceed the request after parsed
74
95
  # @return [nil] nil
75
96
  def pre_proceed
data/lib/midori/runner.rb CHANGED
@@ -8,8 +8,8 @@ class Midori::Runner
8
8
 
9
9
  # Define status of a runner
10
10
  # @param [Class] api inherited from [Midori::API]
11
- # @param [Class] configure inherited from [Midori::Configure]
12
- def initialize(api, configure = Midori::Configure)
11
+ def initialize(api)
12
+ configure = Midori::Configure
13
13
  @logger = configure.logger
14
14
  Midori.logger = configure.logger
15
15
  @bind = configure.bind
@@ -1,5 +1,5 @@
1
1
  # Midori Module
2
2
  module Midori
3
3
  # Current Version Code
4
- VERSION = '0.4.4.1'.freeze
4
+ VERSION = '0.5.0'.freeze
5
5
  end
data/lib/midori.rb CHANGED
@@ -10,6 +10,7 @@ require 'socket'
10
10
 
11
11
  require_relative 'midori_ext'
12
12
  require_relative 'midori/core_ext/configurable'
13
+ require_relative 'midori/core_ext/http_header'
13
14
  require_relative 'midori/core_ext/string'
14
15
  require_relative 'midori/core_ext/define_class'
15
16
  require_relative 'midori/core_ext/proc'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midori.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4.1
4
+ version: 0.5.0
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-10-31 00:00:00.000000000 Z
11
+ date: 2017-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: murasaki
@@ -88,6 +88,7 @@ files:
88
88
  - lib/midori/const.rb
89
89
  - lib/midori/core_ext/configurable.rb
90
90
  - lib/midori/core_ext/define_class.rb
91
+ - lib/midori/core_ext/http_header.rb
91
92
  - lib/midori/core_ext/proc.rb
92
93
  - lib/midori/core_ext/string.rb
93
94
  - lib/midori/env.rb