35 #include <sys/types.h>
49 using std::istringstream;
65 strm <<
"HTTP/1.0 200 OK" <<
CRLF;
68 const time_t t = time(0);
69 strm <<
"Date: " << rfc822_date(t).c_str() <<
CRLF;
70 strm <<
"Last-Modified: " << rfc822_date(t).c_str() <<
CRLF;
72 strm <<
"Content-Type: text/plain" <<
CRLF;
74 strm <<
"Content-Description: unknown" <<
CRLF;
83 strm <<
"HTTP/1.0 200 OK" <<
CRLF;
86 const time_t t = time(0);
87 strm <<
"Date: " << rfc822_date(t).c_str() <<
CRLF;
88 strm <<
"Last-Modified: " << rfc822_date(t).c_str() <<
CRLF;
90 strm <<
"Content-type: text/html" <<
CRLF;
92 strm <<
"Content-Description: unknown" <<
CRLF;
128 static const char *days[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" };
129 static const char *months[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
130 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" };
141 string BESUtil::rfc822_date(
const time_t t) {
142 struct tm *stm = gmtime(&t);
145 snprintf(d, 255,
"%s, %02d %s %4d %02d:%02d:%02d GMT", days[stm->tm_wday],
146 stm->tm_mday, months[stm->tm_mon], 1900 + stm->tm_year,
147 stm->tm_hour, stm->tm_min, stm->tm_sec);
155 ss >> std::hex >> val;
157 tmp_str[0] =
static_cast<char> (val);
159 return string(tmp_str);
165 const string &except) {
166 string::size_type i = 0;
168 while ((i = res.find_first_of(escape, i)) != string::npos) {
169 if (except.find(res.substr(i, 3)) != string::npos) {
173 res.replace(i, 3,
unhexstring(res.substr(i + 1, 2)));
180 string return_string = s;
181 for (
int j = 0; j < static_cast<int> (return_string.length()); j++) {
182 return_string[j] = (char) tolower(return_string[j]);
185 return return_string;
190 string::size_type index = 0;
194 string::size_type bs = s.find(
'\\', index);
195 if (bs == string::npos) {
196 new_str += s.substr(index, s.length() - index);
199 new_str += s.substr(index, bs - index);
200 new_str += s[bs + 1];
230 bool follow_sym_links) {
238 string::size_type dotdot = path.find(
"..");
239 if (dotdot != string::npos) {
240 string s = (string)
"You are not allowed to access the node " + path;
252 rem = rem.substr(1, rem.length() - 1);
253 if (rem[rem.length() - 1] ==
'/')
254 rem = rem.substr(0, rem.length() - 1);
257 string fullpath = root;
258 if (fullpath[fullpath.length() - 1] ==
'/') {
259 fullpath = fullpath.substr(0, fullpath.length() - 1);
266 size_t slash = rem.find(
'/');
267 if (slash == string::npos) {
268 fullpath = fullpath +
"/" + rem;
269 checked = checked +
"/" + rem;
272 fullpath = fullpath +
"/" + rem.substr(0, slash);
273 checked = checked +
"/" + rem.substr(0, slash);
274 rem = rem.substr(slash + 1, rem.length() - slash);
277 if (!follow_sym_links) {
279 int statret = lstat(fullpath.c_str(), &buf);
284 char *s_err = strerror(errsv);
285 string error =
"Unable to access node " + checked +
": ";
287 error = error + s_err;
289 error = error +
"unknown access error";
293 if (errsv == ENOENT) {
300 if (S_ISLNK( buf.st_mode )) {
301 string error =
"You do not have permission to access "
310 int statret = stat(fullpath.c_str(), &buf);
315 char *s_err = strerror(errsv);
316 string error =
"Unable to access node " + checked +
": ";
318 error = error + s_err;
320 error = error +
"unknown access error";
324 if (errsv == ENOENT) {
346 if (base > 36 || base < 2)
353 r = ldiv(labs(val), base);
361 *buf++ =
"0123456789abcdefghijklmnopqrstuvwxyz"[(int) r.rem];
368 string::size_type first = key.find_first_not_of(
" \t\n\r");
369 string::size_type last = key.find_last_not_of(
" \t\n\r");
370 if (first == string::npos)
373 string::size_type num = last - first + 1;
374 string new_key = key.substr(first, num);
380 string BESUtil::entity(
char c) {
404 string::size_type i = 0;
406 while ((i = in.find_first_of(not_allowed, i)) != string::npos) {
407 in.replace(i, 1, entity(in[i]));
420 string::size_type i = 0;
422 while ((i = in.find(
">", i)) != string::npos)
423 in.replace(i, 4,
">");
426 while ((i = in.find(
"<", i)) != string::npos)
427 in.replace(i, 4,
"<");
430 while ((i = in.find(
"&", i)) != string::npos)
431 in.replace(i, 5,
"&");
434 while ((i = in.find(
"'", i)) != string::npos)
435 in.replace(i, 6,
"'");
438 while ((i = in.find(
""", i)) != string::npos)
439 in.replace(i, 6,
"\"");
458 std::string::size_type start = 0;
459 std::string::size_type qstart = 0;
460 std::string::size_type adelim = 0;
461 std::string::size_type aquote = 0;
465 if (str[start] ==
'"') {
466 bool endquote =
false;
469 aquote = str.find(
'"', qstart);
470 if (aquote == string::npos) {
471 string currval = str.substr(start, str.length() - start);
472 string err =
"BESUtil::explode - No end quote after value "
478 if (str[aquote - 1] ==
'\\') {
479 if (str[aquote - 2] ==
'\\') {
490 if (str[qstart] != delim && qstart != str.length()) {
491 string currval = str.substr(start, qstart - start);
492 string err =
"BESUtil::explode - No delim after end quote "
496 if (qstart == str.length()) {
497 adelim = string::npos;
502 adelim = str.find(delim, start);
504 if (adelim == string::npos) {
505 aval = str.substr(start, str.length() - start);
508 aval = str.substr(start, adelim - start);
511 values.push_back(aval);
513 if (start == str.length()) {
514 values.push_back(
"");
532 list<string>::const_iterator i = values.begin();
533 list<string>::const_iterator e = values.end();
536 for (; i != e; i++) {
539 d = (*i).find(delim);
540 if (d != string::npos && (*i)[0] !=
'"') {
542 (string)
"BESUtil::implode - delimiter exists in value "
575 string::size_type colon = url_str.find(
":");
576 if (colon == string::npos) {
577 string err =
"BESUtil::url_explode: missing colon for protocol";
581 url_parts.
protocol = url_str.substr(0, colon);
583 if (url_str.substr(colon, 3) !=
"://") {
584 string err =
"BESUtil::url_explode: no :// in the URL";
589 rest = url_str.substr(colon);
591 string::size_type slash = rest.find(
"/");
592 if (slash == string::npos)
593 slash = rest.length();
595 string::size_type at = rest.find(
"@");
596 if ((at != string::npos) && (at < slash)) {
598 string up = rest.substr(0, at);
599 colon = up.find(
":");
600 if (colon != string::npos) {
601 url_parts.
uname = up.substr(0, colon);
602 url_parts.
psswd = up.substr(colon + 1);
604 url_parts.
uname = up;
607 rest = rest.substr(at + 1);
609 slash = rest.find(
"/");
610 if (slash == string::npos)
611 slash = rest.length();
612 colon = rest.find(
":");
613 if ((colon != string::npos) && (colon < slash)) {
615 url_parts.
domain = rest.substr(0, colon);
617 rest = rest.substr(colon + 1);
618 slash = rest.find(
"/");
619 if (slash != string::npos) {
620 url_parts.
port = rest.substr(0, slash);
621 url_parts.
path = rest.substr(slash + 1);
623 url_parts.
port = rest;
627 slash = rest.find(
"/");
628 if (slash != string::npos) {
629 url_parts.
domain = rest.substr(0, slash);
630 url_parts.
path = rest.substr(slash + 1);
639 if (!url_parts.
uname.empty()) {
640 url += url_parts.
uname;
641 if (!url_parts.
psswd.empty())
642 url +=
":" + url_parts.
psswd;
646 if (!url_parts.
port.empty())
647 url +=
":" + url_parts.
port;
648 if (!url_parts.
path.empty())
649 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.
static void check_path(const string &path, const string &root, bool follow_sym_links)
Check if the specified path is valid.