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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/*
* 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 "HTTPDirectory.h"
#include "CurlFile.h"
#include "FileItem.h"
#include "ServiceBroker.h"
#include "URL.h"
#include "settings/AdvancedSettings.h"
#include "settings/SettingsComponent.h"
#include "utils/CharsetConverter.h"
#include "utils/HTMLUtil.h"
#include "utils/RegExp.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "utils/log.h"
#include <climits>
using namespace XFILE;
CHTTPDirectory::CHTTPDirectory(void) = default;
CHTTPDirectory::~CHTTPDirectory(void) = default;
bool CHTTPDirectory::GetDirectory(const CURL& url, CFileItemList &items)
{
CCurlFile http;
const std::string& strBasePath = url.GetFileName();
if(!http.Open(url))
{
CLog::Log(LOGERROR, "{} - Unable to get http directory ({})", __FUNCTION__, url.GetRedacted());
return false;
}
CRegExp reItem(true); // HTML is case-insensitive
reItem.RegComp("<a href=\"([^\"]*)\"[^>]*>\\s*(.*?)\\s*</a>(.+?)(?=<a|</tr|$)");
CRegExp reDateTimeHtml(true);
reDateTimeHtml.RegComp(
"<td align=\"right\">([0-9]{2})-([A-Z]{3})-([0-9]{4}) ([0-9]{2}):([0-9]{2}) +</td>");
CRegExp reDateTimeLighttp(true);
reDateTimeLighttp.RegComp(
"<td class=\"m\">([0-9]{4})-([A-Z]{3})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})</td>");
CRegExp reDateTimeNginx(true);
reDateTimeNginx.RegComp("([0-9]{2})-([A-Z]{3})-([0-9]{4}) ([0-9]{2}):([0-9]{2})");
CRegExp reDateTimeNginxFancy(true);
reDateTimeNginxFancy.RegComp(
"<td class=\"date\">([0-9]{4})-([A-Z]{3})-([0-9]{2}) ([0-9]{2}):([0-9]{2})</td>");
CRegExp reDateTimeApacheNewFormat(true);
reDateTimeApacheNewFormat.RegComp(
"<td align=\"right\">([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}) +</td>");
CRegExp reDateTime(true);
reDateTime.RegComp("([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2})");
CRegExp reSizeHtml(true);
reSizeHtml.RegComp("> *([0-9.]+) *(B|K|M|G| )(iB)?</td>");
CRegExp reSize(true);
reSize.RegComp(" +([0-9]+)(B|K|M|G)?(?=\\s|<|$)");
/* read response from server into string buffer */
std::string strBuffer;
if (http.ReadData(strBuffer) && strBuffer.length() > 0)
{
/* if Content-Length is found and its not text/html, URL is pointing to file so don't treat URL as HTTPDirectory */
if (!http.GetHttpHeader().GetValue("Content-Length").empty() &&
!StringUtils::StartsWithNoCase(http.GetHttpHeader().GetValue("Content-type"), "text/html"))
{
return false;
}
std::string fileCharset(http.GetProperty(XFILE::FILE_PROPERTY_CONTENT_CHARSET));
if (!fileCharset.empty() && fileCharset != "UTF-8")
{
std::string converted;
if (g_charsetConverter.ToUtf8(fileCharset, strBuffer, converted) && !converted.empty())
strBuffer = converted;
}
unsigned int bufferOffset = 0;
while (bufferOffset < strBuffer.length())
{
int matchOffset = reItem.RegFind(strBuffer.c_str(), bufferOffset);
if (matchOffset < 0)
break;
bufferOffset = matchOffset + reItem.GetSubLength(0);
std::string strLink = reItem.GetMatch(1);
std::string strName = reItem.GetMatch(2);
std::string strMetadata = reItem.GetMatch(3);
StringUtils::Trim(strMetadata);
if(strLink[0] == '/')
strLink = strLink.substr(1);
std::string strNameTemp = StringUtils::Trim(strName);
std::wstring wName, wLink, wConverted;
if (fileCharset.empty())
g_charsetConverter.unknownToUTF8(strNameTemp);
g_charsetConverter.utf8ToW(strNameTemp, wName, false);
HTML::CHTMLUtil::ConvertHTMLToW(wName, wConverted);
g_charsetConverter.wToUTF8(wConverted, strNameTemp);
URIUtils::RemoveSlashAtEnd(strNameTemp);
std::string strLinkBase = strLink;
std::string strLinkOptions;
// split link with url options
size_t pos = strLinkBase.find('?');
if (pos != std::string::npos)
{
strLinkOptions = strLinkBase.substr(pos);
strLinkBase.erase(pos);
}
// strip url fragment from the link
pos = strLinkBase.find('#');
if (pos != std::string::npos)
{
strLinkBase.erase(pos);
}
// Convert any HTTP character entities (e.g.: "&") to percentage encoding
// (e.g.: "%xx") as some web servers (Apache) put these in HTTP Directory Indexes
// this is also needed as CURL objects interpret them incorrectly due to the ;
// also being allowed as URL option separator
if (fileCharset.empty())
g_charsetConverter.unknownToUTF8(strLinkBase);
g_charsetConverter.utf8ToW(strLinkBase, wLink, false);
HTML::CHTMLUtil::ConvertHTMLToW(wLink, wConverted);
g_charsetConverter.wToUTF8(wConverted, strLinkBase);
// encoding + and ; to URL encode if it is not already encoded by http server used on the remote server (example: Apache)
// more characters may be added here when required when required by certain http servers
pos = strLinkBase.find_first_of("+;");
while (pos != std::string::npos)
{
std::stringstream convert;
convert << '%' << std::hex << int(strLinkBase.at(pos));
strLinkBase.replace(pos, 1, convert.str());
pos = strLinkBase.find_first_of("+;");
}
std::string strLinkTemp = strLinkBase;
URIUtils::RemoveSlashAtEnd(strLinkTemp);
strLinkTemp = CURL::Decode(strLinkTemp);
if (StringUtils::EndsWith(strNameTemp, "..>") &&
StringUtils::StartsWith(strLinkTemp, strNameTemp.substr(0, strNameTemp.length() - 3)))
strName = strNameTemp = strLinkTemp;
/* Per RFC 1808 § 5.3, relative paths containing a colon ":" should be either prefixed with
* "./" or escaped (as "%3A"). This handles the prefix case, the escaping should be handled by
* the CURL::Decode above
* - https://tools.ietf.org/html/rfc1808#section-5.3
*/
auto NameMatchesLink([](const std::string& name, const std::string& link) -> bool
{
return (name == link) ||
((std::string::npos != name.find(':')) && (std::string{"./"}.append(name) == link));
});
// we detect http directory items by its display name and its stripped link
// if same, we consider it as a valid item.
if (strLinkTemp != ".." && strLinkTemp != "" && NameMatchesLink(strNameTemp, strLinkTemp))
{
CFileItemPtr pItem(new CFileItem(strNameTemp));
pItem->SetProperty("IsHTTPDirectory", true);
CURL url2(url);
url2.SetFileName(strBasePath + strLinkBase);
url2.SetOptions(strLinkOptions);
pItem->SetURL(url2);
if(URIUtils::HasSlashAtEnd(pItem->GetPath(), true))
pItem->m_bIsFolder = true;
std::string day, month, year, hour, minute;
int monthNum = 0;
if (reDateTimeHtml.RegFind(strMetadata.c_str()) >= 0)
{
day = reDateTimeHtml.GetMatch(1);
month = reDateTimeHtml.GetMatch(2);
year = reDateTimeHtml.GetMatch(3);
hour = reDateTimeHtml.GetMatch(4);
minute = reDateTimeHtml.GetMatch(5);
}
else if (reDateTimeNginxFancy.RegFind(strMetadata.c_str()) >= 0)
{
day = reDateTimeNginxFancy.GetMatch(3);
month = reDateTimeNginxFancy.GetMatch(2);
year = reDateTimeNginxFancy.GetMatch(1);
hour = reDateTimeNginxFancy.GetMatch(4);
minute = reDateTimeNginxFancy.GetMatch(5);
}
else if (reDateTimeNginx.RegFind(strMetadata.c_str()) >= 0)
{
day = reDateTimeNginx.GetMatch(1);
month = reDateTimeNginx.GetMatch(2);
year = reDateTimeNginx.GetMatch(3);
hour = reDateTimeNginx.GetMatch(4);
minute = reDateTimeNginx.GetMatch(5);
}
else if (reDateTimeLighttp.RegFind(strMetadata.c_str()) >= 0)
{
day = reDateTimeLighttp.GetMatch(3);
month = reDateTimeLighttp.GetMatch(2);
year = reDateTimeLighttp.GetMatch(1);
hour = reDateTimeLighttp.GetMatch(4);
minute = reDateTimeLighttp.GetMatch(5);
}
else if (reDateTimeApacheNewFormat.RegFind(strMetadata.c_str()) >= 0)
{
day = reDateTimeApacheNewFormat.GetMatch(3);
monthNum = atoi(reDateTimeApacheNewFormat.GetMatch(2).c_str());
year = reDateTimeApacheNewFormat.GetMatch(1);
hour = reDateTimeApacheNewFormat.GetMatch(4);
minute = reDateTimeApacheNewFormat.GetMatch(5);
}
else if (reDateTime.RegFind(strMetadata.c_str()) >= 0)
{
day = reDateTime.GetMatch(3);
monthNum = atoi(reDateTime.GetMatch(2).c_str());
year = reDateTime.GetMatch(1);
hour = reDateTime.GetMatch(4);
minute = reDateTime.GetMatch(5);
}
if (month.length() > 0)
monthNum = CDateTime::MonthStringToMonthNum(month);
if (day.length() > 0 && monthNum > 0 && year.length() > 0)
{
pItem->m_dateTime = CDateTime(atoi(year.c_str()), monthNum, atoi(day.c_str()), atoi(hour.c_str()), atoi(minute.c_str()), 0);
}
if (!pItem->m_bIsFolder)
{
if (reSizeHtml.RegFind(strMetadata.c_str()) >= 0)
{
double Size = atof(reSizeHtml.GetMatch(1).c_str());
std::string strUnit(reSizeHtml.GetMatch(2));
if (strUnit == "K")
Size = Size * 1024;
else if (strUnit == "M")
Size = Size * 1024 * 1024;
else if (strUnit == "G")
Size = Size * 1024 * 1024 * 1024;
pItem->m_dwSize = (int64_t)Size;
}
else if (reSize.RegFind(strMetadata.c_str()) >= 0)
{
double Size = atof(reSize.GetMatch(1).c_str());
std::string strUnit(reSize.GetMatch(2));
if (strUnit == "K")
Size = Size * 1024;
else if (strUnit == "M")
Size = Size * 1024 * 1024;
else if (strUnit == "G")
Size = Size * 1024 * 1024 * 1024;
pItem->m_dwSize = (int64_t)Size;
}
else
if (CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_bHTTPDirectoryStatFilesize) // As a fallback get the size by stat-ing the file (slow)
{
CCurlFile file;
file.Open(url);
pItem->m_dwSize=file.GetLength();
file.Close();
}
}
items.Add(pItem);
}
}
}
http.Close();
items.SetProperty("IsHTTPDirectory", true);
return true;
}
bool CHTTPDirectory::Exists(const CURL &url)
{
CCurlFile http;
struct __stat64 buffer;
if( http.Stat(url, &buffer) != 0 )
{
return false;
}
if (buffer.st_mode == _S_IFDIR)
return true;
return false;
}
|