From abbcaa52d9e6993686533b8b0ca1558c32526e3e Mon Sep 17 00:00:00 2001 From: Andrei Tserakhau Date: Wed, 11 May 2016 11:23:28 +0300 Subject: [PATCH] feat(universal-cookie): basic implementation of universal cookie - add opaque token to be able inject cookie object - add optional request cookie object into node_http service --- modules/universal/src/common/tokens/index.ts | 2 ++ modules/universal/src/node/http/node_http.ts | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/modules/universal/src/common/tokens/index.ts b/modules/universal/src/common/tokens/index.ts index c503b2dd8..472c2ad75 100644 --- a/modules/universal/src/common/tokens/index.ts +++ b/modules/universal/src/common/tokens/index.ts @@ -8,3 +8,5 @@ export const REQUEST_URL: OpaqueToken = new OpaqueToken('requestUrl'); export const BASE_URL: OpaqueToken = APP_BASE_HREF; export const PRIME_CACHE: OpaqueToken = new OpaqueToken('primeCache'); + +export const REQUEST_COOKIE: OpaqueToken = new OpaqueToken('requestCookie'); diff --git a/modules/universal/src/node/http/node_http.ts b/modules/universal/src/node/http/node_http.ts index 490bf6e50..046b90362 100644 --- a/modules/universal/src/node/http/node_http.ts +++ b/modules/universal/src/node/http/node_http.ts @@ -18,7 +18,7 @@ import * as http from 'http'; import * as https from 'https'; import * as url from 'url'; -import {ORIGIN_URL, BASE_URL} from '../../common'; +import {ORIGIN_URL, BASE_URL, REQUEST_COOKIE, Cookie} from '../../common'; export class NodeConnection implements Connection { public readyState: ReadyState; @@ -30,7 +30,8 @@ export class NodeConnection implements Connection { baseResponseOptions: ResponseOptions, ngZome: NgZone, @Inject(ORIGIN_URL) originUrl: string = '', - @Optional() @Inject(BASE_URL) baseUrl?: string) { + @Optional() @Inject(BASE_URL) baseUrl?: string, + @Optional() @Inject(REQUEST_COOKIE) requestCookie?: Cookie) { this.request = req; baseUrl = baseUrl || '/'; @@ -42,6 +43,14 @@ export class NodeConnection implements Connection { let _reqInfo: any = url.parse(url.resolve(url.resolve(originUrl, baseUrl), req.url)); _reqInfo.method = RequestMethod[req.method].toUpperCase(); + if (isPresent(requestCookie)){ + if (!isPresent(req.headers)){ + req.headers = new Headers(); + } + + req.headers.append('Cookie', requestCookie.get()); + } + if (isPresent(req.headers)) { _reqInfo.headers = {}; req.headers.forEach((values, name) => _reqInfo.headers[name] = values.join(','));