feduxorg / proxy_pac_rb

proxy_pac _rb is a gem to test, compress, lint, and parse proxy auto-config files
https://github.com/fedux-org/proxy_pac_rb
MIT License
3 stars 2 forks source link

encoding issue on MRI ruby #7

Open net1957 opened 7 years ago

net1957 commented 7 years ago

on linux with MRI ruby 2.2.7-p470 i got encoding errors.

If you use a proxy file from a URL http://xxxxx/myproxy.pac it it returned as 'ASCII-8BIT' but it is really 'UTF-8' encoded. If you have some non ascii characters in the script it will fail.

I don't know if all servers have this issue or if it is related to excon gem (see https://github.com/excon/excon/issues/606) or other code.

I see you have already some test for other ruby flavors. I did simply use this monkey patch as a workaround:

# patch to correct utf8 string wrongly encoded as ASCII-8BIT
# tested on linux with ruby 2.2.7-p470
module ProxyPacRb
  # Encodes strings as UTF-8
  module Encoding
    def encode(string)
      if string.encoding.name == 'ASCII-8BIT'
        data = string.dup
        data.force_encoding('UTF-8')

        unless data.valid_encoding?
          raise ::Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8"
        end
      else
        data = string.encode('UTF-8')
      end
      data
    end
  end
end

What do you think about that ? Have you better way to solve this ?

Regards

@maxmeyer

maxmeyer commented 7 years ago

I'm not sure in what constellation - gems, tools etc. you get this error. Is the proxy.pac middleware involved? Can you put together a PR with a failing test?

maxmeyer commented 7 years ago

Do I understand it correctly, that you get the error while downloading the file? I use middleman to compile the proxy.pac, deploy it to a docroot and serve it via apache

maxmeyer commented 7 years ago

So there's noch ruby involved during download.

net1957 commented 7 years ago

on excon i found this bug https://github.com/excon/excon/issues/606 Perhaps it's related

I developed a little rails application to check the result of proxy script urls given by the user I use this in my code on a rails application: ip, name and url are user entries

env = ProxyPacRb::Environment.new(client_ip: ip)
script = ProxyPacRb::Parser.new(environment: env).parse(name)
script.find(url)

name can contains a script or a url pointing to a script

maxmeyer commented 7 years ago

Can you provide a dump of the exception? What you could do for now is to fetch the file "yourself" and just provide the content to the gem.

The problem is this: This gem is directly related to my daily work and I'm still working with my boss on the permission to work on Open Source Software during business hours/in my free time although it's related to my work.

So I would be very happy to merge a PR fixing this, but I cannot provide a PR for the time being.