Android上的libcurl Cookie到期

时间:2019-07-03 11:34:34

标签: c++ http cookies libcurl

我在Android下看到libcurls cookie引擎的奇怪行为,而在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

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

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

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

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

1 个答案:

答案 0 :(得分:0)

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