Browse | Submit A New Snippet | Create A Package

 

WEBrick Reverse Proxy Server

Type:
Class
Category:
Other
License:
Artistic License
Language:
Ruby
 
Description:
The reverse proxy server allows webrick to proxy one or more private servers.
For example, the URL /pear/index.html could be redirected to http://localhost:8081/pear-app/index.html while all other paths not matching the /^pear/ pattern are served normally.

Versions Of This Snippet::

Stephane Elie
Snippet ID Download Version Date Posted Author Delete
2671.0.a2007-08-24 10:49Stephane Elie
Changes since last version::
I didn't use the whole thing but I was looking for code mapping the Net::HTTP to the WEBRick::HTTP request/response.

I found some things that were not working as expected in this snippet.
1621.02006-08-01 13:52Richard Kernahan

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :1.0.a

req.header.keys { |key| header[key] = req.header[key][0] }

didn't work for me, to make it work I had to change the line to:

req.header.keys.each { |key| header[key] = req.header[key][0] }


--------

I also found that to fill the cookies in the WEBRick::HTTPResponse, in the
"Convert Net::HTTP::HTTPResponse to WEBrick::HTTPResponse" section, you need to do something like:

set_cookie(response, res)

  def set_cookie(src, dst)
    if str = src['set-cookie']
      cookies = []
      str.split(/,\s*/).each{|token|
        if /^[^=]+;/o =~ token
          cookies[-1] << ", " << token
        elsif /=/o =~ token
          cookies << token
        else
          cookies[-1] << ", " << token
        end
      }
      dst.cookies.replace(cookies)
    end
  end

--------

Just my 2 cents, thanks for sharing that snippet, it inspired me.
Keep up the good work!
		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..