bugfix: use user:password@ prefix in http basic auth
authorAzul <azul@riseup.net>
Fri, 2 Dec 2016 10:36:02 +0000 (11:36 +0100)
committerAzul <azul@riseup.net>
Fri, 2 Dec 2016 10:36:02 +0000 (11:36 +0100)
http.rb does not do this on its own.

lib/nickserver/adapters/celluloid_http.rb
lib/nickserver/adapters/http.rb

index 92c5c53..62f905f 100644 (file)
@@ -1,28 +1,19 @@
-require 'nickserver/adapters'
-require 'nickserver/config'
+require 'nickserver/adapters/http'
 silence_warnings do
   require 'celluloid/io'
 end
-require 'http'
 
 module Nickserver::Adapters
-  class CelluloidHttp
+  class CelluloidHttp < Http
     silence_warnings do
       include Celluloid::IO
     end
 
-    def get(url, options = {})
-      response = HTTP.get url,
-        params: options[:query],
-        ssl_context: ctx,
-        ssl_socket_class: Celluloid::IO::SSLSocket
-      return response.code, response.to_s
-    end
+    protected
 
-    def ctx
-      OpenSSL::SSL::SSLContext.new.tap do |ctx|
-        ctx.ca_file = Nickserver::Config.hkp_ca_file
-      end
+    def default_options
+      super.merge ssl_socket_class: Celluloid::IO::SSLSocket
     end
+
   end
 end
index b0ba728..636aceb 100644 (file)
@@ -6,12 +6,30 @@ module Nickserver::Adapters
   class Http
 
     def get(url, options = {})
-      response = HTTP.get url,
-        params: options[:query],
-        ssl_context: ctx
+      url = HTTP::URI.parse url.to_s
+      response = get_with_auth url, params: options[:query]
       return response.code, response.to_s
     end
 
+    protected
+
+    def get_with_auth(url, options)
+      options = default_options.merge options
+      http_with_basic_auth(url).get url, options
+    end
+
+    def http_with_basic_auth(url)
+      if url.password && (url.password != '')
+        HTTP.basic_auth(user: url.user, pass: url.password)
+      else
+        HTTP
+      end
+    end
+
+    def default_options
+      { ssl_context: ctx }
+    end
+
     def ctx
       OpenSSL::SSL::SSLContext.new.tap do |ctx|
         ctx.ca_file = Nickserver::Config.hkp_ca_file