c++ - android 上的 libcurl cookie 过期

标签 c++ http cookies libcurl

我发现 libcurls cookie 引擎在 android 下有奇怪的行为,而它在 iOS 上正常工作。

当到期年份为 2038 或更高时,cookie 到期日期的解析在 Android 中似乎不起作用。我知道 unix 时间戳的 int 溢出问题,但这应该只发生在 2038 年 1 月 19 日。使用 libcurl,只要我到 2038 年 1 月 1 日上午 00:00,问题就会发生。

以下不是确切的原始代码,因为那样比较复杂。但是 cookie 字符串和 curl 调用是完全一样的。

// ...create the curl handle...

// Add test cookies in Set-Cookie syntax, because the issue seems to have to do with expiry parsing
static const std::string border = "Tue, 19-Jan-2087 03:14:08 GMT";
static const std::string borderP1 = "Fri, 01-Jan-2038 00:00:00 GMT";
static const std::string borderM1 = "Thu, 31-Dec-2037 23:59:59 GMT";
curl_easy_setopt(curlHandle, CURLOPT_COOKIELIST, ("Set-Cookie: my1=border;Domain=10.101.32.24;Path=/;Expires=" + border).c_str());
curl_easy_setopt(curlHandle, CURLOPT_COOKIELIST, ("Set-Cookie: my2=borderP1;Domain=10.101.32.24;Path=/;Expires=" + borderP1).c_str());
curl_easy_setopt(curlHandle, CURLOPT_COOKIELIST, ("Set-Cookie: my3=borderM1;Domain=10.101.32.24;Path=/;Expires=" + borderM1).c_str());

// Add another cookie in netscape syntax to compare (this one expires on July 10, 3145 9:20:00 AM)
curl_easy_setopt(curlHandle, CURLOPT_COOKIELIST, "10.101.32.24\tFALSE\t/\tFALSE\t37095873600\ttest\tcookie")

// Code to print all cookies known to curl for test purposes:

struct curl_slist *cookies;
curl_easy_getinfo(curlHandle, CURLINFO_COOKIELIST, &cookies);
for (auto c = cookies; c; c = c->next) {
    LogStream::debug("Cookie") << c->data;
}
curl_slist_free_all(cookies);

日志中的结果行如下所示:

Cookie: 10.101.32.24    FALSE   /   FALSE   0   my1 border
Cookie: 10.101.32.24    FALSE   /   FALSE   0   my2 borderP1
Cookie: 10.101.32.24    FALSE   /   FALSE   2145916799  my3 borderM1
Cookie: 10.101.32.24    FALSE   /   FALSE   37095873600 test    cookie

因此,对于 2038 年或更早的前 2 个 cookie,过期结果为 0。这意味着它们被视为 session cookie,这对我不利。奇怪的是,这似乎不是由 32 位 int 溢出引起的,因为使用 netscape 语法,支持更大的到期值。

我无法分享 libcurl 的确切build设置,但它源自此处使用的脚本并且仍然非常相似:https://github.com/gcesarmza/curl-android-ios .我们使用此设置来构建 libcurl(版本 7.62.0)的 iOS 和 Android 二进制文件。同样,对于 iOS 二进制文件,它工作正常(所有 cookie 都有正确的到期时间)。

在实际代码中,我还验证了 curl_easy_setopt 的返回,并且它是成功的。如果您需要更多设置代码,我可以尝试整理一个更完整的示例,但这会花费一些时间。

有谁知道是什么原因造成的?

最佳答案

curl_setopt 调用 Curl_cookie_add,后者又调用 curl_getdate 进行“Set-Cookie”式输入。 这个函数最终以 here 结束。使用以下代码:

/* a signed 32 bit time_t can only hold dates to the beginning of 2038 */
if(yearnum > 2037) {
  *output = TIME_T_MAX;
  return PARSEDATE_LATER;
}

关于c++ - android 上的 libcurl cookie 过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56869205/

相关文章:

c++ - 想知道为什么我会遇到段错误

perl - Firefox 不删除过期的 Perl CGI::Cookie cookie

javascript - 使用 Jquery cookie 存储 div 的切换状态?

jQuery UI 可排序和 Cookie

c++ - 奇怪的编译错误 C++ "undefined reference"& "relocation truncated"

c++ - 位图作为 MDI mainfrm 的背景

C++ 指针类型的隐式转换

java - Viewing Http Calls With Fiddler - 看不到Java程序Http流量

javascript - 如何无限期缓存 HTTP 响应

jquery - IE中请求Content-Length等于0