1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/* -*-mode:c++; c-file-style: "gnu";-*- */
/*
* $Id: upload.cpp,v 1.13 2007/07/02 18:48:19 sebdiaz Exp $
*
* Copyright (C) 1996 - 2004 Stephen F. Booth <sbooth@gnu.org>
* 2007 Sebastien DIAZ <sebastien.diaz@gmail.com>
* Part of the GNU cgicc library, http://www.gnu.org/software/cgicc
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
*/
/*! \file upload.cpp
* \brief File upload demo
*
* Tests and demonstrates how to handle uploaded files using the
* GNU %cgicc library.
*/
#include <new>
#include <string>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <cstdlib>
#include "cgicc/CgiDefs.h"
#include "cgicc/Cgicc.h"
#include "cgicc/HTTPHTMLHeader.h"
#include "cgicc/HTMLClasses.h"
#if HAVE_SYS_UTSNAME_H
# include <sys/utsname.h>
#endif
#if HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#include "styles.h"
using namespace std;
using namespace cgicc;
// Print the form for this CGI
void
printForm(const Cgicc& cgi)
{
cout << "<form method=\"post\" action=\""
<< cgi.getEnvironment().getScriptName()
<< "\" enctype=\"multipart/form-data\">" << endl;
cout << "<table>" << endl;
cout << "<tr><td class=\"title\">Send a file</td>"
<< "<td class=\"form\">"
<< "<input type=\"file\" name=\"userfile\" accept=\"text/plain\" />"
<< "</td></tr>" << endl;
cout << "<tr><td class=\"title\">Upload Redirection</td>"
<< "<td class=\"form\">"
<< "<input type=\"checkbox\" name=\"redirect\" />"
<< "Bounce uploaded file back to browser"
<< "</td></tr>" << endl;
cout << "</table>" << endl;
cout << "<div class=\"center\"><p>"
<< "<input type=\"submit\" name=\"submit\" value=\"Send the file\" />"
<< "<input type=\"reset\" value=\"Nevermind\" />"
<< "</p></div></form>" << endl;
}
// Main Street, USA
int
main(int /*argc*/,
char ** /*argv*/)
{
try {
#if HAVE_GETTIMEOFDAY
timeval start;
gettimeofday(&start, NULL);
#endif
// Create a new Cgicc object containing all the CGI data
Cgicc cgi;
// Redirect output, if desired
if(cgi.queryCheckbox("redirect")) {
const_file_iterator file = cgi.getFile("userfile");
// Only redirect a valid file
if(file != cgi.getFiles().end()) {
cout << HTTPContentHeader(file->getDataType());
file->writeToStream(cout);
return EXIT_SUCCESS;
}
}
// Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
cout << html().set("lang", "en").set("dir", "ltr") << endl;
// Set up the page's header and title.
// I will put in lfs to ease reading of the produced HTML.
cout << head() << endl;
// Output the style sheet portion of the header
cout << style() << comment() << endl;
cout << styles;
cout << comment() << style() << endl;
cout << title() << "GNU cgicc v" << cgi.getVersion()
<< " File Upload Test" << title() << endl;
cout << head() << endl;
// Start the HTML body
cout << body() << endl;
cout << h1() << "GNU cgi" << span("cc").set("class","red")
<< " v"<< cgi.getVersion() << " File Upload Test"
<< h1() << endl;
// Get a pointer to the environment
const CgiEnvironment& env = cgi.getEnvironment();
// Generic thank you message
cout << comment() << "This page generated by cgicc for "
<< env.getRemoteHost() << comment() << endl;
cout << h4() << "Thanks for using cgi" << span("cc").set("class", "red")
<< ", " << env.getRemoteHost()
<< '(' << env.getRemoteAddr() << ")!" << h4() << endl;
// Show the uploaded file
const_file_iterator file;
file = cgi.getFile("userfile");
if(file != cgi.getFiles().end()) {
cout << table() << endl;
cout << tr() << td("Name").set("class","title")
<< td(file->getName()).set("class","data") << tr() << endl;
cout << tr() << td("Data Type").set("class","title")
<< td(file->getDataType()).set("class","data") << tr() << endl;
cout << tr() << td("Filename").set("class","title")
<< td(file->getFilename()).set("class","data") << tr() << endl;
cout << tr() << td("Data Length").set("class","title")
<< td().set("class","data") << file->getDataLength()
<< td() << tr() << endl;
cout << tr() << td("File Data").set("class","title")
<< td().set("class","data") << pre();
file->writeToStream(cout);
cout << pre() << td() << tr() << endl;
cout << table() << endl;
}
// Print out the form to do it again
cout << br() << endl;
printForm(cgi);
cout << hr().set("class", "half") << endl;
// Information on cgicc
cout << cgicc::div().set("align","center").set("class","smaller") << endl;
cout << "GNU cgi" << span("cc").set("class","red") << " v";
cout << cgi.getVersion() << br() << endl;
cout << "Compiled at " << cgi.getCompileTime();
cout << " on " << cgi.getCompileDate() << br() << endl;
cout << "Configured for " << cgi.getHost();
#if HAVE_UNAME
struct utsname info;
if(uname(&info) != -1) {
cout << ". Running on " << info.sysname;
cout << ' ' << info.release << " (";
cout << info.nodename << ")." << endl;
}
#else
cout << "." << endl;
#endif
#if HAVE_GETTIMEOFDAY
// Information on this query
timeval end;
gettimeofday(&end, NULL);
long us = ((end.tv_sec - start.tv_sec) * 1000000)
+ (end.tv_usec - start.tv_usec);
cout << br() << "Total time for request = " << us << " us";
cout << " (" << static_cast<double>(us/1000000.0) << " s)";
#endif
// End of document
cout << cgicc::div() << endl;
cout << body() << html() << endl;
// No chance for failure in this example
return EXIT_SUCCESS;
}
// Did any errors occur?
catch(const std::exception& e) {
// This is a dummy exception handler, as it doesn't really do
// anything except print out information.
// Reset all the HTML elements that might have been used to
// their initial state so we get valid output
html::reset(); head::reset(); body::reset();
title::reset(); h1::reset(); h4::reset();
comment::reset(); td::reset(); tr::reset();
table::reset(); cgicc::div::reset(); p::reset();
a::reset(); h2::reset(); colgroup::reset();
// Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
cout << html().set("lang","en").set("dir","ltr") << endl;
// Set up the page's header and title.
// I will put in lfs to ease reading of the produced HTML.
cout << head() << endl;
// Output the style sheet portion of the header
cout << style() << comment() << endl;
cout << "body { color: black; background-color: white; }" << endl;
cout << "hr.half { width: 60%; align: center; }" << endl;
cout << "span.red, strong.red { color: red; }" << endl;
cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
<< "background: #ddd; }" << endl;
cout << comment() << style() << endl;
cout << title("GNU cgicc exception") << endl;
cout << head() << endl;
cout << body() << endl;
cout << h1() << "GNU cgi" << span("cc", set("class","red"))
<< " caught an exception" << h1() << endl;
cout << cgicc::div().set("align","center").set("class","notice") << endl;
cout << h2(e.what()) << endl;
// End of document
cout << cgicc::div() << endl;
cout << hr().set("class","half") << endl;
cout << body() << html() << endl;
return EXIT_SUCCESS;
}
}
|