aboutsummaryrefslogtreecommitdiff
path: root/xbmc/filesystem/DAVDirectory.cpp
blob: 08905fca31545ca68e0f2400f06310cf69500cb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "DAVDirectory.h"

#include "CurlFile.h"
#include "DAVCommon.h"
#include "DAVFile.h"
#include "FileItem.h"
#include "FileItemList.h"
#include "URL.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "utils/XBMCTinyXML2.h"
#include "utils/log.h"

using namespace XFILE;

CDAVDirectory::CDAVDirectory(void) = default;
CDAVDirectory::~CDAVDirectory(void) = default;

/*
 * Parses a <response>
 *
 * <!ELEMENT response (href, ((href*, status)|(propstat+)), responsedescription?) >
 * <!ELEMENT propstat (prop, status, responsedescription?) >
 *
 */
void CDAVDirectory::ParseResponse(const tinyxml2::XMLElement* element, CFileItem& item)
{
  /* Iterate response children elements */
  for (auto* responseChild = element->FirstChildElement(); responseChild;
       responseChild = responseChild->NextSiblingElement())
  {
    if (CDAVCommon::ValueWithoutNamespace(responseChild, "href") && !responseChild->NoChildren())
    {
      std::string path(responseChild->FirstChild()->Value());
      URIUtils::RemoveSlashAtEnd(path);
      item.SetPath(path);
    }
    else if (CDAVCommon::ValueWithoutNamespace(responseChild, "propstat"))
    {
      if (CDAVCommon::GetStatusTag(responseChild->ToElement()).find("200 OK") != std::string::npos)
      {
        /* Iterate propstat children elements */
        for (auto* propstatChild = responseChild->FirstChild(); propstatChild;
             propstatChild = propstatChild->NextSibling())
        {
          if (CDAVCommon::ValueWithoutNamespace(propstatChild, "prop"))
          {
            /* Iterate all properties available */
            for (auto* propChild = propstatChild->FirstChildElement(); propChild;
                 propChild = propChild->NextSiblingElement())
            {
              if (CDAVCommon::ValueWithoutNamespace(propChild, "getcontentlength") &&
                  !propChild->NoChildren())
              {
                item.m_dwSize = strtoll(propChild->FirstChild()->Value(), NULL, 10);
              }
              else if (CDAVCommon::ValueWithoutNamespace(propChild, "getlastmodified") &&
                       !propChild->NoChildren())
              {
                struct tm timeDate = {};
                strptime(propChild->FirstChild()->Value(), "%a, %d %b %Y %T", &timeDate);
                item.m_dateTime = mktime(&timeDate);
              }
              else if (CDAVCommon::ValueWithoutNamespace(propChild, "displayname") &&
                       !propChild->NoChildren())
              {
                item.SetLabel(CURL::Decode(propChild->FirstChild()->Value()));
              }
              else if (!item.m_dateTime.IsValid() &&
                       CDAVCommon::ValueWithoutNamespace(propChild, "creationdate") &&
                       !propChild->NoChildren())
              {
                struct tm timeDate = {};
                strptime(propChild->FirstChild()->Value(), "%Y-%m-%dT%T", &timeDate);
                item.m_dateTime = mktime(&timeDate);
              }
              else if (CDAVCommon::ValueWithoutNamespace(propChild, "resourcetype"))
              {
                if (CDAVCommon::ValueWithoutNamespace(propChild->FirstChild(), "collection"))
                {
                  item.m_bIsFolder = true;
                }
              }
            }
          }
        }
      }
    }
  }
}

bool CDAVDirectory::GetDirectory(const CURL& url, CFileItemList &items)
{
  CCurlFile dav;
  std::string strRequest = "PROPFIND";

  dav.SetCustomRequest(strRequest);
  dav.SetMimeType("text/xml; charset=\"utf-8\"");
  dav.SetRequestHeader("depth", 1);
  dav.SetPostData(
    "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
    " <D:propfind xmlns:D=\"DAV:\">"
    "   <D:prop>"
    "     <D:resourcetype/>"
    "     <D:getcontentlength/>"
    "     <D:getlastmodified/>"
    "     <D:creationdate/>"
    "     <D:displayname/>"
    "    </D:prop>"
    "  </D:propfind>");

  if (!dav.Open(url))
  {
    CLog::LogF(LOGERROR, "Unable to get dav directory ({})", url.GetRedacted());
    return false;
  }

  std::string strResponse;
  dav.ReadData(strResponse);

  std::string fileCharset(dav.GetProperty(XFILE::FILE_PROPERTY_CONTENT_CHARSET));
  CXBMCTinyXML2 davResponse;
  davResponse.Parse(strResponse);

  if (!davResponse.Parse(strResponse))
  {
    CLog::LogF(LOGERROR, "Unable to process dav directory ({})", url.GetRedacted());
    dav.Close();
    return false;
  }

  // Iterate over all responses
  for (auto* child = davResponse.RootElement()->FirstChild(); child; child = child->NextSibling())
  {
    if (CDAVCommon::ValueWithoutNamespace(child, "response"))
    {
      CFileItem item;
      ParseResponse(child->ToElement(), item);
      const CURL& url2(url);
      CURL url3(item.GetPath());

      std::string itemPath(URIUtils::AddFileToFolder(url2.GetWithoutFilename(), url3.GetFileName()));

      if (item.GetLabel().empty())
      {
        std::string name(itemPath);
        URIUtils::RemoveSlashAtEnd(name);
        item.SetLabel(CURL::Decode(URIUtils::GetFileName(name)));
      }

      if (item.m_bIsFolder)
        URIUtils::AddSlashAtEnd(itemPath);

      // Add back protocol options
      if (!url2.GetProtocolOptions().empty())
        itemPath += "|" + url2.GetProtocolOptions();
      item.SetPath(itemPath);

      if (!item.IsURL(url))
      {
        CFileItemPtr pItem(new CFileItem(item));
        items.Add(pItem);
      }
    }
  }

  dav.Close();

  return true;
}

bool CDAVDirectory::Create(const CURL& url)
{
  CDAVFile dav;
  std::string strRequest = "MKCOL";

  dav.SetCustomRequest(strRequest);

  if (!dav.Execute(url))
  {
    CLog::Log(LOGERROR, "{} - Unable to create dav directory ({}) - {}", __FUNCTION__,
              url.GetRedacted(), dav.GetLastResponseCode());
    return false;
  }

  dav.Close();

  return true;
}

bool CDAVDirectory::Exists(const CURL& url)
{
  CCurlFile dav;

  // Set the PROPFIND custom request else we may not find folders, depending
  // on the server's configuration
  std::string strRequest = "PROPFIND";
  dav.SetCustomRequest(strRequest);
  dav.SetRequestHeader("depth", 0);

  return dav.Exists(url);
}

bool CDAVDirectory::Remove(const CURL& url)
{
  CDAVFile dav;
  std::string strRequest = "DELETE";

  dav.SetCustomRequest(strRequest);

  if (!dav.Execute(url))
  {
    CLog::Log(LOGERROR, "{} - Unable to delete dav directory ({}) - {}", __FUNCTION__,
              url.GetRedacted(), dav.GetLastResponseCode());
    return false;
  }

  dav.Close();

  return true;
}