BESUtil.cc

Go to the documentation of this file.
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     // Note that Content-Description is from RFC 2045 (MIME, pt 1), not 2616.
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     // Note that Content-Description is from RFC 2045 (MIME, pt 1), not 2616.
00043     fprintf( out, "Content-Description: unknown%s", CRLF ) ;
00044     fprintf( out, CRLF ) ;
00045 }
00046 
00047 // Return a MIME rfc-822 date. The grammar for this is:
00048 //       date-time   =  [ day "," ] date time        ; dd mm yy
00049 //                                                   ;  hh:mm:ss zzz
00050 //
00051 //       day         =  "Mon"  / "Tue" /  "Wed"  / "Thu"
00052 //                   /  "Fri"  / "Sat" /  "Sun"
00053 //
00054 //       date        =  1*2DIGIT month 2DIGIT        ; day month year
00055 //                                                   ;  e.g. 20 Jun 82
00056 //                   NB: year is 4 digit; see RFC 1123. 11/30/99 jhrg
00057 //
00058 //       month       =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"
00059 //                   /  "May"  /  "Jun" /  "Jul"  /  "Aug"
00060 //                   /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"
00061 //
00062 //       time        =  hour zone                    ; ANSI and Military
00063 //
00064 //       hour        =  2DIGIT ":" 2DIGIT [":" 2DIGIT]
00065 //                                                   ; 00:00:00 - 23:59:59
00066 //
00067 //       zone        =  "UT"  / "GMT"                ; Universal Time
00068 //                                                   ; North American : UT
00069 //                   /  "EST" / "EDT"                ;  Eastern:  - 5/ - 4
00070 //                   /  "CST" / "CDT"                ;  Central:  - 6/ - 5
00071 //                   /  "MST" / "MDT"                ;  Mountain: - 7/ - 6
00072 //                   /  "PST" / "PDT"                ;  Pacific:  - 8/ - 7
00073 //                   /  1ALPHA                       ; Military: Z = UT;
00074 //                                                   ;  A:-1; (J not used)
00075 //                                                   ;  M:-12; N:+1; Y:+12
00076 //                   / ( ("+" / "-") 4DIGIT )        ; Local differential
00077 //                                                   ;  hours+min. (HHMM)
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             // On Solaris 2.7 this tm_year is years since 1900. 3/17/2000
00101             // jhrg
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 

Generated on Wed Aug 29 03:14:16 2007 for OPeNDAP Back End Server (BES) by  doxygen 1.5.2