2 * Copyright (C) 2011-2013 Team XBMC
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)
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.
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/>.
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"
29 #define DEFAULT_PAGE "index.html"
32 using namespace ADDON;
33 using namespace XFILE;
35 bool CHTTPWebinterfaceHandler::CheckHTTPRequest(const HTTPRequest &request)
40 int CHTTPWebinterfaceHandler::HandleHTTPRequest(const HTTPRequest &request)
42 m_responseCode = ResolveUrl(request.url, m_url);
43 if (m_responseCode != MHD_HTTP_OK)
45 if (m_responseCode == MHD_HTTP_FOUND)
46 m_responseType = HTTPRedirect;
48 m_responseType = HTTPError;
53 m_responseType = HTTPFileDownload;
58 int CHTTPWebinterfaceHandler::ResolveUrl(const std::string &url, std::string &path)
61 return ResolveUrl(url, path, dummyAddon);
64 int CHTTPWebinterfaceHandler::ResolveUrl(const std::string &url, std::string &path, AddonPtr &addon)
67 bool useDefaultWebInterface = true;
70 if (url.find("/addons/") == 0 && url.size() > 8)
72 CStdStringArray components;
73 CUtil::Tokenize(path, components, "/");
74 if (components.size() > 1)
76 CAddonMgr::Get().GetAddon(components.at(1), addon);
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
86 return MHD_HTTP_FOUND;
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/");
96 return MHD_HTTP_NOT_FOUND;
99 if (path.compare("/") == 0)
100 path.append(DEFAULT_PAGE);
102 if (useDefaultWebInterface)
104 CAddonMgr::Get().GetDefault(ADDON_WEB_INTERFACE, addon);
106 addonPath = addon->Path();
110 path = URIUtils::AddFileToFolder(addonPath, path);
112 string realPath = URIUtils::GetRealPath(path);
113 string realAddonPath = URIUtils::GetRealPath(addonPath);
114 if (!URIUtils::IsInPath(realPath, realAddonPath))
115 return MHD_HTTP_NOT_FOUND;
117 if (CDirectory::Exists(path))
119 if (path.at(path.size() -1) == '/')
120 path.append(DEFAULT_PAGE);
124 return MHD_HTTP_FOUND;
128 if (!CFile::Exists(path))
129 return MHD_HTTP_NOT_FOUND;