00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00028 #include <new>
00029 #include <string>
00030 #include <vector>
00031 #include <stdexcept>
00032 #include <iostream>
00033 #include <cstdlib>
00034 
00035 #include "cgicc/CgiDefs.h"
00036 #include "cgicc/Cgicc.h"
00037 #include "cgicc/HTTPHTMLHeader.h"
00038 #include "cgicc/HTMLClasses.h"
00039 
00040 #if HAVE_UNAME
00041 #  include <sys/utsname.h>
00042 #endif
00043 
00044 #if HAVE_SYS_TIME_H
00045 #  include <sys/time.h>
00046 #endif
00047 
00048 
00049 
00050 #if DEBUG
00051   STDNS ofstream gLogFile( "/change_this_path/cgicc.log", STDNS ios::app );
00052 #endif
00053 
00054 #if CGICC_USE_NAMESPACES
00055   using namespace std;
00056   using namespace cgicc;
00057 #else
00058 #  define div div_
00059 #  define link link_
00060 #  define select select_
00061 #endif
00062 
00063 
00064 int
00065 main(int , 
00066      char ** )
00067 {
00068   try {
00069 #if HAVE_GETTIMEOFDAY
00070     timeval start;
00071     gettimeofday(&start, NULL);
00072 #endif
00073 
00074     
00075     Cgicc cgi;
00076     
00077     
00078     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00079     cout << html().set("lang", "en").set("dir", "ltr") << endl;
00080 
00081     
00082     
00083     cout << head() << endl;
00084 
00085     
00086     cout << style() << comment() << endl;
00087     cout << "body { color: black; background-color: white; }" << endl;
00088     cout << "hr.half { width: 60%; align: center; }" << endl;
00089     cout << "span.red, strong.red { color: red; }" << endl;
00090     cout << "div.smaller { font-size: small; }" << endl;
00091     cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
00092          << "background: #ddd; }" << endl;
00093     cout << "span.blue { color: blue; }" << endl;
00094     cout << "col.title { color: white; background-color: black; ";
00095     cout << "font-weight: bold; text-align: center; }" << endl;
00096     cout << "col.data { background-color: #ddd; text-align: left; }" << endl;
00097     cout << "td.data, TR.data { background-color: #ddd; text-align: left; }"
00098          << endl;
00099     cout << "td.grayspecial { background-color: #ddd; text-align: left; }"
00100          << endl;
00101     cout << "td.ltgray, tr.ltgray { background-color: #ddd; }" << endl;
00102     cout << "td.dkgray, tr.dkgray { background-color: #bbb; }" << endl;
00103     cout << "col.black, td.black, td.title, tr.title { color: white; " 
00104          << "background-color: black; font-weight: bold; text-align: center; }"
00105          << endl;
00106     cout << "col.gray, td.gray { background-color: #ddd; text-align: center; }"
00107          << endl;
00108     cout << "table.cgi { left-margin: auto; right-margin: auto; width: 90%; }"
00109          << endl;
00110 
00111     cout << comment() << style() << endl;
00112 
00113     cout << title() << "GNU cgicc v" << cgi.getVersion() 
00114          << " File Upload Test Results" << title() << endl;
00115 
00116     cout << head() << endl;
00117     
00118     
00119     cout << body() << endl;
00120 
00121     cout << h1() << "GNU cgi" << span("cc").set("class","red")
00122          << " v"<< cgi.getVersion() << " File Upload Test Results" 
00123          << h1() << endl;
00124     
00125     
00126     const CgiEnvironment& env = cgi.getEnvironment();
00127     
00128     
00129     cout << comment() << "This page generated by cgicc for "
00130          << env.getRemoteHost() << comment() << endl;
00131     cout << h4() << "Thanks for using cgi" << span("cc").set("class", "red") 
00132          << ", " << env.getRemoteHost() 
00133          << '(' << env.getRemoteAddr() << ")!" << h4() << endl;  
00134     
00135 
00136     
00137     cout << h2("File Uploaded via FormFile") << endl;
00138   
00139     const_file_iterator file;
00140     file = cgi.getFile("userfile");
00141                                 
00142     if(file != cgi.getFiles().end()) {
00143       cout << CGICCNS div().set("align","center") << endl;
00144     
00145       cout << table().set("border","0").set("rules","none").set("frame","void")
00146         .set("cellspacing","2").set("cellpadding","2")
00147         .set("class","cgi") << endl;
00148       cout << colgroup().set("span","2") << endl;
00149       cout << col().set("align","center").set("class","title").set("span","1") 
00150            << endl;
00151       cout << col().set("align","left").set("class","data").set("span","1") 
00152            << endl;
00153       cout << colgroup() << endl;
00154       
00155       cout << tr() << td("Name").set("class","title")
00156            << td((*file).getName()).set("class","data") << tr() << endl;
00157       
00158       cout << tr() << td("Data Type").set("class","title")
00159            << td((*file).getDataType()).set("class","data") << tr() << endl;
00160       
00161       cout << tr() << td("Filename").set("class","title") 
00162            << td((*file).getFilename()).set("class","data") << tr() << endl;
00163       cout << tr() << td("Data Length").set("class","title") 
00164            << td().set("class","data") << (*file).getDataLength() 
00165            << td() << tr() << endl;
00166       
00167       cout << tr() << td("File Data").set("class","title")
00168            << td().set("class","data") << pre();
00169       (*file).writeToStream(cout);
00170 
00171       
00172 
00173 
00174 
00175 
00176 
00177       
00178       cout << pre() << td() << tr() << endl;
00179       
00180       cout << table() << CGICCNS div() << endl;
00181     }
00182     else {
00183       cout << p() << CGICCNS div().set("class", "notice") << endl;
00184       cout << "No file was uploaded." << endl << CGICCNS div() << p() << endl;
00185     }
00186 
00187     
00188     cout << p() << CGICCNS div().set("align","center");
00189     cout << a("Back to form").set("href", cgi.getEnvironment().getReferrer()) 
00190          << endl;
00191     cout << CGICCNS div() << br() << hr(set("class","half")) << endl;
00192     
00193     
00194     cout << CGICCNS div().set("align","center").set("class","smaller") << endl;
00195     cout << "GNU cgi" << span("cc").set("class","red") << " v";
00196     cout << cgi.getVersion() << br() << endl;
00197     cout << "Compiled at " << cgi.getCompileTime();
00198     cout << " on " << cgi.getCompileDate() << br() << endl;
00199 
00200     cout << "Configured for " << cgi.getHost();  
00201 #if HAVE_UNAME
00202     struct utsname info;
00203     if(uname(&info) != -1) {
00204       cout << ". Running on " << info.sysname;
00205       cout << ' ' << info.release << " (";
00206       cout << info.nodename << ")." << endl;
00207     }
00208 #else
00209     cout << "." << endl;
00210 #endif
00211 
00212 #if HAVE_GETTIMEOFDAY
00213     
00214     timeval end;
00215     gettimeofday(&end, NULL);
00216     long us = ((end.tv_sec - start.tv_sec) * 1000000)
00217       + (end.tv_usec - start.tv_usec);
00218 
00219     cout << br() << "Total time for request = " << us << " us";
00220     cout << " (" << (double) (us/1000000.0) << " s)";
00221 #endif
00222 
00223     
00224     cout << CGICCNS div() << endl;
00225     cout << body() << html() << endl;
00226 
00227     
00228     return EXIT_SUCCESS;
00229   }
00230 
00231   
00232   catch(const STDNS exception& e) {
00233 
00234     
00235     
00236 
00237     
00238     
00239     html::reset();      head::reset();          body::reset();
00240     title::reset();     h1::reset();            h4::reset();
00241     comment::reset();   td::reset();            tr::reset(); 
00242     table::reset();     CGICCNS div::reset();   p::reset(); 
00243     a::reset();         h2::reset();            colgroup::reset();
00244 
00245     
00246     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00247     cout << html().set("lang","en").set("dir","ltr") << endl;
00248 
00249     
00250     
00251     cout << head() << endl;
00252 
00253     
00254     cout << style() << comment() << endl;
00255     cout << "body { color: black; background-color: white; }" << endl;
00256     cout << "hr.half { width: 60%; align: center; }" << endl;
00257     cout << "span.red, strong.red { color: red; }" << endl;
00258     cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
00259          << "background: #ddd; }" << endl;
00260 
00261     cout << comment() << style() << endl;
00262 
00263     cout << title("GNU cgicc exception") << endl;
00264     cout << head() << endl;
00265     
00266     cout << body() << endl;
00267     
00268     cout << h1() << "GNU cgi" << span("cc", set("class","red"))
00269          << " caught an exception" << h1() << endl; 
00270   
00271     cout << CGICCNS div().set("align","center").set("class","notice") << endl;
00272 
00273     cout << h2(e.what()) << endl;
00274 
00275     
00276     cout << CGICCNS div() << endl;
00277     cout << hr().set("class","half") << endl;
00278     cout << body() << html() << endl;
00279     
00280     return EXIT_SUCCESS;
00281   }
00282 }