Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

upload.cpp

Go to the documentation of this file.
00001 /*
00002  *  $Id: upload_8cpp-source.html,v 1.1.1.3 2004/06/19 04:36:23 chrisb Exp $
00003  *
00004  *  Copyright (C) 1996 - 2003 Stephen F. Booth
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 // To use logging, the variable gLogFile MUST be defined, and it _must_
00049 // be an ofstream
00050 #if DEBUG
00051   std::ofstream gLogFile( "/change_this_path/cgicc.log", std::ios::app );
00052 #endif
00053 
00054 using namespace std;
00055 using namespace cgicc;
00056 
00057 // Main Street, USA
00058 int
00059 main(int /*argc*/, 
00060      char ** /*argv*/)
00061 {
00062   try {
00063 #if HAVE_GETTIMEOFDAY
00064     timeval start;
00065     gettimeofday(&start, NULL);
00066 #endif
00067 
00068     // Create a new Cgicc object containing all the CGI data
00069     Cgicc cgi;
00070     
00071     // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
00072     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00073     cout << html().set("lang", "en").set("dir", "ltr") << endl;
00074 
00075     // Set up the page's header and title.
00076     // I will put in lfs to ease reading of the produced HTML. 
00077     cout << head() << endl;
00078 
00079     // Output the style sheet portion of the header
00080     cout << style() << comment() << endl;
00081     cout << "body { color: black; background-color: white; }" << endl;
00082     cout << "hr.half { width: 60%; align: center; }" << endl;
00083     cout << "span.red, strong.red { color: red; }" << endl;
00084     cout << "div.smaller { font-size: small; }" << endl;
00085     cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
00086          << "background: #ddd; }" << endl;
00087     cout << "span.blue { color: blue; }" << endl;
00088     cout << "col.title { color: white; background-color: black; ";
00089     cout << "font-weight: bold; text-align: center; }" << endl;
00090     cout << "col.data { background-color: #ddd; text-align: left; }" << endl;
00091     cout << "td.data, TR.data { background-color: #ddd; text-align: left; }"
00092          << endl;
00093     cout << "td.grayspecial { background-color: #ddd; text-align: left; }"
00094          << endl;
00095     cout << "td.ltgray, tr.ltgray { background-color: #ddd; }" << endl;
00096     cout << "td.dkgray, tr.dkgray { background-color: #bbb; }" << endl;
00097     cout << "col.black, td.black, td.title, tr.title { color: white; " 
00098          << "background-color: black; font-weight: bold; text-align: center; }"
00099          << endl;
00100     cout << "col.gray, td.gray { background-color: #ddd; text-align: center; }"
00101          << endl;
00102     cout << "table.cgi { left-margin: auto; right-margin: auto; width: 90%; }"
00103          << endl;
00104 
00105     cout << comment() << style() << endl;
00106 
00107     cout << title() << "GNU cgicc v" << cgi.getVersion() 
00108          << " File Upload Test Results" << title() << endl;
00109 
00110     cout << head() << endl;
00111     
00112     // Start the HTML body
00113     cout << body() << endl;
00114 
00115     cout << h1() << "GNU cgi" << span("cc").set("class","red")
00116          << " v"<< cgi.getVersion() << " File Upload Test Results" 
00117          << h1() << endl;
00118     
00119     // Get a pointer to the environment
00120     const CgiEnvironment& env = cgi.getEnvironment();
00121     
00122     // Generic thank you message
00123     cout << comment() << "This page generated by cgicc for "
00124          << env.getRemoteHost() << comment() << endl;
00125     cout << h4() << "Thanks for using cgi" << span("cc").set("class", "red") 
00126          << ", " << env.getRemoteHost() 
00127          << '(' << env.getRemoteAddr() << ")!" << h4() << endl;  
00128     
00129     // Show the uploaded file
00130     cout << h2("File Uploaded via FormFile") << endl;
00131   
00132     const_file_iterator file;
00133     file = cgi.getFile("userfile");
00134                                 
00135     if(file != cgi.getFiles().end()) {
00136       cout << cgicc::div().set("align","center") << endl;
00137     
00138       cout << table().set("border","0").set("rules","none").set("frame","void")
00139         .set("cellspacing","2").set("cellpadding","2")
00140         .set("class","cgi") << endl;
00141       cout << colgroup().set("span","2") << endl;
00142       cout << col().set("align","center").set("class","title").set("span","1") 
00143            << endl;
00144       cout << col().set("align","left").set("class","data").set("span","1") 
00145            << endl;
00146       cout << colgroup() << endl;
00147       
00148       cout << tr() << td("Name").set("class","title")
00149            << td((*file).getName()).set("class","data") << tr() << endl;
00150       
00151       cout << tr() << td("Data Type").set("class","title")
00152            << td((*file).getDataType()).set("class","data") << tr() << endl;
00153       
00154       cout << tr() << td("Filename").set("class","title") 
00155            << td((*file).getFilename()).set("class","data") << tr() << endl;
00156       cout << tr() << td("Data Length").set("class","title") 
00157            << td().set("class","data") << (*file).getDataLength() 
00158            << td() << tr() << endl;
00159       
00160       cout << tr() << td("File Data").set("class","title")
00161            << td().set("class","data") << pre();
00162       (*file).writeToStream(cout);
00163 
00164       /*
00165         To write the contents of the file to a file "foo" on disk:
00166 
00167         ofstream foo("foo");
00168         (*file).writeToStream(foo);
00169        */
00170       
00171       cout << pre() << td() << tr() << endl;
00172       
00173       cout << table() << cgicc::div() << endl;
00174     }
00175     else {
00176       cout << p() << cgicc::div().set("class", "notice") << endl;
00177       cout << "No file was uploaded." << endl << cgicc::div() << p() << endl;
00178     }
00179 
00180     // Now print out a footer with some fun info
00181     cout << p() << cgicc::div().set("align","center");
00182     cout << a("Back to form").set("href", cgi.getEnvironment().getReferrer()) 
00183          << endl;
00184     cout << cgicc::div() << br() << hr(set("class","half")) << endl;
00185     
00186     // Information on cgicc
00187     cout << cgicc::div().set("align","center").set("class","smaller") << endl;
00188     cout << "GNU cgi" << span("cc").set("class","red") << " v";
00189     cout << cgi.getVersion() << br() << endl;
00190     cout << "Compiled at " << cgi.getCompileTime();
00191     cout << " on " << cgi.getCompileDate() << br() << endl;
00192 
00193     cout << "Configured for " << cgi.getHost();  
00194 #if HAVE_UNAME
00195     struct utsname info;
00196     if(uname(&info) != -1) {
00197       cout << ". Running on " << info.sysname;
00198       cout << ' ' << info.release << " (";
00199       cout << info.nodename << ")." << endl;
00200     }
00201 #else
00202     cout << "." << endl;
00203 #endif
00204 
00205 #if HAVE_GETTIMEOFDAY
00206     // Information on this query
00207     timeval end;
00208     gettimeofday(&end, NULL);
00209     long us = ((end.tv_sec - start.tv_sec) * 1000000)
00210       + (end.tv_usec - start.tv_usec);
00211 
00212     cout << br() << "Total time for request = " << us << " us";
00213     cout << " (" << (double) (us/1000000.0) << " s)";
00214 #endif
00215 
00216     // End of document
00217     cout << cgicc::div() << endl;
00218     cout << body() << html() << endl;
00219 
00220     // No chance for failure in this example
00221     return EXIT_SUCCESS;
00222   }
00223 
00224   // Did any errors occur?
00225   catch(const std::exception& e) {
00226 
00227     // This is a dummy exception handler, as it doesn't really do
00228     // anything except print out information.
00229 
00230     // Reset all the HTML elements that might have been used to 
00231     // their initial state so we get valid output
00232     html::reset();      head::reset();          body::reset();
00233     title::reset();     h1::reset();            h4::reset();
00234     comment::reset();   td::reset();            tr::reset(); 
00235     table::reset();     cgicc::div::reset();    p::reset(); 
00236     a::reset();         h2::reset();            colgroup::reset();
00237 
00238     // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
00239     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00240     cout << html().set("lang","en").set("dir","ltr") << endl;
00241 
00242     // Set up the page's header and title.
00243     // I will put in lfs to ease reading of the produced HTML. 
00244     cout << head() << endl;
00245 
00246     // Output the style sheet portion of the header
00247     cout << style() << comment() << endl;
00248     cout << "body { color: black; background-color: white; }" << endl;
00249     cout << "hr.half { width: 60%; align: center; }" << endl;
00250     cout << "span.red, strong.red { color: red; }" << endl;
00251     cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
00252          << "background: #ddd; }" << endl;
00253 
00254     cout << comment() << style() << endl;
00255 
00256     cout << title("GNU cgicc exception") << endl;
00257     cout << head() << endl;
00258     
00259     cout << body() << endl;
00260     
00261     cout << h1() << "GNU cgi" << span("cc", set("class","red"))
00262          << " caught an exception" << h1() << endl; 
00263   
00264     cout << cgicc::div().set("align","center").set("class","notice") << endl;
00265 
00266     cout << h2(e.what()) << endl;
00267 
00268     // End of document
00269     cout << cgicc::div() << endl;
00270     cout << hr().set("class","half") << endl;
00271     cout << body() << html() << endl;
00272     
00273     return EXIT_SUCCESS;
00274   }
00275 }

GNU cgicc - A C++ class library for writing CGI applications
Copyright © 1996 - 2004 Stephen F. Booth
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front Cover Texts, and with no Back-Cover Texts.
Documentation generated Sat Jun 19 00:36:23 2004 for cgicc by doxygen 1.3.6