forked from queueit/KnownUser.V3.Cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contextProvider.js
63 lines (59 loc) · 2.22 KB
/
contextProvider.js
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
exports.getHttpHandler = function(request, bodyString)
{
var httpProvider = {
getHttpRequest: function () {
var httpRequest = {
getUserAgent: function () {
return this.getHeader("user-agent");
},
getHeader: function (headerNameArg) {
return request.headers.get(headerNameArg) || "";
},
getAbsoluteUri: function () {
return request.url;
},
getUserHostAddress: function () {
return this.getHeader("cf-connecting-ip");
},
getCookieValue: function (cookieKey) {
if (!this.parsedCookieDic) {
this.parsedCookieDic = this.__parseCookies(this.getHeader('cookie'));
}
return decodeURIComponent(this.parsedCookieDic[cookieKey]);
},
getBodyAsString:function()
{
return bodyString;
},
__parseCookies:function(cookieValue) {
let parsedCookie = [];
cookieValue.split(';').forEach(function (cookie) {
if (cookie) {
var parts = cookie.split('=');
if (parts.length >= 2)
parsedCookie[parts[0].trim()] = parts[1].trim();
}
});
return parsedCookie;
}
};
return httpRequest;
},
getHttpResponse: function () {
var httpResponse = {
setCookie: function (cookieName, cookieValue, domain, expiration) {
// expiration is in secs, but Date needs it in milisecs
let expirationDate = new Date(expiration * 1000);
var setCookieString = `${cookieName}=${encodeURIComponent(cookieValue)}; expires=${expirationDate.toGMTString()};`;
if (domain) {
setCookieString += ` domain=${domain};`;
}
setCookieString += " path=/";
httpProvider.outputCookie = setCookieString;
}
};
return httpResponse;
},
};
return httpProvider;
};