ncr / rack-proxy

A request/response rewriting HTTP proxy. A Rack app.
MIT License
269 stars 94 forks source link

Refactor creation of the response object #22

Closed rfletcher closed 11 years ago

rfletcher commented 11 years ago

Proxy#perform_request has gotten pretty big, specifically the target_response initialization. I've refactored that part a little, removing duplication.

The only meaningful change was renaming HttpStreamingResponse#status to #code, so that its API matches Net::HTTP#code. I've added an alias so that #status keeps woking, in case someone's using it externally.

timrwilliams commented 11 years ago

What version of ruby are you running this on? I believe for Ruby 1.9+ compatibility the body of the triplet needs to be wrapped in an array otherwise you will get an error on must handlers: Rack::Lint::LintError: Response body must respond to each

triplet = [target_response.code, headers, [target_response.body]]
rfletcher commented 11 years ago

I use 1.9.3.

You're right, the body needs to respond to #each, but target_response will be either an HttpStreamingResponse or a Net::HTTP instance. In both cases the value of #body already responds to #each. An Array shouldn't be necessary.

More generally, this pull request doesn't change anything about this method's behavior. It just simplifies the logic of target_response initialization.