37#include "GatewayPathInfoResponseHandler.h"
41#include "BESInfoList.h"
44#include "BESRequestHandlerList.h"
45#include "BESRequestHandler.h"
47#include "BESDapNames.h"
48#include "BESDataNames.h"
49#include "BESCatalogList.h"
50#include "BESCatalog.h"
51#include "BESCatalogEntry.h"
52#include "BESCatalogUtils.h"
53#include "BESSyntaxUserError.h"
54#include "BESForbiddenError.h"
55#include "BESNotFoundError.h"
56#include "BESStopWatch.h"
64#define PATH_INFO_RESPONSE "PathInfo"
66#define VALID_PATH "validPath"
67#define REMAINDER "remainder"
68#define IS_DATA "isData"
69#define IS_FILE "isFile"
71#define IS_ACCESSIBLE "access"
73#define LMT "lastModified"
75GatewayPathInfoResponseHandler::GatewayPathInfoResponseHandler(
const string &name) :
80GatewayPathInfoResponseHandler::~GatewayPathInfoResponseHandler()
99 BESDEBUG(SPI_DEBUG_KEY,
"GatewayPathInfoResponseHandler::execute() - BEGIN" << endl );
101 BESInfo *info = BESInfoList::TheList()->build_info();
104 string container = dhi.
data[CONTAINER];
112 throw BESInternalError(
"Not able to find the default catalog.", __FILE__, __LINE__);
115 string::size_type notslash = container.find_first_not_of(
"/", 0);
116 if (notslash != string::npos) {
117 container = container.substr(notslash);
122 string::size_type slash = container.find_first_of(
"/", 0);
123 if (slash != string::npos) {
124 catname = container.substr(0, slash);
134 if (slash != string::npos) {
135 container = container.substr(slash + 1);
138 notslash = container.find_first_not_of(
"/", 0);
139 if (notslash != string::npos) {
140 container = container.substr(notslash);
148 if (container.empty()) container =
"/";
150 if (container[0] !=
'/') container =
"/" + container;
152 BESDEBUG(SPI_DEBUG_KEY,
"GatewayPathInfoResponseHandler::execute() - container: " << container << endl );
156 map<string, string> pathInfoAttrs;
157 pathInfoAttrs[PATH] = container;
159 info->begin_tag(PATH_INFO_RESPONSE, &pathInfoAttrs);
161 string validPath, remainder;
162 bool isFile, isDir, canRead;
163 long long size, time;
170 eval_resource_path(container, utils->
get_root_dir(), utils->follow_sym_links(), validPath, isFile, isDir, size,
171 time, canRead, remainder);
178 if (validPath.length() != 0) {
185 string err = (string)
"Failed to find the validPath node " + validPath
186 +
" this should not be possible. Some thing BAD is happening.";
191 list<string> services = entry->get_service_list();
194 if (services.size()) {
195 list<string>::const_iterator si = services.begin();
196 list<string>::const_iterator se = services.end();
197 for (; si != se; si++) {
198 if ((*si) == OPENDAP_SERVICE) isData =
true;
203 map<string, string> validPathAttrs;
204 validPathAttrs[IS_DATA] = isData ?
"true" :
"false";
205 validPathAttrs[IS_FILE] = isFile ?
"true" :
"false";
206 validPathAttrs[IS_DIR] = isDir ?
"true" :
"false";
207 validPathAttrs[IS_ACCESSIBLE] = canRead ?
"true" :
"false";
210 std::ostringstream os_size;
212 validPathAttrs[SIZE] = os_size.str();
215 std::ostringstream os_time;
217 validPathAttrs[LMT] = os_time.str();
219 info->add_tag(VALID_PATH, validPath, &validPathAttrs);
220 info->add_tag(REMAINDER, remainder);
222 info->end_tag(PATH_INFO_RESPONSE);
225 info->end_response();
227 BESDEBUG(SPI_DEBUG_KEY,
"GatewayPathInfoResponseHandler::execute() - END" << endl );
259 strm << BESIndent::LMarg <<
"GatewayPathInfoResponseHandler::dump - (" << (
void *)
this <<
")" << std::endl;
262 BESIndent::UnIndent();
266GatewayPathInfoResponseHandler::GatewayPathInfoResponseBuilder(
const string &name)
274void GatewayPathInfoResponseHandler::eval_resource_path(
const string &resource_path,
const string &catalog_root,
275 const bool follow_sym_links,
string &validPath,
bool &isFile,
bool &isDir,
long long &size,
276 long long &lastModifiedTime,
bool &canRead,
string &remainder)
279 BESDEBUG(SPI_DEBUG_KEY,
280 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"CatalogRoot: "<< catalog_root << endl);
282 BESDEBUG(SPI_DEBUG_KEY,
283 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"resourceID: "<< resource_path << endl);
288 lastModifiedTime = -1;
291 string rem = resource_path;
297 int (*ye_old_stat_function)(
const char *pathname,
struct stat *buf);
298 if (follow_sym_links) {
299 BESDEBUG(SPI_DEBUG_KEY,
300 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Using 'stat' function (follow_sym_links = true)" << endl);
301 ye_old_stat_function = &stat;
304 BESDEBUG(SPI_DEBUG_KEY,
305 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Using 'lstat' function (follow_sym_links = false)" << endl);
306 ye_old_stat_function = &lstat;
311 if (resource_path ==
"") {
312 BESDEBUG(SPI_DEBUG_KEY,
"GatewayPathInfoResponseHandler::"<<__func__ <<
"() - The resourceID is empty" << endl);
318 string::size_type dotdot = resource_path.find(
"..");
319 if (dotdot != string::npos) {
320 BESDEBUG(SPI_DEBUG_KEY,
321 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
" ERROR: The resourceID '" << resource_path <<
"' contains the substring '..' This is Forbidden." << endl);
322 string s = (string)
"Invalid node name '" + resource_path +
"' ACCESS IS FORBIDDEN";
332 string fullpath = catalog_root;
341 size_t slash = rem.find(
'/');
342 if (slash == string::npos) {
343 BESDEBUG(SPI_DEBUG_KEY,
344 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Checking final path component: " << rem << endl);
353 rem = rem.substr(slash + 1, rem.length() - slash);
356 BESDEBUG(SPI_DEBUG_KEY,
357 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"validPath: "<< validPath << endl);
358 BESDEBUG(SPI_DEBUG_KEY,
359 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"checking: "<< checking << endl);
360 BESDEBUG(SPI_DEBUG_KEY,
361 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"fullpath: "<< fullpath << endl);
363 BESDEBUG(SPI_DEBUG_KEY,
"GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"rem: "<< rem << endl);
365 BESDEBUG(SPI_DEBUG_KEY,
366 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"remainder: "<< remainder << endl);
369 int statret = ye_old_stat_function(fullpath.c_str(), &sb);
373 validPath = checking;
380 char *s_err = strerror(errsv);
381 string error =
"Unable to access node " + checking +
": ";
383 error = error + s_err;
386 error = error +
"unknown access error";
388 BESDEBUG(SPI_DEBUG_KEY,
389 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"error: "<< error <<
" errno: " << errno << endl);
391 BESDEBUG(SPI_DEBUG_KEY,
392 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"remainder: '" << remainder <<
"'" << endl);
396 if (errsv != ENOENT && errsv != ENOTDIR) {
401 size_t s_loc = remainder.find(
'/');
402 if (s_loc == string::npos) {
404 string basename = remainder;
405 bool moreDots =
true;
408 size_t d_loc = basename.find_last_of(
".");
409 if (d_loc != string::npos) {
410 basename = basename.substr(0, d_loc);
411 BESDEBUG(SPI_DEBUG_KEY,
412 "GatewayPathInfoResponseHandler::" << __func__ <<
"() - basename: "<< basename << endl);
414 string candidate_remainder = remainder.substr(basename.length());
415 BESDEBUG(SPI_DEBUG_KEY,
416 "GatewayPathInfoResponseHandler::" << __func__ <<
"() - candidate_remainder: "<< candidate_remainder << endl);
419 BESDEBUG(SPI_DEBUG_KEY,
420 "GatewayPathInfoResponseHandler::" << __func__ <<
"() - candidate_path: "<< candidate_path << endl);
423 BESDEBUG(SPI_DEBUG_KEY,
424 "GatewayPathInfoResponseHandler::" << __func__ <<
"() - full_candidate_path: "<< full_candidate_path << endl);
427 int statret1 = ye_old_stat_function(full_candidate_path.c_str(), &sb1);
428 if (statret1 != -1) {
429 validPath = candidate_path;
430 remainder = candidate_remainder;
435 BESDEBUG(SPI_DEBUG_KEY,
436 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"No dots in remainder: "<< remainder << endl);
442 BESDEBUG(SPI_DEBUG_KEY,
443 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Remainder has slash pollution: "<< remainder << endl);
449 statret = ye_old_stat_function(fullpath.c_str(), &sb);
450 if (S_ISREG(sb.st_mode)) {
451 BESDEBUG(SPI_DEBUG_KEY,
452 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"'"<< fullpath <<
"' Is regular file." << endl);
456 else if (S_ISDIR(sb.st_mode)) {
457 BESDEBUG(SPI_DEBUG_KEY,
458 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"'"<< fullpath <<
"' Is directory." << endl);
462 else if (S_ISLNK(sb.st_mode)) {
463 BESDEBUG(SPI_DEBUG_KEY,
464 "GatewayPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"'"<< fullpath <<
"' Is symbolic Link." << endl);
465 string error =
"Service not configured to traverse symbolic links as embodied by the node '" + checking
466 +
"' ACCESS IS FORBIDDEN";
473 std::ifstream ifile(fullpath.c_str());
474 canRead = ifile.good();
486#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
488 lastModifiedTime = (sb.st_mtimespec.tv_sec * 1000) + (sb.st_mtimespec.tv_nsec / 1000000);
490 lastModifiedTime = sb.st_mtime;
493 BESDEBUG(SPI_DEBUG_KEY,
"GatewayPathInfoResponseHandler::" << __func__ <<
"() - fullpath: " << fullpath << endl);
494 BESDEBUG(SPI_DEBUG_KEY,
"GatewayPathInfoResponseHandler::" << __func__ <<
"() - validPath: " << validPath << endl);
495 BESDEBUG(SPI_DEBUG_KEY,
"GatewayPathInfoResponseHandler::" << __func__ <<
"() - remainder: " << remainder << endl);
496 BESDEBUG(SPI_DEBUG_KEY,
"GatewayPathInfoResponseHandler::" << __func__ <<
"() - rem: " << rem << endl);
497 BESDEBUG(SPI_DEBUG_KEY,
498 "GatewayPathInfoResponseHandler::" << __func__ <<
"() - isFile: " << (isFile?
"true":
"false") << endl);
499 BESDEBUG(SPI_DEBUG_KEY,
500 "GatewayPathInfoResponseHandler::" << __func__ <<
"() - isDir: " << (isDir?
"true":
"false") << endl);
501 BESDEBUG(SPI_DEBUG_KEY,
502 "GatewayPathInfoResponseHandler::" << __func__ <<
"() - access: " << (canRead?
"true":
"false") << endl);
503 BESDEBUG(SPI_DEBUG_KEY,
"GatewayPathInfoResponseHandler::" << __func__ <<
"() - size: " << size << endl);
504 BESDEBUG(SPI_DEBUG_KEY,
505 "GatewayPathInfoResponseHandler::" << __func__ <<
"() - LMT: " << lastModifiedTime << endl);
virtual BESCatalog * default_catalog() const
The the default catalog.
virtual std::string default_catalog_name() const
The name of the default catalog.
static BESCatalogList * TheCatalogList()
Get the singleton BESCatalogList instance.
const std::string & get_root_dir() const
Get the root directory of the catalog.
Catalogs provide a hierarchical organization for data.
virtual BESCatalogEntry * show_catalog(const std::string &container, BESCatalogEntry *entry)=0
virtual BESCatalogUtils * get_catalog_utils() const
Get a pointer to the utilities, customized for this catalog.
Structure storing information used by the BES to handle the request.
std::map< std::string, std::string > data
the map of string data that will be required for the current request.
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
error thrown if the BES is not allowed to access the resource requested
informational response object
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)=0
transmit the informational object
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
exception thrown if internal error encountered
handler object that knows how to create a specific response object
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual bool start(std::string name)
static std::string assemblePath(const std::string &firstPart, const std::string &secondPart, bool leadingSlash=false, bool trailingSlash=false)
Assemble path fragments making sure that they are separated by a single '/' character.
response handler that returns nodes or leaves within the catalog either at the root or at a specified...
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the response object built by the execute command using the specified transmitter object
virtual void execute(BESDataHandlerInterface &dhi)
executes the command 'show catalog|leaves [for <node>];' by returning nodes or leaves at the top leve...