lib/slinky/proxy_server.rb in slinky-0.8.0 vs lib/slinky/proxy_server.rb in slinky-0.8.1
- old
+ new
@@ -21,11 +21,18 @@
def self.find_matcher proxies, path
proxies.find{|p| path.start_with?(p[0])}
end
def self.rewrite_path path, proxy
- path.gsub(/^#{proxy[0]}/, "")
+ if proxy[0] == "/"
+ # If we're proxying everything, we just want to pass the path
+ # through unmodified. Otherwise we end up stripping the
+ # initial slash, which is the wrong behavior.
+ path
+ else
+ path.gsub(/^#{proxy[0]}/, "")
+ end
end
def self.replace_path http, old_path, new_path, addition
# TODO: This may fail in certain, rare cases
addition = addition[0..-2] if addition[-1] == "/"
@@ -42,28 +49,31 @@
Proxy.start(:host => "0.0.0.0", :port => port){|conn|
proxy = nil
start_time = nil
conn.server :slinky, :host => "127.0.0.1", :port => slinky_port
-
+ server = nil
+
conn.on_data do |data|
begin
- _, path = data.match(ProxyServer::HTTP_MATCHER)[1..2]
- proxy = ProxyServer.find_matcher(proxies, path)
- start_time = Time.now
- server = if proxy
- new_path = ProxyServer.rewrite_path path, proxy
- data = ProxyServer.replace_path(data, path, new_path, proxy[1].path)
- new_host = proxy[1].select(:host, :port).join(":")
- data = ProxyServer.replace_host(data, new_host)
- conn.server [proxy[1].host, proxy[1].port],
- :host => proxy[1].host, :port => proxy[1].port
- [proxy[1].host, proxy[1].port]
- else :slinky
- end
+ matches = data.match(ProxyServer::HTTP_MATCHER)
+ if matches
+ path = matches[2]
+ proxy = ProxyServer.find_matcher(proxies, path)
+ start_time = Time.now
+ server = if proxy
+ new_path = ProxyServer.rewrite_path path, proxy
+ data = ProxyServer.replace_path(data, path, new_path, proxy[1].path)
+ new_host = proxy[1].select(:host, :port).join(":")
+ data = ProxyServer.replace_host(data, new_host)
+ conn.server [proxy[1].host, proxy[1].port],
+ :host => proxy[1].host, :port => proxy[1].port
+ [proxy[1].host, proxy[1].port]
+ else :slinky
+ end
+ end
[data, [server]]
rescue
- $stderr.puts "Got error: #{$!}".foreground(:red)
conn.send_data "HTTP/1.1 500 Ooops...something went wrong\r\n"
end
end
conn.on_response do |server, resp|