wovnrb 0.2.09 → 0.2.10

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: 2f04bf4839f7b61aec6530ffff379a9f2385173d
4
- data.tar.gz: e80efaaa47e0f2ee84cb3cdd92561f3c3cd29047
3
+ metadata.gz: fc2f6b32bbb1a1ae62581625150e7192a97e7536
4
+ data.tar.gz: 700ff16d073253ea5e761a1fbba50fa31db4d293
5
5
  SHA512:
6
- metadata.gz: 7df1df8c8b96eee6398c002797ec48a7dc44166bb0f9c95b558ac033e38285eaa76ce0f3efccffdabbcdf42860b21e239c6acef8d2dea01d9f70ad062dc83e57
7
- data.tar.gz: 362d53618e983cab6a3702881d563c58345e4183ffe4e48757796041befd83e191e616cba24c36d8aaed59f6102f039881c84134c3a94c9743c8f62a3d7719a7
6
+ metadata.gz: c8081098e724536d3eaed17c9ba986f1d09a5fcfad40b59cc34393cac266f3dff2cddb42c9c383d334a8a8c60d884fa98304bdd76f18a81628e7e761269f27b2
7
+ data.tar.gz: 09747ebd43f2298e0e2daea26b77603bae9fd2bb43cf2334c20ef80d8fde8cb8d4f8ee8db801d74c8989a9828b1072461962ad8605af774c3e60c46670b45284
data/lib/wovnrb.rb CHANGED
@@ -44,21 +44,23 @@ module Wovnrb
44
44
  # pass to application
45
45
  status, res_headers, body = @app.call(headers.request_out)
46
46
 
47
- if res_headers["Content-Type"] =~ /html/ # && !body[0].nil?
48
- # ApiData creates request for external server, but cannot use async.
49
- # Because some server not allow multi thread. (env['async.callback'] is not supported at all Server).
50
- api_data = ApiData.new(headers.redis_url, @store)
51
- values = api_data.get_data
52
- url = {
47
+ if res_headers["Content-Type"] =~ /html/
48
+ unless @store.settings['ignore_globs'].any?{|g| g.match?(headers.pathname)}
49
+ # ApiData creates request for external server, but cannot use async.
50
+ # Because some server not allow multi thread. (env['async.callback'] is not supported at all Server).
51
+ api_data = ApiData.new(headers.redis_url, @store)
52
+ values = api_data.get_data
53
+ url = {
53
54
  :protocol => headers.protocol,
54
55
  :host => headers.host,
55
56
  :pathname => headers.pathname
56
- }
57
- body = switch_lang(body, values, url, lang, headers) unless status.to_s =~ /^1|302/
57
+ }
58
+ body = switch_lang(body, values, url, lang, headers) unless status.to_s =~ /^1|302/
58
59
 
59
- content_length = 0
60
- body.each { |b| content_length += b.respond_to?(:bytesize) ? b.bytesize : 0 }
61
- res_headers["Content-Length"] = content_length.to_s
60
+ content_length = 0
61
+ body.each { |b| content_length += b.respond_to?(:bytesize) ? b.bytesize : 0 }
62
+ res_headers["Content-Length"] = content_length.to_s
63
+ end
62
64
  end
63
65
 
64
66
  headers.out(res_headers)
@@ -0,0 +1,25 @@
1
+ module Wovnrb
2
+ # inspired by https://github.com/isaacs/node-glob
3
+ #
4
+ # "*" Matches 0 or more characters in a single path portion
5
+ # "**" If a "globstar" is alone in a path portion,
6
+ # then it matches zero or more directories and subdirectories searching for matches.
7
+ #
8
+ # @note "?" or other pattern is not implemented
9
+ class Glob
10
+ def initialize(pattern)
11
+ sub_directories = pattern.split('/**', -1)
12
+ regexp = sub_directories.map do |sub_dir|
13
+ sub_dir.split('*', -1)
14
+ .map {|p| Regexp.escape(p)}
15
+ .join('[^/]*')
16
+ end.join('(/[^/]*)*')
17
+
18
+ @regexp = Regexp.new("^#{regexp}$")
19
+ end
20
+
21
+ def match?(url)
22
+ !@regexp.match(url).nil?
23
+ end
24
+ end
25
+ end
data/lib/wovnrb/store.rb CHANGED
@@ -3,6 +3,7 @@ require 'uri'
3
3
  require 'cgi'
