class WinRM::ResponseHandler

Handles the raw WinRM HTTP response. Returns the body as an XML doc or raises the appropriate WinRM error if the response is an error.

Public Class Methods

new(response_body, status_code) click to toggle source

@param [String] The raw unparsed response body, if any @param [Integer] The HTTP response status code

# File lib/winrm/http/response_handler.rb, line 24
def initialize(response_body, status_code)
  @response_body = response_body
  @status_code = status_code
end

Public Instance Methods

parse_to_xml() click to toggle source

Processes the response from the WinRM service and either returns an XML doc or raises an appropriate error.

@returns [REXML::Document] The parsed response body

# File lib/winrm/http/response_handler.rb, line 33
def parse_to_xml
  raise_if_error
  response_xml
end

Private Instance Methods

raise_if_auth_error() click to toggle source
# File lib/winrm/http/response_handler.rb, line 55
def raise_if_auth_error
  fail WinRMAuthorizationError if @status_code == 401
end
raise_if_error() click to toggle source
# File lib/winrm/http/response_handler.rb, line 47
def raise_if_error
  return if @status_code == 200
  raise_if_auth_error
  raise_if_wsman_fault
  raise_if_wmi_error
  raise_transport_error
end
raise_if_wmi_error() click to toggle source
# File lib/winrm/http/response_handler.rb, line 66
def raise_if_wmi_error
  soap_errors = REXML::XPath.match(response_xml, "//#{NS_SOAP_ENV}:Body/#{NS_SOAP_ENV}:Fault/*")
  return if soap_errors.empty?

  error = REXML::XPath.first(soap_errors, "//#{NS_WSMAN_MSFT}:MSFT_WmiError")
  return if error.nil?

  error_code = REXML::XPath.first(error, "//#{NS_WSMAN_MSFT}:error_Code").text
  fail WinRMWMIError.new(error.to_s, error_code)
end
raise_if_wsman_fault() click to toggle source
# File lib/winrm/http/response_handler.rb, line 59
def raise_if_wsman_fault
  soap_errors = REXML::XPath.match(response_xml, "//#{NS_SOAP_ENV}:Body/#{NS_SOAP_ENV}:Fault/*")
  return if soap_errors.empty?
  fault = REXML::XPath.first(soap_errors, "//#{NS_WSMAN_FAULT}:WSManFault")
  fail WinRMWSManFault.new(fault.to_s, fault.attributes['Code']) unless fault.nil?
end
raise_transport_error() click to toggle source
# File lib/winrm/http/response_handler.rb, line 77
def raise_transport_error
  fail WinRMHTTPTransportError.new('Bad HTTP response returned from server', @status_code)
end
response_xml() click to toggle source
# File lib/winrm/http/response_handler.rb, line 40
def response_xml
  @response_xml ||= REXML::Document.new(@response_body)
rescue REXML::ParseException => e
  raise WinRMHTTPTransportError.new(
    "Unable to parse WinRM response: #{e.message}", @status_code)
end