Merge pull request #4314 from MartijnKaijser/beta1
[vuplus_xbmc] / xbmc / filesystem / HTTPDirectory.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "HTTPDirectory.h"
22 #include "URL.h"
23 #include "CurlFile.h"
24 #include "FileItem.h"
25 #include "utils/RegExp.h"
26 #include "settings/AdvancedSettings.h"
27 #include "utils/StringUtils.h"
28 #include "utils/CharsetConverter.h"
29 #include "utils/log.h"
30 #include "utils/URIUtils.h"
31 #include "utils/HTMLUtil.h"
32 #include "climits"
33
34 using namespace XFILE;
35
36 CHTTPDirectory::CHTTPDirectory(void){}
37 CHTTPDirectory::~CHTTPDirectory(void){}
38
39 bool CHTTPDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
40 {
41   CCurlFile http;
42   CURL url(strPath);
43
44   CStdString strName, strLink;
45   CStdString strBasePath = url.GetFileName();
46
47   if(!http.Open(url))
48   {
49     CLog::Log(LOGERROR, "%s - Unable to get http directory", __FUNCTION__);
50     return false;
51   }
52
53   CRegExp reItem(true); // HTML is case-insensitive
54   reItem.RegComp("<a href=\"(.*)\">(.*)</a>");
55
56   CRegExp reDateTime(true);
57   reDateTime.RegComp("<td align=\"right\">([0-9]{2})-([A-Z]{3})-([0-9]{4}) ([0-9]{2}):([0-9]{2}) +</td>");
58   
59   CRegExp reDateTimeLighttp(true);
60   reDateTimeLighttp.RegComp("<td class=\"m\">([0-9]{4})-([A-Z]{3})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})</td>");
61
62   CRegExp reDateTimeNginx(true);
63   reDateTimeNginx.RegComp("</a> +([0-9]{2})-([A-Z]{3})-([0-9]{4}) ([0-9]{2}):([0-9]{2}) ");
64
65   CRegExp reSize(true);
66   reSize.RegComp("> *([0-9.]+)(B|K|M|G| )</td>");
67
68   CRegExp reSizeNginx(true);
69   reSizeNginx.RegComp("([0-9]+)(B|K|M|G)?$");
70
71   /* read response from server into string buffer */
72   char buffer[MAX_PATH + 1024];
73   while(http.ReadString(buffer, sizeof(buffer)-1))
74   {
75     CStdString strBuffer = buffer;
76     std::string fileCharset(http.GetServerReportedCharset());
77     if (!fileCharset.empty() && fileCharset != "UTF-8")
78     {
79       std::string converted;
80       if (g_charsetConverter.ToUtf8(fileCharset, strBuffer, converted) && !converted.empty())
81         strBuffer = converted;
82     }
83
84     StringUtils::RemoveCRLF(strBuffer);
85
86     if (reItem.RegFind(strBuffer.c_str()) >= 0)
87     {
88       strLink = reItem.GetMatch(1);
89       strName = reItem.GetMatch(2);
90
91       if(strLink[0] == '/')
92         strLink = strLink.substr(1);
93
94       CStdString strNameTemp = StringUtils::Trim(strName);
95
96       CStdStringW wName, wLink, wConverted;
97       if (fileCharset.empty())
98         g_charsetConverter.unknownToUTF8(strNameTemp);
99       g_charsetConverter.utf8ToW(strNameTemp, wName, false);
100       HTML::CHTMLUtil::ConvertHTMLToW(wName, wConverted);
101       g_charsetConverter.wToUTF8(wConverted, strNameTemp);
102       URIUtils::RemoveSlashAtEnd(strNameTemp);
103
104       CStdString strLinkBase = strLink;
105       CStdString strLinkOptions;
106
107       // split link with url options
108       size_t pos = strLinkBase.find('?');
109       if (pos != std::string::npos) {
110         strLinkOptions = strLinkBase.substr(pos);
111         strLinkBase.erase(pos);
112       }
113       CStdString strLinkTemp = strLinkBase;
114
115       URIUtils::RemoveSlashAtEnd(strLinkTemp);
116       strLinkTemp = CURL::Decode(strLinkTemp);
117       if (fileCharset.empty())
118         g_charsetConverter.unknownToUTF8(strLinkTemp);
119       g_charsetConverter.utf8ToW(strLinkTemp, wLink, false);
120       HTML::CHTMLUtil::ConvertHTMLToW(wLink, wConverted);
121       g_charsetConverter.wToUTF8(wConverted, strLinkTemp);
122
123       if (StringUtils::EndsWith(strNameTemp, "..>") &&
124           StringUtils::StartsWith(strLinkTemp, strNameTemp.substr(0, strNameTemp.length() - 3)))
125         strName = strNameTemp = strLinkTemp;
126
127       // we detect http directory items by its display name and its stripped link
128       // if same, we consider it as a valid item.
129       if (strNameTemp == strLinkTemp && strLinkTemp != "..")
130       {
131         CFileItemPtr pItem(new CFileItem(strNameTemp));
132         pItem->SetProperty("IsHTTPDirectory", true);
133         url.SetFileName(strBasePath + strLinkBase);
134         url.SetOptions(strLinkOptions);
135         pItem->SetPath(url.Get());
136
137         if(URIUtils::HasSlashAtEnd(pItem->GetPath(), true))
138           pItem->m_bIsFolder = true;
139
140         CStdString day, month, year, hour, minute;
141
142         if (reDateTime.RegFind(strBuffer.c_str()) >= 0)
143         {
144           day = reDateTime.GetMatch(1);
145           month = reDateTime.GetMatch(2);
146           year = reDateTime.GetMatch(3);
147           hour = reDateTime.GetMatch(4);
148           minute = reDateTime.GetMatch(5);
149         }
150         else if (reDateTimeNginx.RegFind(strBuffer.c_str()) >= 0)
151         {
152           day = reDateTimeNginx.GetMatch(1);
153           month = reDateTimeNginx.GetMatch(2);
154           year = reDateTimeNginx.GetMatch(3);
155           hour = reDateTimeNginx.GetMatch(4);
156           minute = reDateTimeNginx.GetMatch(5);
157         }
158         else if (reDateTimeLighttp.RegFind(strBuffer.c_str()) >= 0)
159         {
160           day = reDateTimeLighttp.GetMatch(3);
161           month = reDateTimeLighttp.GetMatch(2);
162           year = reDateTimeLighttp.GetMatch(1);
163           hour = reDateTimeLighttp.GetMatch(4);
164           minute = reDateTimeLighttp.GetMatch(5);
165         }
166
167         if (day.length() > 0 && month.length() > 0 && year.length() > 0)
168         {
169           pItem->m_dateTime = CDateTime(atoi(year.c_str()), CDateTime::MonthStringToMonthNum(month), atoi(day.c_str()), atoi(hour.c_str()), atoi(minute.c_str()), 0);
170         }
171
172         if (!pItem->m_bIsFolder)
173         {
174           if (reSize.RegFind(strBuffer.c_str()) >= 0)
175           {
176             double Size = atof(reSize.GetMatch(1).c_str());
177             std::string strUnit(reSize.GetMatch(2));
178
179             if (strUnit == "K")
180               Size = Size * 1024;
181             else if (strUnit == "M")
182               Size = Size * 1024 * 1024;
183             else if (strUnit == "G")
184               Size = Size * 1024 * 1024 * 1024;
185
186             pItem->m_dwSize = (int64_t)Size;
187           }
188           else if (reSizeNginx.RegFind(strBuffer.c_str()) >= 0)
189           {
190             double Size = atof(reSizeNginx.GetMatch(1).c_str());
191             std::string strUnit(reSize.GetMatch(2));
192
193             if (strUnit == "K")
194               Size = Size * 1024;
195             else if (strUnit == "M")
196               Size = Size * 1024 * 1024;
197             else if (strUnit == "G")
198               Size = Size * 1024 * 1024 * 1024;
199
200             pItem->m_dwSize = (int64_t)Size;
201           }
202           else
203           if (g_advancedSettings.m_bHTTPDirectoryStatFilesize) // As a fallback get the size by stat-ing the file (slow)
204           {
205             CCurlFile file;
206             file.Open(url);
207             pItem->m_dwSize=file.GetLength();
208             file.Close();
209           }
210         }
211         items.Add(pItem);
212       }
213     }
214   }
215   http.Close();
216
217   items.SetProperty("IsHTTPDirectory", true);
218
219   return true;
220 }
221
222 bool CHTTPDirectory::Exists(const char* strPath)
223 {
224   CCurlFile http;
225   CURL url(strPath);
226   struct __stat64 buffer;
227
228   if( http.Stat(url, &buffer) != 0 )
229   {
230     return false;
231   }
232
233   if (buffer.st_mode == _S_IFDIR)
234           return true;
235
236   return false;
237 }