Class: Fog::Connection
- Inherits:
-
Object
- Object
- Fog::Connection
- Defined in:
- lib/fog/connection.rb,
lib/fog/connection.rb
Instance Method Summary collapse
-
#initialize(url) ⇒ Connection
constructor
A new instance of Connection.
- #request(params) ⇒ Object
Constructor Details
#initialize(url) ⇒ Connection
Returns a new instance of Connection.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/fog/connection.rb', line 14 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 |
Instance Method Details
#request(params) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/fog/connection.rb', line 26 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[: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) body = StringIO.new(params[:body]) else body = params[:body] end while chunk = body.read(1048576) # 1 megabyte @connection.write(chunk) end end response = Fog::Response.new response.request = params response.status = @connection.readline[9..11].to_i if params[:expects] && params[:expects] != 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 if error || params[:parser] if error parser = Fog::Errors::Parser.new elsif params[:parser] parser = params[:parser] end body = Nokogiri::XML::SAX::PushParser.new(parser) elsif params[:block] body = nil else body = '' end unless params[:method] == 'HEAD' if response.headers['Content-Length'] content = @connection.read(response.headers['Content-Length'].to_i) unless params[:block] body << content else params[:block].call(content) end elsif response.headers['Transfer-Encoding'] == 'chunked' while true # 2 == "/r/n".length chunk_size = @connection.readline.chomp!.to_i(16) + 2 chunk = @connection.read(chunk_size)[0...-2] unless params[:block] body << chunk else params[:block].call(chunk) end if chunk_size == 2 break end end end end if parser body.finish response.body = parser.response else response.body = body end if error raise(Fog::Errors.status_error(params[:expects], response.status, response)) else response end end |