bool RequestURI::virtualFileExists(const VirtualHost *vhost, const string &sourceRoot, const string &pathTranslation, CStrRef filename) { if (filename.empty() || filename.charAt(filename.length() - 1) == '/') { return false; } if (!vhost->getDocumentRoot().empty()) { string fullname = filename.data(); if (fullname[0] == '/') { fullname = fullname.substr(1); } else { fullname = pathTranslation + fullname; } m_path = fullname; m_absolutePath = String(sourceRoot) + m_path; processExt(); if (StaticContentCache::TheFileCache && !fullname.empty() && StaticContentCache::TheFileCache->fileExists(fullname.c_str())) { return true; } struct stat st; return RuntimeOption::AllowedFiles.find(fullname.c_str()) != RuntimeOption::AllowedFiles.end() || (stat(m_absolutePath.c_str(), &st) == 0 && (st.st_mode & S_IFMT) == S_IFREG); } m_path = filename; m_absolutePath = String(sourceRoot) + filename; processExt(); return true; }
bool RequestURI::virtualFileExists(const VirtualHost *vhost, const std::string &sourceRoot, const std::string &pathTranslation, const String& filename) { if (filename.empty() || filename.charAt(filename.length() - 1) == '/') { return false; } String canon(Util::canonicalize(filename.c_str(), filename.size()), AttachString); if (!vhost->getDocumentRoot().empty()) { std::string fullname = canon.data(); int i = 0; while (i < fullname.size() && fullname[i] == '/') ++i; if (i) { fullname = fullname.substr(i); } if (!i || !m_rewritten) { fullname = pathTranslation + fullname; } m_path = fullname; m_absolutePath = String(sourceRoot) + m_path; processExt(); if (RuntimeOption::PathDebug) { m_triedURLs.push_back(m_absolutePath.toCppString()); } if (StaticContentCache::TheFileCache && !fullname.empty() && StaticContentCache::TheFileCache->fileExists(fullname.c_str())) { return true; } struct stat st; return RuntimeOption::AllowedFiles.find(fullname.c_str()) != RuntimeOption::AllowedFiles.end() || (stat(m_absolutePath.c_str(), &st) == 0 && (st.st_mode & S_IFMT) == S_IFREG); } m_path = canon; m_absolutePath = String(sourceRoot) + canon; processExt(); return true; }
bool RequestURI::virtualFolderExists(const VirtualHost *vhost, const string &sourceRoot, const string &pathTranslation, const String& foldername) { if (!vhost->getDocumentRoot().empty()) { string fullname = foldername.data(); // If there is a trailing slash, remove it if (fullname.size() > 0 && fullname[fullname.size()-1] == '/') { fullname = fullname.substr(fullname.size()-1); } if (fullname[0] == '/') { fullname = fullname.substr(1); } else { fullname = pathTranslation + fullname; } m_path = fullname; m_absolutePath = String(sourceRoot) + m_path; processExt(); if (StaticContentCache::TheFileCache && !fullname.empty() && StaticContentCache::TheFileCache->dirExists(fullname.c_str())) { return true; } const vector<string> &allowedDirectories = VirtualHost::GetAllowedDirectories(); if (find(allowedDirectories.begin(), allowedDirectories.end(), fullname.c_str()) != allowedDirectories.end()) { return true; } struct stat st; return (stat(m_absolutePath.c_str(), &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR); } m_path = foldername; m_absolutePath = String(sourceRoot) + foldername; processExt(); return true; }
bool RequestURI::process(const VirtualHost *vhost, Transport *transport, const std::string &sourceRoot, const std::string &pathTranslation, const char *url) { splitURL(url, m_originalURL, m_queryString); m_originalURL = StringUtil::UrlDecode(m_originalURL, false); m_rewritten = false; auto pathTranslated = transport->getPathTranslated(); if (!pathTranslated.empty()) { // The transport is overriding everything and just handing us the filename m_path = m_absolutePath = pathTranslated; processExt(); return true; } // Fast path for files that exist if (vhost->checkExistenceBeforeRewrite()) { String canon( Util::canonicalize(m_originalURL.c_str(), m_originalURL.size()), AttachString); if (virtualFileExists(vhost, sourceRoot, pathTranslation, canon)) { m_rewrittenURL = canon; m_resolvedURL = canon; return true; } } if (!rewriteURL(vhost, transport, pathTranslation, sourceRoot)) { // Redirection m_done = true; return true; } if (!resolveURL(vhost, pathTranslation, sourceRoot)) { // Can't find return false; } return true; }