4
4
  require 'singleton'
5
5
  require 'wovnrb/services/wovn_logger'
6
+ require 'wovnrb/services/glob'
6
7
 
7
8
  module Wovnrb
8
9
  class Store
@@ -23,6 +24,8 @@ module Wovnrb
23
24
  'user_token' => '',
24
25
  'secret_key' => '',
25
26
  'log_path' => 'log/wovn_error.log',
27
+ 'ignore_paths' => [],
28
+ 'ignore_globs' => [],
26
29
  'url_pattern' => 'path',
27
30
  'url_pattern_reg' => "/(?<lang>[^/.?]+)",
28
31
  'query' => [],
@@ -48,19 +51,23 @@ module Wovnrb
48
51
  errors = [];
49
52
  if !settings.has_key?('user_token') || settings['user_token'].length < 5 || settings['user_token'].length > 6
50
53
  valid = false
51
- errors.push("User token #{settings['user_token']} is not valid.");
54
+ errors.push("User token #{settings['user_token']} is not valid.")
52
55
  end
53
56
  if !settings.has_key?('secret_key') || settings['secret_key'].length == 0 #|| settings['secret_key'].length < 5 || settings['secret_key'].length > 6
54
57
  valid = false
55
- errors.push("Secret key #{settings['secret_key']} is not valid.");
58
+ errors.push("Secret key #{settings['secret_key']} is not valid.")
59
+ end
60
+ if settings.has_key?('ignore_paths') && !settings['ignore_paths'].kind_of?(Array)
61
+ valid = false
62
+ errors.push("Ignore Paths #{settings['ignore_paths']} should be Array.")
56
63
  end
57
64
  if !settings.has_key?('url_pattern') || settings['url_pattern'].length == 0
58
65
  valid = false
59
- errors.push("Url pattern #{settings['url_pattern']} is not valid.");
66
+ errors.push("Url pattern #{settings['url_pattern']} is not valid.")
60
67
  end
61
68
  if !settings.has_key?('query') || !settings['query'].kind_of?(Array)
62
69
  valid = false
63
- errors.push("query config #{settings['query']} is not valid.");
70
+ errors.push("query config #{settings['query']} is not valid.")
64
71
  end
65
72
  if !settings.has_key?('api_url') || settings['api_url'].length == 0
66
73
  valid = false
@@ -68,11 +75,11 @@ module Wovnrb
68
75
  end
69
76
  if !settings.has_key?('default_lang') || settings['default_lang'].length == 0
70
77
  valid = false
71
- errors.push("Default lang #{settings['default_lang']} is not valid.");
78
+ errors.push("Default lang #{settings['default_lang']} is not valid.")
72
79
  end
73
80
  if !settings.has_key?('supported_langs') || !settings['supported_langs'].kind_of?(Array) || settings['supported_langs'].size < 1
74
81
  valid = false
75
- errors.push("Supported langs configuration is not valid.");
82
+ errors.push("Supported langs configuration is not valid.")
76
83
  end
77
84
  # log errors
78
85
  if errors.length > 0
@@ -92,41 +99,57 @@ module Wovnrb
92
99
  @config_loaded = false
93
100
  end
94
101
 
95
- if !@config_loaded
96
- # get Rails config.wovnrb
97
- if Object.const_defined?('Rails') && Rails.configuration.respond_to?(:wovnrb)
98
- config_settings = Rails.configuration.wovnrb.stringify_keys
99
- if config_settings.has_key?('url_pattern')
100
- if config_settings['url_pattern'] == 'query' || config_settings['url_pattern'] == 'subdomain' || config_settings['url_pattern'] == 'path'
101
- config_settings['url_pattern'] = config_settings['url_pattern']
102
- config_settings.delete('url_pattern')
103
- end
102
+ if @config_loaded
103
+ return @settings
104
+ end
105
+
106
+ # get Rails config.wovnrb
107
+ if Object.const_defined?('Rails') && Rails.configuration.respond_to?(:wovnrb)
108
+ config_settings = Rails.configuration.wovnrb.stringify_keys
109
+ if config_settings.has_key?('url_pattern')
110
+ if config_settings['url_pattern'] == 'query' || config_settings['url_pattern'] == 'subdomain' || config_settings['url_pattern'] == 'path'
111
+ config_settings['url_pattern'] = config_settings['url_pattern']
112
+ config_settings.delete('url_pattern')
104
113
  end
