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