Skip to content

Commit

Permalink
Fixed proxy post bugs. Location header is now rewritten also
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Jolley committed Jul 1, 2008
1 parent 77bc8ae commit 6600537
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/sproutcore/merb/bundle_controller.rb
Expand Up @@ -3,6 +3,9 @@
require 'uri'

module SproutCore

NO_BODY_METHOD = [:delete, :get, :copy, :head, :move, :options, :trace]

module Merb

# A subclass of this controller handles all incoming requests for the
Expand Down Expand Up @@ -153,13 +156,23 @@ def handle_proxy(url, proxy_url, opts ={})

# now make the request...
response = nil

# Handle those that require a body.

::Net::HTTP.start(http_host, http_port) do |http|
response = http.send(http_method, http_path, headers)
if NO_BODY_METHOD.include?(http_method.to_sym)
response = http.send(http_method, http_path, headers)
else
http_body = request.raw_post
response = http.send(http_method, http_path, http_body, headers)
end
end

# Now set the status, headers, and body.
@status = response.code

SC.logger.debug " ~ PROXY: #{@status} #{request.uri} -> http://#{http_host}:#{http_port}#{http_path}"

# Transfer response headers into reponse
ignore = ['transfer-encoding', 'keep-alive', 'connection']
response.each do | key, value |
Expand All @@ -172,11 +185,19 @@ def handle_proxy(url, proxy_url, opts ={})
value.gsub!(/domain=[^\;]+\;? ?/,'')
end

# Location headers should rewrite the hostname if it is included.
if key.downcase == 'location'
value.gsub!(/^http:\/\/#{http_host}(:[0-9]+)?\//, "http://#{request.host}/")
end

# Prep key and set header.
key = key.split('-').map { |x| x.downcase.capitalize }.join('-')
SC.logger.debug " #{key}: #{value}"
@headers[key] = value
end

SC.logger.debug ''

# Transfer response body
return response.body
end
Expand Down

0 comments on commit 6600537

Please sign in to comment.