Skip to content

Commit 4086234

Browse files
author
Wesley Beary
committed
switch to using extracted connection code (excon)
1 parent 3438860 commit 4086234

45 files changed

Lines changed: 61 additions & 354 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Rakefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require "#{current_directory}/lib/fog"
77
begin
88
require 'jeweler'
99
Jeweler::Tasks.new do |gem|
10+
gem.add_dependency('excon')
1011
gem.add_dependency('mime-types')
1112
gem.add_dependency('nokogiri')
1213
gem.add_dependency('ruby-hmac')

‎lib/fog.rb‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'rubygems'
2-
require 'rubygems'
32
require 'base64'
43
require 'cgi'
54
require 'digest/md5'
@@ -26,6 +25,11 @@ def self.mocking?
2625
end
2726

2827
def self.reload
28+
load "fog/collection.rb"
29+
load "fog/connection.rb"
30+
load "fog/model.rb"
31+
load "fog/parser.rb"
32+
2933
load "fog/aws.rb"
3034
load "fog/rackspace.rb"
3135
end

‎lib/fog/aws/ec2.rb‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ def self.reset_data
2020
end
2121

2222
def self.reload
23-
load "fog/collection.rb"
24-
load "fog/connection.rb"
25-
load "fog/model.rb"
26-
load "fog/parser.rb"
27-
load "fog/response.rb"
28-
2923
load "fog/aws/models/ec2/address.rb"
3024
load "fog/aws/models/ec2/addresses.rb"
3125
load "fog/aws/models/ec2/instance.rb"

‎lib/fog/aws/s3.rb‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ def self.data
1212
end
1313

1414
def self.reload
15-
load "fog/collection.rb"
16-
load "fog/connection.rb"
17-
load "fog/model.rb"
18-
load "fog/parser.rb"
19-
load "fog/response.rb"
20-
2115
load "fog/aws/models/s3/bucket.rb"
2216
load "fog/aws/models/s3/buckets.rb"
2317
load "fog/aws/models/s3/object.rb"

‎lib/fog/aws/simpledb.rb‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ def self.data
1212
end
1313

1414
def self.reload
15-
load "fog/connection.rb"
16-
load "fog/parser.rb"
17-
load "fog/response.rb"
18-
1915
load "fog/aws/parsers/simpledb/basic.rb"
2016
load "fog/aws/parsers/simpledb/domain_metadata.rb"
2117
load "fog/aws/parsers/simpledb/get_attributes.rb"

‎lib/fog/connection.rb‎

Lines changed: 11 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
require 'rubygems'
2-
require 'openssl'
3-
require 'socket'
4-
require 'uri'
2+
require 'excon'
53

64
require "fog/errors"
75
require "fog/response"
@@ -11,147 +9,24 @@
119
module Fog
1210
class Connection
1311

14-
unless const_defined?(:CHUNK_SIZE)
15-
CHUNK_SIZE = 1048576 # 1 megabyte
16-
end
17-
1812
def initialize(url)
19-
@uri = URI.parse(url)
20-
end
21-
22-
def connection
23-
if @connection && !@connection.closed?
24-
@connection
25-
else
26-
@connection = TCPSocket.open(@uri.host, @uri.port)
27-
if @uri.scheme == 'https'
28-
@ssl_context = OpenSSL::SSL::SSLContext.new
29-
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
30-
@connection = OpenSSL::SSL::SSLSocket.new(@connection, @ssl_context)
31-
@connection.sync_close = true
32-
@connection.connect
33-
end
34-
end
13+
@excon = Excon.new(url)
3514
end
3615

