mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
bust connection cache on connection errors
This commit is contained in:
parent
1d5b3d19d3
commit
8ef154b1ab
1 changed files with 86 additions and 81 deletions
|
@ -35,100 +35,105 @@ unless Fog.mocking?
|
|||
end
|
||||
|
||||
def request(params)
|
||||
params[:path] ||= ''
|
||||
unless params[:path][0..0] == '/'
|
||||
params[:path] = '/' + params[:path].to_s
|
||||
end
|
||||
if params[:query] && !params[:query].empty?
|
||||
params[:path] << "?#{params[:query]}"
|
||||
end
|
||||
request = "#{params[:method]} #{params[:path]} HTTP/1.1\r\n"
|
||||
params[:headers] ||= {}
|
||||
params[:headers]['Host'] = params[:host]
|
||||
if params[:body] && !params[:headers]['Content-Length']
|
||||
params[:headers]['Content-Length'] = params[:body].length
|
||||
end
|
||||
for key, value in params[:headers]
|
||||
request << "#{key}: #{value}\r\n"
|
||||
end
|
||||
request << "\r\n"
|
||||
connection.write(request)
|
||||
|
||||
if params[:body]
|
||||
if params[:body].is_a?(String)
|
||||
connection.write(params[:body])
|
||||
else
|
||||
while chunk = params[:body].read(CHUNK_SIZE)
|
||||
connection.write(chunk)
|
||||
end
|
||||
begin
|
||||
params[:path] ||= ''
|
||||
unless params[:path][0..0] == '/'
|
||||
params[:path] = '/' + params[:path].to_s
|
||||
end
|
||||
end
|
||||
|
||||
response = Fog::Response.new
|
||||
response.request = params
|
||||
response.status = connection.readline[9..11].to_i
|
||||
if params[:expects] && ![*params[:expects]].include?(response.status)
|
||||
error = true
|
||||
end
|
||||
while true
|
||||
data = connection.readline.chomp!
|
||||
if data == ""
|
||||
break
|
||||
if params[:query] && !params[:query].empty?
|
||||
params[:path] << "?#{params[:query]}"
|
||||
end
|
||||
header = data.split(': ')
|
||||
response.headers[capitalize(header[0])] = header[1]
|
||||
end
|
||||
|
||||
unless params[:method] == 'HEAD' || [204, 304, *(100..199)].include?(response.status)
|
||||
if (error && params[:error_parser]) || params[:parser]
|
||||
if error
|
||||
parser = params[:error_parser]
|
||||
elsif params[:parser]
|
||||
parser = params[:parser]
|
||||
end
|
||||
body = Nokogiri::XML::SAX::PushParser.new(parser)
|
||||
elsif params[:block]
|
||||
body = nil
|
||||
else
|
||||
body = ''
|
||||
request = "#{params[:method]} #{params[:path]} HTTP/1.1\r\n"
|
||||
params[:headers] ||= {}
|
||||
params[:headers]['Host'] = params[:host]
|
||||
if params[:body] && !params[:headers]['Content-Length']
|
||||
params[:headers]['Content-Length'] = params[:body].length
|
||||
end
|
||||
for key, value in params[:headers]
|
||||
request << "#{key}: #{value}\r\n"
|
||||
end
|
||||
request << "\r\n"
|
||||
connection.write(request)
|
||||
|
||||
if response.headers['Content-Length']
|
||||
if error || !params[:block]
|
||||
body << connection.read(response.headers['Content-Length'].to_i)
|
||||
if params[:body]
|
||||
if params[:body].is_a?(String)
|
||||
connection.write(params[:body])
|
||||
else
|
||||
remaining = response.headers['Content-Length'].to_i
|
||||
while remaining > 0
|
||||
params[:block].call(connection.read([CHUNK_SIZE, remaining].min))
|
||||
remaining -= CHUNK_SIZE;
|
||||
while chunk = params[:body].read(CHUNK_SIZE)
|
||||
connection.write(chunk)
|
||||
end
|
||||
end
|
||||
elsif response.headers['Transfer-Encoding'] == 'chunked'
|
||||
while true
|
||||
chunk_size = connection.readline.chomp!.to_i(16)
|
||||
# 2 == "/r/n".length
|
||||
chunk = connection.read(chunk_size + 2)[0...-2]
|
||||
if chunk_size == 0
|
||||
break
|
||||
end
|
||||
|
||||
response = Fog::Response.new
|
||||
response.request = params
|
||||
response.status = connection.readline[9..11].to_i
|
||||
if params[:expects] && ![*params[:expects]].include?(response.status)
|
||||
error = true
|
||||
end
|
||||
while true
|
||||
data = connection.readline.chomp!
|
||||
if data == ""
|
||||
break
|
||||
end
|
||||
header = data.split(': ')
|
||||
response.headers[capitalize(header[0])] = header[1]
|
||||
end
|
||||
|
||||
unless params[:method] == 'HEAD' || [204, 304, *(100..199)].include?(response.status)
|
||||
if (error && params[:error_parser]) || params[:parser]
|
||||
if error
|
||||
parser = params[:error_parser]
|
||||
elsif params[:parser]
|
||||
parser = params[:parser]
|
||||
end
|
||||
body = Nokogiri::XML::SAX::PushParser.new(parser)
|
||||
elsif params[:block]
|
||||
body = nil
|
||||
else
|
||||
body = ''
|
||||
end
|
||||
|
||||
if response.headers['Content-Length']
|
||||
if error || !params[:block]
|
||||
body << connection.read(response.headers['Content-Length'].to_i)
|
||||
else
|
||||
if error || !params[:block]
|
||||
body << chunk
|
||||
else
|
||||
params[:block].call(chunk)
|
||||
remaining = response.headers['Content-Length'].to_i
|
||||
while remaining > 0
|
||||
params[:block].call(connection.read([CHUNK_SIZE, remaining].min))
|
||||
remaining -= CHUNK_SIZE;
|
||||
end
|
||||
end
|
||||
elsif response.headers['Transfer-Encoding'] == 'chunked'
|
||||
while true
|
||||
chunk_size = connection.readline.chomp!.to_i(16)
|
||||
# 2 == "/r/n".length
|
||||
chunk = connection.read(chunk_size + 2)[0...-2]
|
||||
if chunk_size == 0
|
||||
break
|
||||
else
|
||||
if error || !params[:block]
|
||||
body << chunk
|
||||
else
|
||||
params[:block].call(chunk)
|
||||
end
|
||||
end
|
||||
end
|
||||
elsif response.headers['Connection'] == 'close'
|
||||
body << connection.read
|
||||
@connection = nil
|
||||
end
|
||||
elsif response.headers['Connection'] == 'close'
|
||||
body << connection.read
|
||||
@connection = nil
|
||||
end
|
||||
|
||||
if parser
|
||||
body.finish
|
||||
response.body = parser.response
|
||||
else
|
||||
response.body = body
|
||||
if parser
|
||||
body.finish
|
||||
response.body = parser.response
|
||||
else
|
||||
response.body = body
|
||||
end
|
||||
end
|
||||
rescue => connection_error
|
||||
@connection = nil
|
||||
raise(connection_error)
|
||||
end
|
||||
|
||||
if error
|
||||
|
|
Loading…
Add table
Reference in a new issue