Skip to content

Commit

Permalink
rewrote URLRecode to Ruby to reduce Perl dependencies
Browse files Browse the repository at this point in the history
so for package management maybe Perl will not be needed
specifically for "yast2 repositories" it was the only Perl dependency
  • Loading branch information
mvidner committed Mar 27, 2014
1 parent 07b2f57 commit 296163f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 89 deletions.
4 changes: 2 additions & 2 deletions library/types/src/Makefile.am
Expand Up @@ -12,8 +12,8 @@ module_DATA = \
modules/Integer.rb \
modules/Punycode.rb \
modules/Hostname.rb \
modules/URLRecode.pm
modules/URLRecode.rb

EXTRA_DIST = $(module_DATA)

include $(top_srcdir)/Makefile.am.common
include $(top_srcdir)/Makefile.am.common
87 changes: 0 additions & 87 deletions library/types/src/modules/URLRecode.pm

This file was deleted.

62 changes: 62 additions & 0 deletions library/types/src/modules/URLRecode.rb
@@ -0,0 +1,62 @@
# encoding: utf-8

# Copyright 2014 SUSE, LLC

require "yast"

module Yast
# A drop-in replacement of an earlier Perl implementation
class URLRecodeClass < Module
# Escape password, user name and fragment part of URL string
# @param [String] input input string
# @return [String] Escaped string
def EscapePassword(input)
escape(input, USERNAME_PASSWORD_FRAGMENT_SAFE_CHARS)
end

# Escape path part of URL string
# @param [String] input input string
# @return [String] Escaped string
def EscapePath(input)
escape(input, PATH_SAFE_CHARS)
end

# Escape path part of URL string
# @param [String] input input string
# @return [String] Escaped string
def EscapeQuery(input)
escape(input, QUERY_SAFE_CHARS)
end

# UnEscape an URL string, replace %<Hexnum><HexNum> sequences
# by character
# @param [String] input input string
# @return [String] Unescaped string
def UnEscape(input)
input.gsub(/%([0-9A-Fa-f]{2})/) { $1.to_i(16).chr }.force_encoding(input.encoding)
end

private

# these will be substituted to a regex character class
USERNAME_PASSWORD_FRAGMENT_SAFE_CHARS = "-A-Za-z0-9_.!~*'()"
PATH_SAFE_CHARS = "-A-Za-z0-9_.!~*'()/"
QUERY_SAFE_CHARS = "-A-Za-z0-9_.!~*'()/:=&"

def escape(input, safe_chars)
return nil if input.nil?
input.gsub(/[^#{safe_chars}]/) do |unicode_char|
escaped = ""
unicode_char.each_byte {|b| escaped << sprintf("%%%02x", b) }
escaped
end
end

publish :function => :EscapePassword, :type => "string (string)"
publish :function => :EscapePath, :type => "string (string)"
publish :function => :EscapeQuery, :type => "string (string)"
publish :function => :UnEscape, :type => "string (string)"
end

URLRecode = URLRecodeClass.new
end

0 comments on commit 296163f

Please sign in to comment.