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(url, 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
67
68
69
70
71
72
73
74
# File 'lib/esi/proxy.rb', line 27

def process(url, request, response)
 
  status = 200

  http_params = http_params(request.params)

  chunk_count = 0
  bytes_sent = 0
  sent_from_cache = false
  uri = URI.parse(url)

  path_with_query = uri.query ? "#{uri.path}?#{uri.query}" :  uri.path
 
  # check if the origin is cached
  cached_page = @config.cache.get( path_with_query, http_params )
  if cached_page and cached_page.valid?
    cache_buffer = cached_page.body
    head, body = cache_buffer.split("\r\n\r\n")
    buffer = StringIO.new
    bytes_sent = send_esi_buffered( buffer, request, http_params, body )
    buffer.rewind
    bytes_sent = buffer.size
    #puts bytes_sent.inspect
    head = head.sub(/Content-Length:.*$/,"Content-Length: #{bytes_sent}")
    #puts head
    response.write head
    response.write "\r\n\r\n"
    response.write buffer.read
    response.done = true if response.respond_to?(:done)
    sent_from_cache = true
  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
  [chunk_count, bytes_sent, status, sent_from_cache]
end