Class: ESI::Proxy

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/esi/proxy.rb

Constant Summary collapse

SERVER =
"mongrel-esi/#{ESI::VERSION::STRING}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

#log, #log_debug, #log_error, #log_request, #msg

Constructor Details

#initialize(config) ⇒ Proxy

Returns a new instance of Proxy.



21
22
23
24
25
# File 'lib/esi/proxy.rb', line 21

def initialize( config )
  @config = config
  @router = config.router
  @cache_buffer = nil
end

Instance Attribute Details

#cache_bufferObject (readonly)

Returns the value of attribute cache_buffer.



17
18
19
# File 'lib/esi/proxy.rb', line 17

def cache_buffer
  @cache_buffer
end

#configObject (readonly)

Returns the value of attribute config.



17
18
19
# File 'lib/esi/proxy.rb', line 17

def config
  @config
end

#routerObject (readonly)

Returns the value of attribute router.



17
18
19
# File 'lib/esi/proxy.rb', line 17

def router
  @router
end

Instance Method Details

#process(request, response) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/esi/proxy.rb', line 27

def process(request, response)
 
  start = Time.now
  status = 200

  http_params = http_params(request.params)

  url = @router.url_for(request.params["REQUEST_URI"])
  #log_debug "#{request.params["REQUEST_METHOD"]} => #{url}"
  chunk_count = 0
  bytes_sent = 0
  uri = URI.parse(url)

  path_with_query = uri.query ? "#{uri.path}?#{uri.query}" :  uri.path
 
  if @config.cache.cached?( path_with_query, http_params )
    cache_buffer = @config.cache.get( path_with_query, http_params ).body
    bytes_sent = send_esi_buffered( status, response, request, http_params, cache_buffer )
  else
    proxy_request = (request.params["REQUEST_METHOD"] == "POST") ?
                        Net::HTTP::Post.new( path_with_query, http_params ) :
                        Net::HTTP::Get.new( path_with_query, http_params )

    # open the conneciton up so we can start to stream the connection
    Net::HTTP.start(uri.host, uri.port).request(proxy_request,request.body.read) do|proxy_response|
      chunk_count,bytes_sent = send_response( request, response, http_params, proxy_response )
    end # end request
 
    if !@control.nil? and !@cache_buffer.nil? and @control['max-age'].to_i > 0 and @cache_buffer.size > 0
      @cache_buffer.rewind
      @config.cache.put(path_with_query, http_params, @control['max-age'].to_i, @cache_buffer.read )
    end

  end

rescue => e
  STDERR.puts "\n#{e.message}: error at #{e.backtrace.first} msg at #{__FILE__}:#{__LINE__}\n"
ensure
  log_request "\n#{url}, #{Time.now - start} seconds with status #{status} and #{chunk_count} chunks, #{bytes_sent} bytes\n"
end