328: def read_response(sock)
329: data, resp = nil, nil
330: parser = HttpClientParser.new
331: resp = HttpResponse.new
332:
333: notify :read_header do
334: data = sock.readpartial(1024)
335: nread = parser.execute(resp, data, 0)
336:
337: while not parser.finished?
338: data += sock.readpartial(1024)
339: nread += parser.execute(resp, data, nread)
340: end
341: end
342:
343: notify :read_body do
344: if resp[TRANSFER_ENCODING] and resp[TRANSFER_ENCODING].index("chunked")
345: resp.http_body = read_chunked_encoding(resp, sock, parser)
346: elsif resp[CONTENT_LENGTH]
347: cl = resp[CONTENT_LENGTH].to_i
348: if cl - resp.http_body.length > 0
349: resp.http_body += sock.read(cl - resp.http_body.length)
350: elsif cl < resp.http_body.length
351: STDERR.puts "Web site sucks, they said Content-Length: #{cl}, but sent a longer body length: #{resp.http_body.length}"
352: end
353: else
354: resp.http_body += sock.read
355: end
356: end
357:
358: if resp[SET_COOKIE]
359: cookies = query_parse(resp[SET_COOKIE], ';,')
360: @cookies.merge! cookies
361: @cookies.delete "path"
362: end
363:
364: notify :close do
365: sock.close
366: end
367:
368: resp
369: end