forked from huceke/xbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
URL.cpp
806 lines (701 loc) · 19.2 KB
/
URL.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/*
* Copyright (C) 2005-2012 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, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "URL.h"
#include "utils/RegExp.h"
#include "utils/log.h"
#include "utils/URIUtils.h"
#include "Util.h"
#include "filesystem/File.h"
#include "FileItem.h"
#include "filesystem/StackDirectory.h"
#include "addons/Addon.h"
#ifndef _LINUX
#include <sys\types.h>
#include <sys\stat.h>
#endif
using namespace std;
using namespace ADDON;
CStdString URLEncodeInline(const CStdString& strData)
{
CStdString buffer = strData;
CURL::Encode(buffer);
return buffer;
}
CURL::CURL(const CStdString& strURL1)
{
Parse(strURL1);
}
CURL::CURL()
{
m_iPort = 0;
}
CURL::~CURL()
{
}
void CURL::Reset()
{
m_strHostName.clear();
m_strDomain.clear();
m_strUserName.clear();
m_strPassword.clear();
m_strShareName.clear();
m_strFileName.clear();
m_strProtocol.clear();
m_strFileType.clear();
m_iPort = 0;
}
void CURL::Parse(const CStdString& strURL1)
{
Reset();
// start by validating the path
CStdString strURL = CUtil::ValidatePath(strURL1);
// strURL can be one of the following:
// format 1: protocol://[username:password]@hostname[:port]/directoryandfile
// format 2: protocol://file
// format 3: drive:directoryandfile
//
// first need 2 check if this is a protocol or just a normal drive & path
if (!strURL.size()) return ;
if (strURL.Equals("?", true)) return;
// form is format 1 or 2
// format 1: protocol://[domain;][username:password]@hostname[:port]/directoryandfile
// format 2: protocol://file
// decode protocol
int iPos = strURL.Find("://");
if (iPos < 0)
{
// This is an ugly hack that needs some work.
// example: filename /foo/bar.zip/alice.rar/bob.avi
// This should turn into zip://rar:///foo/bar.zip/alice.rar/bob.avi
iPos = 0;
bool is_apk = (strURL.Find(".apk/", iPos) > 0);
while (1)
{
if (is_apk)
iPos = strURL.Find(".apk/", iPos);
else
iPos = strURL.Find(".zip/", iPos);
int extLen = 3;
if (iPos < 0)
{
/* set filename and update extension*/
SetFileName(strURL);
return ;
}
iPos += extLen + 1;
CStdString archiveName = strURL.Left(iPos);
struct __stat64 s;
if (XFILE::CFile::Stat(archiveName, &s) == 0)
{
#ifdef _LINUX
if (!S_ISDIR(s.st_mode))
#else
if (!(s.st_mode & S_IFDIR))
#endif
{
Encode(archiveName);
if (is_apk)
{
CURL c((CStdString)"apk" + "://" + archiveName + '/' + strURL.Right(strURL.size() - iPos - 1));
*this = c;
}
else
{
CURL c((CStdString)"zip" + "://" + archiveName + '/' + strURL.Right(strURL.size() - iPos - 1));
*this = c;
}
return;
}
}
}
}
else
{
SetProtocol(strURL.Left(iPos));
iPos += 3;
}
// virtual protocols
// why not handle all format 2 (protocol://file) style urls here?
// ones that come to mind are iso9660, cdda, musicdb, etc.
// they are all local protocols and have no server part, port number, special options, etc.
// this removes the need for special handling below.
if (
m_strProtocol.Equals("stack") ||
m_strProtocol.Equals("virtualpath") ||
m_strProtocol.Equals("multipath") ||
m_strProtocol.Equals("filereader") ||
m_strProtocol.Equals("special")
)
{
SetFileName(strURL.Mid(iPos));
return;
}
// check for username/password - should occur before first /
if (iPos == -1) iPos = 0;
// for protocols supporting options, chop that part off here
// maybe we should invert this list instead?
int iEnd = strURL.length();
const char* sep = NULL;
//TODO fix all Addon paths
CStdString strProtocol2 = GetTranslatedProtocol();
if(m_strProtocol.Equals("rss") ||
m_strProtocol.Equals("rar") ||
m_strProtocol.Equals("addons") ||
m_strProtocol.Equals("image") ||
m_strProtocol.Equals("videodb") ||
m_strProtocol.Equals("musicdb") ||
m_strProtocol.Equals("androidapp"))
sep = "?";
else
if(strProtocol2.Equals("http")
|| strProtocol2.Equals("https")
|| strProtocol2.Equals("plugin")
|| strProtocol2.Equals("addons")
|| strProtocol2.Equals("hdhomerun")
|| strProtocol2.Equals("rtsp")
|| strProtocol2.Equals("apk")
|| strProtocol2.Equals("zip"))
sep = "?;#|";
else if(strProtocol2.Equals("ftp")
|| strProtocol2.Equals("ftps"))
sep = "?;";
if(sep)
{
int iOptions = strURL.find_first_of(sep, iPos);
if (iOptions >= 0 )
{
// we keep the initial char as it can be any of the above
int iProto = strURL.find_first_of("|",iOptions);
if (iProto >= 0)
{
m_strProtocolOptions = strURL.substr(iProto+1);
m_strOptions = strURL.substr(iOptions,iProto-iOptions);
}
else
m_strOptions = strURL.substr(iOptions);
iEnd = iOptions;
m_options.AddOptions(m_strOptions);
}
}
int iSlash = strURL.Find("/", iPos);
if(iSlash >= iEnd)
iSlash = -1; // was an invalid slash as it was contained in options
if( !m_strProtocol.Equals("iso9660") )
{
int iAlphaSign = strURL.Find("@", iPos);
if (iAlphaSign >= 0 && iAlphaSign < iEnd && (iAlphaSign < iSlash || iSlash < 0))
{
// username/password found
CStdString strUserNamePassword = strURL.Mid(iPos, iAlphaSign - iPos);
// first extract domain, if protocol is smb
if (m_strProtocol.Equals("smb"))
{
int iSemiColon = strUserNamePassword.Find(";");
if (iSemiColon >= 0)
{
m_strDomain = strUserNamePassword.Left(iSemiColon);
strUserNamePassword.Delete(0, iSemiColon + 1);
}
}
// username:password
int iColon = strUserNamePassword.Find(":");
if (iColon >= 0)
{
m_strUserName = strUserNamePassword.Left(iColon);
iColon++;
m_strPassword = strUserNamePassword.Right(strUserNamePassword.size() - iColon);
}
// username
else
{
m_strUserName = strUserNamePassword;
}
iPos = iAlphaSign + 1;
iSlash = strURL.Find("/", iAlphaSign);
if(iSlash >= iEnd)
iSlash = -1;
}
}
// detect hostname:port/
if (iSlash < 0)
{
CStdString strHostNameAndPort = strURL.Mid(iPos, iEnd - iPos);
int iColon = strHostNameAndPort.Find(":");
if (iColon >= 0)
{
m_strHostName = strHostNameAndPort.Left(iColon);
iColon++;
CStdString strPort = strHostNameAndPort.Right(strHostNameAndPort.size() - iColon);
m_iPort = atoi(strPort.c_str());
}
else
{
m_strHostName = strHostNameAndPort;
}
}
else
{
CStdString strHostNameAndPort = strURL.Mid(iPos, iSlash - iPos);
int iColon = strHostNameAndPort.Find(":");
if (iColon >= 0)
{
m_strHostName = strHostNameAndPort.Left(iColon);
iColon++;
CStdString strPort = strHostNameAndPort.Right(strHostNameAndPort.size() - iColon);
m_iPort = atoi(strPort.c_str());
}
else
{
m_strHostName = strHostNameAndPort;
}
iPos = iSlash + 1;
if (iEnd > iPos)
{
m_strFileName = strURL.Mid(iPos, iEnd - iPos);
iSlash = m_strFileName.Find("/");
if(iSlash < 0)
m_strShareName = m_strFileName;
else
m_strShareName = m_strFileName.Left(iSlash);
}
}
// iso9960 doesnt have an hostname;-)
if (m_strProtocol.CompareNoCase("iso9660") == 0
|| m_strProtocol.CompareNoCase("musicdb") == 0
|| m_strProtocol.CompareNoCase("videodb") == 0
|| m_strProtocol.CompareNoCase("sources") == 0
|| m_strProtocol.CompareNoCase("lastfm") == 0
|| m_strProtocol.CompareNoCase("pvr") == 0
|| m_strProtocol.Left(3).CompareNoCase("mem") == 0)
{
if (m_strHostName != "" && m_strFileName != "")
{
CStdString strFileName = m_strFileName;
m_strFileName.Format("%s/%s", m_strHostName.c_str(), strFileName.c_str());
m_strHostName = "";
}
else
{
if (!m_strHostName.IsEmpty() && strURL[iEnd-1]=='/')
m_strFileName = m_strHostName + "/";
else
m_strFileName = m_strHostName;
m_strHostName = "";
}
}
m_strFileName.Replace("\\", "/");
/* update extension */
SetFileName(m_strFileName);
/* decode urlencoding on this stuff */
if(URIUtils::ProtocolHasEncodedHostname(m_strProtocol))
{
Decode(m_strHostName);
// Validate it as it is likely to contain a filename
SetHostName(CUtil::ValidatePath(m_strHostName));
}
Decode(m_strUserName);
Decode(m_strPassword);
}
void CURL::SetFileName(const CStdString& strFileName)
{
m_strFileName = strFileName;
int slash = m_strFileName.find_last_of(GetDirectorySeparator());
int period = m_strFileName.find_last_of('.');
if(period != -1 && (slash == -1 || period > slash))
m_strFileType = m_strFileName.substr(period+1);
else
m_strFileType = "";
m_strFileType.Normalize();
}
void CURL::SetHostName(const CStdString& strHostName)
{
m_strHostName = strHostName;
}
void CURL::SetUserName(const CStdString& strUserName)
{
m_strUserName = strUserName;
}
void CURL::SetPassword(const CStdString& strPassword)
{
m_strPassword = strPassword;
}
void CURL::SetProtocol(const CStdString& strProtocol)
{
m_strProtocol = strProtocol;
m_strProtocol.ToLower();
}
void CURL::SetOptions(const CStdString& strOptions)
{
m_strOptions.Empty();
m_options.Clear();
if( strOptions.length() > 0)
{
if( strOptions[0] == '?' || strOptions[0] == '#' || strOptions[0] == ';' || strOptions.Find("xml") >=0 )
{
m_strOptions = strOptions;
m_options.AddOptions(m_strOptions);
}
else
CLog::Log(LOGWARNING, "%s - Invalid options specified for url %s", __FUNCTION__, strOptions.c_str());
}
}
void CURL::SetProtocolOptions(const CStdString& strOptions)
{
m_strProtocolOptions = strOptions;
}
void CURL::SetPort(int port)
{
m_iPort = port;
}
bool CURL::HasPort() const
{
return (m_iPort != 0);
}
int CURL::GetPort() const
{
return m_iPort;
}
const CStdString& CURL::GetHostName() const
{
return m_strHostName;
}
const CStdString& CURL::GetShareName() const
{
return m_strShareName;
}
const CStdString& CURL::GetDomain() const
{
return m_strDomain;
}
const CStdString& CURL::GetUserName() const
{
return m_strUserName;
}
const CStdString& CURL::GetPassWord() const
{
return m_strPassword;
}
const CStdString& CURL::GetFileName() const
{
return m_strFileName;
}
const CStdString& CURL::GetProtocol() const
{
return m_strProtocol;
}
const CStdString CURL::GetTranslatedProtocol() const
{
return TranslateProtocol(m_strProtocol);
}
const CStdString& CURL::GetFileType() const
{
return m_strFileType;
}
const CStdString& CURL::GetOptions() const
{
return m_strOptions;
}
const CStdString& CURL::GetProtocolOptions() const
{
return m_strProtocolOptions;
}
const CStdString CURL::GetFileNameWithoutPath() const
{
// *.zip and *.rar store the actual zip/rar path in the hostname of the url
if ((m_strProtocol == "rar" ||
m_strProtocol == "zip" ||
m_strProtocol == "apk") &&
m_strFileName.IsEmpty())
return URIUtils::GetFileName(m_strHostName);
// otherwise, we've already got the filepath, so just grab the filename portion
CStdString file(m_strFileName);
URIUtils::RemoveSlashAtEnd(file);
return URIUtils::GetFileName(file);
}
char CURL::GetDirectorySeparator() const
{
#ifndef _LINUX
if ( IsLocal() )
return '\\';
else
#endif
return '/';
}
CStdString CURL::Get() const
{
unsigned int sizeneed = m_strProtocol.length()
+ m_strDomain.length()
+ m_strUserName.length()
+ m_strPassword.length()
+ m_strHostName.length()
+ m_strFileName.length()
+ m_strOptions.length()
+ m_strProtocolOptions.length()
+ 10;
if (m_strProtocol == "")
return m_strFileName;
CStdString strURL;
strURL.reserve(sizeneed);
strURL = GetWithoutFilename();
strURL += m_strFileName;
if( m_strOptions.length() > 0 )
strURL += m_strOptions;
if (m_strProtocolOptions.length() > 0)
strURL += "|"+m_strProtocolOptions;
return strURL;
}
CStdString CURL::GetWithoutUserDetails() const
{
CStdString strURL;
if (m_strProtocol.Equals("stack"))
{
CFileItemList items;
CStdString strURL2;
strURL2 = Get();
XFILE::CStackDirectory dir;
dir.GetDirectory(strURL2,items);
vector<CStdString> newItems;
for (int i=0;i<items.Size();++i)
{
CURL url(items[i]->GetPath());
items[i]->SetPath(url.GetWithoutUserDetails());
newItems.push_back(items[i]->GetPath());
}
dir.ConstructStackPath(newItems,strURL);
return strURL;
}
unsigned int sizeneed = m_strProtocol.length()
+ m_strDomain.length()
+ m_strHostName.length()
+ m_strFileName.length()
+ m_strOptions.length()
+ m_strProtocolOptions.length()
+ 10;
strURL.reserve(sizeneed);
if (m_strProtocol == "")
return m_strFileName;
strURL = m_strProtocol;
strURL += "://";
if (m_strHostName != "")
{
if (URIUtils::ProtocolHasParentInHostname(m_strProtocol))
strURL += CURL(m_strHostName).GetWithoutUserDetails();
else
strURL += m_strHostName;
if ( HasPort() )
{
CStdString strPort;
strPort.Format("%i", m_iPort);
strURL += ":";
strURL += strPort;
}
strURL += "/";
}
strURL += m_strFileName;
if( m_strOptions.length() > 0 )
strURL += m_strOptions;
if( m_strProtocolOptions.length() > 0 )
strURL += "|"+m_strProtocolOptions;
return strURL;
}
CStdString CURL::GetWithoutFilename() const
{
if (m_strProtocol == "")
return "";
unsigned int sizeneed = m_strProtocol.length()
+ m_strDomain.length()
+ m_strUserName.length()
+ m_strPassword.length()
+ m_strHostName.length()
+ 10;
CStdString strURL;
strURL.reserve(sizeneed);
strURL = m_strProtocol;
strURL += "://";
if (m_strDomain != "")
{
strURL += m_strDomain;
strURL += ";";
}
else if (m_strUserName != "")
{
strURL += URLEncodeInline(m_strUserName);
if (m_strPassword != "")
{
strURL += ":";
strURL += URLEncodeInline(m_strPassword);
}
strURL += "@";
}
else if (m_strDomain != "")
strURL += "@";
if (m_strHostName != "")
{
if( URIUtils::ProtocolHasEncodedHostname(m_strProtocol) )
strURL += URLEncodeInline(m_strHostName);
else
strURL += m_strHostName;
if (HasPort())
{
CStdString strPort;
strPort.Format("%i", m_iPort);
strURL += ":";
strURL += strPort;
}
strURL += "/";
}
return strURL;
}
bool CURL::IsLocal() const
{
return (IsLocalHost() || m_strProtocol.IsEmpty());
}
bool CURL::IsLocalHost() const
{
return (m_strHostName.Equals("localhost") || m_strHostName.Equals("127.0.0.1"));
}
bool CURL::IsFileOnly(const CStdString &url)
{
return url.find_first_of("/\\") == CStdString::npos;
}
bool CURL::IsFullPath(const CStdString &url)
{
if (url.size() && url[0] == '/') return true; // /foo/bar.ext
if (url.Find("://") >= 0) return true; // foo://bar.ext
if (url.size() > 1 && url[1] == ':') return true; // c:\\foo\\bar\\bar.ext
if (url.compare(0,2,"\\\\") == 0) return true; // \\UNC\path\to\file
return false;
}
void CURL::Decode(CStdString& strURLData)
//modified to be more accomodating - if a non hex value follows a % take the characters directly and don't raise an error.
// However % characters should really be escaped like any other non safe character (www.rfc-editor.org/rfc/rfc1738.txt)
{
CStdString strResult;
/* result will always be less than source */
strResult.reserve( strURLData.length() );
for (unsigned int i = 0; i < strURLData.size(); ++i)
{
int kar = (unsigned char)strURLData[i];
if (kar == '+') strResult += ' ';
else if (kar == '%')
{
if (i < strURLData.size() - 2)
{
CStdString strTmp;
strTmp.assign(strURLData.substr(i + 1, 2));
int dec_num=-1;
sscanf(strTmp,"%x",(unsigned int *)&dec_num);
if (dec_num<0 || dec_num>255)
strResult += kar;
else
{
strResult += (char)dec_num;
i += 2;
}
}
else
strResult += kar;
}
else strResult += kar;
}
strURLData = strResult;
}
void CURL::Encode(CStdString& strURLData)
{
CStdString strResult;
/* wonder what a good value is here is, depends on how often it occurs */
strResult.reserve( strURLData.length() * 2 );
for (int i = 0; i < (int)strURLData.size(); ++i)
{
int kar = (unsigned char)strURLData[i];
//if (kar == ' ') strResult += '+'; // obsolete
if (isalnum(kar) || strchr("-_.!()" , kar) ) // Don't URL encode these according to RFC1738
{
strResult += kar;
}
else
{
CStdString strTmp;
strTmp.Format("%%%02.2x", kar);
strResult += strTmp;
}
}
strURLData = strResult;
}
std::string CURL::Decode(const std::string& strURLData)
{
CStdString url = strURLData;
Decode(url);
return url;
}
std::string CURL::Encode(const std::string& strURLData)
{
CStdString url = strURLData;
Encode(url);
return url;
}
CStdString CURL::TranslateProtocol(const CStdString& prot)
{
if (prot == "shout"
|| prot == "daap"
|| prot == "dav"
|| prot == "tuxbox"
|| prot == "lastfm"
|| prot == "rss")
return "http";
if (prot == "davs")
return "https";
return prot;
}
void CURL::GetOptions(std::map<CStdString, CStdString> &options) const
{
CUrlOptions::UrlOptions optionsMap = m_options.GetOptions();
for (CUrlOptions::UrlOptions::const_iterator option = optionsMap.begin(); option != optionsMap.end(); option++)
options[option->first] = option->second.asString();
}
bool CURL::HasOption(const CStdString &key) const
{
return m_options.HasOption(key);
}
bool CURL::GetOption(const CStdString &key, CStdString &value) const
{
CVariant valueObj;
if (!m_options.GetOption(key, valueObj))
return false;
value = valueObj.asString();
return true;
}
CStdString CURL::GetOption(const CStdString &key) const
{
CStdString value;
if (!GetOption(key, value))
return "";
return value;
}
void CURL::SetOption(const CStdString &key, const CStdString &value)
{
m_options.AddOption(key, value);
SetOptions(m_options.GetOptionsString(true));
}
void CURL::RemoveOption(const CStdString &key)
{
m_options.RemoveOption(key);
SetOptions(m_options.GetOptionsString(true));
}