File: cookie.lua

package info (click to toggle)
lua-http 0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,100 kB
  • sloc: makefile: 60; sh: 16
file content (867 lines) | stat: -rw-r--r-- 25,261 bytes parent folder | download | duplicates (2)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
--[[
Data structures useful for Cookies
RFC 6265
]]

local http_patts = require "lpeg_patterns.http"
local binaryheap = require "binaryheap"
local http_util = require "http.util"
local has_psl, psl = pcall(require, "psl")

local EOF = require "lpeg".P(-1)
local sane_cookie_date = http_patts.IMF_fixdate * EOF
local Cookie = http_patts.Cookie * EOF
local Set_Cookie = http_patts.Set_Cookie * EOF

local function bake(name, value, expiry_time, domain, path, secure_only, http_only, same_site)
	-- This function is optimised to only do one concat operation at the end
	local cookie = { name, "=", value }
	local n = 3
	if expiry_time and expiry_time ~= (1e999) then
		-- Prefer Expires over Max-age unless it is a deletion request
		if expiry_time == (-1e999) then
			n = n + 1
			cookie[n] = "; Max-Age=0"
		else
			n = n + 2
			cookie[n-1] = "; Expires="
			cookie[n] = http_util.imf_date(expiry_time)
		end
	end
	if domain then
		n = n + 2
		cookie[n-1] = "; Domain="
		cookie[n] = domain
	end
	if path then
		n = n + 2
		cookie[n-1] = "; Path="
		cookie[n] = http_util.encodeURI(path)
	end
	if secure_only then
		n = n + 1
		cookie[n] = "; Secure"
	end
	if http_only then
		n = n + 1
		cookie[n] = "; HttpOnly"
	end
	-- https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02#section-5.2
	if same_site then
		local v
		if same_site == "strict" then
			v = "; SameSite=Strict"
		elseif same_site == "lax" then
			v = "; SameSite=Lax"
		else
			error('invalid value for same_site, expected "strict" or "lax"')
		end
		n = n + 1
		cookie[n] = v
	end
	return table.concat(cookie, "", 1, n)
end

local function parse_cookie(cookie_header)
	return Cookie:match(cookie_header)
end

local function parse_cookies(req_headers)
	local cookie_headers = req_headers:get_as_sequence("cookie")
	local cookies
	for i=1, cookie_headers.n do
		local header_cookies = parse_cookie(cookie_headers[i])
		if header_cookies then
			if cookies then
				for k, v in pairs(header_cookies) do
					cookies[k] = v
				end
			else
				cookies = header_cookies
			end
		end
	end
	return cookies or {}
end

local function parse_setcookie(setcookie_header)
	return Set_Cookie:match(setcookie_header)
end

local canonicalise_host
if has_psl then
	canonicalise_host = psl.str_to_utf8lower
else
	canonicalise_host = function(str)
		-- fail on non-ascii chars
		if str:find("[^%p%w]") then
			return nil
		end
		return str:lower()
	end
end

