bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
BESUtil Class Reference
Collaboration diagram for BESUtil:
Collaboration graph

Classes

struct  url
 

Static Public Member Functions

static long get_current_memory_usage () noexcept
 Get the Resident Set Size in KB.
 
static void trim_if_surrounding_quotes (std::string &value)
 Remove double quotes around a string This function will remove a leading and/or trailing double quote surrounding a string.
 
static void trim_if_trailing_slash (std::string &value)
 If the string ends in a slash, remove it This function works for empty strings (doing nothing). If the string ends in a '/' it will be removed.
 
MIME utility functions

These functions are used to create the MIME headers for a message from a server to a client.

NB: These functions actually write both the response status line and the header.

See also
libdap::escaping.cc
libdap::mime_util.cc
static void set_mime_text (std::ostream &strm)
 Generate an HTTP 1.0 response header for a text document.
 
static void set_mime_html (std::ostream &strm)
 Generate an HTTP 1.0 response header for a html document.
 
static std::string www2id (const std::string &in, const std::string &escape="%", const std::string &except="")
 
static std::string unhexstring (const std::string &s)
 
static std::string lowercase (const std::string &s)
 
static std::string unescape (const std::string &s)
 
static void check_path (const std::string &path, const std::string &root, bool follow_sym_links)
 Is the combination of root + path a pathname the BES can/should access?
 
static char * fastpidconverter (char *buf, int base)
 
static char * fastpidconverter (long val, char *buf, int base)
 
static void removeLeadingAndTrailingBlanks (std::string &key)
 
static std::string id2xml (std::string in, const std::string &not_allowed="><&'\"")
 
static std::string xml2id (std::string in)
 
static void explode (char delim, const std::string &str, std::list< std::string > &values)
 
static std::string implode (const std::list< std::string > &values, char delim)
 
static void url_explode (const std::string &url_str, BESUtil::url &url_parts)
 Given a url, break the url into its different parts.
 
static std::string url_create (BESUtil::url &url_parts)
 
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.
 
static std::string pathConcat (const std::string &firstPart, const std::string &secondPart, char separator='/')
 Concatenate path fragments making sure that they are separated by a single '/' character.
 
static bool endsWith (std::string const &fullString, std::string const &ending)
 
static void conditional_timeout_cancel ()
 Checks if the timeout alarm should be canceled based on the value of the BES key BES.CancelTimeoutOnSend.
 
static void exit_on_request_timeout ()
 Checks if the timeout alarm should be canceled based on the value of the BES key BES.CancelTimeoutOnSend.
 
static unsigned int replace_all (std::string &s, std::string find_this, std::string replace_with_this)
 Operates on the string 's' to replaces every occurrence of the value of the string 'find_this' with the value of the string 'replace_with_this'.
 
static std::string normalize_path (const std::string &path, bool leading_separator, bool trailing_separator, std::string separator="/")
 Removes duplicate separators and provides leading and trailing separators as directed.
 
static void tokenize (const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters="/")
 
static std::string get_time (bool use_local_time=false)
 
static std::string get_time (time_t the_time, bool use_local_time=false)
 Returns the time represented by 'the_time' as an ISO8601 string.
 
static std::vector< std::string > split (const std::string &s, char delim='/', bool skip_empty=true)
 Splits the string s into the return vector of tokens using the delimiter delim and skipping empty values as instructed by skip_empty.
 
static BESCatalogseparateCatalogFromPath (std::string &path)
 
static uint64_t file_to_stream (const std::string &file_name, std::ostream &o_strm, uint64_t read_start_position=0)
 Copies the contents of the file identified by file_name to the stream o_strm.
 
static std::string get_dir_name (const std::string &p)
 
static bool is_directory (const std::string &p)
 Is the given path a directory?
 
static int mkdir_p (const std::string &path, mode_t mode)
 
static std::string file_to_string (const std::string &filename)
 
static std::string file_to_string (const std::string &filename, std::string &error_msg)
 
static int make_temp_file (const std::string &temp_file_dir, std::string &temp_file_name)
 Make and open a temporary file. The file is opened such that we know it is unique and not in use by another process. The name and file descriptor are set in the RemoteResource object.
 
static void string_to_file (const std::string &filename, const std::string &content)
 Write a string to a file.
 
static std::string & remove_crlf (std::string &str)
 "Sanitizes" the string by replacing any 0x0A (new line) or 0x0D (carriage return) characters with 0x20 (space)
 
static std::string uuid ()
 

Detailed Description

Definition at line 43 of file BESUtil.h.

Member Function Documentation

◆ assemblePath()

string BESUtil::assemblePath ( const std::string & firstPart,
const std::string & secondPart,
bool leadingSlash = false,
bool trailingSlash = false )
static

Assemble path fragments making sure that they are separated by a single '/' character.

