bes  Updated for version 3.20.13
BESUtil.h
1 // BESUtil.h
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #ifndef E_BESUtil_h
34 #define E_BESUtil_h 1
35 
36 #include <string>
37 #include <list>
38 #include <iostream>
39 #include <vector>
40 #include <BESCatalog.h>
41 #include <atomic>
42 
43 class BESUtil {
44 private:
45  static std::string rfc822_date(time_t t);
46 
47  static std::string entity(char c);
48 
49 public:
50  static long get_current_memory_usage() noexcept;
51 
52  static void trim_if_trailing_slash(std::string &value);
53  static void trim_if_surrounding_quotes(std::string &value);
54 
65  static void set_mime_text(std::ostream &strm);
66  static void set_mime_html(std::ostream &strm);
67 
69  static std::string www2id(const std::string &in, const std::string &escape = "%", const std::string &except = "");
70  static std::string unhexstring(const std::string& s);
71 
73  static std::string lowercase(const std::string &s);
74 
76  static std::string unescape(const std::string &s);
77 
79  static void check_path(const std::string &path, const std::string &root, bool follow_sym_links);
80 
82  static char * fastpidconverter(char *buf, int base);
83  static char * fastpidconverter(long val, char *buf, int base);
84 
86  static void removeLeadingAndTrailingBlanks(std::string &key);
87 
89  static std::string id2xml(std::string in, const std::string &not_allowed = "><&'\"");
90 
92  static std::string xml2id(std::string in);
93 
95  static void explode(char delim, const std::string &str, std::list<std::string> &values);
96 
98  static std::string implode(const std::list<std::string> &values, char delim);
99 
100  struct url {
101  std::string protocol;
102  std::string domain;
103  std::string uname;
104  std::string psswd;
105  std::string port;
106  std::string path;
107  };
108 
109  static void url_explode(const std::string &url_str, BESUtil::url &url_parts);
110  static std::string url_create(BESUtil::url &url_parts);
111  // static string assemblePath(const string &firstPart, const string &secondPart, bool leadingSlash = false);
112  static std::string assemblePath(const std::string &firstPart, const std::string &secondPart, bool leadingSlash = false, bool trailingSlash = false);
113  static std::string pathConcat(const std::string &firstPart, const std::string &secondPart, char separator='/');
114 
115  static bool endsWith(std::string const &fullString, std::string const &ending);
116  static void conditional_timeout_cancel();
117  static void exit_on_request_timeout();
118 
120  static unsigned int replace_all(std::string &s, std::string find_this, std::string replace_with_this);
121  static std::string normalize_path(const std::string &path, bool leading_separator, bool trailing_separator, std::string separator = "/");
122  static void tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters = "/");
123  static std::string get_time(bool use_local_time = false);
124  static std::string get_time(time_t the_time, bool use_local_time = false);
125  static std::vector<std::string> split(const std::string &s, char delim='/', bool skip_empty=true);
126 
127  static BESCatalog *separateCatalogFromPath(std::string &path);
128 
129  static void file_to_stream(const std::string &file_name, std::ostream &o_strm);
130  static uint64_t file_to_stream_helper(const std::string &file_name, std::ostream &o_strm, uint64_t byteCount);
131  static uint64_t file_to_stream_task(const std::string &file_name, std::atomic<bool> &file_write_done,
132  std::ostream &o_strm);
133 #if 0
134  // Added jhrg 11/9/21
135  static void split(const std::string &s, const std::string &delimiter, std::vector<uint64_t> &res);
136  static void split(const std::string &s, const std::string &delimiter, std::vector<std::string> &res);
137 #endif
138 };
139 
140 #endif // E_BESUtil_h
Catalogs provide a hierarchical organization for data.
Definition: BESCatalog.h:51
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 val...
Definition: BESUtil.cc:1065
static uint64_t file_to_stream_task(const std::string &file_name, std::atomic< bool > &file_write_done, std::ostream &o_strm)
Definition: BESUtil.cc:1303
static void explode(char delim, const std::string &str, std::list< std::string > &values)
Definition: BESUtil.cc:540
static long get_current_memory_usage() noexcept
Get the Resident Set Size in KB.
Definition: BESUtil.cc:86
static void url_explode(const std::string &url_str, BESUtil::url &url_parts)
Given a url, break the url into its different parts.
Definition: BESUtil.cc:657
static bool endsWith(std::string const &fullString, std::string const &ending)
Definition: BESUtil.cc:834
static void tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters="/")
Definition: BESUtil.cc:992
static void set_mime_text(std::ostream &strm)
Generate an HTTP 1.0 response header for a text document.
Definition: BESUtil.cc:136
static std::string id2xml(std::string in, const std::string &not_allowed="><&'\"")
Definition: BESUtil.cc:484
static void conditional_timeout_cancel()
Checks if the timeout alarm should be canceled based on the value of the BES key BES....
Definition: BESUtil.cc:895
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?
Definition: BESUtil.cc:382
static void exit_on_request_timeout()
Checks if the timeout alarm should be canceled based on the value of the BES key BES....
Definition: BESUtil.cc:865
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 t...
Definition: BESUtil.cc:921
static void set_mime_html(std::ostream &strm)
Generate an HTTP 1.0 response header for a html document.
Definition: BESUtil.cc:155
static std::string lowercase(const std::string &s)
Definition: BESUtil.cc:254
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.
Definition: BESUtil.cc:751
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.
Definition: BESUtil.cc:801
static std::string www2id(const std::string &in, const std::string &escape="%", const std::string &except="")
Definition: BESUtil.cc:239
static std::string implode(const std::list< std::string > &values, char delim)
Definition: BESUtil.cc:617
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...
Definition: BESUtil.cc:123
static void file_to_stream(const std::string &file_name, std::ostream &o_strm)
Copies the contents of the file identified by file_name to the stream o_strm.
Definition: BESUtil.cc:1132
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.
Definition: BESUtil.cc:946
static std::string xml2id(std::string in)
Definition: BESUtil.cc:501
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)....
Definition: BESUtil.cc:110
static std::string unescape(const std::string &s)
Definition: BESUtil.cc:264
static char * fastpidconverter(char *buf, int base)
Definition: BESUtil.cc:415
static void removeLeadingAndTrailingBlanks(std::string &key)
Definition: BESUtil.cc:445
static std::string get_time(bool use_local_time=false)
Definition: BESUtil.cc:1014