| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Revactor
Revision: 128
Author: tarcieri
Date: 29 Apr 2008 21:15:36
Changes:Allow block syntax for Actor HTTP requests with ensured auto-cleanup
Files:| ... | ...@@ -51,7 +51,7 @@ | |
| 51 | 51 | end |
| 52 | 52 | |
| 53 | 53 | # Perform an HTTP request for the given method and return a response object |
| 54 | def request(method, uri, options = {}, &block) | |
| 54 | def request(method, uri, options = {}) | |
| 55 | 55 | follow_redirects = options.has_key?(:follow_redirects) ? options[:follow_redirects] : true |
| 56 | 56 | uri = URI.parse(uri) |
| 57 | 57 | |
| ... | ...@@ -60,17 +60,29 @@ | |
| 60 | 60 | request_options = uri.is_a?(URI::HTTPS) ? options.merge(:ssl => true) : options |
| 61 | 61 | |
| 62 | 62 | client = connect(uri.host, uri.port) |
| 63 | response = client.request(method, uri.request_uri, request_options, &block) | |
| 63 | response = client.request(method, uri.request_uri, request_options) | |
| 64 | ||
| 65 | # Request complete | |
| 66 | unless follow_redirects and REDIRECT_STATUSES.include? response.status | |
| 67 | return response unless block_given? | |
| 68 | ||
| 69 | begin | |
| 70 | yield response | |
| 71 | ensure | |
| 72 | response.close | |
| 73 | end | |
| 74 | ||
| 75 | return | |
| 76 | end | |
| 64 | 77 | |
| 65 | return response unless follow_redirects and REDIRECT_STATUSES.include? response.status | |
| 66 | 78 | response.close |
| 67 | 79 | |
| 68 | 80 | location = response.headers['location'] |
| 69 | 81 | raise "redirect with no location header: #{uri}" if location.nil? |
| 70 | 82 | |
| 71 | # Append host to relative URLs | |
| 72 | if location[0] == '/' | |
| 73 | location = "#{uri.scheme}://#{uri.host}" << location | |
| 83 | # Convert path-based redirects to URIs | |
| 84 | unless /^[a-z]+:\/\// === location | |
| 85 | location = "#{uri.scheme}://#{uri.host}" << File.expand_path(location, uri.path) | |
| 74 | 86 | end |
| 75 | 87 | |
| 76 | 88 | uri = URI.parse(location) |