upload.cpp Source File

Main Page Namespace List Class Hierarchy 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.5 2002/03/03 17:41:44 sbooth Exp $
    00003  *
    00004  *  Copyright (C) 1996 - 2002 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   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
    00116     cout << head() << endl;
    00117
    00118     // Start the HTML body
    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     // Get a pointer to the environment
    00126     const CgiEnvironment& env = cgi.getEnvironment();
    00127
    00128     // Generic thank you message
    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     // Show the uploaded file
    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         To write the contents of the file to a file "foo" on disk:
    00173
    00174         ofstream foo("foo");
    00175         (*file).writeToStream(foo);
    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     // Now print out a footer with some fun info
    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     // Information on cgicc
    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     // Information on this query
    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     // End of document
    00224     cout << CGICCNS div() << endl;
    00225     cout << body() << html() << endl;
    00226
    00227     // No chance for failure in this example
    00228     return EXIT_SUCCESS;
    00229   }
    00230
    00231   // Did any errors occur?
    00232   catch(const STDNS exception& e) {
    00233
    00234     // This is a dummy exception handler, as it doesn't really do
    00235     // anything except print out information.
    00236
    00237     // Reset all the HTML elements that might have been used to
    00238     // their initial state so we get valid output
    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     // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
    00246     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
    00247     cout << html().set("lang","en").set("dir","ltr") << endl;
    00248
    00249     // Set up the page's header and title.
    00250     // I will put in lfs to ease reading of the produced HTML.
    00251     cout << head() << endl;
    00252
    00253     // Output the style sheet portion of the header
    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     // End of document
    00276     cout << CGICCNS div() << endl;
    00277     cout << hr().set("class","half") << endl;
    00278     cout << body() << html() << endl;
    00279
    00280     return EXIT_SUCCESS;
    00281   }
    00282 }