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

break: do not decode req.path by default #172

Merged
merged 4 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/polka/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ class Polka extends Router {
}

handler(req, res, next) {
let info = this.parse(req, true);
let info = this.parse(req);
let obj = this.find(req.method, req.path=info.pathname);

req.params = obj.params;
req.params = {};
for (let key in obj.params) {
req.params[key] = decodeURIComponent(obj.params[key]);
}
req.originalUrl = req.originalUrl || req.url;
req.url = info.pathname + info.search;
req.query = info.query || {};
Expand Down
8 changes: 4 additions & 4 deletions packages/polka/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1425,16 +1425,16 @@ test('HEAD', async () => {
});


test('decode url', async () => {
test('encoded url', async () => {
// t.plan(8);

let sub = (
polka()
.get('/:foo', (req, res) => {
assert.ok('~> inside "GET /sub/:foo" handler')
assert.ok(req._decoded, '~> marked as decoded');
assert.is(req.path, '/føøß∂r', '~> decoded "path" value');
assert.is(req.url, '/føøß∂r?phone=%2b8675309', '~> decoded "url" value partially');
assert.ok(!req._decoded, '~> not marked as decoded');
assert.is(req.path, '/f%C3%B8%C3%B8%C3%9F%E2%88%82r', '~> "path" value');
assert.is(req.url, '/f%C3%B8%C3%B8%C3%9F%E2%88%82r?phone=%2b8675309', '~> "url" value');
assert.is(req.params.foo, 'føøß∂r', '~> decoded "params.foo" segment');
assert.is(req.query.phone, '+8675309', '~~> does NOT decode "req.query" keys twice');
res.end('done');
Expand Down
12 changes: 1 addition & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,12 @@ You may also pass a sub-application, which _must_ be accompanied by a `base` pat
Please see [`Middleware`](#middleware) and [Express' middleware examples](http://expressjs.com/en/4x/api.html#middleware-callback-function-examples) for more info.


### parse(req, toDecode=true)
### parse(req, toDecode=false)

Returns: `Object` or `undefined`

As of `v0.5.0`, this is an alias of the [`@polka/url`](/packages/url) module. For nearly all cases, you'll notice no changes.

One important difference is that URL decoding is enabled by default in polka but it is disabled by default in the `@polka/url` module. You can disable the URL decoding if required,

```js
const app = polka();
const { parse } = require('@polka/url');
app.parse = (req, toDecode) => {
return parse(req, false); // or simply, return parse(req);
}
```

But, for whatever reason, you can quickly swap in [`parseurl`](https://github.com/pillarjs/parseurl) again:

```js
Expand Down