CPPSERV


Home Projects Jobs Clientele Contact
CPPSERV Documentation Download TODO Mailing lists Bug tracker News RSS Feed Browse source

HttpServletRequestImpl.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Ilya A. Volynets-Evenbakh                       *
00003  *   ilya@total-knowledge.com                                              *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #ifndef HTTPCSERVIMPLSERVLETREQUESTIMPL_H
00021 #define HTTPCSERVIMPLSERVLETREQUESTIMPL_H
00022 
00023 #include <servlet/HttpServletRequest.h>
00024 #include <servlet/UploadedFile.h>
00025 #include <connection.h>
00026 #include <serverconfig/appcontext.h>
00027 #include <util.h>
00028 
00029 #include <string>
00030 #include <map>
00031 namespace container {
00032 
00033 class HttpServletResponseImpl;
00034 class UploadedFileImpl;
00035 
00043 class HttpServletRequestImpl : public servlet::HttpServletRequest
00044 {
00045 private:
00046     typedef std::map<std::string,servlet::UploadedFile*> filelist_t;
00047     filelist_t m_files;
00048 
00049     std::string m_localAddr;
00050     mutable std::string m_localName;
00051     unsigned short m_localPort;
00052     std::string m_remoteAddr;
00053     mutable std::string m_remoteHost;
00054     unsigned short m_remotePort;
00055     std::string m_serverName;
00056     unsigned short m_serverPort;
00057     std::string m_scheme;
00058     std::string m_characterEncoding;
00059     long m_contentLength;
00060     std::string m_contentType;
00061     util::attr_t m_attributes;
00062     util::mparam_t m_parameters;
00063     util::mparam_t m_headers;
00064     std::string m_method;
00065     std::string m_uri;
00066     std::string m_queryString;
00067     cookielist_t m_cookies;
00068     std::string m_sessionId;
00069     bool m_isSecure;
00070     std::string m_pathInfo;
00071     std::string m_remoteUser;
00072     std::string m_servletPath;
00073     std::string m_protocol;
00074     servlet::HttpSession* m_session;
00075     size_t m_maxRequestSize;
00076     size_t m_maxFileSize;
00077     bool m_inputStreamTaken;
00078 private:
00094     class CookieSeparator
00095     {
00096     private:
00097         struct char_eq
00098         {
00099             char e;
00100             char_eq(char e):e(e) {}
00101             bool operator()(char c)
00102             {
00103                 return (e==c);
00104             }
00105         };
00106         std::string m_escape;
00107         std::string m_sep_drop;
00108         std::string m_sep_return;
00109         std::string m_quote;
00110         char m_cur_quote;
00111         bool is_escape(char e)
00112         {
00113             char_eq f(e);
00114             return std::find_if(m_escape.begin(),m_escape.end(),f)!=m_escape.end();
00115         }
00116         static bool is_sep(char e, const std::string& c_)
00117         {
00118             char_eq f(e);
00119             return std::find_if(c_.begin(),c_.end(),f)!=c_.end();
00120         }
00121         bool is_quote(char e)
00122         {
00123             if(m_cur_quote != 0) {
00124                 if (e==m_cur_quote)
00125                     return true;
00126                 else
00127                     return false;
00128             }
00129             char_eq f(e);
00130             return std::find_if(m_quote.begin(),m_quote.end(),f)!=m_quote.end();
00131         }
00132         void do_escape(std::string::const_iterator next,std::string::const_iterator end,std::string& tok);
00133     public:
00134         // For now I just hardcode escape code, quotes, and separators
00135         CookieSeparator()
00136             : m_escape("\\")
00137             , m_sep_drop(" \t;,")
00138             , m_sep_return("=")
00139             , m_quote("\"'")
00140             , m_cur_quote(0)
00141         {}
00142         void reset() {m_cur_quote=0;}
00143         bool operator()(std::string::const_iterator& next,std::string::const_iterator end,std::string& tok);
00144     };
00145 private:
00146     static void parseAddr(const std::string& addr, std::string& name, unsigned short& port);
00147     static void addr2host(const std::string& addr, std::string& name);
00148 
00149 public:
00150     HttpServletRequestImpl(Connection& con,serverconfig::AppContext* ctx,
00151         const std::string& servletPath, size_t maxRequestSize, size_t maxFileSize);
00152     ~HttpServletRequestImpl();
00153     void load();
00154 
00155     //virtual servlet::HttpSession* getSession() throw (servlet::IllegalStateException);
00156     void setRespRef(HttpServletResponseImpl* resp);
00157     virtual bool isRequestedSessionIdValid();
00158 
00159 private:
00160     void getHeaders();
00161     void getParams();
00162     void parseQueryString(std::istream& queryString);
00163     void parseMultipartData(std::istream& data);
00164 protected:
00165     Connection& m_con;
00166     serverconfig::AppContext* m_ctx;
00167     HttpServletResponseImpl* m_resp;
00168     std::istream m_inputStream;
00169 private:
00170     void parseCookieHeader(const std::string& data);
00171     void parsePart(const std::string& data);
00172 public:
00173     void addUploadedFile(const std::string& name, UploadedFileImpl* file);
00174     void addParam(const std::string& name, const std::string& value);
00175     std::string getFileSaveTemplate() const {return m_ctx->getFileSaveTemplate();}
00177 public:
00178     virtual boost::shared_ptr<void> getAttribute(const std::string& name);
00179     virtual bool hasAttribute(const std::string& name) const;
00180     virtual std::auto_ptr< std::vector<std::string> > getAttributeNames() const;
00181     virtual void  setAttribute(const std::string& name, boost::shared_ptr<void>);
00182     virtual void  removeAttribute(const std::string& name);
00183     virtual std::string  getCharacterEncoding() const;
00184     virtual void  setCharacterEncoding(const std::string& env) ;
00185     virtual int  getContentLength() const;
00186     virtual std::string  getContentType() const;
00187     virtual std::istream&  getInputStream();
00188     virtual std::string  getParameter(const std::string& name) const;
00189     virtual std::auto_ptr< std::vector<std::string> > getParameterNames() const;
00190     virtual std::auto_ptr< std::vector<std::string> > getParameterValues(const std::string& name) const;
00191     virtual std::auto_ptr< std::multimap<std::string,std::string> > getParameterMap() const;
00192     virtual std::string  getProtocol() const;
00193     virtual std::string  getScheme() const;
00194     virtual std::string  getServerName() const;
00195     virtual int  getServerPort() const;
00196     virtual std::string  getRemoteAddr() const;
00197     virtual std::string  getRemoteHost() const;
00198     virtual bool  isSecure() const;
00199     virtual servlet::RequestDispatcher* getRequestDispatcher(const std::string& path);
00200     virtual int  getRemotePort() const;
00201     virtual std::string  getLocalName() const;
00202     virtual std::string  getLocalAddr() const;
00203     virtual int  getLocalPort() const;
00204 
00206 public:
00207     virtual std::string  getAuthType() const ;
00208     virtual std::auto_ptr<cookielist_t> getCookies() const;
00209     virtual long  getDateHeader(const std::string& name) const ;
00210     virtual std::string  getHeader(const std::string& name) const ;
00211     virtual std::auto_ptr< std::vector<std::string> > getHeaders(const std::string& name) const ;
00212     virtual std::auto_ptr< std::vector<std::string> > getHeaderNames() const ;
00213     virtual int  getIntHeader(const std::string& name) const ;
00214     virtual std::string  getMethod() const ;
00215     virtual std::string  getPathInfo() const ;
00216     virtual std::string  getPathTranslated() const ;
00217     virtual std::string  getContextPath() const ;
00218     virtual std::string  getQueryString() const ;
00219     virtual std::string  getRemoteUser() const ;
00220     virtual bool isUserInRole(const std::string& role) const ;
00221     virtual std::string  getRequestedSessionId() const ;
00222     virtual std::string  getRequestURI() const ;
00223     virtual std::string  getRequestURL() const ;
00224     virtual std::string  getServletPath() const ;
00225     virtual servlet::HttpSession* getSession(bool create = true);
00226     virtual bool  isRequestedSessionIdValid() const ;
00227     virtual bool  isRequestedSessionIdFromCookie() const ;
00228     virtual bool  isRequestedSessionIdFromURL() const ;
00229     virtual servlet::UploadedFile* getUploadedFile(const std::string&);
00230 };
00231 
00232 }
00233 
00234 #endif

SourceForge.net Logo