Class: RatHole

Inherits:
Object
  • Object
show all
Defined in:
lib/rat_hole.rb

Direct Known Subclasses

EmptyRatHole

Instance Method Summary collapse

Constructor Details

#initialize(host, tidy = false) ⇒ RatHole

Returns a new instance of RatHole.


9
10
11
12
# File 'lib/rat_hole.rb', line 9

def initialize(host, tidy=false)
  @host = host
  @tidy = tidy
end

Instance Method Details

#call(env) ⇒ Object


26
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
# File 'lib/rat_hole.rb', line 26

def call(env)
  Net::HTTP.start(@host) do |http|
    http.instance_eval{@socket = MethodSpy.new(@socket){|method| method =~ /#{ENV['RH_METHOD_SPY_FILTER']}/}} if $DEBUG
    env.delete('HTTP_ACCEPT_ENCODING')
    source_request = Rack::Request.new(env)
    process_user_request(source_request)
    source_headers = request_headers(source_request.env)
    
    if source_request.query_string.nil? || source_request.query_string == ''
      request_uri = source_request.path_info
    else
      request_uri = "#{source_request.path_info}?#{source_request.query_string}"
    end

    if source_request.get?
      response = http.get(request_uri, source_headers)
    elsif source_request.post?
      post = Net::HTTP::Post.new(request_uri, source_headers)
      prepare_post(source_request, post)
      response = http.request(post)
    end

    code = response.code.to_i
    headers = response.to_hash
    body = response.body || ''
    headers.delete('Transfer-Encoding')

    server_response = Rack::Response.new(body, code, headers)
    whack_array(server_response)
    tidy_html(body) if server_response["Content-Type"] =~ /text\/html/ && @tidy
    
    process_server_response(server_response, source_request)
    server_response.finish
  end
end

#prepare_post(request, post) ⇒ Object


22
23
24
# File 'lib/rat_hole.rb', line 22

def prepare_post(request, post)
  post.form_data = request.POST
end

#process_server_response(rack_response, rack_request) ⇒ Object


18
19
20
# File 'lib/rat_hole.rb', line 18

def process_server_response(rack_response, rack_request)
  rack_response
end

#process_user_request(rack_request) ⇒ Object


14
15
16
# File 'lib/rat_hole.rb', line 14

def process_user_request(rack_request)
  rack_request
end

#request_headers(env) ⇒ Object


84
85
86
87
88
89
# File 'lib/rat_hole.rb', line 84

def request_headers(env)
  env.select{|k,v| k =~ /^HTTP/}.inject({}) do |h, e|
    k,v = e
    h.merge(k.split('_')[1..-1].join('-').to_camel_case => v)
  end
end

#tidy_html(body) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rat_hole.rb', line 70

def tidy_html(body)
  if `which tidy` == ''
    $stderr.puts "tidy not found in path"
    return
  end
  tidied, errors = Open3.popen3('tidy -ascii') do |stdin, stdout, stderr|
    stdin.print body
    stdin.close
    [stdout.read, stderr.read]
  end
  $stderr.puts errors if $DEBUG
  body.replace(tidied) if tidied != ""
end

#whack_array(server_response) ⇒ Object


62
63
64
65
66
67
68
# File 'lib/rat_hole.rb', line 62

def whack_array(server_response)
  server_response.headers.each do |k, v|
    if v.is_a?(Array) && v.size == 1
      server_response[k] = v.first
    end
  end
end