【发布时间】:2021-01-14 14:43:15
【问题描述】:
我需要转换一段代码才能在 Lucee 上运行。
这是 CF 中的工作代码,在 Lucee 中不起作用:
if(structKeyExists(response.responseheader, 'Set-Cookie')) {
var refind = refindNoCase("JSESSIONID=([\w\d]+);", response.responseheader['Set-Cookie'], 1, true);
if(structkeyexists(refind, 'match') and isArray(refind.match) and arraylen(refind.match) == 2) {
variables.sessionId = refind.match[2];
}
}
response.responseheader['Set-Cookie'] 是一个数组:
Array
1
string JSESSIONID=CC319C9B3CFA261A72724EAEB36B5C2D; HttpOnly=false; Secure; SameSite=None
在 CF 中 variables.sessionId = CC319C9B3CFA261A72724EAEB36B5C2D 的输出正是我所需要的。
Lucee 抛出错误:Can't cast Complex Object Type Array to String,所以我将代码更改为:
if(structKeyExists(response.responseheader, 'Set-Cookie')) {
var refind = refindNoCase("JSESSIONID=([\w\d]+);", serialize(response.responseheader['Set-Cookie']), 1, true);
if(structkeyexists(refind, 'match') and isArray(refind.match) and arraylen(refind.match) == 2) {
variables.sessionId = refind.match[2];
}
}
但现在 variables.sessionId 包含 'JSESSIONID=CC319C9B3CFA261A72724EAEB36B5C2D'
这有什么不同? 我还尝试在https://regex101.com/r/cO8lqs/4 上使用硬编码字符串,只给我'CC319C9B3CFA261A72724EAEB36B5C2D') 在https://docs.lucee.org/reference/functions/refindnocase.html 上运行代码 sn-p 时,给我 'JSESSIONID=CC319C9B3CFA261A72724EAEB36B5C2D' 两者都使用完全相同的字符串运行。 我将如何在 Lucee 获得我需要的东西,只有“CC319C9B3CFA261A72724EAEB36B5C2D”? 而且它也需要在 CF 上运行,因为我们的生产服务器仍在 ACF 上..
【问题讨论】:
-
你有
refind.match[2],但你只有一个捕获组。试试refind.match[1]。另外,"(?<=JSESSIONID=)\w+(?=;)"有效吗? -
您使用的是哪个版本的 Lucee?
reFind()和reFindNoCase()没有正确返回捕获组的匹配数组存在问题,并且这仅在几个月前根据 luceeserver.atlassian.net/browse/LDEV-2333?oldIssueView=true 修复在 5.3.8.80 中 -
@WiktorStribiżew 我认为序列化可能使它变得完全不同。我们在 CF 上运行的版本(没有序列化)正在运行,并为我们提供了正确值的 match[2]。尝试这个 (?
-
@SevRoberts 这确实与我遇到的问题一模一样,谢谢!通过命令框在我的本地测试,在 5.3.4.80 上运行。我刚刚更新,但它现在给了我 5.3.7.47,所以还没有修复匹配问题的版本。我们的测试服务器也在 Lucee 上运行,但我无法升级,我认为这需要几周时间才能完成。现在我将使用一种解决方法,就像您在下面的答案中提供的那样
标签: regex coldfusion lucee