Class: Rack::TranslatingProxy

Inherits:
Proxy
  • Object
show all
Defined in:
lib/rack/translating_proxy.rb

Instance Method Summary collapse

Instance Method Details

#_request_mapppingObject



95
96
97
# File 'lib/rack/translating_proxy.rb', line 95

def _request_mappping
  @_request_mappping ||= request_mappping
end

#_response_mappingObject



99
100
101
# File 'lib/rack/translating_proxy.rb', line 99

def _response_mapping
  @_response_mapping ||= request_mapping.invert
end

#_target_hostObject



103
104
105
# File 'lib/rack/translating_proxy.rb', line 103

def _target_host
  @_target_host ||= URI(target_host)
end

#io(stringy_thing) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/rack/translating_proxy.rb', line 54

def io(stringy_thing)
  if stringy_thing.respond_to?(:read)
    stringy_thing
  else
    StringIO.new(stringy_thing)
  end
end

#remove_interfering_response_headers(headers) ⇒ Object



79
80
81
82
83
# File 'lib/rack/translating_proxy.rb', line 79

def remove_interfering_response_headers(headers)
  headers.reject! do |key, _|
    %w(status connection transfer-encoding).include?(key)
  end
end

#request_mappingObject



91
92
93
# File 'lib/rack/translating_proxy.rb', line 91

def request_mapping
  fail 'Not implemented'
end

#rewrite_env(env) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/rack/translating_proxy.rb', line 5

def rewrite_env(env)
  rewrite_request_url env
  rewrite_request_body env
  rewrite_request_query_string env
  rewrite_request_path env

  env
end

#rewrite_request_body(env) ⇒ Object



20
21
22
23
24
25
# File 'lib/rack/translating_proxy.rb', line 20

def rewrite_request_body(env)
  rewritten_input = rewrite_request_string(io(env['rack.input']).read)

  env['rack.input']     = io(rewritten_input)
  env['CONTENT_LENGTH'] = rewritten_input.size
end

#rewrite_request_path(env) ⇒ Object



37
38
39
40
# File 'lib/rack/translating_proxy.rb', line 37

def rewrite_request_path(env)
  env['PATH_INFO']   = rewrite_request_string(env['PATH_INFO'])
  env['REQUEST_URI'] = rewrite_request_string(env['REQUEST_URI'])
end

#rewrite_request_query_string(env) ⇒ Object



33
34
35
# File 'lib/rack/translating_proxy.rb', line 33

def rewrite_request_query_string(env)
  env['QUERY_STRING'] = rewrite_request_string(env['QUERY_STRING'])
end

#rewrite_request_string(str) ⇒ Object



27
28
29
30
31
# File 'lib/rack/translating_proxy.rb', line 27

def rewrite_request_string(str)
  rewrite_string(
    str, request_mapping, URI.method(:encode_www_form_component)
  )
end

#rewrite_request_url(env) ⇒ Object



14
15
16
17
18
# File 'lib/rack/translating_proxy.rb', line 14

def rewrite_request_url(env)
  env['HTTP_HOST']       = _target_host.host
  env['SERVER_PORT']     = _target_host.port
  env['rack.url_scheme'] = _target_host.scheme
end

#rewrite_response(triplet) ⇒ Object



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

def rewrite_response(triplet)
  status, headers, body = triplet
  rewrite_response_location headers
  remove_interfering_response_headers headers
  body = rewrite_response_body(body)
  headers['Content-Length'] = body.size.to_s
  [status, headers, [body]]
end

#rewrite_response_body(body) ⇒ Object



85
86
87
88
89
# File 'lib/rack/translating_proxy.rb', line 85

def rewrite_response_body(body)
  str = rewrite_string(body.to_s, _response_mapping)
  rewrite_string(str, _response_mapping,
                 URI.method(:encode_www_form_component))
end

#rewrite_response_location(headers) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/rack/translating_proxy.rb', line 71

def rewrite_response_location(headers)
  return unless headers['location']

  headers['location'] = headers['location'].map do |location|
    rewrite_string(location, _response_mapping)
  end
end

#rewrite_string(string, mapping, transform = proc { |x| x }) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rack/translating_proxy.rb', line 42

def rewrite_string(string, mapping, transform = proc { |x| x })
  mapping = mapping.map do |source, target|
    [transform[source], transform[target]]
  end

  mapping.each do |source, target|
    string = string.gsub(source, target)
  end

  string
end

#target_hostObject



107
108
109
# File 'lib/rack/translating_proxy.rb', line 107

def target_host
  fail 'Not implemented'
end