class Warc::Proxy::Replay

Public Class Methods

new(app=nil,warc=nil) click to toggle source
Calls superclass method
# File lib/warc/proxy/proxy.rb, line 28
def initialize(app=nil,warc=nil)
  super(app)
  @warc = ::Warc.open_stream(warc)
  @index = {}
  puts "Building index"
  @warc.each do |record|
    if record.header["warc-type"] == "response"
      @index[record.header.uri] = record.offset
    end
  end
  puts "Indexing done"
end
start(warc,port) click to toggle source
# File lib/warc/proxy/proxy.rb, line 73
def self.start(warc,port)
  # Run the app!
  app = Rack::Builder.new {
    run Warc::Proxy::Replay.new(nil,warc)
  }
  puts "Starting proxy server on port #{port}"
  ::Rack::Server.start(:app => app,:Port => port,:server=>:thin)
end

Public Instance Methods

call(env) click to toggle source
Calls superclass method
# File lib/warc/proxy/proxy.rb, line 41
def call(env)
  # Send to Sinatra app
  if env["HTTP_HOST"] == "warc"
    super(env)
    # Or serve from archive
  else
    serve(env)
  end
end
http_response(record) click to toggle source
# File lib/warc/proxy/proxy.rb, line 61
def http_response(record)
  io = StringIO.new(record.content)
  headers = {}
  /^HTTP\/(\d\.\d) (\d++) (.*)/.match(io.readline)
  code = $2

  while m = /^(.*): (.*)/.match(io.readline)
    headers[m.captures[0]] = m.captures[1].chomp("\r")
  end
  [code,headers,io]
end
serve(env) click to toggle source
# File lib/warc/proxy/proxy.rb, line 51
def serve(env)
  uri = "http://#{env['HTTP_HOST']}#{env['REQUEST_URI']}"
  if @index.key?(uri)
    record = @warc.record(@index[uri])
    return http_response(record)
  else
    return [404,{"Content-Type" => "text/html"},["not found"]]
  end
end