On Oct 1, 10:15 am, Phrogz <phr... / mac.com> wrote:
> I have a script that pulls pages from our wiki server. It was working
> using Net:HTTP and open-uri with basic_authentication, but our
> sysadmin disabled basic authentication and left NTLM as the only
> authentication method.
>
> Instead of trying to figure out how to use the Ruby NTLM library, I
> decide to just use curl. It was working nicely for the HTML pages
> using this form:
>   def fetch_http_ntlm( url )
>     `curl #{url} --ntlm -# -u #{USER}:#{PASS}`
>   end
>
> However, the above fails for binary files. (Pulling down images
> embedded in pages.) So I had to switch it to this:
>   def fetch_http_ntlm( url )
>     file_name = "C:\\tmp_#{Time.new.to_i}"
>     `curl #{url} --ntlm -# -u #{USER}:#{PASS} -o #{file_name}`
>     raw = File.open( file_name, 'rb' ){ |f| f.read }
>     File.delete( file_name )
>     raw
>   end
>
> In other words, I have curl write the output to a file, and then read
> in the file using binary mode, and delete the file.
>
> Should I have to do this? Is it a general problem that commands can't
> cleanly return binary data to the 'console', and hence can't be
> captured using the above format? Or is curl on Windows at fault, and
> should be doing something different? Or is Ruby Windows at fault? Or
> is Windows itself at fault?

Followup - this does not seem to be a core problem of terminal
commands returning binary data, or a core failing of Ruby. From my OS
X box at home:

  Slim2:~/Desktop phrogz$ cat send_bytes.rb
  print [13,7,129,250,0,70,111,111].map{ |b| b.chr }.join

  Slim2:~/Desktop phrogz$ cat get_bytes.rb
  result = `ruby send_bytes.rb`
  p result.length, result

  Slim2:~/Desktop phrogz$ ruby get_bytes.rb
  8
  "\r\a\201\372\000Foo"

This is also not a problem with curl (at least on *nix):

  Slim2:~/Desktop phrogz$ curl -s -O http://phrogz.net/tmp/gkhead.jpg
  Slim2:~/Desktop phrogz$ irb
  irb(main):001:0> good = IO.read( 'gkhead.jpg' ); good.length
  => 21443
  irb(main):002:0> url = 'http://phrogz.net/tmp/gkhead.jpg'
  => "http://phrogz.net/tmp/gkhead.jpg"
  irb(main):003:0> test = `curl -s #{url}`; test.length
  => 21443
  irb(main):004:0> test == good
  => true

Tomorrow I'll see which of the above fails back on my Windows box.
Glad this isn't a fundamental Ruby or shell workflow problem, anyhow.