35 #include <sys/types.h> 51 using std::istringstream;
57 #include "BESForbiddenError.h" 58 #include "BESNotFoundError.h" 59 #include "BESInternalError.h" 63 #define debug_key "BesUtil" 71 strm <<
"HTTP/1.0 200 OK" << CRLF;
72 strm <<
"XBES-Server: " << PACKAGE_STRING << CRLF;
74 const time_t t = time(0);
75 strm <<
"Date: " << rfc822_date(t).c_str() << CRLF;
76 strm <<
"Last-Modified: " << rfc822_date(t).c_str() << CRLF;
78 strm <<
"Content-Type: text/plain" << CRLF;
80 strm <<
"Content-Description: unknown" << CRLF;
90 strm <<
"HTTP/1.0 200 OK" << CRLF;
91 strm <<
"XBES-Server: " << PACKAGE_STRING << CRLF;
93 const time_t t = time(0);
94 strm <<
"Date: " << rfc822_date(t).c_str() << CRLF;
95 strm <<
"Last-Modified: " << rfc822_date(t).c_str() << CRLF;
97 strm <<
"Content-type: text/html" << CRLF;
99 strm <<
"Content-Description: unknown" << CRLF;
135 static const char *days[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" };
136 static const char *months[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" };
147 string BESUtil::rfc822_date(
const time_t t)
149 struct tm *stm = gmtime(&t);
152 snprintf(d, 255,
"%s, %02d %s %4d %02d:%02d:%02d GMT", days[stm->tm_wday], stm->tm_mday, months[stm->tm_mon],
153 1900 + stm->tm_year, stm->tm_hour, stm->tm_min, stm->tm_sec);
158 string BESUtil::unhexstring(
string s)
162 ss >> std::hex >> val;
164 tmp_str[0] =
static_cast<char>(val);
166 return string(tmp_str);
173 string::size_type i = 0;
175 while ((i = res.find_first_of(escape, i)) != string::npos) {
176 if (except.find(res.substr(i, 3)) != string::npos) {
180 res.replace(i, 3, unhexstring(res.substr(i + 1, 2)));
188 string return_string = s;
189 for (
int j = 0; j < static_cast<int>(return_string.length()); j++) {
190 return_string[j] = (char) tolower(return_string[j]);
193 return return_string;
199 string::size_type index = 0;
203 string::size_type bs = s.find(
'\\', index);
204 if (bs == string::npos) {
205 new_str += s.substr(index, s.length() - index);
209 new_str += s.substr(index, bs - index);
210 new_str += s[bs + 1];
243 if (path ==
"")
return;
248 int (*ye_old_stat_function)(
const char *pathname,
struct stat *buf);
249 if (follow_sym_links) {
250 BESDEBUG(debug_key,
"eval_w10n_resourceId() - Using 'stat' function (follow_sym_links = true)" << endl);
251 ye_old_stat_function = &stat;
254 BESDEBUG(debug_key,
"eval_w10n_resourceId() - Using 'lstat' function (follow_sym_links = false)" << endl);
255 ye_old_stat_function = &lstat;
260 string::size_type dotdot = path.find(
"..");
261 if (dotdot != string::npos) {
262 string s = (string)
"You are not allowed to access the node " + path;
273 if (rem[0] ==
'/') rem = rem.substr(1, rem.length() - 1);
274 if (rem[rem.length() - 1] ==
'/') rem = rem.substr(0, rem.length() - 1);
277 string fullpath = root;
278 if (fullpath[fullpath.length() - 1] ==
'/') {
279 fullpath = fullpath.substr(0, fullpath.length() - 1);
285 size_t slash = rem.find(
'/');
286 if (slash == string::npos) {
287 fullpath = fullpath +
"/" + rem;
288 checked = checked +
"/" + rem;
292 fullpath = fullpath +
"/" + rem.substr(0, slash);
293 checked = checked +
"/" + rem.substr(0, slash);
294 rem = rem.substr(slash + 1, rem.length() - slash);
298 int statret = ye_old_stat_function(fullpath.c_str(), &buf);
303 char *s_err = strerror(errsv);
304 string error =
"Unable to access node " + checked +
": ";
306 error = error + s_err;
309 error = error +
"unknown access error";
312 BESDEBUG(debug_key,
"check_path() - error: "<< error <<
" errno: " << errno << endl);
317 if (errsv == ENOENT || errsv == ENOTDIR) {
330 if (S_ISLNK(buf.st_mode)) {
331 string error =
"You do not have permission to access " + checked;
339 size_t slash = rem.find(
'/');
340 if (slash == string::npos) {
341 fullpath = fullpath +
"/" + rem;
342 checked = checked +
"/" + rem;
346 fullpath = fullpath +
"/" + rem.substr(0, slash);
347 checked = checked +
"/" + rem.substr(0, slash);
348 rem = rem.substr(slash + 1, rem.length() - slash);
351 if (!follow_sym_links) {
353 int statret = lstat(fullpath.c_str(), &buf);
358 char *s_err = strerror(errsv);
359 string error =
"Unable to access node " + checked +
": ";
361 error = error + s_err;
364 error = error +
"unknown access error";
368 if (errsv == ENOENT) {
377 if (S_ISLNK( buf.st_mode )) {
378 string error =
"You do not have permission to access " 388 int statret = stat(fullpath.c_str(), &buf);
393 char *s_err = strerror(errsv);
394 string error =
"Unable to access node " + checked +
": ";
396 error = error + s_err;
399 error = error +
"unknown access error";
403 if (errsv == ENOENT) {
429 if (base > 36 || base < 2)
434 if (val < 0) *buf++ =
'-';
435 r = ldiv(labs(val), base);
442 *buf++ =
"0123456789abcdefghijklmnopqrstuvwxyz"[(int) r.rem];
450 string::size_type first = key.find_first_not_of(
" \t\n\r");
451 string::size_type last = key.find_last_not_of(
" \t\n\r");
452 if (first == string::npos)
455 string::size_type num = last - first + 1;
456 string new_key = key.substr(first, num);
462 string BESUtil::entity(
char c)
488 string::size_type i = 0;
490 while ((i = in.find_first_of(not_allowed, i)) != string::npos) {
491 in.replace(i, 1, entity(in[i]));
505 string::size_type i = 0;
507 while ((i = in.find(
">", i)) != string::npos)
508 in.replace(i, 4,
">");
511 while ((i = in.find(
"<", i)) != string::npos)
512 in.replace(i, 4,
"<");
515 while ((i = in.find(
"&", i)) != string::npos)
516 in.replace(i, 5,
"&");
519 while ((i = in.find(
"'", i)) != string::npos)
520 in.replace(i, 6,
"'");
523 while ((i = in.find(
""", i)) != string::npos)
524 in.replace(i, 6,
"\"");
544 std::string::size_type start = 0;
545 std::string::size_type qstart = 0;
546 std::string::size_type adelim = 0;
547 std::string::size_type aquote = 0;
551 if (str[start] ==
'"') {
552 bool endquote =
false;
555 aquote = str.find(
'"', qstart);
556 if (aquote == string::npos) {
557 string currval = str.substr(start, str.length() - start);
558 string err =
"BESUtil::explode - No end quote after value " + currval;
563 if (str[aquote - 1] ==
'\\') {
564 if (str[aquote - 2] ==
'\\') {
577 if (str[qstart] != delim && qstart != str.length()) {
578 string currval = str.substr(start, qstart - start);
579 string err =
"BESUtil::explode - No delim after end quote " + currval;
582 if (qstart == str.length()) {
583 adelim = string::npos;
590 adelim = str.find(delim, start);
592 if (adelim == string::npos) {
593 aval = str.substr(start, str.length() - start);
597 aval = str.substr(start, adelim - start);
600 values.push_back(aval);
602 if (start == str.length()) {
603 values.push_back(
"");
622 list<string>::const_iterator i = values.begin();
623 list<string>::const_iterator e = values.end();
626 for (; i != e; i++) {
627 if (!first) result += delim;
628 d = (*i).find(delim);
629 if (d != string::npos && (*i)[0] !=
'"') {
630 string err = (string)
"BESUtil::implode - delimiter exists in value " + (*i);
663 string::size_type colon = url_str.find(
":");
664 if (colon == string::npos) {
665 string err =
"BESUtil::url_explode: missing colon for protocol";
669 url_parts.protocol = url_str.substr(0, colon);
671 if (url_str.substr(colon, 3) !=
"://") {
672 string err =
"BESUtil::url_explode: no :// in the URL";
677 rest = url_str.substr(colon);
679 string::size_type slash = rest.find(
"/");
680 if (slash == string::npos) slash = rest.length();
682 string::size_type at = rest.find(
"@");
683 if ((at != string::npos) && (at < slash)) {
685 string up = rest.substr(0, at);
686 colon = up.find(
":");
687 if (colon != string::npos) {
688 url_parts.uname = up.substr(0, colon);
689 url_parts.psswd = up.substr(colon + 1);
692 url_parts.uname = up;
695 rest = rest.substr(at + 1);
697 slash = rest.find(
"/");
698 if (slash == string::npos) slash = rest.length();
699 colon = rest.find(
":");
700 if ((colon != string::npos) && (colon < slash)) {
702 url_parts.domain = rest.substr(0, colon);
704 rest = rest.substr(colon + 1);
705 slash = rest.find(
"/");
706 if (slash != string::npos) {
707 url_parts.port = rest.substr(0, slash);
708 url_parts.path = rest.substr(slash + 1);
711 url_parts.port = rest;
716 slash = rest.find(
"/");
717 if (slash != string::npos) {
718 url_parts.domain = rest.substr(0, slash);
719 url_parts.path = rest.substr(slash + 1);
722 url_parts.domain = rest;
729 string url = url_parts.protocol +
"://";
730 if (!url_parts.uname.empty()) {
731 url += url_parts.uname;
732 if (!url_parts.psswd.empty()) url +=
":" + url_parts.psswd;
735 url += url_parts.domain;
736 if (!url_parts.port.empty()) url +=
":" + url_parts.port;
737 if (!url_parts.path.empty()) url +=
"/" + url_parts.path;
757 assert(!firstPart.empty());
760 string first = firstPart;
761 string second = secondPart;
764 if (ensureLeadingSlash && first[0] !=
'/')
768 if (second[0] ==
'/')
769 second = second.substr(1);
772 if (first.back() ==
'/')
773 return first.append(second);
775 return first.append(
"/").append(second);
779 BESDEBUG(
"util",
"BESUtil::assemblePath() - firstPart: "<< firstPart << endl);
780 BESDEBUG(
"util",
"BESUtil::assemblePath() - secondPart: "<< secondPart << endl);
782 assert(!firstPart.empty());
784 string first = firstPart;
785 string second = secondPart;
787 if (ensureLeadingSlash) {
788 if (first[0] !=
'/') first =
"/" + first;
793 while (!first.empty() && *first.rbegin() ==
'/') {
795 first = first.substr(0, first.length() - 1);
799 while (!second.empty() && second[0] ==
'/') {
804 string newPath = first.append(
"/").append(second);
806 BESDEBUG(
"util",
"BESUtil::assemblePath() - newPath: "<< newPath << endl);
812 BESDEBUG(
"util",
"BESUtil::assemblePath() - firstPart: "<< firstPart << endl);
813 BESDEBUG(
"util",
"BESUtil::assemblePath() - secondPart: "<< secondPart << endl);
815 string first = firstPart;
816 string second = secondPart;
818 if (ensureLeadingSlash) {
819 if (*first.begin() !=
'/') first =
"/" + first;
823 while (*first.rbegin() ==
'/' && first.length() > 0) {
824 first = first.substr(0, first.length() - 1);
828 if (*first.rbegin() !=
'/') {
833 while (*second.begin() ==
'/' && second.length() > 0) {
834 second = second.substr(1);
837 string newPath = first + second;
839 BESDEBUG(
"util",
"BESUtil::assemblePath() - newPath: "<< newPath << endl);
851 if (fullString.length() >= ending.length()) {
852 return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
error thrown if the resource requested cannot be found
exception thrown if inernal error encountered
static string lowercase(const string &s)
static string www2id(const string &in, const string &escape="%", const string &except="")
static void removeLeadingAndTrailingBlanks(string &key)
static string assemblePath(const string &firstPart, const string &secondPart, bool addLeadingSlash=false)
Assemble path fragments making sure that they are separated by a single '/' character.
static string implode(const list< string > &values, char delim)
static string xml2id(string in)
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)
static void set_mime_text(ostream &strm)
Generate an HTTP 1.0 response header for a text document.
static void url_explode(const string &url_str, BESUtil::url &url_parts)
Given a url, break the url into its different parts.
static string id2xml(string in, const string ¬_allowed="><&'\)
static bool endsWith(std::string const &fullString, std::string const &ending)
error thrown if the BES is not allowed to access the resource requested
static char * fastpidconverter(char *buf, int base)
static string unescape(const string &s)
static void check_path(const string &path, const string &root, bool follow_sym_links)