[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / network / httprequesthandler / HTTPWebinterfaceHandler.cpp
1 /*
2  *      Copyright (C) 2011-2013 Team XBMC
3  *      http://www.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 "HTTPWebinterfaceHandler.h"
22 #include "network/WebServer.h"
23 #include "addons/AddonManager.h"
24 #include "utils/URIUtils.h"
25 #include "filesystem/Directory.h"
26 #include "filesystem/File.h"
27 #include "Util.h"
28
29 #define DEFAULT_PAGE        "index.html"
30
31 using namespace std;
32 using namespace ADDON;
33 using namespace XFILE;
34
35 bool CHTTPWebinterfaceHandler::CheckHTTPRequest(const HTTPRequest &request)
36 {
37   return true;
38 }
39
40 int CHTTPWebinterfaceHandler::HandleHTTPRequest(const HTTPRequest &request)
41 {
42   m_responseCode = ResolveUrl(request.url, m_url);
43   if (m_responseCode != MHD_HTTP_OK)
44   {
45     if (m_responseCode == MHD_HTTP_FOUND)
46       m_responseType = HTTPRedirect;
47     else
48       m_responseType = HTTPError;
49
50     return MHD_YES;
51   }
52
53   m_responseType = HTTPFileDownload;
54
55   return MHD_YES;
56 }
57
58 int CHTTPWebinterfaceHandler::ResolveUrl(const std::string &url, std::string &path)
59 {
60   AddonPtr dummyAddon;
61   return ResolveUrl(url, path, dummyAddon);
62 }
63
64 int CHTTPWebinterfaceHandler::ResolveUrl(const std::string &url, std::string &path, AddonPtr &addon)
65 {
66   string addonPath;
67   bool useDefaultWebInterface = true;
68
69   path = url;
70   if (url.find("/addons/") == 0 && url.size() > 8)
71   {
72     CStdStringArray components;
73     CUtil::Tokenize(path, components, "/");
74     if (components.size() > 1)
75     {
76       CAddonMgr::Get().GetAddon(components.at(1), addon);
77       if (addon)
78       {
79         size_t pos;
80         pos = path.find('/', 8); // /addons/ = 8 characters +1 to start behind the last slash
81         if (pos != string::npos)
82           path = path.substr(pos);
83         else // missing trailing slash
84         {
85           path = url + "/";
86           return MHD_HTTP_FOUND;
87         }
88
89         useDefaultWebInterface = false;
90         addonPath = addon->Path();
91         if (addon->Type() != ADDON_WEB_INTERFACE) // No need to append /htdocs for web interfaces
92           addonPath = URIUtils::AddFileToFolder(addonPath, "/htdocs/");
93       }
94     }
95     else
96       return MHD_HTTP_NOT_FOUND;
97   }
98
99   if (path.compare("/") == 0)
100     path.append(DEFAULT_PAGE);
101
102   if (useDefaultWebInterface)
103   {
104     CAddonMgr::Get().GetDefault(ADDON_WEB_INTERFACE, addon);
105     if (addon)
106       addonPath = addon->Path();
107   }
108
109   if (addon)
110     path = URIUtils::AddFileToFolder(addonPath, path);
111
112   string realPath = URIUtils::GetRealPath(path);
113   string realAddonPath = URIUtils::GetRealPath(addonPath);
114   if (!URIUtils::IsInPath(realPath, realAddonPath))
115     return MHD_HTTP_NOT_FOUND;
116   
117   if (CDirectory::Exists(path))
118   {
119     if (path.at(path.size() -1) == '/')
120       path.append(DEFAULT_PAGE);
121     else
122     {
123       path = url + "/";
124       return MHD_HTTP_FOUND;
125     }
126   }
127
128   if (!CFile::Exists(path))
129     return MHD_HTTP_NOT_FOUND;
130
131   return MHD_HTTP_OK;
132 }