Class: RatHole
- Inherits:
-
Object
- Object
- RatHole
- Defined in:
- lib/rat_hole.rb
Direct Known Subclasses
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(host, tidy = false) ⇒ RatHole
constructor
A new instance of RatHole.
- #process_server_response(rack_response, rack_request) ⇒ Object
- #process_user_request(rack_request) ⇒ Object
- #request_headers(env) ⇒ Object
- #tidy_html(body) ⇒ Object
- #whack_array(server_response) ⇒ Object
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
22 23 24 25 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 |
# File 'lib/rat_hole.rb', line 22 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) post.form_data = 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 |
#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
80 81 82 83 84 85 |
# File 'lib/rat_hole.rb', line 80 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
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rat_hole.rb', line 66 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
58 59 60 61 62 63 64 |
# File 'lib/rat_hole.rb', line 58 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 |