From 9a1d4a15d6d194f9df0e6cf606706701a5f4270c Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Fri, 26 Aug 2016 10:08:38 -0700 Subject: [PATCH] (#27) Handle URLs that do not have HTTP or HTTPS Prior to this commit, if a user provided a url without HTTP or HTTPS Faraday would fail to make a connection to the pooler since it doesn't seem to handle urls without that. This commit adds a simple check to see what kind of URI the user gave us, and if its missing that protocol add it to the beginning of the URL before making a request. --- lib/vmfloaty/http.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/vmfloaty/http.rb b/lib/vmfloaty/http.rb index 07a8a2d..656b732 100644 --- a/lib/vmfloaty/http.rb +++ b/lib/vmfloaty/http.rb @@ -1,11 +1,30 @@ require 'faraday' +require 'uri' class Http + def self.is_url(url) + # This method exists because it seems like Farady + # has no handling around if a user gives us a URI + # with no protocol on the beginning of the URL + + uri = URI.parse(url) + + if uri.kind_of?(URI::HTTP) or uri.kind_of?(URI::HTTPS) + return true + end + + return false + end + def self.get_conn(verbose, url) if url.nil? raise "Did not provide a url to connect to" end + unless is_url(url) + url = "https://#{url}" + end + conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday| faraday.request :url_encoded faraday.response :logger if verbose @@ -24,6 +43,10 @@ class Http raise "You did not provide a user to authenticate with" end + unless is_url(url) + url = "https://#{url}" + end + conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday| faraday.request :url_encoded faraday.request :basic_auth, user, password