Class: Bridge::Server

Inherits:
Jaws::Server
  • Object
show all
Defined in:
lib/bridge/server.rb

Overview

An HTTP Rack-based server based on the Jaws web server.

Constant Summary collapse

DefaultOptions =
Jaws::Server::DefaultOptions.merge({
  :UseKeyFiles => true,
  :Keys => "",
  :Port => 8079,
})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = DefaultOptions) ⇒ Server

Returns a new instance of Server.



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
# File 'lib/bridge/server.rb', line 48

def initialize(options = DefaultOptions)
  super(DefaultOptions.merge(options))

  @use_key_files = @options[:UseKeyFiles]

  hosts, @bridge_server = @host.split("@", 2)
  if (@bridge_server.nil?)
    # no bridge specified, we expect there to be a single host
    # and (once trimmed of wildcards) it becomes our bridge server
    # address.
    @bridge_server = hosts.gsub(%r{^\*\.?}, '')
    hosts = [hosts]
  else
    # there was a bridge, so we can allow multiple hosts and don't need
    # any magic for the bridge
    hosts = hosts.split(',')
  end
  @listen_hosts = hosts.collect do |h|
    # We need to expand *blah into *.blah and blah. This is a client-side
    # convenience, not something the server deals with.
    if (match = %r{^\*[^\.](.+)$}.match(h))
      ["*." << match[1], match[1]]
    else
      h
    end
  end.flatten

  @keys = @options[:Keys].split(",")
  @keys << ENV["BRIDGE_KEYS"] if (ENV["BRIDGE_KEYS"])
  if (@use_key_files)
    @keys << Bridge::KeyTools.load_keys(@listen_hosts)
  end
  @keys.flatten!
end

Instance Attribute Details

#bridge_serverObject (readonly)

The bridge server we intend to connect to. Derived from #host



46
47
48
# File 'lib/bridge/server.rb', line 46

def bridge_server
  @bridge_server
end

#hostObject

The host information to set up where to listen. It’s formatted as follows: [listen.host1,listen.host2,…@]bridge.host where each of the listen.hosts is a host that this server should handle requests for and the bridge.host is the address of the bridge host itself. Note that if you’re only listening on one host and it shares its ip with the bridge server itself, you can simply give it that host (ie. pickles.oncloud.org will connect to pickles.oncloud.org to server for pickles.oncloud.org) An example of a more complicated example might be: www.blah.com,[email protected] You can also have leading wildcards in the hosts: *.blorp.com,[email protected] Or more concisely (*. matches subdomains, *blah matches *.blah and blah): *[email protected] Or yet more concisely (without an @, wildcards are removed to find the address): *blorp.com And of course, to match everything on a bridge server: *@blorp.com



33
34
35
# File 'lib/bridge/server.rb', line 33

def host
  @host
end

#keysObject

The keys that should be sent to the BRIDGE server. Also set with options



41
42
43
# File 'lib/bridge/server.rb', line 41

def keys
  @keys
end

#listen_hostsObject (readonly)

The hosts this server responds to. Derived from #host



44
45
46
# File 'lib/bridge/server.rb', line 44

def listen_hosts
  @listen_hosts
end

#use_key_filesObject

Whether or not the local key files (.bridge_keys in the current app’s directory, the user’s homedir, and /etc) should be searched for matching keys when connecting to the server. Also set with options



38
39
40
# File 'lib/bridge/server.rb', line 38

def use_key_files
  @use_key_files
end

Instance Method Details

#create_listener(options) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/bridge/server.rb', line 83

def create_listener(options)
  l = Bridge::TCPServer.new(bridge_server, port, listen_hosts, keys)
  # There's no shared state externally accessible, so we just make
  # synchronize on the listener a no-op.
  def l.synchronize
    yield
  end
  return l
end