35 #include <sys/types.h>
49 using std::istringstream ;
67 strm <<
"HTTP/1.0 200 OK" <<
CRLF ;
70 const time_t t = time(0);
71 strm <<
"Date: " << rfc822_date(t).c_str() <<
CRLF ;
72 strm <<
"Last-Modified: " << rfc822_date(t).c_str() <<
CRLF ;
74 strm <<
"Content-Type: text/plain" <<
CRLF ;
76 strm <<
"Content-Description: unknown" <<
CRLF ;
87 strm <<
"HTTP/1.0 200 OK" <<
CRLF ;
90 const time_t t = time(0);
91 strm <<
"Date: " << rfc822_date(t).c_str() <<
CRLF ;
92 strm <<
"Last-Modified: " << rfc822_date(t).c_str() <<
CRLF ;
94 strm <<
"Content-type: text/html" <<
CRLF ;
96 strm <<
"Content-Description: unknown" <<
CRLF ;
132 static const char *days[]={
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"};
133 static const char *months[]={
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
134 "Aug",
"Sep",
"Oct",
"Nov",
"Dec"};
146 BESUtil::rfc822_date(
const time_t t)
148 struct tm *stm = gmtime(&t);
151 snprintf(d, 255,
"%s, %02d %s %4d %02d:%02d:%02d GMT", days[stm->tm_wday],
152 stm->tm_mday, months[stm->tm_mon],
154 stm->tm_hour, stm->tm_min, stm->tm_sec);
163 istringstream ss( s ) ;
164 ss >> std::hex >> val;
166 tmp_str[0] =
static_cast<char>(val);
168 return string(tmp_str);
176 string::size_type i = 0;
178 while ((i = res.find_first_of(escape, i)) != string::npos) {
179 if (except.find(res.substr(i, 3)) != string::npos) {
183 res.replace(i, 3,
unhexstring(res.substr(i + 1, 2)));
192 string return_string = s ;
193 for(
int j = 0; j < static_cast<int>(return_string.length()); j++ )
195 return_string[j] = (char)tolower( return_string[j] ) ;
198 return return_string ;
205 string::size_type index = 0 ;
210 string::size_type bs = s.find(
'\\', index ) ;
211 if( bs == string::npos )
213 new_str += s.substr( index, s.length() - index ) ;
218 new_str += s.substr( index, bs - index ) ;
251 bool follow_sym_links )
260 string::size_type dotdot = path.find(
".." ) ;
261 if( dotdot != string::npos )
263 string s = (string)
"You are not allowed to access the node " + path;
275 rem = rem.substr( 1, rem.length() - 1 ) ;
276 if( rem[rem.length()-1] ==
'/' )
277 rem = rem.substr( 0, rem.length() - 1 ) ;
280 string fullpath = root ;
281 if( fullpath[fullpath.length()-1] ==
'/' )
283 fullpath = fullpath.substr( 0, fullpath.length() - 1 ) ;
291 size_t slash = rem.find(
'/' ) ;
292 if( slash == string::npos )
294 fullpath = fullpath +
"/" + rem ;
295 checked = checked +
"/" + rem ;
300 fullpath = fullpath +
"/" + rem.substr( 0, slash ) ;
301 checked = checked +
"/" + rem.substr( 0, slash ) ;
302 rem = rem.substr( slash + 1, rem.length() - slash ) ;
305 if( !follow_sym_links )
308 int statret = lstat( fullpath.c_str(), &buf ) ;
314 char *s_err = strerror( errsv ) ;
315 string error =
"Unable to access node " + checked +
": " ;
318 error = error + s_err ;
322 error = error +
"unknow access error" ;
326 if( errsv == ENOENT )
338 if( S_ISLNK( buf.st_mode ) )
340 string error =
"You do not have permission to access "
351 int statret = stat( fullpath.c_str(), &buf ) ;
357 char *s_err = strerror( errsv ) ;
358 string error =
"Unable to access node " + checked +
": " ;
361 error = error + s_err ;
365 error = error +
"unknow access error" ;
369 if( errsv == ENOENT )
396 if (base > 36 || base < 2)
403 r = ldiv (labs(val), base);
411 *buf++ =
"0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem];
421 string::size_type first = key.find_first_not_of(
" \t\n\r" ) ;
422 string::size_type last = key.find_last_not_of(
" \t\n\r" ) ;
423 if( first == string::npos ) key =
"" ;
426 string::size_type num = last - first + 1 ;
427 string new_key = key.substr( first, num ) ;
434 BESUtil::entity(
char c )
438 case '>':
return ">";
439 case '<':
return "<";
440 case '&':
return "&";
441 case '\'':
return "'";
442 case '\"':
return """;
443 default:
return string(1,c);
456 string::size_type i = 0 ;
458 while( ( i = in.find_first_of( not_allowed, i ) ) != string::npos )
460 in.replace( i, 1, entity( in[i] ) ) ;
475 string::size_type i = 0;
477 while ((i = in.find(
">", i)) != string::npos)
478 in.replace(i, 4,
">");
481 while ((i = in.find(
"<", i)) != string::npos)
482 in.replace(i, 4,
"<");
485 while ((i = in.find(
"&", i)) != string::npos)
486 in.replace(i, 5,
"&");
489 while ((i = in.find(
"'", i)) != string::npos)
490 in.replace(i, 6,
"'");
493 while ((i = in.find(
""", i)) != string::npos)
494 in.replace(i, 6,
"\"");
515 std::string::size_type start = 0 ;
516 std::string::size_type qstart = 0 ;
517 std::string::size_type adelim = 0 ;
518 std::string::size_type aquote = 0 ;
523 if( str[start] ==
'"' )
525 bool endquote = false ;
529 aquote = str.find(
'"', qstart ) ;
530 if( aquote == string::npos )
532 string currval = str.substr( start, str.length() - start ) ;
533 string err =
"BESUtil::explode - No end quote after value "
539 if( str[aquote-1] ==
'\\' )
541 if( str[aquote-2] ==
'\\' )
544 qstart = aquote + 1 ;
554 qstart = aquote + 1 ;
557 if( str[qstart] != delim && qstart != str.length() )
559 string currval = str.substr( start, qstart - start ) ;
560 string err =
"BESUtil::explode - No delim after end quote "
564 if( qstart == str.length() )
566 adelim = string::npos ;
575 adelim = str.find( delim, start ) ;
577 if( adelim == string::npos )
579 aval = str.substr( start, str.length() - start ) ;
584 aval = str.substr( start, adelim - start ) ;
586 values.push_back( aval ) ;
588 if( start == str.length() )
590 values.push_back(
"" ) ;
610 list<string>::const_iterator i = values.begin() ;
611 list<string>::const_iterator e = values.end() ;
616 if( !first ) result += delim ;
617 d = (*i).find( delim ) ;
618 if( d != string::npos && (*i)[0] !=
'"' )
620 string err = (string)
"BESUtil::implode - delimiter exists in value "
655 string::size_type colon = url_str.find(
":" ) ;
656 if( colon == string::npos )
658 string err =
"BESUtil::url_explode: missing colon for protocol" ;
662 url_parts.
protocol = url_str.substr( 0, colon ) ;
664 if( url_str.substr( colon, 3 ) !=
"://" )
666 string err =
"BESUtil::url_explode: no :// in the URL" ;
671 rest = url_str.substr( colon ) ;
673 string::size_type slash = rest.find(
"/" ) ;
674 if( slash == string::npos ) slash = rest.length() ;
676 string::size_type at = rest.find(
"@" ) ;
677 if( ( at != string::npos ) && ( at < slash ) )
680 string up = rest.substr( 0, at ) ;
681 colon = up.find(
":" ) ;
682 if( colon != string::npos )
684 url_parts.
uname = up.substr( 0, colon ) ;
685 url_parts.
psswd = up.substr( colon+1 ) ;
689 url_parts.
uname = up ;
692 rest = rest.substr( at+1 ) ;
694 slash = rest.find(
"/" ) ;
695 if( slash == string::npos ) slash = rest.length() ;
696 colon = rest.find(
":" ) ;
697 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 )
706 url_parts.
port = rest.substr( 0, slash ) ;
707 url_parts.
path = rest.substr( slash+1 ) ;
711 url_parts.
port = rest ;
712 url_parts.
path =
"" ;
717 slash = rest.find(
"/" ) ;
718 if( slash != string::npos )
720 url_parts.
domain = rest.substr( 0, slash ) ;
721 url_parts.
path = rest.substr( slash+1 ) ;
734 if( !url_parts.
uname.empty() )
736 url += url_parts.
uname ;
737 if( !url_parts.
psswd.empty() ) url +=
":" + url_parts.
psswd ;
741 if( !url_parts.
port.empty() ) url +=
":" + url_parts.
port ;
742 if( !url_parts.
path.empty() ) url +=
"/" + url_parts.
path ;