35 #include <sys/types.h>
49 using std::istringstream;
61 #define debug_key "BesUtil"
68 strm <<
"HTTP/1.0 200 OK" <<
CRLF;
71 const time_t t = time(0);
72 strm <<
"Date: " << rfc822_date(t).c_str() <<
CRLF;
73 strm <<
"Last-Modified: " << rfc822_date(t).c_str() <<
CRLF;
75 strm <<
"Content-Type: text/plain" <<
CRLF;
77 strm <<
"Content-Description: unknown" <<
CRLF;
86 strm <<
"HTTP/1.0 200 OK" <<
CRLF;
89 const time_t t = time(0);
90 strm <<
"Date: " << rfc822_date(t).c_str() <<
CRLF;
91 strm <<
"Last-Modified: " << rfc822_date(t).c_str() <<
CRLF;
93 strm <<
"Content-type: text/html" <<
CRLF;
95 strm <<
"Content-Description: unknown" <<
CRLF;
131 static const char *days[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" };
132 static const char *months[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
133 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" };
144 string BESUtil::rfc822_date(
const time_t t) {
145 struct tm *stm = gmtime(&t);
148 snprintf(d, 255,
"%s, %02d %s %4d %02d:%02d:%02d GMT", days[stm->tm_wday],
149 stm->tm_mday, months[stm->tm_mon], 1900 + stm->tm_year,
150 stm->tm_hour, stm->tm_min, stm->tm_sec);
158 ss >> std::hex >> val;
160 tmp_str[0] =
static_cast<char> (val);
162 return string(tmp_str);
168 const string &except) {
169 string::size_type i = 0;
171 while ((i = res.find_first_of(escape, i)) != string::npos) {
172 if (except.find(res.substr(i, 3)) != string::npos) {
176 res.replace(i, 3,
unhexstring(res.substr(i + 1, 2)));
183 string return_string = s;
184 for (
int j = 0; j < static_cast<int> (return_string.length()); j++) {
185 return_string[j] = (char) tolower(return_string[j]);
188 return return_string;
193 string::size_type index = 0;
197 string::size_type bs = s.find(
'\\', index);
198 if (bs == string::npos) {
199 new_str += s.substr(index, s.length() - index);
202 new_str += s.substr(index, bs - index);
203 new_str += s[bs + 1];
233 bool follow_sym_links) {
243 int (*ye_old_stat_function)(
const char *pathname,
struct stat *buf);
244 if(follow_sym_links){
245 BESDEBUG(
debug_key,
"eval_w10n_resourceId() - Using 'stat' function (follow_sym_links = true)" << endl);
246 ye_old_stat_function = &stat;
249 BESDEBUG(
debug_key,
"eval_w10n_resourceId() - Using 'lstat' function (follow_sym_links = false)" << endl);
250 ye_old_stat_function = &lstat;
257 string::size_type dotdot = path.find(
"..");
258 if (dotdot != string::npos) {
259 string s = (string)
"You are not allowed to access the node " + path;
271 rem = rem.substr(1, rem.length() - 1);
272 if (rem[rem.length() - 1] ==
'/')
273 rem = rem.substr(0, rem.length() - 1);
276 string fullpath = root;
277 if (fullpath[fullpath.length() - 1] ==
'/') {
278 fullpath = fullpath.substr(0, fullpath.length() - 1);
284 size_t slash = rem.find(
'/');
285 if (slash == string::npos) {
286 fullpath = fullpath +
"/" + rem;
287 checked = checked +
"/" + rem;
290 fullpath = fullpath +
"/" + rem.substr(0, slash);
291 checked = checked +
"/" + rem.substr(0, slash);
292 rem = rem.substr(slash + 1, rem.length() - slash);
296 int statret = ye_old_stat_function(fullpath.c_str(), &buf);
301 char *s_err = strerror(errsv);
302 string error =
"Unable to access node " + checked +
": ";
304 error = error + s_err;
306 error = error +
"unknown access error";
309 BESDEBUG(
debug_key,
"check_path() - error: "<< error <<
" errno: " << errno << endl);
314 if (errsv == ENOENT || errsv == ENOTDIR) {
325 if (S_ISLNK( buf.st_mode )) {
326 string error =
"You do not have permission to access "
336 size_t slash = rem.find(
'/');
337 if (slash == string::npos) {
338 fullpath = fullpath +
"/" + rem;
339 checked = checked +
"/" + rem;
342 fullpath = fullpath +
"/" + rem.substr(0, slash);
343 checked = checked +
"/" + rem.substr(0, slash);
344 rem = rem.substr(slash + 1, rem.length() - slash);
347 if (!follow_sym_links) {
349 int statret = lstat(fullpath.c_str(), &buf);
354 char *s_err = strerror(errsv);
355 string error =
"Unable to access node " + checked +
": ";
357 error = error + s_err;
359 error = error +
"unknown access error";
363 if (errsv == ENOENT) {
370 if (S_ISLNK( buf.st_mode )) {
371 string error =
"You do not have permission to access "
380 int statret = stat(fullpath.c_str(), &buf);
385 char *s_err = strerror(errsv);
386 string error =
"Unable to access node " + checked +
": ";
388 error = error + s_err;
390 error = error +
"unknown access error";
394 if (errsv == ENOENT) {
418 if (base > 36 || base < 2)
425 r = ldiv(labs(val), base);
433 *buf++ =
"0123456789abcdefghijklmnopqrstuvwxyz"[(int) r.rem];
440 string::size_type first = key.find_first_not_of(
" \t\n\r");
441 string::size_type last = key.find_last_not_of(
" \t\n\r");
442 if (first == string::npos)
445 string::size_type num = last - first + 1;
446 string new_key = key.substr(first, num);
452 string BESUtil::entity(
char c) {
476 string::size_type i = 0;
478 while ((i = in.find_first_of(not_allowed, i)) != string::npos) {
479 in.replace(i, 1, entity(in[i]));
492 string::size_type i = 0;
494 while ((i = in.find(
">", i)) != string::npos)
495 in.replace(i, 4,
">");
498 while ((i = in.find(
"<", i)) != string::npos)
499 in.replace(i, 4,
"<");
502 while ((i = in.find(
"&", i)) != string::npos)
503 in.replace(i, 5,
"&");
506 while ((i = in.find(
"'", i)) != string::npos)
507 in.replace(i, 6,
"'");
510 while ((i = in.find(
""", i)) != string::npos)
511 in.replace(i, 6,
"\"");
530 std::string::size_type start = 0;
531 std::string::size_type qstart = 0;
532 std::string::size_type adelim = 0;
533 std::string::size_type aquote = 0;
537 if (str[start] ==
'"') {
538 bool endquote =
false;
541 aquote = str.find(
'"', qstart);
542 if (aquote == string::npos) {
543 string currval = str.substr(start, str.length() - start);
544 string err =
"BESUtil::explode - No end quote after value "
550 if (str[aquote - 1] ==
'\\') {
551 if (str[aquote - 2] ==
'\\') {
562 if (str[qstart] != delim && qstart != str.length()) {
563 string currval = str.substr(start, qstart - start);
564 string err =
"BESUtil::explode - No delim after end quote "
568 if (qstart == str.length()) {
569 adelim = string::npos;
574 adelim = str.find(delim, start);
576 if (adelim == string::npos) {
577 aval = str.substr(start, str.length() - start);
580 aval = str.substr(start, adelim - start);
583 values.push_back(aval);
585 if (start == str.length()) {
586 values.push_back(
"");
604 list<string>::const_iterator i = values.begin();
605 list<string>::const_iterator e = values.end();
608 for (; i != e; i++) {
611 d = (*i).find(delim);
612 if (d != string::npos && (*i)[0] !=
'"') {
614 (string)
"BESUtil::implode - delimiter exists in value "
647 string::size_type colon = url_str.find(
":");
648 if (colon == string::npos) {
649 string err =
"BESUtil::url_explode: missing colon for protocol";
653 url_parts.
protocol = url_str.substr(0, colon);
655 if (url_str.substr(colon, 3) !=
"://") {
656 string err =
"BESUtil::url_explode: no :// in the URL";
661 rest = url_str.substr(colon);
663 string::size_type slash = rest.find(
"/");
664 if (slash == string::npos)
665 slash = rest.length();
667 string::size_type at = rest.find(
"@");
668 if ((at != string::npos) && (at < slash)) {
670 string up = rest.substr(0, at);
671 colon = up.find(
":");
672 if (colon != string::npos) {
673 url_parts.
uname = up.substr(0, colon);
674 url_parts.
psswd = up.substr(colon + 1);
676 url_parts.
uname = up;
679 rest = rest.substr(at + 1);
681 slash = rest.find(
"/");
682 if (slash == string::npos)
683 slash = rest.length();
684 colon = rest.find(
":");
685 if ((colon != string::npos) && (colon < slash)) {
687 url_parts.
domain = rest.substr(0, colon);
689 rest = rest.substr(colon + 1);
690 slash = rest.find(
"/");
691 if (slash != string::npos) {
692 url_parts.
port = rest.substr(0, slash);
693 url_parts.
path = rest.substr(slash + 1);
695 url_parts.
port = rest;
699 slash = rest.find(
"/");
700 if (slash != string::npos) {
701 url_parts.
domain = rest.substr(0, slash);
702 url_parts.
path = rest.substr(slash + 1);
711 if (!url_parts.
uname.empty()) {
712 url += url_parts.
uname;
713 if (!url_parts.
psswd.empty())
714 url +=
":" + url_parts.
psswd;
718 if (!url_parts.
port.empty())
719 url +=
":" + url_parts.
port;
720 if (!url_parts.
path.empty())
721 url +=
"/" + url_parts.
path;
error thrown if the resource requested cannot be found
static string id2xml(string in, const string ¬_allowed="><&'\"")
convert characters not allowed in xml to escaped characters
exception thrown if inernal error encountered
static string lowercase(const string &s)
Convert a string to all lower case.
static string www2id(const string &in, const string &escape="%", const string &except="")
This functions are used to unescape hex characters from strings.
static void removeLeadingAndTrailingBlanks(string &key)
remove leading and trailing blanks from a string
static string implode(const list< string > &values, char delim)
implode a list of values into a single string delimited by delim
static string xml2id(string in)
unescape xml escaped characters
static void set_mime_html(ostream &strm)
Generate an HTTP 1.0 response header for a html document.
static void explode(char delim, const string &str, list< string > &values)
explode a string into an array given a delimiter
static void set_mime_text(ostream &strm)
Generate an HTTP 1.0 response header for a text document.
static string unhexstring(string s)
static void url_explode(const string &url_str, BESUtil::url &url_parts)
Given a url, break the url into its different parts.
error thrown if the BES is not allowed to access the resource requested
static string url_create(BESUtil::url &url_parts)
static char * fastpidconverter(char *buf, int base)
convert pid and place in provided buffer
static string unescape(const string &s)
Unescape characters with backslash before them.
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
static void check_path(const string &path, const string &root, bool follow_sym_links)
Check if the specified path is valid.