mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
cleanup and separate to allow for requiring individual interfaces and/or whole package
This commit is contained in:
parent
a2920a7742
commit
646f365dbc
6 changed files with 130 additions and 118 deletions
112
lib/fog/aws.rb
112
lib/fog/aws.rb
|
@ -1,116 +1,4 @@
|
|||
require 'rubygems'
|
||||
require 'openssl'
|
||||
require 'socket'
|
||||
require 'uri'
|
||||
|
||||
current_directory = File.dirname(__FILE__)
|
||||
require "#{current_directory}/parser"
|
||||
require "#{current_directory}/aws/ec2"
|
||||
require "#{current_directory}/aws/simpledb"
|
||||
require "#{current_directory}/aws/s3"
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class Connection
|
||||
|
||||
def initialize(url)
|
||||
@uri = URI.parse(url)
|
||||
@connection = TCPSocket.open(@uri.host, @uri.port)
|
||||
if @uri.scheme == 'https'
|
||||
@ssl_context = OpenSSL::SSL::SSLContext.new
|
||||
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
||||
@connection = OpenSSL::SSL::SSLSocket.new(@connection, @ssl_context)
|
||||
@connection.sync_close = true
|
||||
@connection.connect
|
||||
end
|
||||
end
|
||||
|
||||
def request(params)
|
||||
params[:path] ||= ''
|
||||
unless params[:path][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[:body].length
|
||||
end
|
||||
for key, value in params[:headers]
|
||||
request << "#{key}: #{value}\r\n"
|
||||
end
|
||||
request << "\r\n#{params[:body]}"
|
||||
@connection.write(request)
|
||||
|
||||
response = AWS::Response.new
|
||||
response.status = @connection.readline[9..11].to_i
|
||||
while true
|
||||
data = @connection.readline.chomp!
|
||||
if data == ""
|
||||
break
|
||||
end
|
||||
header = data.split(': ')
|
||||
response.headers[capitalize(header[0])] = header[1]
|
||||
end
|
||||
|
||||
if params[:parser]
|
||||
body = Nokogiri::XML::SAX::PushParser.new(params[:parser])
|
||||
else
|
||||
body = ''
|
||||
end
|
||||
|
||||
unless params[:method] == 'HEAD'
|
||||
if response.headers['Content-Length']
|
||||
body << @connection.read(response.headers['Content-Length'].to_i)
|
||||
elsif response.headers['Transfer-Encoding'] == 'chunked'
|
||||
while true
|
||||
# 2 == "/r/n".length
|
||||
chunk_size = @connection.readline.chomp!.to_i(16) + 2
|
||||
body << @connection.read(chunk_size)
|
||||
if chunk_size == 2
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if params[:parser]
|
||||
body.finish
|
||||
response.body = params[:parser].response
|
||||
else
|
||||
response.body = body
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def capitalize(header)
|
||||
words = header.split('-')
|
||||
header = ''
|
||||
for word in words
|
||||
header << word[0..0].upcase << word[1..-1] << '-'
|
||||
end
|
||||
header.chop!
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Response
|
||||
|
||||
attr_accessor :status, :headers, :body
|
||||
|
||||
def initialize
|
||||
@body = ''
|
||||
@headers = {}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,12 @@ require 'base64'
|
|||
require 'cgi'
|
||||
require 'hmac-sha2'
|
||||
|
||||
parsers_directory = "#{File.dirname(__FILE__)}/parsers/ec2"
|
||||
current_directory = File.dirname(__FILE__)
|
||||
require "#{current_directory}/../connection"
|
||||
require "#{current_directory}/../parser"
|
||||
require "#{current_directory}/../response"
|
||||
|
||||
parsers_directory = "#{current_directory}/parsers/ec2"
|
||||
require "#{parsers_directory}/allocate_address"
|
||||
require "#{parsers_directory}/basic"
|
||||
require "#{parsers_directory}/create_key_pair"
|
||||
|
@ -47,7 +52,7 @@ module Fog
|
|||
@host = options[:host] || 'ec2.amazonaws.com'
|
||||
@port = options[:port] || 443
|
||||
@scheme = options[:scheme] || 'https'
|
||||
@connection = AWS::Connection.new("#{@scheme}://#{@host}:#{@port}")
|
||||
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
|
||||
end
|
||||
|
||||
# Acquire an elastic IP address.
|
||||
|
|
|
@ -5,7 +5,12 @@ require 'digest/md5'
|
|||
require 'hmac-sha1'
|
||||
require 'mime/types'
|
||||
|
||||
parsers_directory = "#{File.dirname(__FILE__)}/parsers/s3"
|
||||
current_directory = File.dirname(__FILE__)
|
||||
require "#{current_directory}/../connection"
|
||||
require "#{current_directory}/../parser"
|
||||
require "#{current_directory}/../response"
|
||||
|
||||
parsers_directory = "#{current_directory}/parsers/s3"
|
||||
require "#{parsers_directory}/copy_object"
|
||||
require "#{parsers_directory}/get_bucket"
|
||||
require "#{parsers_directory}/get_bucket_location"
|
||||
|
@ -40,7 +45,7 @@ module Fog
|
|||
@host = options[:host] || 's3.amazonaws.com'
|
||||
@port = options[:port] || 443
|
||||
@scheme = options[:scheme] || 'https'
|
||||
@connection = AWS::Connection.new("#{@scheme}://#{@host}:#{@port}")
|
||||
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
|
||||
end
|
||||
|
||||
# Copy an object from one S3 bucket to another
|
||||
|
|
|
@ -3,7 +3,12 @@ require 'base64'
|
|||
require 'cgi'
|
||||
require 'hmac-sha2'
|
||||
|
||||
parsers_directory = "#{File.dirname(__FILE__)}/parsers/simpledb"
|
||||
current_directory = File.dirname(__FILE__)
|
||||
require "#{current_directory}/../connection"
|
||||
require "#{current_directory}/../parser"
|
||||
require "#{current_directory}/../response"
|
||||
|
||||
parsers_directory = "#{current_directory}/parsers/simpledb"
|
||||
require "#{parsers_directory}/basic"
|
||||
require "#{parsers_directory}/domain_metadata"
|
||||
require "#{parsers_directory}/get_attributes"
|
||||
|
@ -39,7 +44,7 @@ module Fog
|
|||
@nil_string = options[:nil_string]|| 'nil'
|
||||
@port = options[:port] || 443
|
||||
@scheme = options[:scheme] || 'https'
|
||||
@connection = AWS::Connection.new("#{@scheme}://#{@host}:#{@port}")
|
||||
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
|
||||
end
|
||||
|
||||
# Put items attributes into a SimpleDB domain
|
||||
|
|
97
lib/fog/connection.rb
Normal file
97
lib/fog/connection.rb
Normal file
|
@ -0,0 +1,97 @@
|
|||
require 'rubygems'
|
||||
require 'openssl'
|
||||
require 'socket'
|
||||
require 'uri'
|
||||
|
||||
require "#{File.dirname(__FILE__)}/response"
|
||||
|
||||
module Fog
|
||||
class Connection
|
||||
|
||||
def initialize(url)
|
||||
@uri = URI.parse(url)
|
||||
@connection = TCPSocket.open(@uri.host, @uri.port)
|
||||
if @uri.scheme == 'https'
|
||||
@ssl_context = OpenSSL::SSL::SSLContext.new
|
||||
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
||||
@connection = OpenSSL::SSL::SSLSocket.new(@connection, @ssl_context)
|
||||
@connection.sync_close = true
|
||||
@connection.connect
|
||||
end
|
||||
end
|
||||
|
||||
def request(params)
|
||||
params[:path] ||= ''
|
||||
unless params[:path][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[:body].length
|
||||
end
|
||||
for key, value in params[:headers]
|
||||
request << "#{key}: #{value}\r\n"
|
||||
end
|
||||
request << "\r\n#{params[:body]}"
|
||||
@connection.write(request)
|
||||
|
||||
response = AWS::Response.new
|
||||
response.status = @connection.readline[9..11].to_i
|
||||
while true
|
||||
data = @connection.readline.chomp!
|
||||
if data == ""
|
||||
break
|
||||
end
|
||||
header = data.split(': ')
|
||||
response.headers[capitalize(header[0])] = header[1]
|
||||
end
|
||||
|
||||
if params[:parser]
|
||||
body = Nokogiri::XML::SAX::PushParser.new(params[:parser])
|
||||
else
|
||||
body = ''
|
||||
end
|
||||
|
||||
unless params[:method] == 'HEAD'
|
||||
if response.headers['Content-Length']
|
||||
body << @connection.read(response.headers['Content-Length'].to_i)
|
||||
elsif response.headers['Transfer-Encoding'] == 'chunked'
|
||||
while true
|
||||
# 2 == "/r/n".length
|
||||
chunk_size = @connection.readline.chomp!.to_i(16) + 2
|
||||
body << @connection.read(chunk_size)
|
||||
if chunk_size == 2
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if params[:parser]
|
||||
body.finish
|
||||
response.body = params[:parser].response
|
||||
else
|
||||
response.body = body
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def capitalize(header)
|
||||
words = header.split('-')
|
||||
header = ''
|
||||
for word in words
|
||||
header << word[0..0].upcase << word[1..-1] << '-'
|
||||
end
|
||||
header.chop!
|
||||
end
|
||||
|
||||
end
|
||||
end
|
12
lib/fog/response.rb
Normal file
12
lib/fog/response.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
module Fog
|
||||
class Response
|
||||
|
||||
attr_accessor :status, :headers, :body
|
||||
|
||||
def initialize
|
||||
@body = ''
|
||||
@headers = {}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue