libcurl cookie在Android上有效

libcurl cookie expiry on android

本文关键字:有效 Android cookie libcurl      更新时间:2023-10-16

我在Android下看到Libcurls Cookie引擎的怪异行为,而它在iOS中正常工作。

当到期年度2038年或更高时,Cookie到期日期的解析似乎在Android中起作用。我知道Unix Timestamps的INT溢出问题,但这只能在2038年1月19日发生。与Libcurl一起,我一旦我去2038年1月1日00:00 AM就会发生问题。

以下不是确切的原始代码,因为这更复杂。但是cookie字符串和卷发呼叫完全相同。

// ...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.24tFALSEt/tFALSEt37095873600ttesttcookie")
// 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。这意味着它们被视为会话cookie,这对我不利。奇怪的是,这似乎不是由32位int溢出引起的,因为使用Netscape语法,支持更大的到期值。

我无法共享libcurl的确切构建设置,但它是从这里使用的脚本派生的,并且仍然相当相似:https://github.com/gcesarmza/curl-android-ios。我们使用此设置来构建Libcurl的iOS和Android二进制文件(版本7.62.0(。同样,使用iOS二进制文件,它可以正常工作(所有饼干都有正确的到期(。

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

有人知道是什么原因?

curl_setopt调用 Curl_cookie_add,又调用" set-cookie"式输入的 curl_getdate。此功能最终以以下代码结束:

/* 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;
}