Class: Dap::Filter::FilterDecodeHTTPReply

Inherits:
Object
  • Object
show all
Includes:
BaseDecoder
Defined in:
lib/dap/filter/http.rb

Instance Attribute Summary

Attributes included from Base

#name, #opts

Instance Method Summary collapse

Methods included from BaseDecoder

#process

Methods included from Base

#initialize, #process

Instance Method Details

#decode(data) ⇒ Object

TODO: Decode transfer-chunked responses



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/dap/filter/http.rb', line 138

def decode(data)
  lines = data.split(/\r?\n/)
  resp  = lines.shift
  save  = {}
  return save if resp !~ /^HTTP\/\d+\.\d+\s+(\d+)\s+(.*)/

  save["http_code"] = $1.to_i
  save["http_message"] = $2.strip
  save["http_raw_headers"] = {}

  clen = nil

  while lines.length > 0
    hline = lines.shift
    case hline
    when /^ETag:\s*(.*)/i
      save["http_etag"] = $1

    when /^Set-Cookie:\s*(.*)/i
      bits = $1.gsub(/\;?\s*path=.*/i, '').gsub(/\;?\s*expires=.*/i, '').gsub(/\;\s*HttpOnly.*/, '')
      save["http_cookie"] = bits.strip

    when /^Server:\s*(.*)/i
      save["http_server"] = $1.strip

    when /^X-Powered-By:\s*(.*)/i
      save["http_powered"] = $1.strip

    when /^Date:\s*(.*)/i
      d = DateTime.parse($1.strip) rescue nil
      save["http_date"] = d.to_time.strftime("%Y%m%dT%H:%M:%S") if d

    when /^Last-modified:\s*(.*)/i
      d = DateTime.parse($1.strip) rescue nil
      save["http_modified"] = d.to_time.strftime("%Y%m%dT%H:%M:%S") if d

    when /^Location:\s*(.*)/i
      save["http_location"] = $1.strip

    when /^WWW-Authenticate:\s*(.*)/i
      save["http_auth"] = $1.strip

    when /^Content-Length:\s*(.*)/i
      clen = $1.strip.to_i

    when /^([A-Za-z0-9\-]+):\s*(.*)/i
      save["http_raw_headers"][$1.downcase.strip] = $2.strip

    when ""
      break
    end
  end

  head, body = data.split(/\r?\n\r?\n/, 2)

  # Some buggy systems exclude the header entirely
  body ||= head

  if save["http_raw_headers"]["content-encoding"] == "gzip"
    begin
      gunzip = Zlib::GzipReader.new(StringIO.new(body))
      body = gunzip.read.encode('UTF-8', :invalid=>:replace, :replace=>'?')
      gunzip.close()
    rescue
    end
  end
  save["http_body"] = body

  if body =~ /<title>([^>]+)</mi
    save["http_title"] = $1.strip
  end

  save
end