Class: Aspera::ProxyAutoConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/proxy_auto_config.rb

Overview

evaluate a proxy autoconfig script

Instance Method Summary collapse

Constructor Details

#initialize(proxy_auto_config) ⇒ ProxyAutoConfig

Returns a new instance of ProxyAutoConfig.

Parameters:

  • proxy_auto_config

    the proxy auto config script to be evaluated



12
13
14
# File 'lib/aspera/proxy_auto_config.rb', line 12

def initialize(proxy_auto_config)
  @proxy_auto_config=proxy_auto_config
end

Instance Method Details

#get_proxy(service_url) ⇒ Object

execut proxy auto config script for the given URL



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aspera/proxy_auto_config.rb', line 17

def get_proxy(service_url)
  # require at runtime, in case there is no js engine
  require 'execjs'
  # variables starting with "context_" are replaced in the ERB template file
  # I did not find an easy way for the javascript to callback ruby
  # and anyway, it only needs to get DNS translation
  context_self='127.0.0.1'
  context_host=URI.parse(service_url).host
  context_ip=nil
  Resolv::DNS.open{|dns|dns.each_address(context_host){|r_addr|context_ip=r_addr.to_s if r_addr.is_a?(Resolv::IPv4)}}
  raise "DNS name not found: #{context_host}" if context_ip.nil?
  pac_functions=ERB.new(PAC_FUNC_TEMPLATE).result(binding)
  context = ExecJS.compile(pac_functions+@proxy_auto_config)
  return context.call("FindProxyForURL", service_url, context_host)
end