Skip to content
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

Is it possible to prevent the default URL decoding? #167

Closed
ghost opened this issue Jun 22, 2021 · 3 comments
Closed

Is it possible to prevent the default URL decoding? #167

ghost opened this issue Jun 22, 2021 · 3 comments

Comments

@ghost
Copy link

ghost commented Jun 22, 2021

The percent encodings (such as %2B) are decoded automatically when a request is received in the polka server.

Original request URL: http://localhost:5000/proxy_path?query=N4IgLgng%2B
Received request URL: http://localhost:5000/proxy_path?query=N4IgLgng+

I don't want the URL decoding to happen automatically as I want to proxy the original request as such to a different server. How can I achieve this?

Sample server code:

const polka = require("polka");
const app = polka();

app.use((req, res, next) => {
    console.log(req.url);
});

app.listen(5000, () => {
    console.log('server listening on 5000');
});
@ghost
Copy link
Author

ghost commented Jun 23, 2021

I was running some experiments and found a weird behavior when I used @polka/url to parse the incoming request.

The URL decoding is not happening, when an arrow function is used. More details below,

Snippet1:

const polka = require("polka");
const app = polka();
const parseUrl = require('@polka/url');

app.parse = parseUrl; // #5

app.use((req, res, next) => {
    console.log(req.url); // /proxy_path?query=Ngig+
    next();
});

app.listen(5000, () => {
    console.log('server listening on 5000');
});

Snippet2: (changes in line:5)

const polka = require("polka");
const app = polka();
const parseUrl = require('@polka/url');

app.parse = (req) => parseUrl(req);  // #5

app.use((req, res, next) => {
    console.log(req.url); // /proxy_path?query=Ngig%2B
    next();
});

app.listen(5000, () => {
    console.log('server listening on 5000');
});

The results should be consistent and this issue should be fixed.

Another note is that, if the parseurl module is used instead of @polka/url the URL decoding is not happening with both of the above snippets.
i.e. When I hit http://localhost:5000/proxy_path?query=N4IgLgng%2B the value of req.url is always /proxy_path?query=Ngig%2B

@lukeed
Copy link
Owner

lukeed commented Jun 23, 2021

This is by design, see https://github.com/lukeed/polka#parsereq & you can search for other parse/decode issues in the repo. By default, decoding is enabled because 99.9% of applications want/need this. You didn't mention what version you're using, but I imagine it's a @next version, in which case, this is done thru the 2nd parameter to polka/url which you omitted in your override. Has nothing to do with the arrow function itself.

Either way, this is working as intended and not an issue. And the parseurl module does not decode, ever.

@lukeed lukeed closed this as completed Jun 23, 2021
@ghost
Copy link
Author

ghost commented Jun 23, 2021

Hey @lukeed, thanks for the response. It makes sense now. Yes, I was using the @next version, which I didn't realize, apologies.

In the next version, the @polka/url module has the decoding disabled by default, whereas the decoding is enabled by default in polka. Please update the readme section accordingly. You can use this PR if helps.

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant