lib/rfuzz/client.rb in rfuzz-0.6 vs lib/rfuzz/client.rb in rfuzz-0.7

- old
+ new

@@ -1,9 +1,10 @@ require 'http11_client' require 'socket' require 'stringio' require 'rfuzz/stats' +require 'timeout' module RFuzz # A simple hash is returned for each request made by HttpClient with @@ -37,20 +38,21 @@ # A mixin that has most of the HTTP encoding methods you need to work # with the protocol. It's used by HttpClient, but you can use it # as well. module HttpEncoding + COOKIE="Cookie" # Converts a Hash of cookies to the appropriate simple cookie # headers. def encode_cookies(cookies) result = "" cookies.each do |k,v| if v.kind_of? Array - v.each {|x| result += encode_field("Cookie", encode_param(k,x)) } + v.each {|x| result += encode_field(COOKIE, encode_param(k,x)) } else - result += encode_field("Cookie", encode_param(k,v)) + result += encode_field(COOKIE, encode_param(k,v)) end end return result end @@ -200,10 +202,12 @@ CONTENT_LENGTH="CONTENT_LENGTH" SET_COOKIE="SET_COOKIE" LOCATION="LOCATION" HOST="HOST" HTTP_REQUEST_HEADER="%s %s HTTP/1.1\r\n" + REQ_CONTENT_LENGTH="Content-Length" + REQ_HOST="Host" # Access to the host, port, default options, and cookies currently in play attr_accessor :host, :port, :options, :cookies, :allowed_methods, :notifier # Doesn't make the connect until you actually call a .put,.get, etc. @@ -230,12 +234,12 @@ # merge head differently since that's typically what they mean head = req[:head] || {} head = ops[:head].merge(head) if ops[:head] # setup basic headers we always need - head[HOST] = encode_host(@host,@port) - head[CONTENT_LENGTH] = ops[:body] ? ops[:body].length : 0 + head[REQ_HOST] = encode_host(@host,@port) + head[REQ_CONTENT_LENGTH] = ops[:body] ? ops[:body].length : 0 # blast it out out.write(HTTP_REQUEST_HEADER % [method, encode_query(uri,query)]) out.write(encode_headers(head)) out.write(encode_cookies(@cookies.merge(req[:cookies] || {}))) @@ -259,11 +263,12 @@ size = chunk.http_chunk_size ? chunk.http_chunk_size.to_i(base=16) : 0 if size == 0 return :finished, nil end - remain = size -out.write(input.read(size)) + + remain = size - out.write(input.read(size)) return :incomplete_body, remain if remain > 0 line = input.read(2) if line.nil? or line.length < 2 return :incomplete_trailer, line @@ -288,10 +293,10 @@ case status when :incomplete_trailer if result.nil? sock.read(2) else - sock.read(result.length - 2) + sock.read((result.length - 2).abs) end when :incomplete_body out.write(sock.read(result)) # read the remaining sock.read(2) when :incomplete_header