wovnrb 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +1 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +1 -0
- data/.rubocop_todo.yml +308 -0
- data/Rakefile +13 -14
- data/lib/wovnrb.rb +43 -98
- data/lib/wovnrb/api_translator.rb +143 -0
- data/lib/wovnrb/headers.rb +78 -92
- data/lib/wovnrb/helpers/nokogumbo_helper.rb +1 -1
- data/lib/wovnrb/lang.rb +93 -125
- data/lib/wovnrb/railtie.rb +5 -7
- data/lib/wovnrb/services/glob.rb +3 -3
- data/lib/wovnrb/services/html_converter.rb +192 -0
- data/lib/wovnrb/services/html_replace_marker.rb +38 -0
- data/lib/wovnrb/services/wovn_logger.rb +8 -4
- data/lib/wovnrb/settings.rb +5 -3
- data/lib/wovnrb/store.rb +35 -26
- data/lib/wovnrb/text_caches/cache_base.rb +3 -2
- data/lib/wovnrb/text_caches/memory_cache.rb +2 -2
- data/lib/wovnrb/version.rb +1 -1
- data/test/fixtures/html/test.html +8 -0
- data/test/fixtures/html/test_translated.html +8 -0
- data/test/lib/api_translator_test.rb +109 -0
- data/test/lib/headers_test.rb +84 -55
- data/test/lib/lang_test.rb +157 -357
- data/test/lib/services/glob_test.rb +1 -1
- data/test/lib/services/html_converter_test.rb +166 -0
- data/test/lib/services/html_replace_marker_test.rb +75 -0
- data/test/lib/services/wovn_logger_test.rb +6 -6
- data/test/lib/store_test.rb +25 -69
- data/test/lib/text_caches/cache_base_test.rb +1 -1
- data/test/lib/text_caches/memory_cache_test.rb +10 -11
- data/test/lib/wovnrb_test.rb +77 -310
- data/test/test_helper.rb +22 -32
- data/wovnrb.gemspec +35 -44
- metadata +86 -205
- data/ext/dom/Makefile +0 -239
- data/lib/wovnrb/api_data.rb +0 -59
- data/lib/wovnrb/html_replacers/image_replacer.rb +0 -69
- data/lib/wovnrb/html_replacers/input_replacer.rb +0 -38
- data/lib/wovnrb/html_replacers/link_replacer.rb +0 -78
- data/lib/wovnrb/html_replacers/meta_replacer.rb +0 -28
- data/lib/wovnrb/html_replacers/replacer_base.rb +0 -49
- data/lib/wovnrb/html_replacers/script_replacer.rb +0 -39
- data/lib/wovnrb/html_replacers/text_replacer.rb +0 -21
- data/lib/wovnrb/html_replacers/unified_values/dst_swapping_targets_creator.rb +0 -76
- data/lib/wovnrb/html_replacers/unified_values/element_category.rb +0 -242
- data/lib/wovnrb/html_replacers/unified_values/node_swapping_targets_creator.rb +0 -134
- data/lib/wovnrb/html_replacers/unified_values/text_replacer.rb +0 -35
- data/lib/wovnrb/html_replacers/unified_values/text_scraper.rb +0 -152
- data/lib/wovnrb/html_replacers/unified_values/values_stack.rb +0 -65
- data/lib/wovnrb/services/url.rb +0 -12
- data/lib/wovnrb/services/value_agent.rb +0 -9
- data/test/fixtures/unified_values/site_html/simple_actual.html +0 -96
- data/test/fixtures/unified_values/site_html/simple_expected.json +0 -251
- data/test/fixtures/unified_values/site_html/wovn.io_actual.html +0 -686
- data/test/fixtures/unified_values/site_html/wovn.io_expected.json +0 -543
- data/test/fixtures/unified_values/site_html/www.yahoo.co.jp_actual.html +0 -1024
- data/test/fixtures/unified_values/site_html/www.yahoo.co.jp_expected.json +0 -3345
- data/test/fixtures/unified_values/small_html/block_inside_inline_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/block_inside_inline_expected.json +0 -22
- data/test/fixtures/unified_values/small_html/br_tag_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/br_tag_expected.json +0 -12
- data/test/fixtures/unified_values/small_html/comment_tag_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/comment_tag_expected.json +0 -10
- data/test/fixtures/unified_values/small_html/complex_text_with_html_entity_actual.html +0 -7
- data/test/fixtures/unified_values/small_html/complex_text_with_html_entity_expected.json +0 -11
- data/test/fixtures/unified_values/small_html/deep_nested_block_actual.html +0 -14
- data/test/fixtures/unified_values/small_html/deep_nested_block_expected.json +0 -8
- data/test/fixtures/unified_values/small_html/deep_nested_inline_actual.html +0 -20
- data/test/fixtures/unified_values/small_html/deep_nested_inline_expected.json +0 -20
- data/test/fixtures/unified_values/small_html/empty_tag_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/empty_tag_expected.json +0 -12
- data/test/fixtures/unified_values/small_html/empty_text_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/empty_text_expected.json +0 -1
- data/test/fixtures/unified_values/small_html/ignore_tag_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/ignore_tag_expected.json +0 -16
- data/test/fixtures/unified_values/small_html/ignored_class_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/ignored_class_expected.json +0 -13
- data/test/fixtures/unified_values/small_html/img_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/img_expected.json +0 -23
- data/test/fixtures/unified_values/small_html/nested_and_complex_wovn_ignore_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/nested_and_complex_wovn_ignore_expected.json +0 -16
- data/test/fixtures/unified_values/small_html/nested_text_value_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/nested_text_value_expected.json +0 -12
- data/test/fixtures/unified_values/small_html/nested_text_value_mixed_plan_text_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/nested_text_value_mixed_plan_text_expected.json +0 -14
- data/test/fixtures/unified_values/small_html/option_tag_actual.html +0 -9
- data/test/fixtures/unified_values/small_html/option_tag_expected.json +0 -13
- data/test/fixtures/unified_values/small_html/text_different_inline_each_other_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/text_different_inline_each_other_expected.json +0 -22
- data/test/fixtures/unified_values/small_html/text_in_svg_actual.html +0 -9
- data/test/fixtures/unified_values/small_html/text_in_svg_expected.json +0 -8
- data/test/fixtures/unified_values/small_html/text_with_html_entity_actual.html +0 -6
- data/test/fixtures/unified_values/small_html/text_with_html_entity_expected.json +0 -8
- data/test/fixtures/unified_values/small_html/unknown_or_custom_tag_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/unknown_or_custom_tag_expected.json +0 -24
- data/test/fixtures/unified_values/small_html/unnecessay_top_end_tag_actual.html +0 -12
- data/test/fixtures/unified_values/small_html/unnecessay_top_end_tag_expected.json +0 -14
- data/test/fixtures/unified_values/small_html/wovn_ignore_actual.html +0 -10
- data/test/fixtures/unified_values/small_html/wovn_ignore_expected.json +0 -13
- data/test/lib/api_data_test.rb +0 -83
- data/test/lib/html_replacers/image_replacer_test.rb +0 -165
- data/test/lib/html_replacers/input_replacer_test.rb +0 -140
- data/test/lib/html_replacers/link_replacer_test.rb +0 -328
- data/test/lib/html_replacers/meta_replacer_test.rb +0 -157
- data/test/lib/html_replacers/replacer_base_test.rb +0 -128
- data/test/lib/html_replacers/script_replacer_test.rb +0 -139
- data/test/lib/html_replacers/text_replacer_test.rb +0 -99
- data/test/lib/html_replacers/unified_values/dst_swapping_targets_creator_test.rb +0 -137
- data/test/lib/html_replacers/unified_values/element_category_test.rb +0 -49
- data/test/lib/html_replacers/unified_values/node_swapping_targets_creator_test.rb +0 -137
- data/test/lib/html_replacers/unified_values/text_replacer_test.rb +0 -270
- data/test/lib/html_replacers/unified_values/text_scraper_test.rb +0 -121
- data/test/lib/html_replacers/unified_values/values_stack_test.rb +0 -122
- data/test/lib/services/url_test.rb +0 -9
- data/test/lib/services/value_agent_test.rb +0 -32
- data/test/services/url_test.rb +0 -163
- data/values/values +0 -1
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'addressable'
|
2
|
+
require 'digest'
|
3
|
+
require 'json'
|
4
|
+
require 'zlib'
|
5
|
+
|
6
|
+
module Wovnrb
|
7
|
+
class ApiTranslator
|
8
|
+
def initialize(store, headers)
|
9
|
+
@store = store
|
10
|
+
@headers = headers
|
11
|
+
end
|
12
|
+
|
13
|
+
def translate(body)
|
14
|
+
connection = prepare_connection
|
15
|
+
request = prepare_request(body)
|
16
|
+
|
17
|
+
begin
|
18
|
+
response = connection.request(request)
|
19
|
+
rescue => e
|
20
|
+
WovnLogger.error("\"#{e.message}\" error occurred when contacting WOVNio translation API")
|
21
|
+
return body
|
22
|
+
end
|
23
|
+
|
24
|
+
case response
|
25
|
+
when Net::HTTPSuccess
|
26
|
+
if response.header['Content-Encoding'] == 'gzip'
|
27
|
+
response_body = Zlib::GzipReader.new(StringIO.new(response.body)).read
|
28
|
+
|
29
|
+
JSON.parse(response_body)['body'] || body
|
30
|
+
else
|
31
|
+
WovnLogger.error("Received invalid content (\"#{response.header['Content-Encoding']}\") from WOVNio translation API.")
|
32
|
+
body
|
33
|
+
end
|
34
|
+
else
|
35
|
+
WovnLogger.error("Received \"#{response.message}\" from WOVNio translation API.")
|
36
|
+
body
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def prepare_connection
|
43
|
+
connection = Net::HTTP.new(api_uri.host, api_uri.port)
|
44
|
+
|
45
|
+
connection.open_timeout = api_timeout
|
46
|
+
connection.read_timeout = api_timeout
|
47
|
+
|
48
|
+
connection
|
49
|
+
end
|
50
|
+
|
51
|
+
def prepare_request(body)
|
52
|
+
data = compress_request_data(generate_request_data(body))
|
53
|
+
headers = {
|
54
|
+
'Accept-Encoding' => 'gzip',
|
55
|
+
'Content-Type' => 'application/octet-stream',
|
56
|
+
'Content-Length' => data.bytesize.to_s
|
57
|
+
}
|
58
|
+
request = Net::HTTP::Post.new(generate_request_path(body), headers)
|
59
|
+
|
60
|
+
request.body = data
|
61
|
+
|
62
|
+
request
|
63
|
+
end
|
64
|
+
|
65
|
+
def generate_request_path(body)
|
66
|
+
"#{api_uri.path.sub(/\/$/, '')}/translation?cache_key=#{generate_cache_key(body)}"
|
67
|
+
end
|
68
|
+
|
69
|
+
def generate_cache_key(body)
|
70
|
+
cache_key_components = {
|
71
|
+
'token' => token,
|
72
|
+
'settings_hash' => settings_hash,
|
73
|
+
'body_hash' => Digest::MD5.hexdigest(body),
|
74
|
+
'path' => page_pathname,
|
75
|
+
'lang' => lang_code
|
76
|
+
}.map { |k, v| "#{k}=#{v}" }.join('&')
|
77
|
+
|
78
|
+
CGI.escape("(#{cache_key_components})")
|
79
|
+
end
|
80
|
+
|
81
|
+
def generate_request_data(body)
|
82
|
+
data = {
|
83
|
+
'url' => page_url,
|
84
|
+
'token' => token,
|
85
|
+
'lang_code' => lang_code,
|
86
|
+
'url_pattern' => url_pattern,
|
87
|
+
'product' => 'WOVN.rb',
|
88
|
+
'version' => VERSION,
|
89
|
+
'body' => body
|
90
|
+
}
|
91
|
+
|
92
|
+
data['custom_lang_aliases'] = JSON.dump(custom_lang_aliases) unless custom_lang_aliases.empty?
|
93
|
+
|
94
|
+
data
|
95
|
+
end
|
96
|
+
|
97
|
+
def compress_request_data(data_hash)
|
98
|
+
encoded_data_components = data_hash.map do |key, value|
|
99
|
+
"#{key}=#{CGI.escape(value)}"
|
100
|
+
end
|
101
|
+
|
102
|
+
gzip = Zlib::GzipWriter.new(StringIO.new)
|
103
|
+
gzip << encoded_data_components.join('&')
|
104
|
+
gzip.close.string
|
105
|
+
end
|
106
|
+
|
107
|
+
def api_uri
|
108
|
+
Addressable::URI.parse(@store.settings['api_url'])
|
109
|
+
end
|
110
|
+
|
111
|
+
def api_timeout
|
112
|
+
@store.settings['api_timeout_seconds']
|
113
|
+
end
|
114
|
+
|
115
|
+
def settings_hash
|
116
|
+
Digest::MD5.hexdigest(JSON.dump(@store.settings))
|
117
|
+
end
|
118
|
+
|
119
|
+
def token
|
120
|
+
@store.settings['project_token']
|
121
|
+
end
|
122
|
+
|
123
|
+
def lang_code
|
124
|
+
@headers.lang_code
|
125
|
+
end
|
126
|
+
|
127
|
+
def url_pattern
|
128
|
+
@store.settings['url_pattern']
|
129
|
+
end
|
130
|
+
|
131
|
+
def custom_lang_aliases
|
132
|
+
@store.settings['custom_lang_aliases']
|
133
|
+
end
|
134
|
+
|
135
|
+
def page_url
|
136
|
+
"#{@headers.protocol}://#{@headers.url}"
|
137
|
+
end
|
138
|
+
|
139
|
+
def page_pathname
|
140
|
+
@headers.pathname_with_trailing_slash_if_present
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
data/lib/wovnrb/headers.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
module Wovnrb
|
2
|
-
|
3
2
|
class Headers
|
4
3
|
attr_reader :unmasked_url
|
5
4
|
attr_reader :url
|
@@ -8,7 +7,7 @@ module Wovnrb
|
|
8
7
|
attr_reader :host
|
9
8
|
attr_reader :unmasked_pathname
|
10
9
|
attr_reader :pathname
|
11
|
-
attr_reader :
|
10
|
+
attr_reader :pathname_with_trailing_slash_if_present
|
12
11
|
attr_reader :redis_url
|
13
12
|
|
14
13
|
# Generates new instance of Wovnrb::Headers.
|
@@ -20,52 +19,49 @@ module Wovnrb
|
|
20
19
|
@env = env
|
21
20
|
@settings = settings
|
22
21
|
@protocol = request.scheme
|
23
|
-
if settings['use_proxy'] && @env.
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
unless @env.
|
22
|
+
@unmasked_host = if settings['use_proxy'] && @env.key?('HTTP_X_FORWARDED_HOST')
|
23
|
+
@env['HTTP_X_FORWARDED_HOST']
|
24
|
+
else
|
25
|
+
@env['HTTP_HOST']
|
26
|
+
end
|
27
|
+
unless @env.key?('REQUEST_URI')
|
29
28
|
# Add '/' to PATH_INFO as a possible fix for pow server
|
30
|
-
@env['REQUEST_URI'] = (@env['PATH_INFO'] =~ /^[^\/]/ ? '/' : '') + @env['PATH_INFO'] + (@env['QUERY_STRING'].
|
29
|
+
@env['REQUEST_URI'] = (@env['PATH_INFO'] =~ /^[^\/]/ ? '/' : '') + @env['PATH_INFO'] + (@env['QUERY_STRING'].empty? ? '' : "?#{@env['QUERY_STRING']}")
|
31
30
|
end
|
32
31
|
# REQUEST_URI is expected to not contain the server name
|
33
32
|
# heroku contains http://...
|
34
|
-
if @env['REQUEST_URI'] =~ /:\/\//
|
35
|
-
@env['REQUEST_URI'] = @env['REQUEST_URI'].sub(/^.*:\/\/[^\/]+/, '')
|
36
|
-
end
|
33
|
+
@env['REQUEST_URI'] = @env['REQUEST_URI'].sub(/^.*:\/\/[^\/]+/, '') if @env['REQUEST_URI'] =~ /:\/\//
|
37
34
|
@unmasked_pathname = @env['REQUEST_URI'].split('?')[0]
|
38
35
|
@unmasked_pathname += '/' unless @unmasked_pathname =~ /\/$/ || @unmasked_pathname =~ /\/[^\/.]+\.[^\/.]+$/
|
39
36
|
@unmasked_url = "#{@protocol}://#{@unmasked_host}#{@unmasked_pathname}"
|
40
|
-
if settings['use_proxy'] && @env.
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
@env['wovnrb.target_lang'] =
|
46
|
-
@host = settings['url_pattern'] == 'subdomain' ? remove_lang(@host,
|
37
|
+
@host = if settings['use_proxy'] && @env.key?('HTTP_X_FORWARDED_HOST')
|
38
|
+
@env['HTTP_X_FORWARDED_HOST']
|
39
|
+
else
|
40
|
+
@env['HTTP_HOST']
|
41
|
+
end
|
42
|
+
@env['wovnrb.target_lang'] = lang_code
|
43
|
+
@host = settings['url_pattern'] == 'subdomain' ? remove_lang(@host, lang_code) : @host
|
47
44
|
@pathname, @query = @env['REQUEST_URI'].split('?')
|
48
|
-
@pathname = settings['url_pattern'] == 'path' ? remove_lang(@pathname,
|
49
|
-
@query
|
50
|
-
@url = "#{@host}#{@pathname}#{(
|
51
|
-
if settings['query'].
|
45
|
+
@pathname = settings['url_pattern'] == 'path' ? remove_lang(@pathname, lang_code) : @pathname
|
46
|
+
@query ||= ''
|
47
|
+
@url = "#{@host}#{@pathname}#{(!@query.empty? ? '?' : '') + remove_lang(@query, lang_code)}"
|
48
|
+
if !settings['query'].empty?
|
52
49
|
query_vals = []
|
53
50
|
settings['query'].each do |qv|
|
54
51
|
rx = Regexp.new("(^|&)(?<query_val>#{qv}[^&]+)(&|$)")
|
55
52
|
m = @query.match(rx)
|
56
|
-
if m && m[:query_val]
|
57
|
-
query_vals.push(m[:query_val])
|
58
|
-
end
|
59
|
-
end
|
60
|
-
if query_vals.length > 0
|
61
|
-
@query = "?#{query_vals.sort.join('&')}"
|
62
|
-
else
|
63
|
-
@query = ''
|
53
|
+
query_vals.push(m[:query_val]) if m && m[:query_val]
|
64
54
|
end
|
55
|
+
@query = if !query_vals.empty?
|
56
|
+
"?#{query_vals.sort.join('&')}"
|
57
|
+
else
|
58
|
+
''
|
59
|
+
end
|
65
60
|
else
|
66
61
|
@query = ''
|
67
62
|
end
|
68
|
-
@query = remove_lang(@query,
|
63
|
+
@query = remove_lang(@query, lang_code)
|
64
|
+
@pathname_with_trailing_slash_if_present = @pathname
|
69
65
|
@pathname = @pathname.gsub(/\/$/, '')
|
70
66
|
@redis_url = "#{@host}#{@pathname}#{@query}"
|
71
67
|
end
|
@@ -74,7 +70,7 @@ module Wovnrb
|
|
74
70
|
#
|
75
71
|
# @return [String] The lang code of the current page
|
76
72
|
def lang_code
|
77
|
-
|
73
|
+
path_lang && !path_lang.empty? ? path_lang : @settings['default_lang']
|
78
74
|
end
|
79
75
|
|
80
76
|
# picks up language code from requested URL by using url_pattern_reg setting.
|
@@ -85,18 +81,18 @@ module Wovnrb
|
|
85
81
|
def path_lang
|
86
82
|
if @path_lang.nil?
|
87
83
|
rp = Regexp.new(@settings['url_pattern_reg'])
|
88
|
-
if @settings['use_proxy'] && @env.
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
if match && match[:lang] && Lang.get_lang(match[:lang])
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
84
|
+
match = if @settings['use_proxy'] && @env.key?('HTTP_X_FORWARDED_HOST')
|
85
|
+
"#{@env['HTTP_X_FORWARDED_HOST']}#{@env['REQUEST_URI']}".match(rp)
|
86
|
+
else
|
87
|
+
"#{@env['SERVER_NAME']}#{@env['REQUEST_URI']}".match(rp)
|
88
|
+
end
|
89
|
+
@path_lang = if match && match[:lang] && Lang.get_lang(match[:lang])
|
90
|
+
Lang.get_code(match[:lang])
|
91
|
+
else
|
92
|
+
''
|
93
|
+
end
|
98
94
|
end
|
99
|
-
|
95
|
+
@path_lang
|
100
96
|
end
|
101
97
|
|
102
98
|
def browser_lang
|
@@ -105,7 +101,7 @@ module Wovnrb
|
|
105
101
|
if match && match[:lang] && Lang.get_lang(match[:lang])
|
106
102
|
@browser_lang = match[:lang]
|
107
103
|
else
|
108
|
-
# IS THIS RIGHT?
|
104
|
+
# IS THIS RIGHT?
|
109
105
|
@browser_lang = ''
|
110
106
|
accept_langs = (@env['HTTP_ACCEPT_LANGUAGE'] || '').split(/[,;]/)
|
111
107
|
accept_langs.each do |l|
|
@@ -116,87 +112,78 @@ module Wovnrb
|
|
116
112
|
end
|
117
113
|
end
|
118
114
|
end
|
119
|
-
|
115
|
+
@browser_lang
|
120
116
|
end
|
121
117
|
|
122
|
-
def redirect(lang=
|
118
|
+
def redirect(lang = browser_lang)
|
123
119
|
redirect_headers = {}
|
124
|
-
redirect_headers['location'] =
|
120
|
+
redirect_headers['location'] = redirect_location(lang)
|
125
121
|
redirect_headers['content-length'] = '0'
|
126
|
-
|
122
|
+
redirect_headers
|
127
123
|
end
|
128
124
|
|
129
125
|
def redirect_location(lang)
|
130
126
|
if lang == @settings['default_lang']
|
131
|
-
# IS THIS RIGHT??
|
132
|
-
|
133
|
-
#return remove_lang("#{@env['HTTP_HOST']}#{@env['REQUEST_URI']}", lang)
|
127
|
+
# IS THIS RIGHT??
|
128
|
+
"#{protocol}://#{url}"
|
129
|
+
# return remove_lang("#{@env['HTTP_HOST']}#{@env['REQUEST_URI']}", lang)
|
134
130
|
else
|
135
131
|
# TODO test
|
136
132
|
lang_code = Store.instance.settings['custom_lang_aliases'][lang] || lang
|
137
|
-
location =
|
133
|
+
location = url
|
138
134
|
case @settings['url_pattern']
|
139
135
|
when 'query'
|
140
136
|
if location !~ /\?/
|
141
137
|
location = "#{location}?wovn=#{lang_code}"
|
142
138
|
else @env['REQUEST_URI'] !~ /(\?|&)wovn=/
|
143
|
-
|
139
|
+
location = "#{location}&wovn=#{lang_code}"
|
144
140
|
end
|
145
141
|
when 'subdomain'
|
146
142
|
location = "#{lang_code.downcase}.#{location}"
|
147
|
-
|
143
|
+
# when 'path'
|
148
144
|
else
|
149
|
-
location = location.sub(/(\/|$)/, "/#{lang_code}/")
|
145
|
+
location = location.sub(/(\/|$)/, "/#{lang_code}/")
|
150
146
|
end
|
151
|
-
|
147
|
+
"#{protocol}://#{location}"
|
152
148
|
end
|
153
149
|
end
|
154
150
|
|
155
|
-
def request_out(
|
151
|
+
def request_out(_def_lang = @settings['default_lang'])
|
156
152
|
case @settings['url_pattern']
|
157
153
|
when 'query'
|
158
|
-
@env['REQUEST_URI'] = remove_lang(@env['REQUEST_URI']) if @env.
|
159
|
-
@env['QUERY_STRING'] = remove_lang(@env['QUERY_STRING']) if @env.
|
160
|
-
@env['ORIGINAL_FULLPATH'] = remove_lang(@env['ORIGINAL_FULLPATH']) if @env.
|
154
|
+
@env['REQUEST_URI'] = remove_lang(@env['REQUEST_URI']) if @env.key?('REQUEST_URI')
|
155
|
+
@env['QUERY_STRING'] = remove_lang(@env['QUERY_STRING']) if @env.key?('QUERY_STRING')
|
156
|
+
@env['ORIGINAL_FULLPATH'] = remove_lang(@env['ORIGINAL_FULLPATH']) if @env.key?('ORIGINAL_FULLPATH')
|
161
157
|
when 'subdomain'
|
162
|
-
if @settings['use_proxy'] && @env.
|
158
|
+
if @settings['use_proxy'] && @env.key?('HTTP_X_FORWARDED_HOST')
|
163
159
|
@env['HTTP_X_FORWARDED_HOST'] = remove_lang(@env['HTTP_X_FORWARDED_HOST'])
|
164
160
|
else
|
165
|
-
@env[
|
166
|
-
@env[
|
161
|
+
@env['HTTP_HOST'] = remove_lang(@env['HTTP_HOST'])
|
162
|
+
@env['SERVER_NAME'] = remove_lang(@env['SERVER_NAME'])
|
167
163
|
end
|
168
|
-
if @env.
|
169
|
-
|
170
|
-
end
|
171
|
-
#when 'path'
|
164
|
+
@env['HTTP_REFERER'] = remove_lang(@env['HTTP_REFERER']) if @env.key?('HTTP_REFERER')
|
165
|
+
# when 'path'
|
172
166
|
else
|
173
167
|
@env['REQUEST_URI'] = remove_lang(@env['REQUEST_URI'])
|
174
|
-
if @env.
|
175
|
-
@env['REQUEST_PATH'] = remove_lang(@env['REQUEST_PATH'])
|
176
|
-
end
|
168
|
+
@env['REQUEST_PATH'] = remove_lang(@env['REQUEST_PATH']) if @env.key?('REQUEST_PATH')
|
177
169
|
@env['PATH_INFO'] = remove_lang(@env['PATH_INFO'])
|
178
|
-
if @env.
|
179
|
-
|
180
|
-
end
|
181
|
-
if @env.has_key?('HTTP_REFERER')
|
182
|
-
@env["HTTP_REFERER"] = remove_lang(@env["HTTP_REFERER"])
|
183
|
-
end
|
170
|
+
@env['ORIGINAL_FULLPATH'] = remove_lang(@env['ORIGINAL_FULLPATH']) if @env.key?('ORIGINAL_FULLPATH')
|
171
|
+
@env['HTTP_REFERER'] = remove_lang(@env['HTTP_REFERER']) if @env.key?('HTTP_REFERER')
|
184
172
|
end
|
185
173
|
@env
|
186
174
|
end
|
187
175
|
|
176
|
+
# TODO: this should be in Lang for reusability
|
188
177
|
# Remove language code from the URI.
|
189
178
|
#
|
190
179
|
# @param uri [String] original URI
|
191
180
|
# @param lang_code [String] language code
|
192
181
|
# @return [String] removed URI
|
193
|
-
def remove_lang(uri, lang=
|
182
|
+
def remove_lang(uri, lang = path_lang)
|
194
183
|
lang_code = Store.instance.settings['custom_lang_aliases'][lang] || lang
|
195
184
|
|
196
185
|
# Do nothing if lang is empty.
|
197
|
-
if lang_code.nil? || lang_code.empty?
|
198
|
-
return uri
|
199
|
-
end
|
186
|
+
return uri if lang_code.nil? || lang_code.empty?
|
200
187
|
|
201
188
|
case @settings['url_pattern']
|
202
189
|
when 'query'
|
@@ -204,27 +191,27 @@ module Wovnrb
|
|
204
191
|
when 'subdomain'
|
205
192
|
rp = Regexp.new('(^|(//))' + lang_code + '\.', 'i')
|
206
193
|
return uri.sub(rp, '\1')
|
207
|
-
|
194
|
+
# when 'path'
|
208
195
|
else
|
209
196
|
return uri.sub(/\/#{lang_code}(\/|$)/, '/')
|
210
197
|
end
|
211
198
|
end
|
212
199
|
|
213
200
|
def out(headers)
|
214
|
-
r = Regexp.new(
|
201
|
+
r = Regexp.new('//' + @host)
|
215
202
|
lang_code = Store.instance.settings['custom_lang_aliases'][self.lang_code] || self.lang_code
|
216
|
-
if lang_code != @settings['default_lang'] && headers.
|
203
|
+
if lang_code != @settings['default_lang'] && headers.key?('Location') && headers['Location'] =~ r
|
217
204
|
case @settings['url_pattern']
|
218
205
|
when 'query'
|
219
|
-
if headers['Location'] =~ /\?/
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
206
|
+
headers['Location'] += if headers['Location'] =~ /\?/
|
207
|
+
'&'
|
208
|
+
else
|
209
|
+
'?'
|
210
|
+
end
|
224
211
|
headers['Location'] += "wovn=#{lang_code}"
|
225
212
|
when 'subdomain'
|
226
|
-
headers['Location'] = headers[
|
227
|
-
|
213
|
+
headers['Location'] = headers['Location'].sub(/\/\/([^.]+)/, '//' + lang_code + '.\1')
|
214
|
+
# when 'path'
|
228
215
|
else
|
229
216
|
headers['Location'] = headers['Location'].sub(/(\/\/[^\/]+)/, '\1/' + lang_code)
|
230
217
|
end
|
@@ -240,5 +227,4 @@ module Wovnrb
|
|
240
227
|
end
|
241
228
|
end
|
242
229
|
end
|
243
|
-
|
244
230
|
end
|