Class: Warc::Proxy::Replay

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/warc/proxy/proxy.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, warc = nil) ⇒ Replay

Returns a new instance of Replay.



28
29
30
31
32
33
34
35
36
37
38
39
# 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

Class Method Details

.start(warc, port) ⇒ Object



73
74
75
76
77
78
79
80
# 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

Instance Method Details

#call(env) ⇒ Object



41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
# 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