1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

seperating connection/request

This commit is contained in:
Wesley Beary 2009-06-11 10:25:05 -07:00
parent d3f26ab7a0
commit ccdebb0163
3 changed files with 61 additions and 29 deletions

View file

@ -8,27 +8,44 @@ require 'uri'
module Fog
module AWS
class Connection < EventMachine::Connection
attr_accessor :scheme
attr_reader :request
def post_init
@connected = EM::DefaultDeferrable.new
end
def connection_completed
start_tls if @scheme == 'https'
@connected.succeed
end
def send(request)
@request = request
@connected.callback { @request.execute }
@request
end
def receive_data(data)
p data
@request.receive_data(data)
end
end
class Request
include EventMachine::Deferrable
attr_accessor :body, :headers, :method, :parser, :url
attr_reader :response
def post_init
@body ||= nil
@data ||= nil
def initialize(connection)
@connection = connection
@headers ||= {}
@method ||= 'GET'
@parser ||= nil
@response ||= Fog::AWS::Response.new
end
def connection_completed
uri = URI.parse(@url)
start_tls if uri.scheme == 'https'
request
end
def request
def execute
uri = URI.parse(@url)
path = "#{uri.path}"
path << "?#{uri.query}" if uri.query
@ -41,11 +58,12 @@ module Fog
end
request << "\r\n#{@body}" if @body
request << "\r\n"
send_data(request)
p request
@connection.send_data(request)
end
def receive_data(data)
# p data
p data
unless @data
if data =~ /\AHTTP\/1\.[01] ([\d]{3})/
@response.status = $1.to_i

View file

@ -36,6 +36,13 @@ module Fog
@host = options[:host] || 's3.amazonaws.com'
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
EventMachine::run {
@connection = EventMachine.connect(@host, @port, Fog::AWS::Connection) {|connection|
connection.scheme = @scheme
}
EventMachine.stop_event_loop
}
end
# List information about S3 buckets for authorized user
@ -206,11 +213,8 @@ module Fog
end
def canonicalize_amz_headers(headers)
if headers.empty?
nil
else
headers.select {|key,value| key.match(/^x-amz-/iu)}.sort {|x,y| x[0] <=> y[0]}.collect {|header| header.join(':')}.join("\n").downcase
end
headers = headers.select {|key,value| key.match(/^x-amz-/iu)}.sort {|x,y| x[0] <=> y[0]}.collect {|header| header.join(':')}.join("\n").downcase
headers.empty? ? nil : headers
end
def canonicalize_resource(uri)
@ -261,13 +265,18 @@ module Fog
response = nil
EventMachine::run {
http = EventMachine.connect(@host, @port, Fog::AWS::Connection) {|connection|
connection.body = data
connection.headers = headers
connection.method = method
connection.parser = parser
connection.url = url
connection.scheme = @scheme
}
http.callback {|http| response = http.response}
request = Fog::AWS::Request.new(http)
request.body = data
request.headers = headers
request.method = method
request.parser = parser
request.url = url
http.send(request)
request.callback {|request| response = request.response}
}
response
end

View file

@ -302,11 +302,16 @@ module Fog
response = nil
EventMachine::run {
http = EventMachine.connect(@host, @port, Fog::AWS::Connection) {|connection|
connection.method = method
connection.parser = parser
connection.url = "#{@scheme}://#{@host}:#{@port}/#{method == 'GET' ? "?#{query}" : ""}"
connection.scheme = @scheme
}
http.callback {|http| response = http.response}
request = Fog::AWS::Request.new(http)
request.method = method
request.parser = parser
request.url = "#{@scheme}://#{@host}:#{@port}/#{method == 'GET' ? "?#{query}" : ""}"
http.send(request)
request.callback {|http| response = request.response}
}
response
end