Class: Owasp::Esapi::Codec::PercentCodec

Inherits:
BaseCodec
  • Object
show all
Defined in:
lib/codec/percent_codec.rb

Constant Summary

Constants inherited from BaseCodec

BaseCodec::END_CODE_POINT, BaseCodec::START_CODE_POINT

Instance Method Summary collapse

Methods inherited from BaseCodec

#decode, #encode, #hex, #min

Instance Method Details

#decode_char(input) ⇒ Object

Formats all are legal both upper/lower case: %hh;



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/codec/percent_codec.rb', line 21

def decode_char(input)
  input.mark
  first = input.next
  if first.nil?
    input.reset
    return nil
  end
  # check if this is an encoded character
  if first != '%'
    input.reset
    return nil
  end
  # search for 2 hex digits
  tmp = ''
  for i in 0..1 do
    c = input.next_hex
    tmp << c unless c.nil?
  end
  # we found 2, convert to a number
  if tmp.size == 2
    i = tmp.hex
    begin
      return i.chr(Encoding::UTF_8) if i >= START_CODE_POINT and i <= END_CODE_POINT
    rescue Exception => e
    end
  end
  input.reset
  nil
end

#encode_char(immune, input) ⇒ Object

Encode a character for URLs



8
9
10
11
12
13
14
15
16
17
# File 'lib/codec/percent_codec.rb', line 8

def encode_char(immune,input)
  return input if input =~ /[a-zA-Z0-9_.-]/
  # RFC compliance
  return "+" if input == " "
  val = ''
  input.each_byte do |b|
    val << '%' << b.ord.to_h.upcase
  end
  val
end