GIT-VERSION-GEN: fix install for non-tag versions
[kcar.git] / lib / kcar / parser.rb
blob4dd59e2f6f00b96de5347b30e36fc96323b41789
1 # -*- encoding: binary -*-
2 class Kcar::Parser
4   # extract trailers that were set in the header object as
5   # an array of arrays
6   #
7   #   parser.extract_trailers(hdr) =>
8   #       [ [ 'Content-MD5', '1B2M2Y8AsgTpgAmY7PhCfg==' ] ]
9   def extract_trailers(hdr)
10     trailers = []
12     if hdr.kind_of?(Array)
13       t = {}
15       # the HTTP spec (and our parser) guarantees trailers will appear
16       # after the "Trailer" header is inserted in the array
17       hdr.each do |key, value|
18         if key =~ %r{\ATrailer\z}i
19           value.split(/\s*,+\s*/).each do |k|
20             t[k] = true
21           end
22         elsif false == t.empty? && key =~ /\A(#{t.keys.join('|')})\z/i
23           k = $1
24           trailers.concat(value.split(/\n+/).map! { |v| [ k, v ] })
25         end
26       end
27     elsif t = hdr['Trailer']
28       t.split(/\s*[,\n]+\s*/).each do |k|
29         value = hdr[k] or next
30         trailers.concat(value.split(/\n+/).map! { |v| [ k, v ] })
31       end
32     end
34     trailers
35   end
36 end # module Kcar