This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
/
DAVDirectory.cpp
257 lines (227 loc) · 7.24 KB
/
DAVDirectory.cpp
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Copyright (C) 2005-2009 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "DAVDirectory.h"
#include "URL.h"
#include "FileCurl.h"
#include "FileItem.h"
#include "utils/RegExp.h"
#include "utils/StringUtils.h"
#include "utils/CharsetConverter.h"
#include "utils/log.h"
#include "utils/URIUtils.h"
using namespace XFILE;
CDAVDirectory::CDAVDirectory(void) {}
CDAVDirectory::~CDAVDirectory(void) {}
/*
* Return true if pElement value is equal value without namespace.
*
* if pElement is <DAV:foo> and value is foo then ValueWithoutNamespace is true
*/
bool CDAVDirectory::ValueWithoutNamespace(const TiXmlNode *pNode, CStdString value)
{
CStdStringArray result;
const TiXmlElement *pElement;
if (!pNode)
{
return false;
}
pElement = pNode->ToElement();
if (!pElement)
{
return false;
}
StringUtils::SplitString(pElement->Value(), ":", result, 2);
if (result.size() == 1 && result[0] == value)
{
return true;
}
else if (result.size() == 2 && result[1] == value)
{
return true;
}
else if (result.size() > 2)
{
CLog::Log(LOGERROR, "%s - Splitting %s failed, size(): %lu, value: %s", __FUNCTION__, pElement->Value(), (unsigned long int)result.size(), value.c_str());
}
return false;
}
/*
* Search for <status> and return its content
*/
CStdString CDAVDirectory::GetStatusTag(const TiXmlElement *pElement)
{
const TiXmlElement *pChild;
for (pChild = pElement->FirstChild()->ToElement(); pChild != 0; pChild = pChild->NextSibling()->ToElement())
{
if (ValueWithoutNamespace(pChild, "status"))
{
return CStdString(pChild->GetText());
}
}
return CStdString("");
}
/*
* Parses a <response>
*
* <!ELEMENT response (href, ((href*, status)|(propstat+)), responsedescription?) >
* <!ELEMENT propstat (prop, status, responsedescription?) >
*
*/
void CDAVDirectory::ParseResponse(const TiXmlElement *pElement, CFileItem &item)
{
const TiXmlNode *pResponseChild;
const TiXmlNode *pPropstatChild;
const TiXmlNode *pPropChild;
/* Iterate response children elements */
for (pResponseChild = pElement->FirstChild(); pResponseChild != 0; pResponseChild = pResponseChild->NextSibling())
{
if (ValueWithoutNamespace(pResponseChild, "href"))
{
CStdString path(pResponseChild->ToElement()->GetText());
URIUtils::RemoveSlashAtEnd(path);
item.SetPath(path);
}
else
if (ValueWithoutNamespace(pResponseChild, "propstat"))
{
if (GetStatusTag(pResponseChild->ToElement()) == "HTTP/1.1 200 OK")
{
/* Iterate propstat children elements */
for (pPropstatChild = pResponseChild->FirstChild(); pPropstatChild != 0; pPropstatChild = pPropstatChild->NextSibling())
{
if (ValueWithoutNamespace(pPropstatChild, "prop"))
{
/* Iterate all properties available */
for (pPropChild = pPropstatChild->FirstChild(); pPropChild != 0; pPropChild = pPropChild->NextSibling())
{
if (ValueWithoutNamespace(pPropChild, "getcontentlength"))
{
item.m_dwSize = strtoll(pPropChild->ToElement()->GetText(), NULL, 10);
}
else
if (ValueWithoutNamespace(pPropChild, "getlastmodified"))
{
struct tm timeDate = {0};
strptime(pPropChild->ToElement()->GetText(), "%a, %d %b %Y %T", &timeDate);
item.m_dateTime = mktime(&timeDate);
}
else
if (ValueWithoutNamespace(pPropChild, "displayname"))
{
item.SetLabel(pPropChild->ToElement()->GetText());
}
else
if (!item.m_dateTime.IsValid() && ValueWithoutNamespace(pPropChild, "creationdate"))
{
struct tm timeDate = {0};
strptime(pPropChild->ToElement()->GetText(), "%Y-%m-%dT%T", &timeDate);
item.m_dateTime = mktime(&timeDate);
}
else
if (ValueWithoutNamespace(pPropChild, "resourcetype"))
{
if (ValueWithoutNamespace(pPropChild->FirstChild(), "collection"))
{
item.m_bIsFolder = true;
}
}
}
}
}
}
}
}
}
bool CDAVDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
{
CFileCurl dav;
CURL url(strPath);
CStdString strRequest = "PROPFIND";
dav.SetCustomRequest(strRequest);
dav.SetMimeType("text/xml; charset=\"utf-8\"");
dav.SetRequestHeader("content-type", "text/xml");
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::Log(LOGERROR, "%s - Unable to get dav directory (%s)", __FUNCTION__, strPath.c_str());
return false;
}
CStdString strResponse;
dav.ReadData(strResponse);
CXBMCTinyXML davResponse;
davResponse.Parse(strResponse.c_str());
if (!davResponse.Parse(strResponse))
{
CLog::Log(LOGERROR, "%s - Unable to process dav directory (%s)", __FUNCTION__, strPath.c_str());
dav.Close();
return false;
}
TiXmlNode *pChild;
// Iterate over all responses
for (pChild = davResponse.RootElement()->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
{
if (ValueWithoutNamespace(pChild, "response"))
{
CFileItem item;
ParseResponse(pChild->ToElement(), item);
CURL url2(strPath);
CURL url3(item.GetPath());
CStdString itemPath(URIUtils::AddFileToFolder(url2.GetWithoutFilename(), url3.GetFileName()));
if (item.GetLabel().IsEmpty())
{
CStdString name(itemPath);
URIUtils::RemoveSlashAtEnd(name);
CURL::Decode(name);
item.SetLabel(URIUtils::GetFileName(name));
}
if (item.m_bIsFolder)
URIUtils::AddSlashAtEnd(itemPath);
// Add back protocol options
if (!url2.GetProtocolOptions().IsEmpty())
itemPath += "|" + url2.GetProtocolOptions();
item.SetPath(itemPath);
if (!item.GetPath().Equals(strPath))
{
CFileItemPtr pItem(new CFileItem(item));
items.Add(pItem);
}
}
}
dav.Close();
return true;
}
bool CDAVDirectory::Exists(const char* strPath)
{
CFileCurl dav;
CURL url(strPath);
return dav.Exists(url);
}