--[[
A string domain-matches a given domain string if at least one of the following
conditions hold:
  - The domain string and the string are identical. (Note that both the domain
    string and the string will have been canonicalized to lower case at this point.)
  - All of the following conditions hold:
	  - The domain string is a suffix of the string.
	  - The last character of the string that is not included in the domain string
	    is a %x2E (".") character.
	  - The string is a host name (i.e., not an IP address).
]]
local function domain_match(domain_string, str)
	return str == domain_string or (
		str:sub(-#domain_string) == domain_string
		and str:sub(-#domain_string-1, -#domain_string-1) == "."
		and not http_util.is_ip(str)
	)
end

--[[ A request-path path-matches a given cookie-path if at least one of the following conditions holds:
  - The cookie-path and the request-path are identical.
  - The cookie-path is a prefix of the request-path, and the last
    character of the cookie-path is %x2F ("/").
  - The cookie-path is a prefix of the request-path, and the first
    character of the request-path that is not included in the cookie-path is a %x2F ("/") character.
]]
local function path_match(path, req_path)
	if path == req_path then
		return true
	elseif path == req_path:sub(1, #path) then
		if path:sub(-1, -1) == "/" then
			return true
		elseif req_path:sub(#path + 1, #path + 1) == "/" then
			return true
		end
	end
	return false
end

local cookie_methods = {}
local cookie_mt = {
	__name = "http.cookie.cookie";
	__index = cookie_methods;
}

function cookie_methods:netscape_format()
	return string.format("%s%s\t%s\t%s\t%s\t%d\t%s\t%s\n",
		self.http_only and "#HttpOnly_" or "",
		self.domain or "unknown",
		self.host_only and "TRUE" or "FALSE",
		self.path,
		self.secure_only and "TRUE" or "FALSE",
		math.max(0, math.min(2147483647, self.expiry_time)),
		self.name,
		self.value)
end


local default_psl
if has_psl and psl.latest then
	default_psl = psl.latest()
elseif has_psl then
	default_psl = psl.builtin()
end
local store_methods = {
	psl = default_psl;
	time = function() return os.time() end;
	max_cookie_length = (1e999);
	max_cookies = (1e999);
	max_cookies_per_domain = (1e999);
}

local store_mt = {
	__name = "http.cookie.store";
	__index = store_methods;
}

local function new_store()
	return setmetatable({
		domains = {};
		expiry_heap = binaryheap.minUnique();
		n_cookies = 0;
		n_cookies_per_domain = {};
	}, store_mt)
end

local function add_to_store(self, cookie, req_is_http, now)
	if cookie.expiry_time < now then
		-- This was all just a trigger to delete the old cookie
		self:remove(cookie.domain, cookie.path, cookie.name)
	else
		local name = cookie.name
		local cookie_length = #name + 1 + #cookie.value
		if cookie_length > self.max_cookie_length then
			return false
		end

		local domain = cookie.domain
		local domain_cookies = self.domains[domain]
		local path_cookies
		local old_cookie
		if domain_cookies ~= nil then
			path_cookies = domain_cookies[cookie.path]
			if path_cookies ~= nil then
				old_cookie = path_cookies[name]
			end
		end

		-- If the cookie store contains a cookie with the same name,
		-- domain, and path as the newly created cookie:
		if old_cookie then
			-- If the newly created cookie was received from a "non-HTTP"
			-- API and the old-cookie's http-only-flag is set, abort these
			-- steps and ignore the newly created cookie entirely.
			if not req_is_http and old_cookie.http_only then
				return false
			end

			-- Update the creation-time of the newly created cookie to
			-- match the creation-time of the old-cookie.
			cookie.creation_time = old_cookie.creation_time

			-- Remove the old-cookie from the cookie store.
			self.expiry_heap:remove(old_cookie)
		else
			if self.n_cookies >= self.max_cookies or self.max_cookies_per_domain < 1 then
				return false
			end

			-- Cookie will be added
			if domain_cookies == nil then
				path_cookies = {}
				domain_cookies = {
					[cookie.path] = path_cookies;
				}
				self.domains[domain] = domain_cookies
				self.n_cookies_per_domain[domain] = 1
			else
				local n_cookies_per_domain = self.n_cookies_per_domain[domain]
				if n_cookies_per_domain >= self.max_cookies_per_domain then
					return false
				end
				path_cookies = domain_cookies[cookie.path]
				if path_cookies == nil then
					path_cookies = {}
					domain_cookies[cookie.path] = path_cookies
				end
				self.n_cookies_per_domain[domain] = n_cookies_per_domain
			end

			self.n_cookies = self.n_cookies + 1
		end

		path_cookies[name] = cookie
		self.expiry_heap:insert(cookie.expiry_time, cookie)
	end

	return true
end

function store_methods:store(req_domain, req_path, req_is_http, req_is_secure, req_site_for_cookies, name, value, params)
	assert(type(req_domain) == "string")
	assert(type(req_path) == "string")
	assert(type(name) == "string")
	assert(type(value) == "string")
	assert(type(params) == "table")

	local now = self.time()

	req_domain = assert(canonicalise_host(req_domain), "invalid request domain")

	-- Clean now so that we can assume there are no expired cookies in store
	self:clean()

	-- RFC 6265 Section 5.3
	local cookie = setmetatable({
		name = name;
		value = value;
		expiry_time = (1e999);
		domain = req_domain;
		path = nil;
		creation_time = now;
		last_access_time = now;
		persistent = false;
		host_only = true;
		secure_only = not not params.secure;
		http_only = not not params.httponly;
		same_site = nil;
	}, cookie_mt)

	-- If a cookie has both the Max-Age and the Expires attribute, the Max-
	-- Age attribute has precedence and controls the expiration date of the
	-- cookie.
	local max_age = params["max-age"]
	if max_age and max_age:find("^%-?[0-9]+$") then
		max_age = tonumber(max_age, 10)
		cookie.persistent = true
		if max_age <= 0 then
			cookie.expiry_time = (-1e999)
		else
			cookie.expiry_time = now + max_age
		end
	elseif params.expires then
		local date = sane_cookie_date:match(params.expires)
		if date then
			cookie.persistent = true
			cookie.expiry_time = os.time(date)
		end
	end

	local domain = params.domain or "";

	-- If the first character of the attribute-value string is %x2E ("."):
	-- Let cookie-domain be the attribute-value without the leading %x2E (".") character.
	if domain:sub(1, 1) == "." then
		domain = domain:sub(2)
	end

	-- Convert the cookie-domain to lower case.
	domain = canonicalise_host(domain)
	if not domain then
		return false
	end

	-- If the user agent is configured to reject "public suffixes" and
	-- the domain-attribute is a public suffix:
	if domain ~= "" and self.psl and self.psl:is_public_suffix(domain) then
		-- If the domain-attribute is identical to the canonicalized request-host:
		if domain == req_domain then
			-- Let the domain-attribute be the empty string.
			domain = ""
		else
			-- Ignore the cookie entirely and abort these steps.
			return false
		end
	end

	-- If the domain-attribute is non-empty:
	if domain ~= "" then
		-- If the canonicalized request-host does not domain-match the
		-- domain-attribute:
		if not domain_match(domain, req_domain) then
			-- Ignore the cookie entirely and abort these steps.
			return false
		else
			-- Set the cookie's host-only-flag to false.
			cookie.host_only = false
			-- Set the cookie's domain to the domain-attribute.
			cookie.domain = domain
		end
	end

	-- RFC 6265 Section 5.2.4
	-- If the attribute-value is empty or if the first character of the
	-- attribute-value is not %x2F ("/")
	local path = params.path or ""
	if path:sub(1, 1) ~= "/" then
		-- Let cookie-path be the default-path.
		local default_path
		-- RFC 6265 Section 5.1.4
		-- Let uri-path be the path portion of the request-uri if such a
		-- portion exists (and empty otherwise).  For example, if the
		-- request-uri contains just a path (and optional query string),
		-- then the uri-path is that path (without the %x3F ("?") character
		-- or query string), and if the request-uri contains a full
		-- absoluteURI, the uri-path is the path component of that URI.

		-- If the uri-path is empty or if the first character of the uri-
		-- path is not a %x2F ("/") character, output %x2F ("/") and skip
		-- the remaining steps.
		-- If the uri-path contains no more than one %x2F ("/") character,
		-- output %x2F ("/") and skip the remaining step.
		if req_path:sub(1, 1) ~= "/" or not req_path:find("/", 2, true) then
			default_path = "/"
		else
			-- Output the characters of the uri-path from the first character up
			-- to, but not including, the right-most %x2F ("/").
			default_path = req_path:match("^([^?]*)/")
		end
		cookie.path = default_path
	else
		cookie.path = path
	end

	-- If the scheme component of the request-uri does not denote a
	-- "secure" protocol (as defined by the user agent), and the
	-- cookie's secure-only-flag is true, then abort these steps and
	-- ignore the cookie entirely.
	if not req_is_secure and cookie.secure_only then
		return false
	end

	-- If the cookie was received from a "non-HTTP" API and the
	-- cookie's http-only-flag is set, abort these steps and ignore the
	-- cookie entirely.
	if not req_is_http and cookie.http_only then
		return false
	end

	-- If the cookie's secure-only-flag is not set, and the scheme
	-- component of request-uri does not denote a "secure" protocol,
	if not req_is_secure and not cookie.secure_only then
		-- then abort these steps and ignore the cookie entirely if the
		-- cookie store contains one or more cookies that meet all of the
		-- following criteria:
		for d, domain_cookies in pairs(self.domains) do
			-- See '3' below
			if domain_match(cookie.domain, d) or domain_match(d, cookie.domain) then
				for p, path_cookies in pairs(domain_cookies) do
					local cmp_cookie = path_cookies[name]
					-- 1. Their name matches the name of the newly-created cookie.
					if cmp_cookie
						-- 2. Their secure-only-flag is true.
						and cmp_cookie.secure_only
						-- 3. Their domain domain-matches the domain of the newly-created
						-- cookie, or vice-versa.
						-- Note: already checked above in domain_match
						-- 4. The path of the newly-created cookie path-matches the path
						-- of the existing cookie.
						and path_match(p, cookie.path)
					then
						return false
					end
				end
			end
		end
	end

	-- If the cookie-attribute-list contains an attribute with an
	-- attribute-name of "SameSite", set the cookie's same-site-flag to
	-- attribute-value (i.e. either "Strict" or "Lax").  Otherwise, set
	-- the cookie's same-site-flag to "None".
	local same_site = params.samesite
	if same_site then
		same_site = same_site:lower()
		if same_site == "lax" or same_site == "strict" then
			-- If the cookie's "same-site-flag" is not "None", and the cookie
			-- is being set from a context whose "site for cookies" is not an
			-- exact match for request-uri's host's registered domain, then
			-- abort these steps and ignore the newly created cookie entirely.
			if req_domain ~= req_site_for_cookies then
				return false
			end

			cookie.same_site = same_site
		end
	end

	-- If the cookie-name begins with a case-sensitive match for the
	-- string "__Secure-", abort these steps and ignore the cookie
	-- entirely unless the cookie's secure-only-flag is true.
	if not cookie.secure_only and name:sub(1, 9) == "__Secure-" then
		return false
	end

	-- If the cookie-name begins with a case-sensitive match for the
	-- string "__Host-", abort these steps and ignore the cookie
	-- entirely unless the cookie meets all the following criteria:
	-- 1.  The cookie's secure-only-flag is true.
	-- 2.  The cookie's host-only-flag is true.
	-- 3.  The cookie-attribute-list contains an attribute with an
	--     attribute-name of "Path", and the cookie's path is "/".
	if not (cookie.secure_only and cookie.host_only and cookie.path == "/") and name:sub(1, 7) == "__Host-" then
		return false
	end

	return add_to_store(self, cookie, req_is_http, now)
end

function store_methods:store_from_request(req_headers, resp_headers, req_host, req_site_for_cookies)
	local set_cookies = resp_headers:get_as_sequence("set-cookie")
	local n = set_cookies.n
	if n == 0 then
		return true
	end

	local req_scheme = req_headers:get(":scheme")
	local req_authority = req_headers:get(":authority")
	local req_domain
	if req_authority then
		req_domain = http_util.split_authority(req_authority, req_scheme)
	else -- :authority can be missing for HTTP/1.0 requests; fall back to req_host
		req_domain = req_host
	end
	local req_path = req_headers:get(":path")
	local req_is_secure = req_scheme == "https"

	for i=1, n do
		local name, value, params = parse_setcookie(set_cookies[i])
		if name then
			self:store(req_domain, req_path, true, req_is_secure, req_site_for_cookies, name, value, params)
		end
	end
	return true
end

function store_methods:get(domain, path, name)
	assert(type(domain) == "string")
	assert(type(path) == "string")
	assert(type(name) == "string")

	-- Clean now so that we can assume there are no expired cookies in store
	self:clean()

	local domain_cookies = self.domains[domain]
	if domain_cookies then
		local path_cookies = domain_cookies[path]
		if path_cookies then
			local cookie = path_cookies[name]
			if cookie then
				return cookie.value
			end
		end
	end
	return nil
end

function store_methods:remove(domain, path, name)
	assert(type(domain) == "string")
	assert(type(path) == "string" or (path == nil and name == nil))
	assert(type(name) == "string" or name == nil)
	local domain_cookies = self.domains[domain]
	if not domain_cookies then
		return
	end
	local n_cookies = self.n_cookies
	if path == nil then
		-- Delete whole domain
		for _, path_cookies in pairs(domain_cookies) do
			for _, cookie in pairs(path_cookies) do
				self.expiry_heap:remove(cookie)
				n_cookies = n_cookies - 1
			end
		end
		self.domains[domain] = nil
		self.n_cookies_per_domain[domain] = nil
	else
		local path_cookies = domain_cookies[path]
		if path_cookies then
			if name == nil then
				-- Delete all names at path
				local domains_deleted = 0
				for _, cookie in pairs(path_cookies) do
					self.expiry_heap:remove(cookie)
					domains_deleted = domains_deleted + 1
				end
				domain_cookies[path] = nil
				n_cookies = n_cookies - domains_deleted
				if next(domain_cookies) == nil then
					self.domains[domain] = nil
					self.n_cookies_per_domain[domain] = nil
				else
					self.n_cookies_per_domain[domain] = self.n_cookies_per_domain[domain] - domains_deleted
				end
			else
				-- Delete singular cookie
				local cookie = path_cookies[name]
				if cookie then
					self.expiry_heap:remove(cookie)
					n_cookies = n_cookies - 1
					self.n_cookies_per_domain[domain] = self.n_cookies_per_domain[domain] - 1
					path_cookies[name] = nil
					if next(path_cookies) == nil then
						domain_cookies[path] = nil
						if next(domain_cookies) == nil then
							self.domains[domain] = nil
							self.n_cookies_per_domain[domain] = nil
						end
					end
				end
			end
		end
	end
	self.n_cookies = n_cookies
end

--[[ The user agent SHOULD sort the cookie-list in the following order:
  - Cookies with longer paths are listed before cookies with shorter paths.
  - Among cookies that have equal-length path fields, cookies with earlier
	creation-times are listed before cookies with later creation-times.
]]
local function cookie_cmp(a, b)
	if #a.path ~= #b.path then
		return #a.path > #b.path
	end
	if a.creation_time ~= b.creation_time then
		return a.creation_time < b.creation_time
	end
	-- Now order doesn't matter, but have to be consistent for table.sort:
	-- use the fields that make a cookie unique
	if a.domain ~= b.domain then
		return a.domain < b.domain
	end
	return a.name < b.name
end

local function cookie_match(cookie, req_domain, req_is_http, req_is_secure, req_is_safe_method, req_site_for_cookies, req_is_top_level)
	-- req_domain should be already canonicalized

	if cookie.host_only then -- Either:
		-- The cookie's host-only-flag is true and the canonicalized
		-- request-host is identical to the cookie's domain.
		if cookie.domain ~= req_domain then
			return false
		end
	end
	-- Or:
	-- The cookie's host-only-flag is false and the canonicalized
	-- request-host domain-matches the cookie's domain.

	-- already done domain_match and path_match

	-- If the cookie's http-only-flag is true, then exclude the
	-- cookie if the cookie-string is being generated for a "non-
	-- HTTP" API (as defined by the user agent).
	if cookie.http_only and not req_is_http then
		return false
	end

	if cookie.secure_only and not req_is_secure then
		return false
	end

	-- If the cookie's same-site-flag is not "None", and the HTTP
	-- request is cross-site (as defined in Section 5.2) then exclude
	-- the cookie unless all of the following statements hold:
	if cookie.same_site and req_site_for_cookies ~= req_domain and not (
		-- 1. The same-site-flag is "Lax"
		cookie.same_site == "lax"
		-- 2. The HTTP request's method is "safe".
		and req_is_safe_method
		-- 3. The HTTP request's target browsing context is a top-level browsing context.
		and req_is_top_level
	) then
		return false
	end

	return true
end

function store_methods:lookup(req_domain, req_path, req_is_http, req_is_secure, req_is_safe_method, req_site_for_cookies, req_is_top_level, max_cookie_length)
	req_domain = assert(type(req_domain) == "string" and canonicalise_host(req_domain), "invalid request domain")
	assert(type(req_path) == "string")
	if max_cookie_length ~= nil then
		assert(type(max_cookie_length) == "number")
	else
		max_cookie_length = self.max_cookie_length
	end

	local now = self.time()

	-- Clean now so that we can assume there are no expired cookies in store
	self:clean()

	local list = {}
	local n = 0
	for domain, domain_cookies in pairs(self.domains) do
		if domain_match(domain, req_domain) then
			for path, path_cookies in pairs(domain_cookies) do
				if path_match(path, req_path) then
					for _, cookie in pairs(path_cookies) do
						if cookie_match(cookie, req_domain, req_is_http, req_is_secure, req_is_safe_method, req_site_for_cookies, req_is_top_level) then
							cookie.last_access_time = now
							n = n + 1
							list[n] = cookie
						end
					end
				end
			end
		end
	end
	table.sort(list, cookie_cmp)
	local cookie_length = -2 -- length of separator ("; ")
	for i=1, n do
		local cookie = list[i]
		-- TODO: validate?
		local cookie_pair = cookie.name .. "=" .. cookie.value
		local new_length = cookie_length + #cookie_pair + 2
		if new_length > max_cookie_length then
			break
		end
		list[i] = cookie_pair
		cookie_length = new_length
	end
	return table.concat(list, "; ", 1, n)
end

function store_methods:lookup_for_request(req_headers, req_host, req_site_for_cookies, req_is_top_level, max_cookie_length)
	local req_method = req_headers:get(":method")
	if req_method == "CONNECT" then
		return ""
	end
	local req_scheme = req_headers:get(":scheme")
	local req_authority = req_headers:get(":authority")
	local req_domain
	if req_authority then
		req_domain = http_util.split_authority(req_authority, req_scheme)
	else -- :authority can be missing for HTTP/1.0 requests; fall back to req_host
		req_domain = req_host
	end
	local req_path = req_headers:get(":path")
	local req_is_secure = req_scheme == "https"
	local req_is_safe_method = http_util.is_safe_method(req_method)
	return self:lookup(req_domain, req_path, true, req_is_secure, req_is_safe_method, req_site_for_cookies, req_is_top_level, max_cookie_length)
end

function store_methods:clean_due()
	local next_expiring = self.expiry_heap:peek()
	if not next_expiring then
		return (1e999)
	end
	return next_expiring.expiry_time
end

function store_methods:clean()
	local now = self.time()
	while self:clean_due() < now do
		local cookie = self.expiry_heap:pop()
		self.n_cookies = self.n_cookies - 1
		local domain = cookie.domain
		local domain_cookies = self.domains[domain]
		if domain_cookies then
			self.n_cookies_per_domain[domain] = self.n_cookies_per_domain[domain] - 1
			local path_cookies = domain_cookies[cookie.path]
			if path_cookies then
				path_cookies[cookie.name] = nil
				if next(path_cookies) == nil then
					domain_cookies[cookie.path] = nil
					if next(domain_cookies) == nil then
						self.domains[domain] = nil
						self.n_cookies_per_domain[domain] = nil
					end
				end
			end
		end
	end
	return true
end

-- Files in 'netscape format'
-- curl's lib/cookie.c is best reference for the format
local function parse_netscape_format(line, now)
	if line == "" then
		return
	end
	local i = 1
	local http_only = false
	if line:sub(1, 1) == "#" then
		if line:sub(1, 10) == "#HttpOnly_" then
			http_only = true
			i = 11
		else
			return
		end
	end

	local domain, host_only, path, secure_only, expiry, name, value =
		line:match("^%.?([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t(%d+)\t([^\t]+)\t(.+)", i)
	if not domain then
		return
	end
	domain = canonicalise_host(domain)
	if domain == nil then
		return
	end

	if host_only == "TRUE" then
		host_only = true
	elseif host_only == "FALSE" then
		host_only = false
	else
		return
	end

	if secure_only == "TRUE" then
		secure_only = true
	elseif secure_only == "FALSE" then
		secure_only = false
	else
		return
	end

	expiry = tonumber(expiry, 10)

	return setmetatable({
		name = name;
		value = value;
		expiry_time = expiry;
		domain = domain;
		path = path;
		creation_time = now;
		last_access_time = now;
		persistent = expiry == 0;
		host_only = host_only;
		secure_only = secure_only;
		http_only = http_only;
		same_site = nil;
	}, cookie_mt)
end

function store_methods:load_from_file(file)
	local now = self.time()

	-- Clean now so that we don't hit storage limits
	self:clean()

	local cookies = {}
	local n = 0
	while true do
		local line, err, errno = file:read()
		if not line then
			if err ~= nil then
				return nil, err, errno
			end
			break
		end
		local cookie = parse_netscape_format(line, now)
		if cookie then
			n = n + 1
			cookies[n] = cookie
		end
	end
	for i=1, n do
		local cookie = cookies[i]
		add_to_store(self, cookie, cookie.http_only, now)
	end
	return true
end

function store_methods:save_to_file(file)
	do -- write a preamble
		local ok, err, errno = file:write [[
# Netscape HTTP Cookie File
# This file was generated by lua-http

]]
		if not ok then
			return nil, err, errno
		end
	end
	for _, domain_cookies in pairs(self.domains) do
		for _, path_cookies in pairs(domain_cookies) do
			for _, cookie in pairs(path_cookies) do
				local ok, err, errno = file:write(cookie:netscape_format())
				if not ok then
					return nil, err, errno
				end
			end
		end
	end
	return true
end

return {
	bake = bake;

	parse_cookie = parse_cookie;
	parse_cookies = parse_cookies;
	parse_setcookie = parse_setcookie;

	new_store = new_store;
	store_mt = store_mt;
	store_methods = store_methods;
}