00001
00002 #include <sstream>
00003
00004 using std::istringstream ;
00005
00006 #include "BESUtil.h"
00007 #include "config.h"
00008
00009 #define CRLF "\r\n"
00010
00015 void
00016 BESUtil::set_mime_text( FILE *out )
00017 {
00018 fprintf( out, "HTTP/1.0 200 OK%s", CRLF ) ;
00019 fprintf( out, "XBES-Server: %s%s", PACKAGE_STRING, CRLF ) ;
00020
00021 const time_t t = time(0);
00022 fprintf( out, "Date: %s%s", rfc822_date(t).c_str(), CRLF ) ;
00023 fprintf( out, "Last-Modified: %sw%s", rfc822_date(t).c_str(), CRLF ) ;
00024
00025 fprintf( out, "Content-Type: text/plain%s", CRLF ) ;
00026
00027 fprintf( out, "Content-Description: unknown%s", CRLF ) ;
00028 fprintf( out, CRLF ) ;
00029 }
00030
00031 void
00032 BESUtil::set_mime_html( FILE *out )
00033 {
00034 fprintf( out, "HTTP/1.0 200 OK%s", CRLF ) ;
00035 fprintf( out, "XBES-Server: %s%s", PACKAGE_STRING, CRLF ) ;
00036
00037 const time_t t = time(0);
00038 fprintf( out, "Date: %s%s", rfc822_date(t).c_str(), CRLF ) ;
00039 fprintf( out, "Last-Modified: %sw%s", rfc822_date(t).c_str(), CRLF ) ;
00040
00041 fprintf( out, "Content-type: text/html%s", CRLF ) ;
00042
00043 fprintf( out, "Content-Description: unknown%s", CRLF ) ;
00044 fprintf( out, CRLF ) ;
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 static const char *days[]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
00080 static const char *months[]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
00081 "Aug", "Sep", "Oct", "Nov", "Dec"};
00082
00091 string
00092 BESUtil::rfc822_date(const time_t t)
00093 {
00094 struct tm *stm = gmtime(&t);
00095 char d[256];
00096
00097 sprintf(d, "%s, %02d %s %4d %02d:%02d:%02d GMT", days[stm->tm_wday],
00098 stm->tm_mday, months[stm->tm_mon],
00099 #if 0
00100
00101
00102 stm->tm_year < 100 ? 1900 + stm->tm_year : stm->tm_year,
00103 #endif
00104 1900 + stm->tm_year,
00105 stm->tm_hour, stm->tm_min, stm->tm_sec);
00106 return string(d);
00107 }
00108
00109 string
00110 BESUtil::unhexstring( string s )
00111 {
00112 int val;
00113 istringstream ss( s ) ;
00114 ss >> std::hex >> val;
00115 char tmp_str[2];
00116 tmp_str[0] = static_cast<char>(val);
00117 tmp_str[1] = '\0';
00118 return string(tmp_str);
00119 }
00120
00121 string
00122 BESUtil::www2id(const string &in, const string &escape, const string &except)
00123 {
00124 string::size_type i = 0;
00125 string res = in;
00126 while ((i = res.find_first_of(escape, i)) != string::npos) {
00127 if (res.substr(i, 3) == except) {
00128 i += 3;
00129 continue;
00130 }
00131 res.replace(i, 3, unhexstring(res.substr(i + 1, 2)));
00132 }
00133
00134 return res;
00135 }
00136