# Copyright 2014 Shawn Neal # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'rexml/document' module WinRM # 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. class ResponseHandler # @param [String] The raw unparsed response body, if any # @param [Integer] The HTTP response status code def initialize(response_body, status_code) @response_body = response_body @status_code = status_code end # 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 def parse_to_xml() raise_if_error() response_xml end private 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 def raise_if_error() return if @status_code == 200 raise_if_auth_error() raise_if_soap_fault() raise_transport_error() end def raise_if_auth_error() raise WinRMAuthorizationError.new if @status_code == 401 end def raise_if_soap_fault() soap_errors = REXML::XPath.match(response_xml, "//#{NS_SOAP_ENV}:Body/#{NS_SOAP_ENV}:Fault/*") if !soap_errors.empty? fault = REXML::XPath.first(soap_errors, "//#{NS_WSMAN_FAULT}:WSManFault") raise WinRMWSManFault.new(fault.to_s, fault.attributes['Code']) end end def raise_transport_error() raise WinRMHTTPTransportError.new('Bad HTTP response returned from server', @status_code) end end end