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

http/server : add cookie property on ServerRequest #338

Open
wants to merge 9 commits into
base: master
from

Conversation

3 participants
@zekth
Copy link
Contributor

zekth commented Apr 13, 2019

cookie is now part of ServerRequest and can be accessed by ServerRequest.cookie
Resolve #334

zekth added some commits Apr 13, 2019

@zekth zekth changed the title http/server : add cookie helper on request http/server : add cookie property on ServerRequest Apr 13, 2019

req.headers = new Headers();
assertEquals(req.cookie, {});
req.headers = new Headers();
req.headers.set("Cookie", "foo=bar");
assertEquals(req.cookie, { foo: "bar" });
assertEquals(req.cookie, {});

This comment has been minimized.

Copy link
@ry

ry Apr 13, 2019

Contributor

This seems wrong?

This comment has been minimized.

Copy link
@zekth

zekth Apr 13, 2019

Author Contributor

To read this yes it can feel wrong but it's just to show the cache handling.
At first the ServerRequest is created and the headers are set. After this the req.cookie is called, which will try to parse the headers to find cookies. It can't find anything so {} is stored in _cookie. Next headers is set to foo=bar and we try to recall req.cookie and it returns {} which is right because we have already called it and it's cached. The only way to get foo=bar is to recreate a ServerRequest like shown after in the test. Why do this? Because you don't set request cookie header when you try to access it, it's already set by the client request. It was just to cache the cookie parsing, especially in case of web server frameworks and middleware which can call this a lot while processing a request.

@zekth

This comment has been minimized.

Copy link
Contributor Author

zekth commented Apr 14, 2019

Another possibility:
Replace get cookie with function cookie(reload: boolean)
If reload is true it will parse again the headers to set _cookie value. IMO it's not really necessary as explained into the review and i think it's easier to call req.headers.cookie instead of req.headers.cookie()

Show resolved Hide resolved http/server.ts Outdated
Show resolved Hide resolved http/server.ts Outdated

zekth added some commits Apr 14, 2019

Show resolved Hide resolved http/server.ts Outdated

zekth added some commits Apr 14, 2019

Show resolved Hide resolved http/server.ts
@@ -122,6 +126,26 @@ export class ServerRequest {
conn: Conn;
r: BufReader;
w: BufWriter;
private _cookie: Cookie;

This comment has been minimized.

Copy link
@axetroy

axetroy Apr 14, 2019

Contributor

The last question about the cache of cookie:

Headers got the interface to delete/update the field.

Caching is to improve performance, but this may get a different result.

eg.

import { serve } from 'https://deno.land/x/std/http/server.ts';
import { assertEquals } from 'https://deno.land/x/std/testing/asserts.ts';

const s = serve('0.0.0.0:8000');

async function main() {
  for await (const req of s) {
    // read it first
    const cookie1 = req.cookie;
    // update cookie
    req.headers.set('cookie', "foo=bar");
    // read again. but it will read from the cache
    const cookie2 = req.cookie;

    // they should not equals
    assertEquals(cookie1, cookie2);

    req.respond({ body: new TextEncoder().encode('Hello World\n') });
  }
}

main();

How do you see? @ry

This comment has been minimized.

Copy link
@zekth

zekth Apr 14, 2019

Author Contributor

Server never respond with Cookie header but with only Set-Cookie. The Cookie header is only sent by client so this case will never appears. I think?

Also that's the case when we'd prefer to use cookie(reload: boolean) instead for get cookie for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.