Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

upload.cpp

Go to the documentation of this file.
00001 /*
00002  *  $Id: upload.cpp,v 1.3 2001/09/03 16:14:26 sbooth Exp $
00003  *
00004  *  Copyright (C) 2000, 2001 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/HTTPHeaders.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   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 // Main Street, USA
00064 int
00065 main(int /*argc*/, 
00066      char ** /*argv*/)
00067 {
00068   try {
00069 #if HAVE_GETTIMEOFDAY
00070     timeval start;
00071     gettimeofday(&start, NULL);
00072 #endif
00073 
00074     // Create a new Cgicc object containing all the CGI data
00075     Cgicc cgi;
00076     
00077     // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
00078     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00079     cout << html().set("lang", "en").set("dir", "ltr") << endl;
00080 
00081     // Set up the page's header and title.
00082     // I will put in lfs to ease reading of the produced HTML. 
00083     cout << head() << endl;
00084 
00085     // Output the style sheet portion of the header
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     cout << meta().set("name", "author").set("content", "Stephen F. Booth") 
00116          << endl;
00117 
00118     cout << head() << endl;
00119     
00120     // Start the HTML body
00121     cout << body() << endl;
00122 
00123     cout << h1() << "GNU cgi" << span("cc").set("class","red")
00124          << " v"<< cgi.getVersion() << " File Upload Test Results" 
00125          << h1() << endl;
00126     
00127     // Get a pointer to the environment
00128     const CgiEnvironment& env = cgi.getEnvironment();
00129     
00130     // Generic thank you message
00131     cout << comment() << "This page generated by cgicc for "
00132          << env.getRemoteHost() << comment() << endl;
00133     cout << h4() << "Thanks for using cgi" << span("cc").set("class", "red") 
00134          << ", " << env.getRemoteHost() 
00135          << '(' << env.getRemoteAddr() << ")!" << h4() << endl;  
00136     
00137 
00138     // Show the uploaded file
00139     cout << h2("File Uploaded via FormFile") << endl;
00140   
00141     const_file_iterator file;
00142     file = cgi.getFile("userfile");
00143                                 
00144     if(file != cgi.getFiles().end()) {
00145       cout << CGICCNS div().set("align","center") << endl;
00146     
00147       cout << table().set("border","0").set("rules","none").set("frame","void")
00148         .set("cellspacing","2").set("cellpadding","2")
00149         .set("class","cgi") << endl;
00150       cout << colgroup().set("span","2") << endl;
00151       cout << col().set("align","center").set("class","title").set("span","1") 
00152            << endl;
00153       cout << col().set("align","left").set("class","data").set("span","1") 
00154            << endl;
00155       cout << colgroup() << endl;
00156       
00157       cout << tr() << td("Name").set("class","title")
00158            << td((*file).getName()).set("class","data") << tr() << endl;
00159       
00160       cout << tr() << td("Data Type").set("class","title")
00161            << td((*file).getDataType()).set("class","data") << tr() << endl;
00162       
00163       cout << tr() << td("Filename").set("class","title") 
00164            << td((*file).getFilename()).set("class","data") << tr() << endl;
00165       cout << tr() << td("Data Length").set("class","title") 
00166            << td().set("class","data") << (*file).getDataLength() 
00167            << td() << tr() << endl;
00168       
00169       cout << tr() << td("File Data").set("class","title")
00170            << td().set("class","data") << pre();
00171       (*file).writeToStream(cout);
00172 
00173       /*
00174         To write the contents of the file to a file "foo" on disk:
00175 
00176         ofstream foo("foo");
00177         (*file).writeToStream(foo);
00178        */
00179       
00180       cout << pre() << td() << tr() << endl;
00181       
00182       cout << table() << CGICCNS div() << endl;
00183     }
00184     else {
00185       cout << p() << CGICCNS div().set("class", "notice") << endl;
00186       cout << "No file was uploaded." << endl << CGICCNS div() << p() << endl;
00187     }
00188 
00189     // Now print out a footer with some fun info
00190     cout << p() << CGICCNS div().set("align","center");
00191     cout << a("Back to form").set("href", cgi.getEnvironment().getReferrer()) 
00192          << endl;
00193     cout << CGICCNS div() << br() << hr(set("class","half")) << endl;
00194     
00195     // Information on cgicc
00196     cout << CGICCNS div().set("align","center").set("class","smaller") << endl;
00197     cout << "GNU cgi" << span("cc").set("class","red") << " v";
00198     cout << cgi.getVersion();
00199     cout << " by " << a("Stephen F. Booth")
00200                        .set("href", "mailto:sbooth@gnu.org") 
00201          << br() << endl;
00202     cout << "Compiled at " << cgi.getCompileTime();
00203     cout << " on " << cgi.getCompileDate() << br() << endl;
00204 
00205     cout << "Configured for " << cgi.getHost();  
00206 #if HAVE_UNAME
00207     struct utsname info;
00208     if(uname(&info) != -1) {
00209       cout << ". Running on " << info.sysname;
00210       cout << ' ' << info.release << " (";
00211       cout << info.nodename << ")." << endl;
00212     }
00213 #else
00214     cout << "." << endl;
00215 #endif
00216 
00217 #if HAVE_GETTIMEOFDAY
00218     // Information on this query
00219     timeval end;
00220     gettimeofday(&end, NULL);
00221     long us = ((end.tv_sec - start.tv_sec) * 1000000)
00222       + (end.tv_usec - start.tv_usec);
00223 
00224     cout << br() << "Total time for request = " << us << " us";
00225     cout << " (" << (double) (us/1000000.0) << " s)";
00226 #endif
00227 
00228     // End of document
00229     cout << CGICCNS div() << endl;
00230     cout << body() << html() << endl;
00231 
00232     // No chance for failure in this example
00233     return EXIT_SUCCESS;
00234   }
00235 
00236   // Did any errors occur?
00237   catch(const STDNS exception& e) {
00238 
00239     // This is a dummy exception handler, as it doesn't really do
00240     // anything except print out information.
00241 
00242     // Reset all the HTML elements that might have been used to 
00243     // their initial state so we get valid output
00244     html::reset();      head::reset();          body::reset();
00245     title::reset();     h1::reset();            h4::reset();
00246     comment::reset();   td::reset();            tr::reset(); 
00247     table::reset();     CGICCNS div::reset();   p::reset(); 
00248     a::reset();         h2::reset();            colgroup::reset();
00249 
00250     // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
00251     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00252     cout << html().set("lang","en").set("dir","ltr") << endl;
00253 
00254     // Set up the page's header and title.
00255     // I will put in lfs to ease reading of the produced HTML. 
00256     cout << head() << endl;
00257 
00258     // Output the style sheet portion of the header
00259     cout << style() << comment() << endl;
00260     cout << "body { color: black; background-color: white; }" << endl;
00261     cout << "hr.half { width: 60%; align: center; }" << endl;
00262     cout << "span.red, strong.red { color: red; }" << endl;
00263     cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
00264          << "background: #ddd; }" << endl;
00265 
00266     cout << comment() << style() << endl;
00267 
00268     cout << title("GNU cgicc exception") << endl;
00269     cout << meta().set("name", "author")
00270                   .set("content", "Stephen F. Booth") << endl;
00271     cout << head() << endl;
00272     
00273     cout << body() << endl;
00274     
00275     cout << h1() << "GNU cgi" << span("cc", set("class","red"))
00276          << " caught an exception" << h1() << endl; 
00277   
00278     cout << CGICCNS div().set("align","center").set("class","notice") << endl;
00279 
00280     cout << h2(e.what()) << endl;
00281 
00282     // End of document
00283     cout << CGICCNS div() << endl;
00284     cout << hr().set("class","half") << endl;
00285     cout << body() << html() << endl;
00286     
00287     return EXIT_SUCCESS;
00288   }
00289 }

GNU cgicc - A C++ class library for writing CGI applications
Copyright © 1996, 1997, 1998, 1999, 2000, 2001, 2002 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 Wed Jan 9 12:31:26 2002 for cgicc by doxygen 1.2.13.1