Veni
Veni

Reputation: 151

Regex in Lucee refindNoCase

I need to convert a piece of code so it works on Lucee.

This is the working code in CF, which does not work in 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'] is an array :

Array
1   
string  JSESSIONID=CC319C9B3CFA261A72724EAEB36B5C2D; HttpOnly=false; Secure; SameSite=None

In CF the output of variables.sessionId = CC319C9B3CFA261A72724EAEB36B5C2D which is exactly what I need.

Lucee throws an error : Can't cast Complex Object Type Array to String, so I changed my code into :


        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];
            }
        }

But now variables.sessionId holds 'JSESSIONID=CC319C9B3CFA261A72724EAEB36B5C2D'

How can this be different? I also tried with hardcoded strings on https://regex101.com/r/cO8lqs/4, giving me only 'CC319C9B3CFA261A72724EAEB36B5C2D') And when running a code snippet on https://docs.lucee.org/reference/functions/refindnocase.html, giving me 'JSESSIONID=CC319C9B3CFA261A72724EAEB36B5C2D' Both running with exactly the same string. How would I go about in Lucee to get what I need, only 'CC319C9B3CFA261A72724EAEB36B5C2D'? And it needs to run on CF as well, since our production server is still on ACF..

Upvotes: 1

Views: 213

Answers (1)

Sev Roberts
Sev Roberts

Reputation: 1295

This doesn't explain why it is different in Lucee - which may be a consequence of the bug recently fixed in https://luceeserver.atlassian.net/browse/LDEV-2333?oldIssueView=true - but an answer for your question:

"How would I go about in Lucee to get what I need, only 'CC319C9B3CFA261A72724EAEB36B5C2D'? And it needs to run on CF as well, since our production server is still on ACF"

...that gives you the desired result with both Adobe and Lucee, would be to use variables.sessionId = listFirst(listLast(refind.match[2],'='),';');

Standalone example (var removed because it's not in a function here) =

<cfscript>
response.responseheader['Set-Cookie'] = ["JSESSIONID=CC319C9B3CFA261A72724EAEB36B5C2D; HttpOnly=false; Secure; SameSite=None"];
if(structKeyExists(response.responseheader, 'Set-Cookie')) {
        refind = refindNoCase("JSESSIONID=([\w\d]+);", response.responseheader['Set-Cookie'][1], 1, true);
        if(structkeyexists(refind, 'match') and isArray(refind.match) and arraylen(refind.match) == 2) {
            variables.sessionId = listFirst(listLast(refind.match[2],'='),';');
        }
    }
writeDump(variables.sessionId);
</cfscript>

Upvotes: 1

Related Questions