kcar.git  about / heads / tags
bytestream to Rack response converter
blob 64fb43be16d8f9131c26c8c67d873e456102d062 994 bytes (raw)
$ git show HEAD:lib/kcar/parser.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
# -*- encoding: binary -*-
class Kcar::Parser

  # extract trailers that were set in the header object as
  # an array of arrays
  #
  #   parser.extract_trailers(hdr) =>
  #       [ [ 'Content-MD5', '1B2M2Y8AsgTpgAmY7PhCfg==' ] ]
  def extract_trailers(hdr)
    trailers = []

    if hdr.kind_of?(Array)
      t = {}

      # the HTTP spec (and our parser) guarantees trailers will appear
      # after the "Trailer" header is inserted in the array
      hdr.each do |key, value|
        if key =~ %r{\ATrailer\z}i
          value.split(/\s*,+\s*/).each do |k|
            t[k] = true
          end
        elsif !t.empty? && key =~ /\A(#{t.keys.join('|')})\z/i
          k = $1
          trailers.concat(value.split(/\n+/).map! { |v| [ k, v ] })
        end
      end
    elsif t = hdr['Trailer']
      t.split(/\s*[,\n]+\s*/).each do |k|
        value = hdr[k] or next
        trailers.concat(value.split(/\n+/).map! { |v| [ k, v ] })
      end
    end

    trailers
  end
end # module Kcar

git clone https://yhbt.net/kcar.git