如何解析cpprestsdk生成的多个Set-Cookie?

How to parse multiple Set-Cookie generated by cpprestsdk?

tgw_l7_route=d0bf4a9ab78d53762b596c0a48dabcdf; Expires=Thu, 03-May-2018 11:42:51 GMT; Path=/, session=a1d25e28-0084-421d-ae71-9ae18c7f6b50; Expires=Sun, 03-Jun-2018 10:42:51 GMT; HttpOnly; Path=/

有 2 Set-Cookie 项由 ', ' 连接,此字符串的问题是过期日期也包含 ', '。

此字符串由cpprestsdk 库生成。我需要解析它并生成 'Cookie' header 以便在 on-going 请求中发送到服务器。

// Example program
#include <iostream>
#include <string>
#include <regex>
#include <iterator>

int main()
{
  std::string cookieStr = "tgw_l7_route=d0bf4a9ab78d53762b596c0a48dabcdf; Expires=Thu, 03-May-2018 11:42:51 GMT; Path=/, session=a1d25e28-0084-421d-ae71-9ae18c7f6b50; Expires=Sun, 03-Jun-2018 10:42:51 GMT; HttpOnly; Path=/";
  std::regex rgx(", [^ ]+=");
  std::sregex_token_iterator iter(cookieStr.begin(),
    cookieStr.end(),
    rgx,
    -1);
  std::sregex_token_iterator end;
  for ( ; iter != end; ++iter)
    std::cout << *iter << '\n';
}

以上代码输出:

tgw_l7_route=d0bf4a9ab78d53762b596c0a48dabcdf; Expires=Thu, 03-May-2018 11:42:51 GMT; Path=/ 
a1d25e28-0084-421d-ae71-9ae18c7f6b50; Expires=Sun, 03-Jun-2018 10:42:51 GMT; HttpOnly; Path=/

有没有办法在第二个字符串中保留 "session="?

您需要将您使用的模式包装到 positive lookahead非消耗 结构中。

"(?=, [^ ]+=)"
 ^^^        ^

此构造匹配字符串中紧跟 ,、space 的位置,然后是 space 以外的 1+ 个字符,然后是 = sign without 将匹配的值压入匹配堆栈。这意味着匹配的文本不会被分割,它保留在分割块的结果数组中。

参见regex demo

C++ demo:

std::string cookieStr = "tgw_l7_route=d0bf4a9ab78d53762b596c0a48dabcdf; Expires=Thu, 03-May-2018 11:42:51 GMT; Path=/, session=a1d25e28-0084-421d-ae71-9ae18c7f6b50; Expires=Sun, 03-Jun-2018 10:42:51 GMT; HttpOnly; Path=/";
std::regex rgx("(?=, [^ ]+=)");
std::sregex_token_iterator iter(cookieStr.begin(),
  cookieStr.end(),
  rgx,
  -1);
std::sregex_token_iterator end;
for ( ; iter != end; ++iter)
    std::cout << *iter << '\n';

输出:

tgw_l7_route=d0bf4a9ab78d53762b596c0a48dabcdf; Expires=Thu, 03-May-2018 11:42:51 GMT; Path=/
, session=a1d25e28-0084-421d-ae71-9ae18c7f6b50; Expires=Sun, 03-Jun-2018 10:42:51 GMT; HttpOnly; Path=/