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

90 lines
2.3 KiB
Ruby
Raw Normal View History

2009-05-19 02:06:49 -04:00
require File.dirname(__FILE__) + '/aws/simpledb'
require File.dirname(__FILE__) + '/aws/s3'
require 'rubygems'
2009-06-22 01:13:01 -04:00
require 'openssl'
require 'socket'
require 'uri'
module Fog
module AWS
2009-06-22 01:13:01 -04:00
class Connection
2009-06-11 13:25:05 -04:00
2009-06-22 01:13:01 -04:00
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
2009-06-22 01:13:01 -04:00
def request(params)
uri = URI.parse(params[:url])
path = "#{uri.path}"
2009-06-22 13:06:49 -04:00
if uri.query
path << "?#{uri.query}"
end
2009-06-22 01:13:01 -04:00
host = "#{uri.host}"
2009-06-22 13:06:49 -04:00
if (uri.scheme == "http" && uri.port != 80) ||
(uri.scheme == 'https' && uri.port != 443)
host << ":#{uri.port}"
end
2009-06-22 01:13:01 -04:00
request = "#{params[:method]} #{path} HTTP/1.1\r\n"
params[:headers] ||= {}
2009-06-22 01:13:01 -04:00
params[:headers]['Host'] = uri.host
2009-06-22 13:06:49 -04:00
if params[:body]
params[:headers]['Content-Length'] = params[:body].length
end
2009-06-22 01:13:01 -04:00
for key, value in params[:headers]
request << "#{key}: #{value}\r\n"
end
2009-06-22 13:06:49 -04:00
request << "\r\n#{params[:body]}"
2009-06-22 01:13:01 -04:00
@connection.write(request)
2009-06-22 01:13:01 -04:00
response = AWS::Response.new
2009-06-22 14:32:27 -04:00
response.status = @connection.readline[9..11].to_i
2009-06-22 01:13:01 -04:00
while true
data = @connection.readline[0..-3]
if data == ""
2009-06-22 13:06:49 -04:00
break
end
header = data.split(': ')
response.headers[header[0]] = header[1]
2009-06-22 01:13:01 -04:00
end
if response.headers['Content-Length']
2009-06-23 03:16:08 -04:00
response.body << @connection.read(response.headers['Content-Length'].to_i)
2009-06-22 01:13:01 -04:00
elsif response.headers['Transfer-Encoding'] == 'chunked'
while true
2009-06-23 03:16:08 -04:00
# 2 == "/r/n".length
chunk_size = @connection.readline.chomp!.to_i(16) + 2
2009-06-22 01:13:01 -04:00
response.body << @connection.read(chunk_size)
2009-06-23 03:16:08 -04:00
if chunk_size == 2
2009-06-22 13:06:49 -04:00
break
end
end
end
2009-06-22 01:13:01 -04:00
response
end
2009-06-22 01:13:01 -04:00
end
class Response
2009-06-22 01:13:01 -04:00
attr_accessor :status, :headers, :body
def initialize
2009-06-22 01:13:01 -04:00
@body = ''
@headers = {}
end
2009-06-22 01:13:01 -04:00
end
2009-06-22 01:13:01 -04:00
end
2009-06-07 03:54:40 -04:00
end