If the parameter ensureLeadingSlash is true then the returned string will begin with a single '/' character followed by the string firstPart, a single '/' character, and the string secondPart.

Note
I replaced the code here with a simpler version that assumes the two string arguments do not contain multiple consecutive slashes - I don't think the original version will work in cases where the string is only slashes because it will dereference the return value of begin()
Parameters
firstPartThe first string to concatenate.
secondPartThe second string to concatenate.
leadingSlashIf this bool value is true then the returned string will have a leading slash. If the value of leadingSlash is false then the first character of the returned string will be the first character of the passed firstPart, which may be a slash. Default False.
trailingSlashIf this bool is true then the returned string will end it a slash. If trailingSlash is false, then the returned string will not end with a slash. If trailing slash(es) need to be removed to accomplish this, then they will be removed. Default False.

Definition at line 804 of file BESUtil.cc.

◆ check_path()

void BESUtil::check_path ( const std::string & path,
const std::string & root,
bool follow_sym_links )
static

Is the combination of root + path a pathname the BES can/should access?

Check if the specified path is valid

Note
If follow_sym_links is false and any part of the specified path is a symbolic link, this function will return false.
Parameters
pathThe path relative to the BES catalog root directory
rootThe BES catalog root directory
follow_sym_linksTrue if the bes conf allows symbolic links, false by default

Definition at line 385 of file BESUtil.cc.

◆ conditional_timeout_cancel()

void BESUtil::conditional_timeout_cancel ( )
static

Checks if the timeout alarm should be canceled based on the value of the BES key BES.CancelTimeoutOnSend.

