123456789_123456789_123456789_123456789_123456789_

Class: WEBrick::HTTPProxyServer

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: WEBrick::HTTPServer
Defined in: lib/webrick/httpproxy.rb

Overview

An HTTP Proxy server which proxies GET, HEAD and POST requests.

To create a simple proxy server:

require 'webrick'
require 'webrick/httpproxy'

proxy = WEBrick::HTTPProxyServer.new Port: 8000

trap 'INT'  do proxy.shutdown end
trap 'TERM' do proxy.shutdown end

proxy.start

See .new for proxy-specific configuration items.

Modifying proxied responses

To modify content the proxy server returns use the :ProxyContentHandler option:

handler = proc do |req, res|
  if res['content-type'] == 'text/plain' then
    res.body << "\nThis content was proxied!\n"
  end
end

proxy =
  WEBrick::HTTPProxyServer.new Port: 8000, ProxyContentHandler: handler

Class Method Summary

HTTPServer - Inherited

.new

Creates a new HTTP server according to config

GenericServer - Inherited

.new

Creates a new generic server from config.

Instance Attribute Summary

GenericServer - Inherited

#config

The server configuration.

#listeners

Sockets listening for connections.

#logger

The server logger.

#status

The server status.

#tokens

Tokens control the number of outstanding clients.

Instance Method Summary

HTTPServer - Inherited

#access_log

Logs req and res in the access logs.

#do_OPTIONS

The default OPTIONS request handler says GET, HEAD, POST and OPTIONS requests are allowed.

#lookup_server

Finds the appropriate virtual host to handle req

#mount

Mounts servlet on dir passing options to the servlet at creation time.

#mount_proc

Mounts proc or block on dir and calls it with a HTTPRequest and HTTPResponse

#run

Processes requests on sock

#search_servlet

Finds a servlet for path

#service

Services req and fills in res

#umount
#unmount

Unmounts dir

#virtual_host

Adds server as a virtual host.

GenericServer - Inherited

#[]

Retrieves key from the configuration.

#run

You must subclass GenericServer and implement #run which accepts a TCP client socket.

#shutdown

Shuts down the server and all listening sockets.

#start

Starts the server and runs the block for each connection.

#stop

Stops the server from accepting new connections.

Constructor Details

.new(config = {}, default = Config::HTTP) ⇒ HTTPProxyServer

Proxy server configurations. The proxy server handles the following configuration items in addition to those supported by HTTPServer:

:ProxyAuthProc

Called with a request and response to authorize a request

:ProxyVia

Appended to the via header

:ProxyURI

The proxy server's URI

:ProxyContentHandler

Called with a request and response and allows modification of the response

:ProxyTimeout

Sets the proxy timeouts to 30 seconds for open and 60 seconds for read operations

[ GitHub ]

  
# File 'lib/webrick/httpproxy.rb', line 84

def initialize(config={}, default=Config::HTTP)
  super(config, default)
  c = @config
  @via = "#{c[:HTTPVersion]} #{c[:ServerName]}:#{c[:Port]}"
end