forked from ballerina-platform/module-ballerina-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie_utils.bal
206 lines (195 loc) · 7.19 KB
/
cookie_utils.bal
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
import ballerina/lang.'int as ints;
import ballerina/log;
import ballerina/time;
// Returns the cookie object from the string value of the "Set-Cookie" header.
isolated function parseSetCookieHeader(string cookieStringValue) returns Cookie {
string cookieValue = cookieStringValue;
string[] result = re`;\s`.split(cookieValue);
string[] nameValuePair = re`=`.split(result[0]);
string cookieName = nameValuePair[0];
string cookieVal = nameValuePair[1];
CookieOptions options = {};
foreach var item in result {
nameValuePair = re`=`.split(item);
match nameValuePair[0] {
DOMAIN_ATTRIBUTE => {
options.domain = nameValuePair[1];
}
PATH_ATTRIBUTE => {
options.path = nameValuePair[1];
}
MAX_AGE_ATTRIBUTE => {
int|error age = ints:fromString(nameValuePair[1]);
if age is int {
options.maxAge = age;
}
}
EXPIRES_ATTRIBUTE => {
options.expires = nameValuePair[1];
}
SECURE_ATTRIBUTE => {
options.secure = true;
}
HTTP_ONLY_ATTRIBUTE => {
options.httpOnly = true;
}
}
}
Cookie cookie = new (cookieName, cookieVal, options);
return cookie;
}
// Returns an array of cookie objects from the string value of the "Cookie" header.
isolated function parseCookieHeader(string cookieStringValue) returns Cookie[] {
Cookie[] cookiesInRequest = [];
string cookieValue = cookieStringValue;
string[] nameValuePairs = re`;\s`.split(cookieValue);
foreach var item in nameValuePairs {
if re`^([^=]+)=.*$`.isFullMatch(item) {
string[] nameValue = re`=`.split(item);
Cookie cookie;
if nameValue.length() > 1 {
cookie = new (nameValue[0], nameValue[1]);
} else {
cookie = new (nameValue[0], "");
}
cookiesInRequest.push(cookie);
} else {
log:printError("Invalid cookie: " + item + ", which must be in the format as [{name}=].");
}
}
return cookiesInRequest;
}
// Returns a value to be used for sorting an array of cookies in order to create the "Cookie" header in the request.
// This value is returned according to the rules in [RFC-6265](https://tools.ietf.org/html/rfc6265#section-5.4).
isolated function comparator(Cookie c1, Cookie c2) returns int {
var cookiePath1 = c1.path;
var cookiePath2 = c2.path;
int l1 = 0;
int l2 = 0;
if cookiePath1 is string {
l1 = cookiePath1.length();
}
if cookiePath2 is string {
l2 = cookiePath2.length();
}
if l1 != l2 {
return l2 - l1;
}
return <int> time:utcDiffSeconds(c1.createdTime, c2.createdTime);
}
isolated function getClone(Cookie cookie, time:Utc createdTime, time:Utc lastAccessedTime) returns Cookie {
CookieOptions options = {};
if cookie.domain is string {
options.domain = <string> cookie.domain;
}
if cookie.path is string {
options.path = <string> cookie.path;
}
if cookie.expires is string {
options.expires = <string> cookie.expires;
}
options.maxAge = cookie.maxAge;
options.httpOnly = cookie.httpOnly;
options.secure = cookie.secure;
options.hostOnly = cookie.hostOnly;
options.createdTime = createdTime;
options.lastAccessedTime = lastAccessedTime;
return new Cookie(cookie.name, cookie.value, options);
}
isolated function getCloneWithExpiresAndMaxAge(Cookie cookie, string expires, int maxAge) returns Cookie {
CookieOptions options = {};
if cookie.domain is string {
options.domain = <string> cookie.domain;
}
if cookie.path is string {
options.path = <string> cookie.path;
}
options.expires = expires;
options.maxAge = maxAge;
options.httpOnly = cookie.httpOnly;
options.secure = cookie.secure;
options.hostOnly = cookie.hostOnly;
options.createdTime = cookie.createdTime;
options.lastAccessedTime = cookie.lastAccessedTime;
return new Cookie(cookie.name, cookie.value, options);
}
isolated function getCloneWithPath(Cookie cookie, string path) returns Cookie {
CookieOptions options = {};
if cookie.domain is string {
options.domain = <string> cookie.domain;
}
if cookie.expires is string {
options.expires = <string> cookie.expires;
}
options.path = path;
options.maxAge = cookie.maxAge;
options.httpOnly = cookie.httpOnly;
options.secure = cookie.secure;
options.hostOnly = cookie.hostOnly;
options.createdTime = cookie.createdTime;
options.lastAccessedTime = cookie.lastAccessedTime;
return new Cookie(cookie.name, cookie.value, options);
}
isolated function getCloneWithDomainAndHostOnly(Cookie cookie, string domain, boolean hostOnly) returns Cookie {
CookieOptions options = {};
if cookie.path is string {
options.path = <string> cookie.path;
}
if cookie.expires is string {
options.expires = <string> cookie.expires;
}
options.domain = domain;
options.maxAge = cookie.maxAge;
options.httpOnly = cookie.httpOnly;
options.secure = cookie.secure;
options.hostOnly = hostOnly;
options.createdTime = cookie.createdTime;
options.lastAccessedTime = cookie.lastAccessedTime;
return new Cookie(cookie.name, cookie.value, options);
}
isolated function getCloneWithHostOnly(Cookie cookie, boolean hostOnly) returns Cookie {
CookieOptions options = {};
if cookie.path is string {
options.path = <string> cookie.path;
}
if cookie.domain is string {
options.domain = <string> cookie.domain;
}
if cookie.expires is string {
options.expires = <string> cookie.expires;
}
options.maxAge = cookie.maxAge;
options.httpOnly = cookie.httpOnly;
options.secure = cookie.secure;
options.hostOnly = hostOnly;
options.createdTime = cookie.createdTime;
options.lastAccessedTime = cookie.lastAccessedTime;
return new Cookie(cookie.name, cookie.value, options);
}
isolated function updateLastAccessedTime(Cookie[] cookiesToAdd) {
Cookie[] tempCookies = [];
int endValue = cookiesToAdd.length();
foreach var i in 0 ..< endValue {
Cookie cookie = cookiesToAdd.pop();
time:Utc lastAccessedTime = time:utcNow();
tempCookies.push(getClone(cookie, cookie.createdTime, lastAccessedTime));
}
foreach var i in 0 ..< endValue {
cookiesToAdd.push(tempCookies.pop());
}
}