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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Http.Cookie;
interface
uses
Boolean,
Integer._32,
Object_.List,
Text,
Uri;
type
THttpCookieType = (hctRequest, hctResponse);
THttpCookie = class(TObject)
protected
function AttributeValueToText(CookieType: THttpCookieType; const Name: TText; const Value: TText): TText;
public
Path: TUriPath;
Name: TText;
Value: TText;
function Copy(): THttpCookie;
function ToText(CookieType: THttpCookieType): TText;
end;
THttpDuplicateCookiePolicy = (hdcpException, hdcpFree, hdcpOverwrite);
THttpCookies = class(TObject)
protected
FCookies: TObjectList<THttpCookie>;
function GetCookies(Index: TInteger32): THttpCookie;
function GreaterThan(const Path: TUriPath; const Name: TText; Cookie: THttpCookie): TBoolean;
function Equal(const Path: TUriPath; const Name: TText; Cookie: THttpCookie): TBoolean;
function BinarySearch(const Path: TUriPath; const Name: TText): TInteger32;
public
DuplicatePolicy: THttpDuplicateCookiePolicy;
constructor Create(); virtual;
destructor Destroy(); override;
procedure Add(Cookie: THttpCookie);
function FindWithName(const Path: TUriPath; const Name: TText): THttpCookie;
function Count: TInteger32;
procedure ParseFromRequest(Value: TText);
procedure ParseFromResponse(Value: TText);
function ToText(CookieType: THttpCookieType): TText;
property Cookies[Index: TInteger32]: THttpCookie read GetCookies; default;
end;
implementation
uses
Exception,
Internet.TextMessages,
Text.Collation;
{ THttpCookie }
function THttpCookie.AttributeValueToText(CookieType: THttpCookieType; const Name: TText; const Value: TText): TText;
begin
Result := Name + '=' + Value;
if CookieType = hctRequest then
Result := '$' + Result;
end;
function THttpCookie.Copy(): THttpCookie;
begin
Result := THttpCookie.Create();
Result.Path := Path;
Result.Name := Name;
Result.Value := Value;
end;
function THttpCookie.ToText(CookieType: THttpCookieType): TText;
begin
Result := Name;
if Value <> '' then
Result := Result + '=' + {QuoteText(}Value{)};
Result := Result + '; ' + AttributeValueToText(CookieType, 'Version', '1');
if not Path.IsEmpty then
Result := Result + '; ' + AttributeValueToText(CookieType, 'Path', Path.ToText());
end;
{ THttpCookies }
procedure THttpCookies.Add(Cookie: THttpCookie);
var
Index: TInteger32;
begin
Index := BinarySearch(Cookie.Path, Cookie.Name);
if (Index = Count) or not Equal(Cookie.Path, Cookie.Name, Self[Index]) then
FCookies.AddAt(Index, Cookie)
else
case DuplicatePolicy of
hdcpFree:
Cookie.Free();
hdcpOverwrite:
begin
FCookies.RemoveAt(Index);
FCookies.AddAt(Index, Cookie);
end;
hdcpException:
raise EException.Create('Duplicate cookie: ' + Cookie.Name);
else
raise EException.Create('Unknown duplicate cookie policy');
end;
end;
function THttpCookies.ToText(CookieType: THttpCookieType): TText;
var
i: TInteger32;
begin
if Count > 0 then
begin
Result := Self[0].ToText(CookieType);
for i := 1 to Count - 1 do
Result := Result + '; ' + Self[i].ToText(CookieType); // TODO: Separator should be a ', '
end
else
Result := '';
end;
function THttpCookies.BinarySearch(const Path: TUriPath; const Name: TText): TInteger32;
var
Low: TInteger32;
Middle: TInteger32;
High: TInteger32;
begin
Low := 0;
High := Count;
while Low < High do
begin
Middle := (Low + High) div 2;
if GreaterThan(Path, Name, Self[Middle]) then
Low := Middle + 1
else
High := Middle;
end;
Result := Low;
end;
function THttpCookies.Count: TInteger32;
begin
Result := FCookies.Count;
end;
constructor THttpCookies.Create();
begin
inherited Create();
FCookies := TObjectList<THttpCookie>.Create();
end;
destructor THttpCookies.Destroy();
begin
FCookies.Free();
inherited Destroy();
end;
function THttpCookies.Equal(const Path: TUriPath; const Name: TText; Cookie: THttpCookie): TBoolean;
begin
Result := {(Path = Cookie.Path) and} TTextCollation.DefaultCaseInsensitive.Equal(Name, Cookie.Name);
end;
function THttpCookies.FindWithName(const Path: TUriPath; const Name: TText): THttpCookie;
var
Index: TInteger32;
begin
Index := BinarySearch(Path, Name);
if (Index < Count) and Equal(Path, Name, Self[Index]) then
Result := Self[Index]
else
Result := nil;
end;
function THttpCookies.GetCookies(Index: TInteger32): THttpCookie;
begin
Result := FCookies[Index];
end;
function THttpCookies.GreaterThan(const Path: TUriPath; const Name: TText; Cookie: THttpCookie): TBoolean;
begin
// if Path = Cookie.Path then
Result := TTextCollation.DefaultCaseInsensitive.GreaterThan(Name, Cookie.Name);
// else
// Result := Path > Cookie.Path;
end;
procedure THttpCookies.ParseFromRequest(Value: TText);
var
Cookie: THttpCookie;
begin
while Value <> '' do
begin
Cookie := THttpCookie.Create();
Cookie.Name := Value.Parse('=');
Cookie.Name := Cookie.Name.Trim();
if Value.StartsWith('"') then
begin
Cookie.Value := ParseQuotedText(Value);
Value.Parse(';');
end
else
begin
Cookie.Value := Value.Parse(';'); // TODO: ',' can also be a delimiter
Cookie.Value := Cookie.Value.Trim();
end;
Add(Cookie);
end;
end;
procedure THttpCookies.ParseFromResponse(Value: TText);
var
Cookie: THttpCookie;
begin
while Value <> '' do
begin
Cookie := THttpCookie.Create();
Cookie.Name := Value.Parse('=');
Cookie.Name := Cookie.Name.Trim();
if Value.StartsWith('"') then
begin
Cookie.Value := ParseQuotedText(Value);
Value.Parse(',');
end
else
begin
Cookie.Value := Value.Parse(',');
// TODO: Parse path and version from value up to ',', for now just get value
Cookie.Value := Cookie.Value.Parse(';');
Cookie.Value := Cookie.Value.Trim();
end;
Add(Cookie);
end;
end;
end.
|