Davical Sync-Token Web请求

时间:2014-12-22 15:03:03

标签: httpwebrequest sync webdav caldav

我试图不在这里重新发明轮子......

我找到了一些关于CalDav同步实现there

的好文档

根据其网站,DaviCal符合rfc6578标准,自0.9.9版(见here)。

因此,我首先发送请求获取同步令牌,如下所示:

PROPFIND http://my_cal_srv/user/calendar_path HTTP/1.1
Content-Type: application/xml; charset="utf-8"
    <?xml version="1.0" encoding="utf-8" ?>
      <d:propfind xmlns:d='DAV:'> 
        <d:prop>
          <d:displayname />
          <d:sync-token />
        </d:prop>
       </d:propfind>

按预期返回数据:

<?xml version="1.0" encoding="utf-8" ?>
<multistatus xmlns="DAV:">
 <response>
  <href>/caldav.php/user/calendar_path/</href>
  <propstat>
   <prop>
    <displayname>My Calendar</displayname>
    <sync-token>data:,9</sync-token>
   </prop>
   <status>HTTP/1.1 200 OK</status>
  </propstat>
 </response>
</multistatus>

到目前为止,我有一个令牌,它是“数据:9”。所以,让我们尝试从8开始进行更改,这是我在添加一些事件之前查询服务器时的标记。

REPORT http://my_cal_srv/user/calendar_path HTTP/1.1
Content-Type: application/xml; charset="utf-8"
<?xml version="1.0" encoding="utf-8" ?>
<d:sync-collection xmlns:d="DAV:">
  <d:sync-token>8</d:sync-token>
  <d:sync-level>1</d:sync-level>
  <d:prop>
    <d:getetag/>
  </d:prop>
</d:sync-collection>

答案是:

<?xml version="1.0" encoding="utf-8" ?>
<multistatus xmlns="DAV:">
 <response>
  <href>/caldav.php/user/path/86166f9c-3e2e-4242-9a28-0f3bfb1dd67a-caldavsyncadapter.ics</href>
  <propstat>
   <prop>
    <getetag>"5ed2101b0c867e490dbd71d40c7071bb"</getetag>
   </prop>
   <status>HTTP/1.1 200 OK</status>
  </propstat>
 </response>
 <response>
  <href>/caldav.php/user/path/cb354fab-b41d-49ad-8a4f-8d68c9090ea0.ics</href>
  <propstat>
   <prop>
    <getetag>"334892703f4151024e9232eab9b515a7"</getetag>
   </prop>
   <status>HTTP/1.1 200 OK</status>
  </propstat>
 </response>
 <sync-token>data:,9</sync-token>
</multistatus>

删除条目后(所以我得到同步令牌10,仍然使用令牌8进行比较),我得到以下结果:

<?xml version="1.0" encoding="utf-8" ?>
<multistatus xmlns="DAV:">
 <response>
  <href>/caldav.php/user/cal_path/86166f9c-3e2e-4242-9a28-0f3bfb1dd67a-caldavsyncadapter.ics</href>
  <status>HTTP/1.1 404 Not Found</status>
 </response>
 <response>
  <href>/caldav.php/user/cal_path/cb354fab-b41d-49ad-8a4f-8d68c9090ea0.ics</href>
  <propstat>
   <prop>
    <getetag>"334892703f4151024e9232eab9b515a7"</getetag>
   </prop>
   <status>HTTP/1.1 200 OK</status>
  </propstat>
 </response>
 <sync-token>data:,10</sync-token>
</multistatus>

所以我在这里有点困惑,因为我真的不知道如何解释这些结果......

有人可以向我解释如何从这里提取同步信息吗?要弄清楚变化类型有点难,因为ICS的命名还不清楚......

提前感谢帮助...并且欢乐X-Mas! 问候, Ñ

2 个答案:

答案 0 :(得分:1)

您没有做出正确的请求。在您的请求中,您有:

<d:sync-token>8</d:sync-token>

但这应该是:

<d:sync-token>data:,8</d:sync-token>

除此之外,您收到的第一个回复告诉您:

These resources have been changed or newly created:
/caldav.php/user/path/86166f9c-3e2e-4242-9a28-0f3bfb1dd67a-caldavsyncadapter.ics
/caldav.php/user/path/cb354fab-b41d-49ad-8a4f-8d68c9090ea0.ics

第二个回复告诉你:

This resource has been changed or newly created:
/caldav.php/user/cal_path/cb354fab-b41d-49ad-8a4f-8d68c9090ea0.ics

This resource has been deleted:
/caldav.php/user/cal_path/86166f9c-3e2e-4242-9a28-0f3bfb1dd67a-caldavsyncadapter.ics

答案 1 :(得分:1)

你得到一个“数据:,9”并不意味着你实际上可以查询“数据:,8”或7等。同步令牌是不透明的,不会给你一个版本控制系统(你需要......) {3}}为此。)

DAV sync-tokens是一种简单的优化技术 - 仅此而已。它们对客户端完全不透明,服务器可以随时使同步令牌过期(并且不需要保存逻辑删除等)。例如,无法存储逻辑删除的服务器可以简单地使DELETE请求上的令牌失效。

使用sync-tokens的方式是:

  1. 确定需要重新同步父集合的哪些子集合
  2. 优化子集合中资源的同步
  3. 1)需要同步哪些子集合

    假设您有一组日历(例如my_cal_srv / user /),并且您在此集合上执行PROPFIND深度:1,要求获取子集合的同步标记。如果那些与您的客户端缓存不匹配,您知道您需要仅执行这些子集合的同步。

    注意:请勿使用您从此请求中返回的令牌来同步子集合(这是您在上面执行的操作)。它可能已经过期了。在同步报告中,只使用来自同步报告的令牌!

    2)优化集合内容的同步

    再次说明:sync-token是一种优化,仅此而已。您始终需要准备好获取(in)valid-sync-token前置条件错误(这意味着服务器使令牌过期)并对集合内容进行完全重新获取!然后将其(URL,ETag)与缓存版本进行比较,以确定更改的内容。 (基本上当你有一台不支持同步报告的服务器时,你需要做的所有步骤。)

    如果您在同步报告结果中获得了同步令牌,则可以在下一个同步请求中使用它。如果服务器仍然具有状态,它只会给你更改。如果它使令牌过期,它会给你同步令牌错误。

    注意:如果不明显 - 在第一个同步请求中,您不能(不能)提供令牌。您运行没有令牌的查询并获取所有内容。如果服务器向您发送(in)valid-sync-token错误,则会再次执行相同的操作。