-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
utf8 redirect breaks stuff #376
Comments
test |
@tomas , could you take a look? Unhandled error is a serious problem. If you have no time - we can do PR. We need new release with fix. Error handling - mandatory, redirect encoding - if possible. |
@tomas can we help anyhow? Fix of this issue is important for us. |
http headers are expected to be ISO-8859-1. the server sends the location header in utf-8, the http module interprets it as latin1 and we end up with mojibake. most browsers these days gracefully accept headers in utf-8 and don't break. i think it's worth doing the same. |
@yetzt note, bad headers is not a big deal. Problem is in unhandled exception and loss of promise end. First post contains all details how to fix with minimal efforts. |
if so, open a pull request. @puzrin |
Part 1: error
There is a server on the internet that shows following headers:
I'm trying to follow that redirect, and it fails:
It produces this:
This error is not intercepted by needle and is thrown into the void (or rather node.js own unhandledException handler).
Promise is never resolved or rejected, and user has no way of intercepting it.
Part 2: header encoding
Sometimes someone upstream makes a decision that every single downstream developer has to fix on their own. Sad fact of life. So...
Turns out, node.js itself returns headers in binary encoding, as explained here: nodejs/node#7928
But if you try to parse that URL, you'll get either incorrect result (with
url.parse
) or an exception (withnew URL
).Got resolved this by using
Buffer.from(res.headers.location, 'binary').toString()
instead ofres.headers.location
as shown here: sindresorhus/got#214Needle fails here:
needle/lib/needle.js
Line 582 in b4913a5
So instead of:
It should be:
User should probably get fixed location header as well (shouldn't break api because it was never working in the first place):
needle/lib/needle.js
Line 555 in b4913a5
Part 3: intercepting an error
Even if we fix encoding on a legitimate redirect, someone can intentionally or accidentally throw in something that breaks
new URL
syntax.So I believe the call to
new URL
should be wrapped intry..catch
and error thrown whenever request errors would normally go.The text was updated successfully, but these errors were encountered: