@@ -6,6 +6,7 @@ class ReverseProxy
6
6
def initialize ( app = nil , &b )
7
7
@app = app || lambda { [ 404 , [ ] , [ ] ] }
8
8
@paths = { }
9
+ @opts = { :preserve_host => false }
9
10
instance_eval &b if block_given?
10
11
end
11
12
@@ -21,7 +22,7 @@ def call(env)
21
22
headers [ $1] = value
22
23
end
23
24
}
24
- headers [ 'HOST' ] = uri . host
25
+ headers [ 'HOST' ] = uri . host if @opts [ :preserve_host ]
25
26
26
27
session = Net ::HTTP . new ( uri . host , uri . port )
27
28
session . use_ssl = ( uri . scheme == 'https' )
@@ -31,8 +32,10 @@ def call(env)
31
32
case m
32
33
when "GET" , "HEAD" , "DELETE" , "OPTIONS" , "TRACE"
33
34
req = Net ::HTTP . const_get ( m . capitalize ) . new ( uri . request_uri , headers )
35
+ req . basic_auth @opts [ :username ] , @opts [ :password ] if @opts [ :username ] and @opts [ :password ]
34
36
when "PUT" , "POST"
35
37
req = Net ::HTTP . const_get ( m . capitalize ) . new ( uri . request_uri , headers )
38
+ req . basic_auth @opts [ :username ] , @opts [ :password ] if @opts [ :username ] and @opts [ :password ]
36
39
req . content_length = rackreq . body . length
37
40
req . body_stream = rackreq . body
38
41
else
@@ -70,6 +73,8 @@ def create_response_headers http_response
70
73
response_headers = Rack ::Utils ::HeaderHash . new ( http_response . to_hash )
71
74
# handled by Rack
72
75
response_headers . delete ( 'status' )
76
+ # TODO: figure out how to handle chunked responses
77
+ response_headers . delete ( 'transfer-encoding' )
73
78
# TODO: Verify Content Length, and required Rack headers
74
79
response_headers
75
80
end
@@ -91,9 +96,10 @@ def get_uri(url, matcher, path)
91
96
end
92
97
end
93
98
94
- def reverse_proxy matcher , url
99
+ def reverse_proxy matcher , url , opts = { }
95
100
raise GenericProxyURI . new ( url ) if matcher . is_a? ( String ) && URI ( url ) . class == URI ::Generic
96
101
@paths . merge! ( matcher => url )
102
+ @opts . merge! ( opts )
97
103
end
98
104
end
99
105
0 commit comments