Skip to content

Commit 54f2461

Browse files
committed
MediaWikiApi.parseCookie: handle single-value cookie
1 parent 9f9cc63 commit 54f2461

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed

dtMediaWiki.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Dependencies:
99
]]
1010
local dt = require "darktable"
1111
local MediaWikiApi = require "contrib/dtMediaWiki/lib/mediawikiapi"
12-
local version = 87
12+
local version = 90
1313

1414
--[[The version number is generated by .git/hooks/pre-commit (+x)
1515
with the following content:

lib/mediawikiapi.lua

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,70 @@ local function throwUserError(text)
5858
end
5959

6060
-- parse a received cookie and update MediaWikiApi.cookie
61-
function MediaWikiApi.parseCookie(unparsedcookie)
62-
while unparsedcookie and string.len(unparsedcookie) > 0 do
63-
local i = string.find(unparsedcookie, ";")
64-
local crumb = string.sub(unparsedcookie, 1, i - 1)
65-
local isep = string.find(crumb, "=")
66-
if isep then
67-
local cvar = string.sub(crumb, 1, isep - 1)
68-
local icvarcomma = string.find(cvar, ",")
69-
while icvarcomma do
70-
cvar = string.sub(cvar, icvarcomma + 2)
71-
icvarcomma = string.find(cvar, ",")
72-
end
73-
MediaWikiApi.cookie[cvar] = string.sub(crumb, isep + 1)
61+
function MediaWikiApi.parseCookie(unparsedcookie_header) -- Renamed for clarity
62+
if not unparsedcookie_header or string.len(unparsedcookie_header) == 0 then -- Guard against nil or empty input
63+
return
64+
end
65+
66+
local current_cookie_definitions = unparsedcookie_header
67+
68+
while current_cookie_definitions and string.len(current_cookie_definitions) > 0 do
69+
-- Trim leading whitespace from the remaining definitions string for the current iteration
70+
current_cookie_definitions = string.match(current_cookie_definitions, "^%s*(.*)")
71+
if string.len(current_cookie_definitions) == 0 then
72+
break -- Nothing left to parse
73+
end
74+
75+
-- Isolate the current cookie definition string (up to the next comma, or the whole remaining string)
76+
local next_comma_pos = string.find(current_cookie_definitions, ",")
77+
local single_cookie_def_str
78+
local remaining_definitions_after_this = ""
79+
80+
if next_comma_pos then
81+
single_cookie_def_str = string.sub(current_cookie_definitions, 1, next_comma_pos - 1)
82+
remaining_definitions_after_this = string.sub(current_cookie_definitions, next_comma_pos + 1)
83+
else
84+
single_cookie_def_str = current_cookie_definitions
85+
-- remaining_definitions_after_this remains an empty string, loop will terminate
7486
end
75-
local nexti = string.find(unparsedcookie, ",")
76-
if not nexti then
77-
return
87+
88+
single_cookie_def_str = string.match(single_cookie_def_str, "^%s*(.-)%s*$") -- Trim whitespace from this single definition
89+
90+
if string.len(single_cookie_def_str) > 0 then
91+
-- Now, find the semicolon within this single_cookie_def_str to separate name=value from attributes
92+
local semicolon_in_def_pos = string.find(single_cookie_def_str, ";")
93+
local crumb -- This is the "name=value" part
94+
95+
if semicolon_in_def_pos then
96+
crumb = string.sub(single_cookie_def_str, 1, semicolon_in_def_pos - 1)
97+
else
98+
crumb = single_cookie_def_str -- No semicolon, so the whole definition is the name=value part
99+
end
100+
crumb = string.match(crumb, "^%s*(.-)%s*$") -- Trim whitespace from the extracted crumb
101+
102+
local equals_sep_pos = string.find(crumb, "=")
103+
if equals_sep_pos then
104+
local cvar = string.sub(crumb, 1, equals_sep_pos - 1)
105+
local cval = string.sub(crumb, equals_sep_pos + 1)
106+
107+
-- Original logic for transforming cvar (e.g., stripping "Set-Cookie, " prefixes)
108+
local icvarcomma = string.find(cvar, ",")
109+
while icvarcomma do
110+
cvar = string.sub(cvar, icvarcomma + 2) -- Assumes skipping ", "
111+
icvarcomma = string.find(cvar, ",")
112+
end
113+
114+
-- Trim cvar and cval before storing
115+
cvar = string.match(cvar, "^%s*(.-)%s*$")
116+
cval = string.match(cval, "^%s*(.-)%s*$")
117+
118+
if string.len(cvar) > 0 then -- Ensure cvar is not empty after potential stripping
119+
MediaWikiApi.cookie[cvar] = cval
120+
end
121+
end
78122
end
79-
unparsedcookie = string.sub(unparsedcookie, nexti + 2)
123+
124+
current_cookie_definitions = remaining_definitions_after_this -- Move to the next part of the string
80125
end
81126
end
82127

@@ -338,3 +383,4 @@ end
338383
-- end of LrMediaWiki code
339384

340385
return MediaWikiApi
386+

0 commit comments

Comments
 (0)