3716
def request(params)
38-
begin
39-
params[:path] ||= ''
40-
unless params[:path][0..0] == '/'
41-
params[:path] = '/' + params[:path].to_s
42-
end
43-
if params[:query] && !params[:query].empty?
44-
params[:path] << "?#{params[:query]}"
45-
end
46-
request = "#{params[:method]} #{params[:path]} HTTP/1.1\r\n"
47-
params[:headers] ||= {}
48-
params[:headers]['Host'] = params[:host]
49-
if params[:body] && !params[:headers]['Content-Length']
50-
params[:headers]['Content-Length'] = params[:body].length
51-
end
52-
for key, value in params[:headers]
53-
request << "#{key}: #{value}\r\n"
54-
end
55-
request << "\r\n"
56-
connection.write(request)
57-
58-
if params[:body]
59-
if params[:body].is_a?(String)
60-
connection.write(params[:body])
61-
else
62-
while chunk = params[:body].read(CHUNK_SIZE)
63-
connection.write(chunk)
64-
end
65-
end
66-
end
67-
68-
response = Fog::Response.new
69-
response.request = params
70-
response.status = connection.readline[9..11].to_i
71-
if params[:expects] && ![*params[:expects]].include?(response.status)
72-
error = true
73-
end
74-
while true
75-
data = connection.readline.chomp!
76-
if data == ""
77-
break
78-
end
79-
header = data.split(': ')
80-
response.headers[capitalize(header[0])] = header[1]
81-
end
82-
83-
unless params[:method] == 'HEAD' || [204, 304, *(100..199)].include?(response.status)
84-
if (error && params[:error_parser]) || params[:parser]
85-
if error
86-
parser = params[:error_parser]
87-
elsif params[:parser]
88-
parser = params[:parser]
89-
end
90-
body = Nokogiri::XML::SAX::PushParser.new(parser)
91-
elsif params[:block]
92-
body = nil
93-
else
94-
body = ''
95-
end
96-
97-
if response.headers['Content-Length']
98-
if error || !params[:block]
99-
body << connection.read(response.headers['Content-Length'].to_i)
100-
else
101-
remaining = response.headers['Content-Length'].to_i
102-
while remaining > 0
103-
params[:block].call(connection.read([CHUNK_SIZE, remaining].min))
104-
remaining -= CHUNK_SIZE;
105-
end
106-
end
107-
elsif response.headers['Transfer-Encoding'] == 'chunked'
108-
while true
109-
chunk_size = connection.readline.chomp!.to_i(16)
110-
# 2 == "/r/n".length
111-
chunk = connection.read(chunk_size + 2)[0...-2]
112-
if chunk_size == 0
113-
break
114-
else
115-
if error || !params[:block]
116-
body << chunk
117-
else
118-
params[:block].call(chunk)
119-
end
120-
end
121-
end
122-
elsif response.headers['Connection'] == 'close'
123-
body << connection.read
124-
@connection = nil
125-
end
126-
127-
if parser
128-
body.finish
129-
response.body = parser.response
130-
else
131-
response.body = body
132-
end
133-
end
134-
rescue => connection_error
135-
@connection = nil
136-
raise(connection_error)
17+
if parser = params.delete(:parser)
18+
body = Nokogiri::XML::SAX::PushParser.new(parser)
19+
params[:block] = lambda { |chunk| body << chunk }
13720
end
13821

139-
if error
140-
raise(Fog::Errors.status_error(params[:expects], response.status, response))
141-
else
142-
response
143-
end
144-
end
145-
146-
private
22+
response = @excon.request(params)
14723

148-
def capitalize(header)
149-
words = header.split('-')
150-
header = ''
151-
for word in words
152-
header << word[0..0].upcase << word[1..-1] << '-'
24+
if parser
25+
body.finish
26+
response.body = parser.response
15327
end
154-
header.chop!
28+
29+
response
15530
end
15631

15732
end

‎lib/fog/errors.rb‎

Lines changed: 0 additions & 145 deletions
This file was deleted.

‎lib/fog/rackspace/files.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Rackspace
33
class Files
44

55
def self.reload
6-
load "fog/rackspace/requests/files/delete_containers.rb"
6+
load "fog/rackspace/requests/files/delete_container.rb"
77
load "fog/rackspace/requests/files/get_container.rb"
88
load "fog/rackspace/requests/files/get_containers.rb"
99
load "fog/rackspace/requests/files/head_container.rb"

‎lib/fog/response.rb‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)