-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2083 from 3scale/truncate-long-url-labels
Uri shortener for long host label endpoints
- Loading branch information
1 parent
7ecc385
commit c4530bb
Showing
8 changed files
with
97 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
class UriShortener | ||
def self.call(uri) | ||
new(uri).call | ||
end | ||
|
||
def initialize(uri) | ||
@uri = URI.parse(uri) | ||
rescue URI::InvalidURIError | ||
end | ||
|
||
attr_reader :uri | ||
|
||
def call | ||
return unless uri | ||
labels = uri.host.split('.') | ||
uri.host = labels.map(&UriLabelShortner.method(:call)).join('.') | ||
uri | ||
end | ||
|
||
class UriLabelShortner | ||
def self.call(label) | ||
new(label).call | ||
end | ||
|
||
def initialize(label) | ||
@label = label | ||
end | ||
|
||
attr_reader :label | ||
delegate :size, to: :label | ||
|
||
MAX_LABEL_SIZE = UriValidator::UriThreeScaleComplianceChecker::MAX_LABEL_SIZE | ||
HASH_SIZE = 7 | ||
KEEP_SIZE = (MAX_LABEL_SIZE - HASH_SIZE - 1).freeze | ||
|
||
def call | ||
return label if size <= MAX_LABEL_SIZE | ||
[*label.slice(0, KEEP_SIZE).split('-'), hash].join('-') | ||
end | ||
|
||
def hash | ||
Digest::SHA1.hexdigest(label).slice(0, HASH_SIZE) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class UriShortenerTest < ActiveSupport::TestCase | ||
test 'uri with long label' do | ||
shortener = UriShortener.new('http://this-hostname-label-is-longer-than-63-chars-which-is-not-allowed-according-to-rfc-1035.example.com') | ||
assert_equal 'http://this-hostname-label-is-longer-than-63-chars-which-is-no-a6ca0fb.example.com', shortener.call.to_s | ||
end | ||
|
||
test 'uri with multiple long labels' do | ||
shortener = UriShortener.new('http://this-hostname-label-is-longer-than-63-chars-which-is-not-allowed-according-to-rfc-1035.this-other-label-is-as-well-longer-than-63-chars-also-violating-the-rfc-1035.example.com') | ||
assert_equal 'http://this-hostname-label-is-longer-than-63-chars-which-is-no-a6ca0fb.this-other-label-is-as-well-longer-than-63-chars-also-v-57fa2c4.example.com', shortener.call.to_s | ||
end | ||
|
||
test 'does not duplicate leading dash' do | ||
shortener = UriShortener.new('http://long-system-name-that-will-cause-invalid-host-needs-to-be-63-chars-or-shorter.example.com') | ||
assert_not_equal 'http://long-system-name-that-will-cause-invalid-host-needs-to--4207987.example.com', shortener.call.to_s | ||
assert_equal 'http://long-system-name-that-will-cause-invalid-host-needs-to-4207987.example.com', shortener.call.to_s | ||
end | ||
|
||
test 'uri with no long labels' do | ||
shortener = UriShortener.new('http://this-label-is-ok.example.com') | ||
assert_equal 'http://this-label-is-ok.example.com', shortener.call.to_s | ||
end | ||
|
||
test 'invalid uri' do | ||
shortener = UriShortener.new('not a uri') | ||
refute shortener.call | ||
end | ||
end |