|
| 1 | +/*************************************************************** |
| 2 | + * |
| 3 | + * Copyright (C) 2024, Pelican Project, Morgridge Institute for Research |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you |
| 6 | + * may not use this file except in compliance with the License. You may |
| 7 | + * obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + ***************************************************************/ |
| 18 | + |
| 19 | +#include "HTTPDirectory.hh" |
| 20 | +#include "HTTPCommands.hh" |
| 21 | +#include "HTTPFile.hh" |
| 22 | +#include "HTTPFileSystem.hh" |
| 23 | +#include "logging.hh" |
| 24 | +#include "stl_string_utils.hh" |
| 25 | + |
| 26 | +#include <XrdOuc/XrdOucEnv.hh> |
| 27 | +#include <XrdOuc/XrdOucStream.hh> |
| 28 | +#include <XrdSec/XrdSecEntity.hh> |
| 29 | +#include <XrdSec/XrdSecEntityAttr.hh> |
| 30 | +#include <XrdSfs/XrdSfsInterface.hh> |
| 31 | +#include <XrdSys/XrdSysError.hh> |
| 32 | +#include <XrdVersion.hh> |
| 33 | + |
| 34 | +#include <curl/curl.h> |
| 35 | +#include <filesystem> |
| 36 | +#include <iostream> |
| 37 | +#include <map> |
| 38 | +#include <memory> |
| 39 | +#include <mutex> |
| 40 | +#include <regex> |
| 41 | +#include <sstream> |
| 42 | +#include <string> |
| 43 | +#include <tinyxml2.h> |
| 44 | + |
| 45 | +using namespace XrdHTTPServer; |
| 46 | + |
| 47 | +HTTPDirectory::HTTPDirectory(XrdSysError &log, HTTPFileSystem &oss) |
| 48 | + : m_log(log), m_oss(oss) {} // Initialize it to false. |
| 49 | + |
| 50 | +void HTTPDirectory::parseHTMLToListing(const std::string &htmlContent) { |
| 51 | + m_remoteList.clear(); |
| 52 | + |
| 53 | + tinyxml2::XMLDocument doc; |
| 54 | + tinyxml2::XMLError error = doc.Parse(htmlContent.c_str()); |
| 55 | + if (error != tinyxml2::XML_SUCCESS) { |
| 56 | + m_log.Log(LogMask::Warning, "HTTPDirectory", |
| 57 | + "Failed to parse HTML in directory response"); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // Root of the HTML document |
| 62 | + auto root = doc.FirstChild(); |
| 63 | + if (!root) { |
| 64 | + m_log.Log(LogMask::Warning, "HTTPDirectory", |
| 65 | + "No root found in HTML in directory response"); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // Traverse the rows in the table |
| 70 | + for (auto row = root->FirstChildElement("tr"); row != nullptr; |
| 71 | + row = row->NextSiblingElement("tr")) { |
| 72 | + Entry entry; |
| 73 | + int columnIndex = 0; |
| 74 | + |
| 75 | + // Traverse each cell in the row |
| 76 | + for (auto cell = row->FirstChildElement("td"); cell != nullptr; |
| 77 | + cell = cell->NextSiblingElement("td")) { |
| 78 | + const char *cellText = cell->GetText() ? cell->GetText() : ""; |
| 79 | + |
| 80 | + switch (columnIndex) { |
| 81 | + case 0: // Mode |
| 82 | + entry.mode = cellText; |
| 83 | + break; |
| 84 | + case 1: // Flags |
| 85 | + entry.flags = cellText; |
| 86 | + break; |
| 87 | + case 2: // Size |
| 88 | + entry.size = cellText; |
| 89 | + break; |
| 90 | + case 3: // Modified |
| 91 | + entry.modified = cellText; |
| 92 | + break; |
| 93 | + case 4: // Name |
| 94 | + if (auto aTag = cell->FirstChildElement("a")) { |
| 95 | + const char *nameText = |
| 96 | + aTag->GetText() ? aTag->GetText() : ""; |
| 97 | + entry.name = nameText; |
| 98 | + } |
| 99 | + break; |
| 100 | + default: |
| 101 | + break; |
| 102 | + } |
| 103 | + columnIndex++; |
| 104 | + } |
| 105 | + |
| 106 | + // Skip adding invalid/empty rows |
| 107 | + if (entry.name.empty()) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + struct stat workingFile; |
| 112 | + workingFile.st_size = std::stoul(entry.size, nullptr, 10); |
| 113 | + // workingFile.st_mtime = std::stoul(entry.modified, nullptr, 10); |
| 114 | + if (entry.mode.substr(0, 1) == "d") |
| 115 | + workingFile.st_mode = 0600 | S_IFDIR; |
| 116 | + else |
| 117 | + workingFile.st_mode = 0600 | S_IFREG; |
| 118 | + |
| 119 | + workingFile.st_nlink = 1; |
| 120 | + workingFile.st_uid = 1; |
| 121 | + workingFile.st_gid = 1; |
| 122 | + workingFile.st_atime = 0; |
| 123 | + workingFile.st_ctime = 0; |
| 124 | + workingFile.st_dev = 0; |
| 125 | + workingFile.st_ino = 0; |
| 126 | + m_remoteList.push_back({entry.name, workingFile}); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +std::string HTTPDirectory::extractHTMLTable(const std::string &htmlContent) { |
| 131 | + std::regex tableRegex(R"(<table[^>]*>[\s\S]*?</table>)", |
| 132 | + std::regex_constants::icase); |
| 133 | + |
| 134 | + std::smatch match; |
| 135 | + if (std::regex_search(htmlContent, match, tableRegex)) { |
| 136 | + return match.str(); |
| 137 | + } |
| 138 | + |
| 139 | + return ""; // Return an empty string if no table is found |
| 140 | +} |
| 141 | + |
| 142 | +int HTTPDirectory::Readdir(char *buff, int blen) { |
| 143 | + if (m_remoteList.size() > 0) { |
| 144 | + std::string name = m_remoteList.begin()->first; |
| 145 | + struct stat currentRecord = m_remoteList.begin()->second; |
| 146 | + mystat->st_size = currentRecord.st_size; |
| 147 | + mystat->st_mode = currentRecord.st_mode; |
| 148 | + mystat->st_nlink = currentRecord.st_nlink; |
| 149 | + mystat->st_uid = currentRecord.st_uid; |
| 150 | + mystat->st_gid = currentRecord.st_gid; |
| 151 | + mystat->st_atime = currentRecord.st_atime; |
| 152 | + mystat->st_ctime = currentRecord.st_ctime; |
| 153 | + mystat->st_dev = currentRecord.st_dev; |
| 154 | + mystat->st_ino = currentRecord.st_ino; |
| 155 | + memcpy(buff, name.c_str(), name.size() + 1); |
| 156 | + m_remoteList.erase(m_remoteList.begin()); |
| 157 | + return name.size(); |
| 158 | + } else { |
| 159 | + buff[0] = '\0'; |
| 160 | + return 0; |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +int HTTPDirectory::Opendir(const char *path, XrdOucEnv &env) { |
| 165 | + m_log.Log(LogMask::Debug, "HTTPDirectory::Opendir", "Opendir called"); |
| 166 | + auto configured_hostname = m_oss.getHTTPHostName(); |
| 167 | + auto configured_hostUrl = m_oss.getHTTPHostUrl(); |
| 168 | + const auto &configured_url_base = m_oss.getHTTPUrlBase(); |
| 169 | + if (!configured_url_base.empty()) { |
| 170 | + configured_hostUrl = configured_url_base; |
| 171 | + configured_hostname = m_oss.getStoragePrefix(); |
| 172 | + } |
| 173 | + |
| 174 | + // |
| 175 | + // Check the path for validity. |
| 176 | + // |
| 177 | + std::string object; |
| 178 | + int rv = parse_path(m_oss.getHTTPHostName(), path, object); |
| 179 | + |
| 180 | + if (rv != 0) { |
| 181 | + return rv; |
| 182 | + } |
| 183 | + |
| 184 | + if (m_remoteList.empty()) { |
| 185 | + m_log.Log(LogMask::Debug, "HTTPFile::Opendir", "Opendir called"); |
| 186 | + HTTPList list(configured_hostUrl, object, m_log, m_oss.getToken()); |
| 187 | + m_log.Log(LogMask::Debug, "HTTPDirectory::Opendir", |
| 188 | + "About to perform download from HTTPDirectory::Opendir(): " |
| 189 | + "hostname / object:", |
| 190 | + configured_hostname.c_str(), object.c_str()); |
| 191 | + if (!list.SendRequest()) { |
| 192 | + std::stringstream ss; |
| 193 | + ss << "Failed to send GetObject command: " << list.getResponseCode() |
| 194 | + << "'" << list.getResultString() << "'"; |
| 195 | + m_log.Log(LogMask::Warning, "HTTPDirectory::Opendir", |
| 196 | + ss.str().c_str()); |
| 197 | + return 0; |
| 198 | + } |
| 199 | + |
| 200 | + parseHTMLToListing(extractHTMLTable(list.getResultString())); |
| 201 | + } |
| 202 | + |
| 203 | + return 0; |
| 204 | +} |
0 commit comments