Class: Net::HTTP
- Inherits:
-
Object
- Object
- Net::HTTP
- Defined in:
- lib/fredo/ext/net_http.rb
Instance Method Summary collapse
- #connect_with_fredo ⇒ Object (also: #connect)
-
#request_with_fredo(request, body = nil, &block) ⇒ Object
(also: #request)
class << self def socket_type_with_fredo Fredo::StubSocket end alias_method :socket_type_without_fredo, :socket_type alias_method :socket_type, :socket_type_with_fredo end.
Instance Method Details
#connect_with_fredo ⇒ Object Also known as: connect
95 96 97 98 99 100 101 |
# File 'lib/fredo/ext/net_http.rb', line 95 def connect_with_fredo unless @@alredy_checked_for_net_http_replacement_libs ||= false # Fredo::Utility.puts_warning_for_net_http_replacement_libs_if_needed @@alredy_checked_for_net_http_replacement_libs = true end nil end |
#request_with_fredo(request, body = nil, &block) ⇒ Object Also known as: request
class << self
def socket_type_with_fredo
Fredo::StubSocket
end
alias_method :socket_type_without_fredo, :socket_type
alias_method :socket_type, :socket_type_with_fredo
end
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fredo/ext/net_http.rb', line 38 def request_with_fredo(request, body = nil, &block) request_body = body case request_body when nil then body = StringIO.new when String then body = StringIO.new(body) when File then body else body = StringIO.new(body.to_s) end uri = URI.parse(request.path) protocol = use_ssl? ? "https" : "http" full_path = "#{protocol}://#{self.address}:#{self.port}#{request.path}" rack_env ={'REQUEST_METHOD' => request.method, 'SCRIPT_NAME' => '', 'PATH_INFO' => uri.path, 'QUERY_STRING' => (uri.query || ''), 'SERVER_NAME' => self.address, 'SERVER_PORT' => self.port, 'rack.version' => [1,1], 'rack.url_scheme' => protocol, 'rack.input' => body, 'rack.errors' => $stderr, 'rack.multithread' => true, 'rack.multiprocess' => true, 'rack.run_once' => false} # @socket = Net::HTTP.socket_type.new # Perform the request status, header, body = Fredo.call(rack_env) response = Net::HTTPResponse.send(:response_class, "#{status}").new("1.0", "#{status}", body) response.instance_variable_set(:@body, body) header.each { |name, value| response[name] = value } response.instance_variable_set(:@read, true) def response.read_body(*args, &block) yield @body.join("\n") if block_given? @body.join("\n") end yield response if block_given? response rescue Fredo::NotFound raise Fredo::NetConnectNotAllowedError.new(full_path) unless Fredo.allow_net_connect? connect_without_fredo return request_without_fredo(request, request_body, &block) end |