105
- @settings.merge!(Rails.configuration.wovnrb.stringify_keys)
106
114
  end
115
+ @settings.merge!(Rails.configuration.wovnrb.stringify_keys)
116
+ end
117
+ cleanSettings
107
118
 
108
- # fix settings object
109
- @settings['default_lang'] = Lang.get_code(@settings['default_lang'])
110
- if !@settings.has_key?('supported_langs')
111
- @settings['supported_langs'] = [@settings['default_lang']]
112
- end
113
- if @settings['url_pattern'] == 'path'
114
- @settings['url_pattern_reg'] = "/(?<lang>[^/.?]+)"
115
- elsif @settings['url_pattern'] == 'query'
116
- @settings['url_pattern_reg'] = "((\\?.*&)|\\?)wovn=(?<lang>[^&]+)(&|$)"
117
- elsif @settings['url_pattern'] == 'subdomain'
118
- @settings['url_pattern_reg'] = "^(?<lang>[^.]+)\."
119
- end
120
- if @settings['test_mode'] != true || @settings['test_mode'] != 'on'
121
- @settings['test_mode'] = false
122
- else
123
- @settings['test_mode'] = true
124
- end
119
+ # fix settings object
120
+ @settings['default_lang'] = Lang.get_code(@settings['default_lang'])
121
+ if !@settings.has_key?('supported_langs')
122
+ @settings['supported_langs'] = [@settings['default_lang']]
123
+ end
125
124
 
126
- @config_loaded = true
125
+ if @settings['url_pattern'] == 'path'
126
+ @settings['url_pattern_reg'] = "/(?<lang>[^/.?]+)"
127
+ elsif @settings['url_pattern'] == 'query'
128
+ @settings['url_pattern_reg'] = "((\\?.*&)|\\?)wovn=(?<lang>[^&]+)(&|$)"
129
+ elsif @settings['url_pattern'] == 'subdomain'
130
+ @settings['url_pattern_reg'] = "^(?<lang>[^.]+)\."
127
131
  end
132
+
133
+ if @settings['test_mode'] != true || @settings['test_mode'] != 'on'
134
+ @settings['test_mode'] = false
135
+ else
136
+ @settings['test_mode'] = true
137
+ end
138
+
139
+ if @settings['ignore_paths'].kind_of?(Array)
140
+ @settings['ignore_globs'] = @settings['ignore_paths'].map do |pattern|
141
+ Glob.new(pattern)
142
+ end
143
+ end
144
+
145
+ @config_loaded = true
128
146
  @settings
129
147
  end
148
+
149
+ private
150
+ def cleanSettings
151
+ @settings['ignore_globs'] = []
152
+ end
130
153
  end
131
154
 
132
155
  end
@@ -1,3 +1,3 @@
1
1
  module Wovnrb
2
- VERSION = "0.2.09"
2
+ VERSION = "0.2.10"
3
3
  end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+ require 'wovnrb/services/glob'
3
+
4
+ module Wovnrb
5
+ class GlobTest < WovnMiniTest
6
+ def test_match
7
+ assert_equal(false, Glob.new('api/*').match?('api'))
8
+ assert_equal(true, Glob.new('api/*').match?('api/'))
9
+ assert_equal(true, Glob.new('api/*').match?('api/a'))
10
+ assert_equal(false, Glob.new('api/*').match?('api/a/b'))
11
+ assert_equal(true, Glob.new('api/*.html').match?('api/a.html'))
12
+ assert_equal(false, Glob.new('api/*.html').match?('api/a/b.html'))
13
+ end
14
+
15
+ def test_match_2_star
16
+ assert_equal(false, Glob.new('api/**/*').match?('api'))
17
+ assert_equal(true, Glob.new('api/**/*').match?('api/'))
18
+ assert_equal(true, Glob.new('api/**/*').match?('api/a'))
19
+ assert_equal(true, Glob.new('api/**/*').match?('api/a/b'))
20
+ assert_equal(true, Glob.new('api/**/*.html').match?('api/a.html'))
21
+ assert_equal(true, Glob.new('api/**/*.html').match?('api/a/b.html'))
22
+ assert_equal(false, Glob.new('api/**/*.html').match?('api/a/b'))
23
+ assert_equal(true, Glob.new('api/**').match?('api/a'))
24
+ assert_equal(true, Glob.new('api/**').match?('api/a/b.html'))
25
+ end
26
+
27
+ def test_match_regex_injection
28
+ assert_equal(false, Glob.new('api/a?*').match?('api/a'))
29
+ assert_equal(true, Glob.new('api/a?*').match?('api/a?'))
30
+ assert_equal(true, Glob.new('api/a?*').match?('api/a?b'))
31
+ assert_equal(false, Glob.new('api/**?').match?('api/a'))
32
+ assert_equal(true, Glob.new('api/**?').match?('api/?'))
33
+ end
34
+ end
35
+ end
@@ -43,5 +43,45 @@ module Wovnrb
43
43
  assert_equal(false, valid)
