Class: Rubroxy::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/rubroxy/proxy_core.rb

Overview

Main class of the proxy object, includes control methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, logger_level = 3) ⇒ Proxy

Creates the proxy object, defines logging levels and host:port

Parameters:

  • host (String)

    Hostname of the proxy (eg: localhost, 127.0.0.1 etc)

  • port (Integer)

    The expected port number (eg: 8080)

  • logger_level (Integer) (defaults to: 3)

    Sets the detail of the logger included.



12
13
14
15
16
17
18
19
20
# File 'lib/rubroxy/proxy_core.rb', line 12

def initialize(host, port, logger_level = 3)
  logger = Logger.new($stderr)
  logger.level = logger_level
  access_log = [] if logger.level == 3
  @server = WEBrick::HTTPProxyServer.new(Port: port,
                                         Logger: logger,
                                         AccessLog: access_log,
                                         BindAddress: host)
end

Instance Attribute Details

#serverObject

Returns the value of attribute server.



4
5
6
# File 'lib/rubroxy/proxy_core.rb', line 4

def server
  @server
end

Instance Method Details

#add_rules(handler) ⇒ Object

Adds a Proc object to the Proxy server

Parameters:

  • handler (Proc)

    The proc object to be passed into the server config



25
26
27
# File 'lib/rubroxy/proxy_core.rb', line 25

def add_rules(handler)
  @server.config[:ProxyContentHandler] = handler
end

#start_serverObject

Starts the server process, including traps for process termination



30
31
32
33
34
35
# File 'lib/rubroxy/proxy_core.rb', line 30

def start_server
  Signal.trap('INT') { stop_server }
  Signal.trap('TERM') { stop_server }

  @server.start
end

#stop_serverObject

Shuts down the proxy server



38
39
40
# File 'lib/rubroxy/proxy_core.rb', line 38

def stop_server
  @server.shutdown
end