crossutility / Quantumult-X

2.73k stars 278 forks source link

[bug] Can't set multiple "Set-Cookie" response headers #135

Open ethancn opened 1 year ago

ethancn commented 1 year ago

So I'm using a script to rewrite response headers (script-response-header). When the server responds with multiple Set-Cookie headers, the script value $response.headers['Set-Cookie'] seems automatically concatenated, for example:

Response headers:

Set-Cookie: name1=val1
Set-Cookie: name2=val2
Set-Cookie: name3=val3

$response.headers['Set-Cookie'] value:

var cookies = $response.headers['Set-Cookie']; // "name1=val1, name2=val2, name3=val3"

This will cause a problem even if I just copy the headers and set them back:

var headers = $response.headers;
$done({ headers });

The resulting header is incorrect:

Set-Cookie: name1=val1, name2=val2, name3=val3

What I want to do is modify the Set-Cookie response headers with new values, like below:

Set-Cookie: name1=new-val1
Set-Cookie: name2=new-val2
Set-Cookie: name3=new-val3

But currently I‘ve found no way to set multiple Set-Cookie headers using script. I have tried an array and it didn't seem to work either (version 1.3.0):

var headers = $response.headers;
headers['Set-Cookie'] = [
  "name1=new-val1",
  "name2=new-val2",
  "name3=new-val3"
]
ethancn commented 1 year ago

For anyone who has the same question, here's the solution: to add repeating headers, just append spaces to the name as many as you need, like below

headers['Set-Cookie'] = "name1=new-val1";
headers['Set-Cookie '] = "name2=new-val2";
headers['Set-Cookie  '] = "name3=new-val3";

Quantumult X will turn them into multiple Set-Cookie headers in response.