Class: Net::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/win32/sspi/http_proxy_patch.rb

Overview

Replaces Net::HTTP.request to understand Negotiate HTTP proxy authorization. Uses native Win32 libraries to authentic as the current user (as defined by the ENV and ENV environment variables.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sspi?Boolean

Indicates that net/http has been patched by SSPI.

Returns:

  • (Boolean)


26
27
28
# File 'lib/win32/sspi/http_proxy_patch.rb', line 26

def self.sspi?
	true
end

Instance Method Details

#request(req, body = nil, &block) ⇒ Object

:yield: response



34
35
36
37
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/win32/sspi/http_proxy_patch.rb', line 34

def request(req, body = nil, &block) # :yield: +response+
  unless started?
    start {
      req['connection'] ||= 'close'
      return request(req, body, &block)
    }
  end
  if proxy_user()
    unless use_ssl?
      req.proxy_basic_auth proxy_user(), proxy_pass()
    end
  end

  req.set_body_internal body
  begin_transport req
    req.exec @socket, @curr_http_version, edit_path(req.path)
    begin
      res = HTTPResponse.read_new(@socket)
    end while res.kind_of?(HTTPContinue)
    # If proxy was specified, and the server is demanding authentication, negotiate with it
    if res.kind_of?(HTTPProxyAuthenticationRequired) && proxy? && res["Proxy-Authenticate"].include?("Negotiate")
      begin
        n = Win32::SSPI::NegotiateAuth.new
        res.reading_body(@socket, req.response_body_permitted?) { }
        end_transport req, res
        begin_transport req
        req["Proxy-Authorization"] = "Negotiate #{n.get_initial_token}"
        # Some versions of ISA will close the connection if this isn't present.
        req["Connection"] = "Keep-Alive"
        req["Proxy-Connection"] = "Keep-Alive"
        req.exec @socket, @curr_http_version, edit_path(req.path)
        begin
          res = HTTPResponse.read_new(@socket)
        end while res.kind_of?(HTTPContinue)
        if res["Proxy-Authenticate"]
          res.reading_body(@socket, req.response_body_permitted?) { }
          req["Proxy-Authorization"] = "Negotiate #{n.complete_authentication res["Proxy-Authenticate"]}"
          req["Connection"] = "Keep-Alive"
          req["Proxy-Connection"] = "Keep-Alive"
          req.exec @socket, @curr_http_version, edit_path(req.path)
          begin
            res = HTTPResponse.read_new(@socket)
          end while res.kind_of?(HTTPContinue)
        end
      rescue
        exc = $!.exception("Error occurred during proxy negotiation. socket.io: #{@socket.inspect}; socket.closed? #{@socket.closed?}; req: #{req.inspect}; res: #{res.inspect}; Original message: #{$!.message}")
        exc.set_backtrace $!.backtrace
        raise exc
      end
    end
    res.reading_body(@socket, req.response_body_permitted?) {
      yield res if block_given?
    }
  end_transport req, res

  res
end