44
44
  assert_equal(['User token is not valid.', 'Secret key is not valid.'], mock.errors)
45
45
  end
46
+
47
+ def test_settings_ignore_paths
48
+ s = Wovnrb::Store.instance
49
+ s.settings({'ignore_paths' => ['/api/**']})
50
+ assert_equal(1, s.settings['ignore_globs'].size)
51
+ assert_equal(true, s.settings['ignore_globs'].first.match?('/api/a/b'))
52
+ assert_equal(false, s.settings['ignore_globs'].first.match?('/a/b'))
53
+ end
54
+
55
+ def test_settings_ignore_paths_multiple
56
+ s = Wovnrb::Store.instance
57
+ s.settings({'ignore_paths' => ['/api/a/**', '/api/b/**']})
58
+ assert_equal(2, s.settings['ignore_globs'].size)
59
+ assert_equal(true, s.settings['ignore_globs'].any?{|g| g.match?('/api/a')})
60
+ assert_equal(true, s.settings['ignore_globs'].any?{|g| g.match?('/api/b')})
61
+ assert_equal(false, s.settings['ignore_globs'].any?{|g| g.match?('/api/c')})
62
+ end
63
+
64
+ def test_settings_ignore_paths_empty
65
+ s = Wovnrb::Store.instance
66
+ s.settings({'ignore_paths' => []})
67
+ assert_equal([], s.settings['ignore_globs'])
68
+ end
69
+
70
+ def test_settings_invalid_ignore_paths
71
+ mock = LogMock.mock_log
72
+ store = Wovnrb::Store.instance
73
+ store.settings({'ignore_paths' => 'aaaa'})
74
+
75
+ assert_equal(false, store.valid_settings?)
76
+ assert_equal(['User token is not valid.', 'Secret key is not valid.', 'Ignore Paths aaaa should be Array.'], mock.errors)
77
+ end
78
+
79
+ def test_settings_ignore_glob_injection
80
+ s = Wovnrb::Store.instance
81
+ s.settings({'ignore_paths' => nil})
82
+ s.settings({'ignore_globs' => [1, 2]})
83
+
84
+ assert_equal([], s.settings['ignore_globs'])
85
+ end
46
86
  end
47
87
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wovnrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.09
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Sandford
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-07-19 00:00:00.000000000 Z
12
+ date: 2016-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogumbo
@@ -317,6 +317,7 @@ files:
317
317
  - lib/wovnrb/html_replacers/text_replacer.rb
318
318
  - lib/wovnrb/lang.rb
319
319
  - lib/wovnrb/railtie.rb
320
+ - lib/wovnrb/services/glob.rb
320
321
  - lib/wovnrb/services/url.rb
321
322
  - lib/wovnrb/services/wovn_logger.rb
322
323
  - lib/wovnrb/store.rb
@@ -334,6 +335,7 @@ files:
334
335
  - test/lib/html_replacers/script_replacer_test.rb
335
336
  - test/lib/html_replacers/text_replacer_test.rb
336
337
  - test/lib/lang_test.rb
338
+ - test/lib/services/glob_test.rb
337
339
  - test/lib/services/url_test.rb
338
340
  - test/lib/services/wovn_logger_test.rb
339
341
  - test/lib/store_test.rb
@@ -380,6 +382,7 @@ test_files:
380
382
  - test/lib/html_replacers/script_replacer_test.rb
381
383
  - test/lib/html_replacers/text_replacer_test.rb
382
384
  - test/lib/lang_test.rb
385
+ - test/lib/services/glob_test.rb
383
386
  - test/lib/services/url_test.rb
384
387
  - test/lib/services/wovn_logger_test.rb
385
388
  - test/lib/store_test.rb