BESUtil.cc

Go to the documentation of this file.
00001 // BESUtil.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004,2005 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
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     // Note that Content-Description is from RFC 2045 (MIME, pt 1), not 2616.
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     // Note that Content-Description is from RFC 2045 (MIME, pt 1), not 2616.
00078     strm << "Content-Description: unknown" << CRLF ;
00079     strm << CRLF ;
00080 }
00081 
00082 // Return a MIME rfc-822 date. The grammar for this is:
00083 //       date-time   =  [ day "," ] date time        ; dd mm yy
00084 //                                                   ;  hh:mm:ss zzz
00085 //
00086 //       day         =  "Mon"  / "Tue" /  "Wed"  / "Thu"
00087 //                   /  "Fri"  / "Sat" /  "Sun"
00088 //
00089 //       date        =  1*2DIGIT month 2DIGIT        ; day month year
00090 //                                                   ;  e.g. 20 Jun 82
00091 //                   NB: year is 4 digit; see RFC 1123. 11/30/99 jhrg
00092 //
00093 //       month       =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"
00094 //                   /  "May"  /  "Jun" /  "Jul"  /  "Aug"
00095 //                   /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"
00096 //
00097 //       time        =  hour zone                    ; ANSI and Military
00098 //
00099 //       hour        =  2DIGIT ":" 2DIGIT [":" 2DIGIT]
00100 //                                                   ; 00:00:00 - 23:59:59
00101 //
00102 //       zone        =  "UT"  / "GMT"                ; Universal Time
00103 //                                                   ; North American : UT
00104 //                   /  "EST" / "EDT"                ;  Eastern:  - 5/ - 4
00105 //                   /  "CST" / "CDT"                ;  Central:  - 6/ - 5
00106 //                   /  "MST" / "MDT"                ;  Mountain: - 7/ - 6
00107 //                   /  "PST" / "PDT"                ;  Pacific:  - 8/ - 7
00108 //                   /  1ALPHA                       ; Military: Z = UT;
00109 //                                                   ;  A:-1; (J not used)
00110 //                                                   ;  M:-12; N:+1; Y:+12
00111 //                   / ( ("+" / "-") 4DIGIT )        ; Local differential
00112 //                                                   ;  hours+min. (HHMM)
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 

Generated on Wed Jan 2 06:00:40 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.4