root / trunk / lib / mouseHole / proxyhandler.rb

Revision 107, 2.2 kB (checked in by why, 22 months ago)
  • lib/mouseHole/: builtin installer for .user.rb files found in the wild.
Line 
1require 'mouseHole/page'
2
3module MouseHole
4
5  class MountHandler < Mongrel::HttpHandler
6    include HandlerMixin
7    include LoggerMixin
8
9    def initialize(block)
10      @block = block
11    end
12
13    def process(request, response)
14      reqh, env = page_headers(request)
15      header = []
16      choose_header(reqh, header)
17      page = Page.new(URI(env['request-uri']), 404, header)
18      @block.call(page)
19      output(page, response)
20    end
21
22  end
23
24  class ProxyHandler < Mongrel::HttpHandler
25    include HandlerMixin
26    include LoggerMixin
27
28    def initialize(central)
29      @central = central
30    end
31
32    def process(request, response)
33      start = Time.now
34      uri, reqh, env = page_prep(request)
35       
36      if uri.path =~ %r!/([\w\-]{32})/!
37        token, trail = $1, $'
38        app = @central.find_app :token => token
39        if app
40          hdlr = app.find_handler :is => :mount, :on => :all, :name => trail
41          return hdlr.process(request, response)
42        end
43      end
44
45      header = []
46      choose_header(reqh, header)
47      set_via(header)
48
49      http = Net::HTTP.new(env['server-name'], env['server-port'], @central.proxy_host, @central.proxy_port)
50      http.open_timeout = 10
51      http.read_timeout = 20
52      reqm = Net::HTTP.const_get(env['request-method'].capitalize)
53      debug "-> connecting to #{uri}", :since => start
54      resin = http.request(reqm.new(uri.request_uri, header), reqm::REQUEST_HAS_BODY ? request.body : nil) do |resin|
55        header = []
56        debug " > opened #{uri}", :since => start
57        choose_header(resin.to_hash, header)
58        set_via(header)
59
60        page = Page.new(uri, resin.code, header)
61        if page.converter and !DOMAINS.include?(env['server-name']) and @central.rewrite(page, resin)
62          info "*> rewriting #{page.location}", :since => start
63          output(page, response)
64        else
65          debug " > streaming #{page.location}", :since => start
66          response.status = resin.code.to_i
67          header.each { |k, v| response.header[k] = v }
68          response.send_plain_status
69          response.send_header
70          resin.read_body do |chunk|
71            response.write(chunk)
72          end
73        end
74      end
75      debug "-> finished #{uri}", :since => start
76      resin
77    end
78
79  end
80
81end
Note: See TracBrowser for help on using the browser.