SPIN Framework

include/pocoUtil.h

00001 // -----------------------------------------------------------------------------
00002 // |    ___  ___  _  _ _     ___                                        _      |
00003 // |   / __>| . \| || \ |   | __>_ _  ___ ._ _ _  ___  _ _ _  ___  _ _ | |__   |
00004 // |   \__ \|  _/| ||   |   | _>| '_><_> || ' ' |/ ._>| | | |/ . \| '_>| / /   |
00005 // |   <___/|_|  |_||_\_|   |_| |_|  <___||_|_|_|\___.|__/_/ \___/|_|  |_\_\   |
00006 // |                                                                           |
00007 // |---------------------------------------------------------------------------|
00008 //
00009 // http://spinframework.sourceforge.net
00010 // Copyright (C) 2009 Mike Wozniewski, Zack Settel
00011 //
00012 // Developed/Maintained by:
00013 //    Mike Wozniewski (http://www.mikewoz.com)
00014 //    Zack Settel (http://www.sheefa.net/zack)
00015 //
00016 // Principle Partners:
00017 //    Shared Reality Lab, McGill University (http://www.cim.mcgill.ca/sre)
00018 //    La Societe des Arts Technologiques (http://www.sat.qc.ca)
00019 //
00020 // Funding by:
00021 //    NSERC/Canada Council for the Arts - New Media Initiative
00022 //    Heritage Canada
00023 //    Ministere du Developpement economique, de l'Innovation et de l'Exportation
00024 //
00025 // -----------------------------------------------------------------------------
00026 //  This file is part of the SPIN Framework.
00027 //
00028 //  SPIN Framework is free software: you can redistribute it and/or modify
00029 //  it under the terms of the GNU Lesser General Public License as published by
00030 //  the Free Software Foundation, either version 3 of the License, or
00031 //  (at your option) any later version.
00032 //
00033 //  SPIN Framework is distributed in the hope that it will be useful,
00034 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00035 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00036 //  GNU Lesser General Public License for more details.
00037 //
00038 //  You should have received a copy of the GNU Lesser General Public License
00039 //  along with SPIN Framework. If not, see <http://www.gnu.org/licenses/>.
00040 // -----------------------------------------------------------------------------
00041 #ifndef __pocoUtil_H
00042 #define __pocoUtil_H
00043 
00044 #include "config.h"
00045 
00046 #ifdef WITH_POCO
00047 
00048 #include "Poco/Net/HTTPServer.h"
00049 #include "Poco/Net/HTTPRequestHandler.h"
00050 #include "Poco/Net/HTTPRequestHandlerFactory.h"
00051 #include "Poco/Net/HTTPServerParams.h"
00052 #include "Poco/Net/HTTPServerRequest.h"
00053 #include "Poco/Net/HTTPServerResponse.h"
00054 #include "Poco/Net/HTMLForm.h"
00055 #include "Poco/Net/PartHandler.h"
00056 #include "Poco/Net/MessageHeader.h"
00057 #include "Poco/Net/ServerSocket.h"
00058 #include "Poco/CountingStream.h"
00059 #include "Poco/NullStream.h"
00060 #include "Poco/StreamCopier.h"
00061 #include "Poco/Exception.h"
00062 #include "Poco/Util/ServerApplication.h"
00063 #include "Poco/Util/Option.h"
00064 #include "Poco/Util/OptionSet.h"
00065 #include "Poco/Util/HelpFormatter.h"
00066 
00067 namespace spin
00068 {
00069   
00073 bool applyHTTPMessage(std::string path, const Poco::Net::HTMLForm &form);
00074     
00075 class PartHandler: public Poco::Net::PartHandler
00076 {
00077 public:
00078     PartHandler():
00079     _length(0)
00080     {
00081     }
00082     
00083     void handlePart(const Poco::Net::MessageHeader& header, std::istream& stream)
00084     {
00085         _type = header.get("Content-Type", "(unspecified)");
00086         if (header.has("Content-Disposition"))
00087         {
00088             std::string disp;
00089             Poco::Net::NameValueCollection params;
00090             Poco::Net::MessageHeader::splitParameters(header["Content-Disposition"], disp, params);
00091             _name = params.get("name", "(unnamed)");
00092             _fileName = params.get("filename", "(unnamed)");
00093         }
00094         
00095         Poco::CountingInputStream istr(stream);
00096         Poco::NullOutputStream ostr;
00097         Poco::StreamCopier::copyStream(istr, ostr);
00098         _length = istr.chars();
00099     }
00100     
00101     int length() const
00102     {
00103         return _length;
00104     }
00105     
00106     const std::string& name() const
00107     {
00108         return _name;
00109     }
00110     
00111     const std::string& fileName() const
00112     {
00113         return _fileName;
00114     }
00115     
00116     const std::string& contentType() const
00117     {
00118         return _type;
00119     }
00120     
00121 private:
00122     int _length;
00123     std::string _type;
00124     std::string _name;
00125     std::string _fileName;
00126 };
00127 
00128 
00129 class FormRequestHandler: public Poco::Net::HTTPRequestHandler
00130 {
00131 public:
00132     FormRequestHandler() 
00133     {
00134     }
00135     
00136     void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response);
00137 };
00138         
00139 class FormRequestHandlerFactory: public Poco::Net::HTTPRequestHandlerFactory
00140 {
00141 public:
00142     FormRequestHandlerFactory()
00143     {
00144     }
00145     
00146     Poco::Net::HTTPRequestHandler* createRequestHandler(const Poco::Net::HTTPServerRequest& request)
00147     {
00148         return new FormRequestHandler();
00149     }
00150 };
00151         
00152         
00153 } // end of namespace spin
00154 
00155 #endif // WITH_POCO
00156 
00157 #endif