-
Notifications
You must be signed in to change notification settings - Fork 5
/
akamaiHttpContextProvider.ts
84 lines (65 loc) · 2.34 KB
/
akamaiHttpContextProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/// <reference path="types/akamai.d.ts"/>
import {Cookies} from 'cookies';
export class AkamaiHttpContextProvider {
private _httpRequest: AkamaiHttpRequest;
private _httpResponse: AkamaiHttpResponse;
public constructor(akamiNativeRequest: any, akamaiNativeResponse: any) {
this._httpRequest = new AkamaiHttpRequest(akamiNativeRequest);
this._httpResponse = new AkamaiHttpResponse(akamaiNativeResponse);
}
public getHttpRequest() {
return this._httpRequest;
}
public getHttpResponse() {
return this._httpResponse;
}
}
class AkamaiHttpRequest {
constructor(private _akamiNativeRequest: any) {
}
public getUserAgent() {
return this.getHeader("user-agent");
}
public getHeader(headerNameArg: string) {
return (this._akamiNativeRequest.getHeader(headerNameArg) || []).toString();
}
public getAbsoluteUri() {
return `${this._akamiNativeRequest.scheme}://${this._akamiNativeRequest.host}${this._akamiNativeRequest.url}`;
}
public getUserHostAddress() {
return this._akamiNativeRequest.getVariable('PMUSER_TRUE_CLIENT_IP');
}
public getCookieValue(cookieKey: string) {
try {
let cookies = new Cookies(this._akamiNativeRequest.getHeader('Cookie'));
let cookieValue = cookies.get(cookieKey);
if (cookieValue)
return decodeURIComponent(cookieValue);
} catch {
return undefined;
}
}
public getRequestBodyAsString() {
return "";
}
}
class AkamaiHttpResponse {
constructor(private _akamiNativeRequest: any) {
}
public setCookie(cookieName: string, cookieValue: string, domain: string, expiration, httpOnly: boolean, isSecure: boolean) {
// expiration is in secs, but Date needs it in milisecs
let expirationDate = new Date(expiration * 1000);
let setCookieString = `${cookieName}=${encodeURIComponent(cookieValue)}; expires=${expirationDate.toUTCString()};`;
if (domain) {
setCookieString += ` domain=${domain};`;
}
if (httpOnly) {
setCookieString += " HttpOnly;"
}
if (isSecure) {
setCookieString += " Secure;"
}
setCookieString += " path=/";
this._akamiNativeRequest.storeCookie(setCookieString);
}
}