If the value of the BES Key BES.CancelTimeoutOnSend is false || no, then do not cancel the timeout alarm. The intent of this is to stop the timeout counter once the BES starts sending data back since, the network link used by a remote client may be low-bandwidth and data providers might want to ensure those users get their data (and don't submit second, third, ..., requests when/if the first one fails). The timeout is initiated in the BES framework when it first processes the request.

Default: If the BES key BES.CancelTimeoutOnSend is not set, or if it is set to true || yes then the timeout alarm will be canceled.

Note
The BES timeout is set/controlled in bes/dispatch/BESInterface in the 'int BESInterface::execute_request(const string &from)' method.
See also
See the send_data(BESResponseObject *obj, BESDataHandlerInterface &dhi) methods of the children of BESTransmitter

Definition at line 898 of file BESUtil.cc.

◆ endsWith()

bool BESUtil::endsWith ( std::string const & fullString,
std::string const & ending )
static

Returns true if (the value of) 'fullString' ends with (the value of) 'ending', false otherwise.

Definition at line 837 of file BESUtil.cc.

◆ exit_on_request_timeout()

void BESUtil::exit_on_request_timeout ( )
static

Checks if the timeout alarm should be canceled based on the value of the BES key BES.CancelTimeoutOnSend.

If the value of the BES Key BES.CancelTimeoutOnSend is false || no, then do not cancel the timeout alarm. The intent of this is to stop the timeout counter once the BES starts sending data back since, the network link used by a remote client may be low-bandwidth and data providers might want to ensure those users get their data (and don't submit second, third, ..., requests when/if the first one fails). The timeout is initiated in the BES framework when it first processes the request.

Default: If the BES key BES.CancelTimeoutOnSend is not set, or if it is set to true || yes then the timeout alrm will be canceled.

Note
The BES timeout is set/controlled in bes/dispatch/BESInterface in the 'int BESInterface::execute_request(const string &from)' method.
See also
See the send_data(BESResponseObject *obj, BESDataHandlerInterface &dhi) methods of the children of BESTransmitter

Definition at line 868 of file BESUtil.cc.

◆ explode()

void BESUtil::explode ( char delim,
const std::string & str,
std::list< std::string > & values )
static

explode a string into an array given a delimiter

Given a string of values separated by a delimiter, break out the values and store in the list.

Quoted values must be escaped.

If values contain the delimiter then the value must be wrapped in quotes.

Parameters
delimdelimiter separating the values
strthe original string
valueslist of the delimited values returned to caller
Exceptions
BESInternalErrorif missing ending quote or delimiter does not follow end quote

Definition at line 543 of file BESUtil.cc.

◆ fastpidconverter() [1/2]

char * BESUtil::fastpidconverter ( char * buf,
int base )
static

convert pid and place in provided buffer

Definition at line 418 of file BESUtil.cc.

◆ fastpidconverter() [2/2]

char * BESUtil::fastpidconverter ( long val,
char * buf,
int base )
static

Definition at line 424 of file BESUtil.cc.

◆ file_to_stream()

uint64_t BESUtil::file_to_stream ( const std::string & file_name,
std::ostream & o_strm,
uint64_t read_start_position = 0 )
static

Copies the contents of the file identified by file_name to the stream o_strm.

Thanks to O'Reilly: https://www.oreilly.com/library/view/c-cookbook/0596007612/ch10s08.html

Parameters
file_name
o_strm
Returns
The number of bytes read/written

Definition at line 1136 of file BESUtil.cc.

◆ file_to_string()

string BESUtil::file_to_string ( const std::string & filename)
static

Definition at line 1280 of file BESUtil.cc.

◆ get_current_memory_usage()

long BESUtil::get_current_memory_usage ( )
staticnoexcept

Get the Resident Set Size in KB.

Returns
The RSS or 0 if getrusage() returns an error

Definition at line 89 of file BESUtil.cc.

◆ get_dir_name()

string BESUtil::get_dir_name ( const std::string & p)
static

For the given path, return the directory name. If the path is a directory, return the path. If the path is a file, return the directory name. This does not check if the path exists, but instead just parses the path.

Definition at line 1235 of file BESUtil.cc.

◆ get_time() [1/2]

string BESUtil::get_time ( bool use_local_time = false)
static

Returns the current time as an ISO8601 string.

Parameters
use_local_timeTrue to use the local time, false (default) to use GMT
Returns
The time, either local or GMT/UTC as an ISO8601 string

Definition at line 1017 of file BESUtil.cc.

◆ get_time() [2/2]

string BESUtil::get_time ( time_t the_time,
bool use_local_time = false )
static

Returns the time represented by 'the_time' as an ISO8601 string.

Parameters
the_timeA time_t value
use_local_timeTrue to use the local time, false (default) to use GMT
Returns
The time, either local or GMT/UTC as an ISO8601 string

Definition at line 1029 of file BESUtil.cc.

◆ id2xml()

string BESUtil::id2xml ( std::string in,
const std::string & not_allowed = "><&'\"" )
static

convert characters not allowed in xml to escaped characters

Replace characters that are not allowed in XML

Parameters
inThe string in which to replace characters.
not_allowedThe set of characters that are not allowed in XML. default: ><&'(single quote)"(double quote)
Returns
The modified identifier.

Definition at line 487 of file BESUtil.cc.

◆ implode()

string BESUtil::implode ( const std::list< std::string > & values,
char delim )
static

implode a list of values into a single string delimited by delim

Given a list of string values create a single string of values delimited by delim

If the delimiter exists in a value in the list then that value must be enclosed in quotes

Parameters
valueslist of string values to implode
delimthe delimiter to use in creating the resulting string
Returns
the delim delimited string of values

Definition at line 620 of file BESUtil.cc.

◆ is_directory()

bool BESUtil::is_directory ( const std::string & p)
static

Is the given path a directory?

Definition at line 1222 of file BESUtil.cc.

◆ lowercase()

string BESUtil::lowercase ( const std::string & s)
static

Convert a string to all lower case

Definition at line 257 of file BESUtil.cc.

◆ make_temp_file()

int BESUtil::make_temp_file ( const std::string & temp_file_dir,
std::string & temp_file_name )
static

Make and open a temporary file. The file is opened such that we know it is unique and not in use by another process. The name and file descriptor are set in the RemoteResource object.

Parameters
temp_file_dirThe directory to hold the temporary file.
temp_file_nameThe name of the temporary file. A value-result parameter.

Definition at line 1297 of file BESUtil.cc.

◆ mkdir_p()

int BESUtil::mkdir_p ( const std::string & path,
mode_t mode )
static

Create a directory and any parent directories that do not exist.

Parameters
path
mode
Returns
The return value from mkdir() or 0 if the directory already exists.

Definition at line 1254 of file BESUtil.cc.

◆ normalize_path()

string BESUtil::normalize_path ( const std::string & path,
bool leading_separator,
bool trailing_separator,
std::string separator = "/" )
static

Removes duplicate separators and provides leading and trailing separators as directed.

Parameters
raw_pathThe string to normalize
leading_separatorif true then the result will begin with a single separator character. If false the result will not begin with a separator character.
trailing_separatorIf true the result will end with a single separator character. If false the result will not end with a separator character.
separatorA string, of length one, containing the separator character for the path. This parameter is optional and its value defaults to the slash '/' character.

Definition at line 949 of file BESUtil.cc.

◆ pathConcat()

string BESUtil::pathConcat ( const std::string & firstPart,
const std::string & secondPart,
char separator = '/' )
static

Concatenate path fragments making sure that they are separated by a single '/' character.

Returns a new string made from appending secondPart to firstPart while ensuring that a single separator appears between the two parts.

Parameters
firstPartThe first string to concatenate.
secondPartThe second string to concatenate.
separatorThe separator character to use between the two concatenated strings. Default: '/'

Definition at line 754 of file BESUtil.cc.

◆ remove_crlf()

std::string & BESUtil::remove_crlf ( std::string & str)
static

"Sanitizes" the string by replacing any 0x0A (new line) or 0x0D (carriage return) characters with 0x20 (space)

Parameters
msgThe string to "sanitize"
Returns
A reference to the sanitized string.

Definition at line 1334 of file BESUtil.cc.

◆ removeLeadingAndTrailingBlanks()

void BESUtil::removeLeadingAndTrailingBlanks ( std::string & key)
static

remove leading and trailing blanks from a string

Definition at line 448 of file BESUtil.cc.

◆ replace_all()

unsigned int BESUtil::replace_all ( std::string & s,
std::string find_this,
std::string replace_with_this )
static

Operates on the string 's' to replaces every occurrence of the value of the string 'find_this' with the value of the string 'replace_with_this'.

Convert a string to all lower case

Parameters

Definition at line 924 of file BESUtil.cc.

◆ separateCatalogFromPath()

BESCatalog * BESUtil::separateCatalogFromPath ( std::string & path)
static

Definition at line 1085 of file BESUtil.cc.

◆ set_mime_html()

void BESUtil::set_mime_html ( std::ostream & strm)
static

Generate an HTTP 1.0 response header for a html document.

Parameters
strmWrite the MIME header to this ostream.

Definition at line 158 of file BESUtil.cc.

◆ set_mime_text()

void BESUtil::set_mime_text ( std::ostream & strm)
static

Generate an HTTP 1.0 response header for a text document.

Parameters
strmWrite the MIME header to this ostream.

Definition at line 139 of file BESUtil.cc.

◆ split()

vector< string > BESUtil::split ( const std::string & s,
char delim = '/',
bool skip_empty = true )
static

Splits the string s into the return vector of tokens using the delimiter delim and skipping empty values as instructed by skip_empty.

Parameters
sThe string to tokenize.
delimThe character delimiter to utilize during tokenization. default: '/'
skip_emptyA boolean flag which controls if empty tokens are returned.
Returns
A vector of strings each of which is a token in the string read left to right.
See also
BESUtil::tokenize() is probably faster

Definition at line 1068 of file BESUtil.cc.

◆ string_to_file()

void BESUtil::string_to_file ( const std::string & filename,
const std::string & content )
static

Write a string to a file.

Note
Truncates the file if it exists.
Parameters
filename
content

Definition at line 1321 of file BESUtil.cc.

◆ tokenize()

void BESUtil::tokenize ( const std::string & str,
std::vector< std::string > & tokens,
const std::string & delimiters = "/" )
static

A call out thanks to: http://www.oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html for the tokenizer.

Definition at line 995 of file BESUtil.cc.

◆ trim_if_surrounding_quotes()

void BESUtil::trim_if_surrounding_quotes ( std::string & value)
static

Remove double quotes around a string This function will remove a leading and/or trailing double quote surrounding a string.

Parameters
valueThe string, modified

Definition at line 126 of file BESUtil.cc.

◆ trim_if_trailing_slash()

void BESUtil::trim_if_trailing_slash ( std::string & value)
static

If the string ends in a slash, remove it This function works for empty strings (doing nothing). If the string ends in a '/' it will be removed.

Note
See https://stackoverflow.com/questions/2310939/remove-last-character-from-c-string
Parameters
valueThe string, modified in place

Definition at line 113 of file BESUtil.cc.

◆ unescape()

string BESUtil::unescape ( const std::string & s)
static

Unescape characters with backslash before them

Definition at line 267 of file BESUtil.cc.

◆ unhexstring()

string BESUtil::unhexstring ( const std::string & s)
static

Definition at line 229 of file BESUtil.cc.

◆ url_create()

string BESUtil::url_create ( BESUtil::url & url_parts)
static

Definition at line 728 of file BESUtil.cc.

◆ url_explode()

void BESUtil::url_explode ( const std::string & url_str,
BESUtil::url & url_parts )
static

Given a url, break the url into its different parts.

The different parts are the protocol, the domain name, a username if specified, a password if specified, a port if specified, and a path if specified.

struct url { string protocol ; string domain ; string uname ; string psswd ; string port ; string path ; } ;

Parameters
urlstring representation of the URL

Definition at line 660 of file BESUtil.cc.

◆ uuid()

std::string BESUtil::uuid ( )
static

Definition at line 1344 of file BESUtil.cc.

◆ www2id()

string BESUtil::www2id ( const std::string & in,
const std::string & escape = "%",
const std::string & except = "" )
static

This functions are used to unescape hex characters from strings

Definition at line 242 of file BESUtil.cc.

◆ xml2id()

string BESUtil::xml2id ( std::string in)
static

unescape xml escaped characters

Given a string that contains XML escape sequences (i.e., entities), translate those back into ASCII characters. Return the modified string.

Parameters
inThe string to modify.
Returns
The modified string.

Definition at line 504 of file BESUtil.cc.


The documentation for this class was generated from the following files: