@@ -666,8 +666,96 @@ function store_methods:clean()
666
666
return true
667
667
end
668
668
669
- -- Saves to a file in netscape format
669
+ -- Files in ' netscape format'
670
670
-- curl's lib/cookie.c is best reference for the format
671
+ local function parse_netscape_format (line , now )
672
+ if line == " " then
673
+ return
674
+ end
675
+ local i = 1
676
+ local http_only = false
677
+ if line :sub (1 , 1 ) == " #" then
678
+ if line :sub (1 , 10 ) == " #HttpOnly_" then
679
+ http_only = true
680
+ i = 11
681
+ else
682
+ return
683
+ end
684
+ end
685
+
686
+ local domain , host_only , path , secure_only , expiry , name , value =
687
+ line :match (" ^%.?([^\t ]+)\t ([^\t ]+)\t ([^\t ]+)\t ([^\t ]+)\t (%d+)\t ([^\t ]+)\t (.+)" , i )
688
+ if not domain then
689
+ return
690
+ end
691
+ domain = canonicalise_host (domain )
692
+ if domain == nil then
693
+ return
694
+ end
695
+
696
+ if host_only == " TRUE" then
697
+ host_only = true
698
+ elseif host_only == " FALSE" then
699
+ host_only = false
700
+ else
701
+ return
702
+ end
703
+
704
+ if secure_only == " TRUE" then
705
+ secure_only = true
706
+ elseif secure_only == " FALSE" then
707
+ secure_only = false
708
+ else
709
+ return
710
+ end
711
+
712
+ expiry = tonumber (expiry , 10 )
713
+
714
+ return setmetatable ({
715
+ name = name ;
716
+ value = value ;
717
+ expiry_time = expiry ;
718
+ domain = domain ;
719
+ path = path ;
720
+ creation_time = now ;
721
+ last_access_time = now ;
722
+ persistent = expiry == 0 ;
723
+ host_only = host_only ;
724
+ secure_only = secure_only ;
725
+ http_only = http_only ;
726
+ same_site = nil ;
727
+ }, cookie_mt )
728
+ end
729
+
730
+ function store_methods :load_from_file (file )
731
+ local now = self .time ()
732
+
733
+ -- Clean now so that we don't hit storage limits
734
+ self :clean ()
735
+
736
+ local cookies = {}
737
+ local n = 0
738
+ while true do
739
+ local line , err , errno = file :read ()
740
+ if not line then
741
+ if err ~= nil then
742
+ return nil , err , errno
743
+ end
744
+ break
745
+ end
746
+ local cookie = parse_netscape_format (line , now )
747
+ if cookie then
748
+ n = n + 1
749
+ cookies [n ] = cookie
750
+ end
751
+ end
752
+ for i = 1 , n do
753
+ local cookie = cookies [i ]
754
+ add_to_store (self , cookie , cookie .http_only , now )
755
+ end
756
+ return true
757
+ end
758
+
671
759
function store_methods :save_to_file (file )
672
760
do -- write a preamble
673
761
local ok , err , errno = file :write [[
0 commit comments