lib/wagon/connection.rb in wagon-0.9.5 vs lib/wagon/connection.rb in wagon-0.10.0

- old
+ new

@@ -1,22 +1,50 @@ require 'net/http' require 'net/https' +require 'curb' require 'uri' require 'digest/sha1' require 'wagon/ward' module Wagon - class AuthenticationFailure < StandardError; end class Connection HOST = 'secure.lds.org' LOGIN_PATH = '/units/a/login/1,21568,779-1,00.html?URL=' - CACHE_PATH = File.join(File.expand_path('~'), '.wagon_cache') + # For asynchronous procedures + @@trigger = ConditionVariable.new + @@lock = Mutex.new + @@queue = [] + + (1..30).collect do + Thread.new do + http = Net::HTTP.new(HOST, 443) + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + http + + while true + connection, path, callback = nil, nil, nil + @@lock.synchronize do + connection, path, callback = *@@queue.shift + end + + if connection + callback.call(http.request(Net::HTTP::Get.new(path, {'Cookie' => connection.cookies || ''}))) + else + sleep(0.5) + end + end + end + end + + attr_reader :cookies + def initialize(username, password) - response = post(LOGIN_PATH, 'username' => username, 'password' => password) + response = _post(LOGIN_PATH, 'username' => username, 'password' => password) @cookies = response['set-cookie'] @home_path = URI.parse(response['location']).path raise AuthenticationFailure.new("Invalid username and/or password") unless @cookies end @@ -28,39 +56,23 @@ def ward @ward ||= Ward.new(self, home_path) end def get(path) - Connection.perform_caching? ? get_with_caching(path) : get_without_caching(path) + _get(path).body end - def get_without_caching(path) - _http.request(Net::HTTP::Get.new(path, {'Cookie' => @cookies || ''})).body + def get_async(path, &block) + @@lock.synchronize do + @@queue.push([self, path, block]) + end end - def get_with_caching(path) - FileUtils::mkdir(CACHE_PATH) unless File.directory?(CACHE_PATH) - cache_path = File.join(CACHE_PATH, Digest::SHA1.hexdigest(path) + ".cache") - return open(cache_path).read if File.exists?(cache_path) - open(cache_path, "w").write(data = get_without_caching(path)) - data + def expired? + _head(ward.directory_path).class != Net::HTTPOK end - def self.perform_caching? - @@perform_caching ||= true - end - - def self.perform_caching(true_or_false) - @@perform_caching = true_or_false - end - - def post(path, data) - request = Net::HTTP::Post.new(path, {'Cookie' => @cookies || ''}) - request.set_form_data(data) - _http.request(request) - end - def _dump(depth) Marshal.dump([@cookies, @home_path]) end def self._load(string) @@ -76,8 +88,28 @@ return @http unless @http.nil? @http = Net::HTTP.new(HOST, 443) @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_NONE @http + end + + def _curl + @curl ||= Curl::Easy.new do |curl| + curl.headers = {'Cookie' => @cookies || ''} + end + end + + def _get(path) + _http.request(Net::HTTP::Get.new(path, {'Cookie' => @cookies || ''})) + end + + def _head(path) + _http.request(Net::HTTP::Head.new(path, {'Cookie' => @cookies || ''})) + end + + def _post(path, data) + request = Net::HTTP::Post.new(path, {'Cookie' => @cookies || ''}) + request.set_form_data(data) + _http.request(request) end end end \ No newline at end of file