WinRb / WinRM

Ruby library for Windows Remote Management
Apache License 2.0
411 stars 117 forks source link

Strange problem with a WMI query and non ascii characters #187

Closed Mazwak closed 8 years ago

Mazwak commented 8 years ago

Following #143 , I now have a problem I can't debug.

I'm still using the same query : ASSOCIATORS OF {Win32_Directory.Name="#{path}"} WHERE AssocClass = Win32_Subdirectory ResultRole = PartComponent

When the path contains 'é', and most likely other accented characters, I get an error : WinRM/lib/winrm/http/response_handler.rb:79:inraise_transport_error': Bad HTTP response returned from server (400). (WinRM::WinRMHTTPTransportError)`

The SOAP response is indeed bad, in fact totally empty :

#<HTTP::Message:0x00556505f40be0 
  @http_header=
    #<HTTP::Message::Headers:0x00556505f40bb8
      @http_version="1.1",
      @body_size=0,
      @chunked=false,
      @request_method="POST",
      @request_uri=#<URI::HTTP http://shydfiler:5985/wsman>,
      @request_query=nil,
      @request_absolute_uri=nil,
      @status_code=400,
      @reason_phrase=nil,
      @body_type=nil,
      @body_charset=nil,
      @body_date=nil,
      @body_encoding=nil,
      @is_request=false,
      @header_item=[["Server", "Microsoft-HTTPAPI/2.0"], ["Date", "Fri, 19 Feb 2016 17:00:39 GMT"], ["Connection", "close"], ["Content-Length", "0"]],
      @dumped=false>,
    @peer_cert=nil,
    @http_body=#<HTTP::Message::Body:0x00556505f40b40
      @body="",
      @size=0,
      @positions=nil,
      @chunk_size=nil>,
    @previous=nil>

The exact query is : ASSOCIATORS OF {Win32_Directory.Name="e:\\Maldi-Tof\\ESEC\\doc périmés"} WHERE AssocClass = Win32_Subdirectory ResultRole = PartComponent

Running the query remotely from windows seven works.

I've been searching the whole afternoon, but to no avail.

Do you have an idea ?

sneal commented 8 years ago

Have you tried playing with the locale option?

Mazwak commented 8 years ago

Locale did not change anything.

However, after 3 hours, I got it working. I think it comes from the message size, where you use the number of characters, instead of the size in bytes.

In http_transport, line 168 : original_length = message.bytesize

Does it looks good to you ?

sneal commented 8 years ago

Ah hah! So we have some double byte characters causing the message length != message byte size I presume.

Mazwak commented 8 years ago

Yes, as UTF-8 uses 1 to 4 bytes for a single characters.

'é' uses 2, in "Docs périmés".

I did not search winrm documentation, but it is most likely expecting the size of the message in bytes, and not the number of character. Which makes sense.

sneal commented 8 years ago

I found this in the WSMV spec: Content-Length: Contains the size, in bytes, of the message body. It MUST be present. Do you mind sending a PR?

Mazwak commented 8 years ago

Done