tisba / faraday-follow-redirects Goto Github PK
View Code? Open in Web Editor NEWFaraday 1.x and 2.x compatible extraction of FaradayMiddleware::FollowRedirects
License: MIT License
Faraday 1.x and 2.x compatible extraction of FaradayMiddleware::FollowRedirects
License: MIT License
The Metainspector gem uses Faraday as a dependency and a bug was reported there which seems to be related to this gem.
jaimeiniesta/metainspector#414
Summary:
The redirect response changes the encoding from UTF-8 to ASCII-8BIT
Steps to reproduce:
irb
require 'faraday'
require 'faraday/follow_redirects'
require 'faraday-cookie_jar'
session = Faraday.new(url: 'https://apps.apple.com/us/app/id1501495423') do |faraday|
faraday.use Faraday::FollowRedirects::Middleware
faraday.use :cookie_jar
end
session.get
This is the error I got:
/home/ma/.rbenv/versions/3.2.2/lib/ruby/3.2.0/unicode_normalize/normalize.rb:141:in `normalize': Unicode Normalization not appropriate for ASCII-8BIT (Encoding::CompatibilityError)
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/domain_name-0.6.20231109/lib/domain_name.rb:292:in `unicode_normalize'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/domain_name-0.6.20231109/lib/domain_name.rb:292:in `normalize'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/domain_name-0.6.20231109/lib/domain_name.rb:100:in `initialize'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie.rb:553:in `new'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie.rb:553:in `acceptable_from_uri?'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie.rb:592:in `valid_for_uri?'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie_jar/hash_store.rb:82:in `block (3 levels) in each'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie_jar/hash_store.rb:76:in `delete_if'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie_jar/hash_store.rb:76:in `block (2 levels) in each'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie_jar/hash_store.rb:74:in `each'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie_jar/hash_store.rb:74:in `block in each'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie_jar/hash_store.rb:73:in `each'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie_jar/hash_store.rb:73:in `each'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie_jar.rb:163:in `each'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie_jar.rb:131:in `each'
from /home/ma/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/http-cookie-1.0.5/lib/http/cookie_jar.rb:131:in `sort'
... 14 levels...
From a Rails app, I got the same error, this is the full stack of errors:
[/home/ma/.rbenv/versions/3.2.2/lib/ruby/3.2.0/unicode_normalize/normalize.rb:141:in `normalize'](http://localhost:3000/#)
[domain_name (0.6.20231109) lib/domain_name.rb:292:in `unicode_normalize'](http://localhost:3000/#)
[domain_name (0.6.20231109) lib/domain_name.rb:292:in `normalize'](http://localhost:3000/#)
[domain_name (0.6.20231109) lib/domain_name.rb:100:in `initialize'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie.rb:553:in `new'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie.rb:553:in `acceptable_from_uri?'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie.rb:592:in `valid_for_uri?'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie_jar/hash_store.rb:82:in `block (3 levels) in each'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie_jar/hash_store.rb:76:in `delete_if'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie_jar/hash_store.rb:76:in `block (2 levels) in each'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie_jar/hash_store.rb:74:in `each'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie_jar/hash_store.rb:74:in `block in each'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie_jar/hash_store.rb:73:in `each'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie_jar/hash_store.rb:73:in `each'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie_jar.rb:163:in `each'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie_jar.rb:131:in `each'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie_jar.rb:131:in `sort'](http://localhost:3000/#)
[http-cookie (1.0.5) lib/http/cookie_jar.rb:131:in `cookies'](http://localhost:3000/#)
[faraday-cookie_jar (0.0.7) lib/faraday/cookie_jar.rb:12:in `call'](http://localhost:3000/#)
[faraday-follow_redirects (0.3.0) lib/faraday/follow_redirects/middleware.rb:77:in `perform_with_redirection'](http://localhost:3000/#)
[faraday-follow_redirects (0.3.0) lib/faraday/follow_redirects/middleware.rb:85:in `block in perform_with_redirection'](http://localhost:3000/#)
[faraday (2.7.12) lib/faraday/response.rb:42:in `on_complete'](http://localhost:3000/#)
[faraday-follow_redirects (0.3.0) lib/faraday/follow_redirects/middleware.rb:79:in `perform_with_redirection'](http://localhost:3000/#)
[faraday-follow_redirects (0.3.0) lib/faraday/follow_redirects/middleware.rb:65:in `call'](http://localhost:3000/#)
[faraday (2.7.12) lib/faraday/rack_builder.rb:153:in `build_response'](http://localhost:3000/#)
[faraday (2.7.12) lib/faraday/connection.rb:444:in `run_request'](http://localhost:3000/#)
[faraday (2.7.12) lib/faraday/connection.rb:200:in `get'](http://localhost:3000/#)
I could not find exactly the source of the issue, so I don't really have a fully thought-out solution for this but I could fix this issue by changing line 93 of lib/faraday/follow_redirects.rb:
From:
redirect_to_url = safe_escape(response['location'] || '')
To:
redirect_to_url = safe_escape(response['location'].force_encoding("UTF-8") || '')
Same behaviour happens with other urls that will trigger the redirecting behaviour, like for example https://nytimes.com
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.