bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
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
43class BESUtil {
44private:
45 static std::string rfc822_date(time_t t);
46
47 static std::string entity(char c);
48
49public:
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 uint64_t file_to_stream(const std::string &file_name, std::ostream &o_strm, uint64_t read_start_position=0);
130
131 static std::string get_dir_name(const std::string &p);
132 static bool is_directory(const std::string &p);
133
134 static int mkdir_p(const std::string &path, mode_t mode);
135 static std::string file_to_string(const std::string &filename);
136 static std::string file_to_string(const std::string &filename, std::string &error_msg);
137 static int make_temp_file(const std::string &temp_file_dir, std::string &temp_file_name);
138 static void string_to_file(const std::string &filename, const std::string &content);
139
140 static std::string &remove_crlf(std::string &str);
141 static std::string uuid();
142
143};
144
145#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:1068
static void explode(char delim, const std::string &str, std::list< std::string > &values)
Definition BESUtil.cc:543
static long get_current_memory_usage() noexcept
Get the Resident Set Size in KB.
Definition BESUtil.cc:89
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:660
static bool endsWith(std::string const &fullString, std::string const &ending)
Definition BESUtil.cc:837
static void tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters="/")
Definition BESUtil.cc:995
static std::string get_dir_name(const std::string &p)
Definition BESUtil.cc:1235
static void set_mime_text(std::ostream &strm)
Generate an HTTP 1.0 response header for a text document.
Definition BESUtil.cc:139
static std::string id2xml(std::string in, const std::string &not_allowed="><&'\"")
Definition BESUtil.cc:487
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:898
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:385
static int mkdir_p(const std::string &path, mode_t mode)
Definition BESUtil.cc:1254
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:868
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:924
static void set_mime_html(std::ostream &strm)
Generate an HTTP 1.0 response header for a html document.
Definition BESUtil.cc:158
static std::string lowercase(const std::string &s)
Definition BESUtil.cc:257
static bool is_directory(const std::string &p)
Is the given path a directory?
Definition BESUtil.cc:1222
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:754
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:804
static std::string www2id(const std::string &in, const std::string &escape="%", const std::string &except="")
Definition BESUtil.cc:242
static std::string implode(const std::list< std::string > &values, char delim)
Definition BESUtil.cc:620
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:126
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:949
static std::string xml2id(std::string in)
Definition BESUtil.cc:504
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.
Definition BESUtil.cc:1136
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:113
static std::string unescape(const std::string &s)
Definition BESUtil.cc:267
static void string_to_file(const std::string &filename, const std::string &content)
Write a string to a file.
Definition BESUtil.cc:1321
static char * fastpidconverter(char *buf, int base)
Definition BESUtil.cc:418
static void removeLeadingAndTrailingBlanks(std::string &key)
Definition BESUtil.cc:448
static std::string & remove_crlf(std::string &str)
"Sanitizes" the string by replacing any 0x0A (new line) or 0x0D (carriage return) characters with 0x2...
Definition BESUtil.cc:1334
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 a...
Definition BESUtil.cc:1297
static std::string get_time(bool use_local_time=false)
Definition BESUtil.cc:1017