00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <sstream>
00035 #include <iostream>
00036
00037 using std::istringstream ;
00038 using std::cout ;
00039 using std::endl ;
00040
00041 #include "BESUtil.h"
00042 #include "config.h"
00043
00044 #define CRLF "\r\n"
00045
00050 void
00051 BESUtil::set_mime_text( ostream &strm )
00052 {
00053 strm << "HTTP/1.0 200 OK" << CRLF ;
00054 strm << "XBES-Server: " << PACKAGE_STRING << CRLF ;
00055
00056 const time_t t = time(0);
00057 strm << "Date: " << rfc822_date(t).c_str() << CRLF ;
00058 strm << "Last-Modified: " << rfc822_date(t).c_str() << CRLF ;
00059
00060 strm << "Content-Type: text/plain" << CRLF ;
00061
00062 strm << "Content-Description: unknown" << CRLF ;
00063 strm << CRLF ;
00064 }
00065
00066 void
00067 BESUtil::set_mime_html( ostream &strm )
00068 {
00069 strm << "HTTP/1.0 200 OK" << CRLF ;
00070 strm << "XBES-Server: " << PACKAGE_STRING << CRLF ;
00071
00072 const time_t t = time(0);
00073 strm << "Date: " << rfc822_date(t).c_str() << CRLF ;
00074 strm << "Last-Modified: " << rfc822_date(t).c_str() << CRLF ;
00075
00076 strm << "Content-type: text/html" << CRLF ;
00077
00078 strm << "Content-Description: unknown" << CRLF ;
00079 strm << CRLF ;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 static const char *days[]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
00115 static const char *months[]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
00116 "Aug", "Sep", "Oct", "Nov", "Dec"};
00117
00126 string
00127 BESUtil::rfc822_date(const time_t t)
00128 {
00129 struct tm *stm = gmtime(&t);
00130 char d[256];
00131
00132 snprintf(d, 255, "%s, %02d %s %4d %02d:%02d:%02d GMT", days[stm->tm_wday],
00133 stm->tm_mday, months[stm->tm_mon],
00134 1900 + stm->tm_year,
00135 stm->tm_hour, stm->tm_min, stm->tm_sec);
00136 d[255] = '\0';
00137 return string(d);
00138 }
00139
00140 string
00141 BESUtil::unhexstring( string s )
00142 {
00143 int val;
00144 istringstream ss( s ) ;
00145 ss >> std::hex >> val;
00146 char tmp_str[2];
00147 tmp_str[0] = static_cast<char>(val);
00148 tmp_str[1] = '\0';
00149 return string(tmp_str);
00150 }
00151
00152 string
00153 BESUtil::www2id(const string &in, const string &escape, const string &except)
00154 {
00155 string::size_type i = 0;
00156 string res = in;
00157 while ((i = res.find_first_of(escape, i)) != string::npos) {
00158 if (res.substr(i, 3) == except) {
00159 i += 3;
00160 continue;
00161 }
00162 res.replace(i, 3, unhexstring(res.substr(i + 1, 2)));
00163 }
00164
00165 return res;
00166 }
00167
00168 string
00169 BESUtil::lowercase( const string &s )
00170 {
00171 string return_string = s ;
00172 for( int j = 0; j < return_string.length(); j++ )
00173 {
00174 return_string[j] = (char)tolower( return_string[j] ) ;
00175 }
00176
00177 return return_string ;
00178 }
00179
00180 string
00181 BESUtil::unescape( const string &s )
00182 {
00183 bool done = false ;
00184 string::size_type index = 0 ;
00185 string::size_type new_index = 0 ;
00186 string new_str ;
00187 while( !done )
00188 {
00189 string::size_type bs = s.find( '\\', index ) ;
00190 if( bs == string::npos )
00191 {
00192 new_str += s.substr( index, s.length() - index ) ;
00193 done = true ;
00194 }
00195 else
00196 {
00197 new_str += s.substr( index, bs - index ) ;
00198 new_str += s[bs+1] ;
00199 index = bs+2 ;
00200 }
00201 }
00202
00203 return new_str ;
00204 }
00205