35 #include <sys/types.h> 49 using std::istringstream;
55 #include "BESForbiddenError.h" 56 #include "BESNotFoundError.h" 57 #include "BESInternalError.h" 61 #define debug_key "BesUtil" 69 strm <<
"HTTP/1.0 200 OK" << CRLF;
70 strm <<
"XBES-Server: " << PACKAGE_STRING << CRLF;
72 const time_t t = time(0);
73 strm <<
"Date: " << rfc822_date(t).c_str() << CRLF;
74 strm <<
"Last-Modified: " << rfc822_date(t).c_str() << CRLF;
76 strm <<
"Content-Type: text/plain" << CRLF;
78 strm <<
"Content-Description: unknown" << CRLF;
88 strm <<
"HTTP/1.0 200 OK" << CRLF;
89 strm <<
"XBES-Server: " << PACKAGE_STRING << CRLF;
91 const time_t t = time(0);
92 strm <<
"Date: " << rfc822_date(t).c_str() << CRLF;
93 strm <<
"Last-Modified: " << rfc822_date(t).c_str() << CRLF;
95 strm <<
"Content-type: text/html" << CRLF;
97 strm <<
"Content-Description: unknown" << CRLF;
133 static const char *days[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" };
134 static const char *months[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" };
145 string BESUtil::rfc822_date(
const time_t t)
147 struct tm *stm = gmtime(&t);
150 snprintf(d, 255,
"%s, %02d %s %4d %02d:%02d:%02d GMT", days[stm->tm_wday], stm->tm_mday, months[stm->tm_mon],
151 1900 + stm->tm_year, stm->tm_hour, stm->tm_min, stm->tm_sec);
156 string BESUtil::unhexstring(
string s)
160 ss >> std::hex >> val;
162 tmp_str[0] =
static_cast<char>(val);
164 return string(tmp_str);
171 string::size_type i = 0;
173 while ((i = res.find_first_of(escape, i)) != string::npos) {
174 if (except.find(res.substr(i, 3)) != string::npos) {
178 res.replace(i, 3, unhexstring(res.substr(i + 1, 2)));
186 string return_string = s;
187 for (
int j = 0; j < static_cast<int>(return_string.length()); j++) {
188 return_string[j] = (char) tolower(return_string[j]);
191 return return_string;
197 string::size_type index = 0;
201 string::size_type bs = s.find(
'\\', index);
202 if (bs == string::npos) {
203 new_str += s.substr(index, s.length() - index);
207 new_str += s.substr(index, bs - index);
208 new_str += s[bs + 1];
241 if (path ==
"")
return;
246 int (*ye_old_stat_function)(
const char *pathname,
struct stat *buf);
247 if (follow_sym_links) {
248 BESDEBUG(debug_key,
"eval_w10n_resourceId() - Using 'stat' function (follow_sym_links = true)" << endl);
249 ye_old_stat_function = &stat;
252 BESDEBUG(debug_key,
"eval_w10n_resourceId() - Using 'lstat' function (follow_sym_links = false)" << endl);
253 ye_old_stat_function = &lstat;
258 string::size_type dotdot = path.find(
"..");
259 if (dotdot != string::npos) {
260 string s = (string)
"You are not allowed to access the node " + path;
271 if (rem[0] ==
'/') rem = rem.substr(1, rem.length() - 1);
272 if (rem[rem.length() - 1] ==
'/') rem = rem.substr(0, rem.length() - 1);
275 string fullpath = root;
276 if (fullpath[fullpath.length() - 1] ==
'/') {
277 fullpath = fullpath.substr(0, fullpath.length() - 1);
283 size_t slash = rem.find(
'/');
284 if (slash == string::npos) {
285 fullpath = fullpath +
"/" + rem;
286 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;
307 error = error +
"unknown access error";
310 BESDEBUG(debug_key,
"check_path() - error: "<< error <<
" errno: " << errno << endl);
315 if (errsv == ENOENT || errsv == ENOTDIR) {
328 if (S_ISLNK(buf.st_mode)) {
329 string error =
"You do not have permission to access " + checked;
337 size_t slash = rem.find(
'/');
338 if (slash == string::npos) {
339 fullpath = fullpath +
"/" + rem;
340 checked = checked +
"/" + rem;
344 fullpath = fullpath +
"/" + rem.substr(0, slash);
345 checked = checked +
"/" + rem.substr(0, slash);
346 rem = rem.substr(slash + 1, rem.length() - slash);
349 if (!follow_sym_links) {
351 int statret = lstat(fullpath.c_str(), &buf);
356 char *s_err = strerror(errsv);
357 string error =
"Unable to access node " + checked +
": ";
359 error = error + s_err;
362 error = error +
"unknown access error";
366 if (errsv == ENOENT) {
375 if (S_ISLNK( buf.st_mode )) {
376 string error =
"You do not have permission to access " 386 int statret = stat(fullpath.c_str(), &buf);
391 char *s_err = strerror(errsv);
392 string error =
"Unable to access node " + checked +
": ";
394 error = error + s_err;
397 error = error +
"unknown access error";
401 if (errsv == ENOENT) {
427 if (base > 36 || base < 2)
432 if (val < 0) *buf++ =
'-';
433 r = ldiv(labs(val), base);
440 *buf++ =
"0123456789abcdefghijklmnopqrstuvwxyz"[(int) r.rem];
448 string::size_type first = key.find_first_not_of(
" \t\n\r");
449 string::size_type last = key.find_last_not_of(
" \t\n\r");
450 if (first == string::npos)
453 string::size_type num = last - first + 1;
454 string new_key = key.substr(first, num);
460 string BESUtil::entity(
char c)
486 string::size_type i = 0;
488 while ((i = in.find_first_of(not_allowed, i)) != string::npos) {
489 in.replace(i, 1, entity(in[i]));
503 string::size_type i = 0;
505 while ((i = in.find(
">", i)) != string::npos)
506 in.replace(i, 4,
">");
509 while ((i = in.find(
"<", i)) != string::npos)
510 in.replace(i, 4,
"<");
513 while ((i = in.find(
"&", i)) != string::npos)
514 in.replace(i, 5,
"&");
517 while ((i = in.find(
"'", i)) != string::npos)
518 in.replace(i, 6,
"'");
521 while ((i = in.find(
""", i)) != string::npos)
522 in.replace(i, 6,
"\"");
542 std::string::size_type start = 0;
543 std::string::size_type qstart = 0;
544 std::string::size_type adelim = 0;
545 std::string::size_type aquote = 0;
549 if (str[start] ==
'"') {
550 bool endquote =
false;
553 aquote = str.find(
'"', qstart);
554 if (aquote == string::npos) {
555 string currval = str.substr(start, str.length() - start);
556 string err =
"BESUtil::explode - No end quote after value " + currval;
561 if (str[aquote - 1] ==
'\\') {
562 if (str[aquote - 2] ==
'\\') {
575 if (str[qstart] != delim && qstart != str.length()) {
576 string currval = str.substr(start, qstart - start);
577 string err =
"BESUtil::explode - No delim after end quote " + currval;
580 if (qstart == str.length()) {
581 adelim = string::npos;
588 adelim = str.find(delim, start);
590 if (adelim == string::npos) {
591 aval = str.substr(start, str.length() - start);
595 aval = str.substr(start, adelim - start);
598 values.push_back(aval);
600 if (start == str.length()) {
601 values.push_back(
"");
620 list<string>::const_iterator i = values.begin();
621 list<string>::const_iterator e = values.end();
624 for (; i != e; i++) {
625 if (!first) result += delim;
626 d = (*i).find(delim);
627 if (d != string::npos && (*i)[0] !=
'"') {
628 string err = (string)
"BESUtil::implode - delimiter exists in value " + (*i);
661 string::size_type colon = url_str.find(
":");
662 if (colon == string::npos) {
663 string err =
"BESUtil::url_explode: missing colon for protocol";
667 url_parts.protocol = url_str.substr(0, colon);
669 if (url_str.substr(colon, 3) !=
"://") {
670 string err =
"BESUtil::url_explode: no :// in the URL";
675 rest = url_str.substr(colon);
677 string::size_type slash = rest.find(
"/");
678 if (slash == string::npos) slash = rest.length();
680 string::size_type at = rest.find(
"@");
681 if ((at != string::npos) && (at < slash)) {
683 string up = rest.substr(0, at);
684 colon = up.find(
":");
685 if (colon != string::npos) {
686 url_parts.uname = up.substr(0, colon);
687 url_parts.psswd = up.substr(colon + 1);
690 url_parts.uname = up;
693 rest = rest.substr(at + 1);
695 slash = rest.find(
"/");
696 if (slash == string::npos) slash = rest.length();
697 colon = rest.find(
":");
698 if ((colon != string::npos) && (colon < slash)) {
700 url_parts.domain = rest.substr(0, colon);
702 rest = rest.substr(colon + 1);
703 slash = rest.find(
"/");
704 if (slash != string::npos) {
705 url_parts.port = rest.substr(0, slash);
706 url_parts.path = rest.substr(slash + 1);
709 url_parts.port = rest;
714 slash = rest.find(
"/");
715 if (slash != string::npos) {
716 url_parts.domain = rest.substr(0, slash);
717 url_parts.path = rest.substr(slash + 1);
720 url_parts.domain = rest;
727 string url = url_parts.protocol +
"://";
728 if (!url_parts.uname.empty()) {
729 url += url_parts.uname;
730 if (!url_parts.psswd.empty()) url +=
":" + url_parts.psswd;
733 url += url_parts.domain;
734 if (!url_parts.port.empty()) url +=
":" + url_parts.port;
735 if (!url_parts.path.empty()) url +=
"/" + url_parts.path;
753 string firstPathFragment = firstPart;
754 string secondPathFragment = secondPart;
757 if(ensureLeadingSlash){
758 if(*firstPathFragment.begin() !=
'/')
759 firstPathFragment =
"/" + firstPathFragment;
763 while(*firstPathFragment.rbegin() ==
'/' && firstPathFragment.length()>0){
764 firstPathFragment = firstPathFragment.substr(0,firstPathFragment.length()-1);
769 if(*firstPathFragment.rbegin() !=
'/'){
770 firstPathFragment +=
"/";
775 while(*secondPathFragment.begin() ==
'/' && secondPathFragment.length()>0){
776 secondPathFragment = secondPathFragment.substr(1);
781 string newPath = firstPathFragment + secondPathFragment;
error thrown if the resource requested cannot be found
static string id2xml(string in, const string ¬_allowed="><&'\"")
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.
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)