CGI.h

00001 // $Id: CGI.h 21 2010-09-05 04:18:17Z cschwarz1 $
00002 
00003 #ifndef BASE_CGI_H
00004 #define BASE_CGI_H
00005 
00006 #include <cstdarg>
00007 #include <fstream>
00008 #include <map>
00009 #include <vector>
00010 #include "CGIFile.h"
00011 #include "libppbase/base/Config.h"
00012 #include "libppbase/base/Log.h"
00013 
00015 namespace base_cgi {
00017 
00021     class PPBASE_CGI_EXPORT CGI: public std::map<base::String, base::String> {
00022     public:
00024 
00027         CGI(const base::String &program);
00028 
00030         virtual ~CGI();
00031 
00033 
00037         void add(const base::String &var, const base::String &val);
00038 
00040         void dump(bool showenv = false);
00041 
00043 
00047         bool exists(const base::String &var) const;
00048 
00050         virtual void flush() = 0;
00051 
00053 
00057         base::String get(const base::String &var) const;
00058 
00060         const base::Config &getConfig() const;
00061 
00063 
00067         base::String getCookie(const char *name) const;
00068 
00070 
00075         virtual const char *getenv(const char *var, const char *defval = NULL) const = 0;
00076 
00078 
00082         CGIFile *getFile(const base::String &name) const;
00083 
00085 
00088         std::vector<CGIFile*> getFiles() const;
00089 
00095         base::String getHost() const;
00096 
00098 
00101         base::Log *getLog() const;
00102 
00108         base::String getPath() const;
00109 
00115         base::String getUserAgent() const;
00116 
00118 
00123         static base::String htmlEncode(const base::String &s, bool crlf = false);
00124 
00126         void init();
00127 
00129 
00132         void initGet(const base::String &s);
00133 
00135         void initPost();
00136 
00138 
00144         void log(base::Log::levels level, const char *func, const char *fmt, ...) const
00145         #ifdef __GNUC__
00146             __attribute__((format(printf, 4, 5)))
00147         #endif
00148             ;
00149 
00151 
00155         virtual void out(const char *s, size_t len = base::String::npos) = 0;
00156 
00158 
00161         void out(const base::String &s);
00162 
00164 
00168         void outf(const char *fmt, ...)
00169         #ifdef __GNUC__
00170             __attribute__((format(printf, 2, 3)))
00171         #endif
00172             ;
00173 
00183         void outputDomainCookie(const base::String &name, const base::String &value, const base::String &domain, int timeout, const base::String &path = "/");
00184 
00186 
00190         void outputHeader(const base::String &contenttype = "text/html", const base::String &charset = "iso-8859-1");
00191 
00200         void outputHostCookie(const base::String &name, const base::String &value, int timeout, const base::String &path = "/");
00201 
00203 
00206         void redirect(const base::String &url);
00207 
00209 
00213         void set(const base::String &var, const base::String &val);
00214 
00216 
00220         void set(const base::String &var, int val);
00221 
00223 
00226         const base::String &ssiBase() const;
00227 
00229         void ssiBegin();
00230 
00232         void ssiEnd();
00233 
00235 
00239         void ssiError(const char *fmt, ...)
00240         #ifdef __GNUC__
00241             __attribute__((format(printf, 2, 3)))
00242         #endif
00243             ;
00244 
00246 
00249         void ssiError(const base::String &msg);
00250 
00252 
00255         int ssiErrors() const;
00256 
00258 
00261         void ssiGeneralError(const base::String &msg = "");
00262 
00264 
00268         void ssiInfo(const char *fmt, ...)
00269         #ifdef __GNUC__
00270             __attribute__((format(printf, 2, 3)))
00271         #endif
00272             ;
00273 
00275 
00278         void ssiInfo(const base::String &msg);
00279 
00281 
00284         void ssiOutput(std::ifstream &in);
00285 
00287 
00290         void ssiOutput(const base::String &filename);
00291 
00293 
00297         static base::String ssiValidExpand(const base::String &pat);
00298 
00300 
00310         bool ssiValidInput(const base::String &descr, const base::String &var, bool select, size_t minlen, size_t maxlen, const base::String &allowed,
00311             const base::String &denied);
00312 
00314 
00318         static base::String urlDecode(const base::String &s);
00319 
00321 
00325         static base::String urlEncode(const base::String &s);
00326 
00327     protected:
00329 
00332         virtual int inChar() = 0;
00333 
00335 
00340         virtual int inLine(char *buf, int len) = 0;
00341 
00342         base::Config           _config;    
00343         int                    _errors;    
00344         std::vector<CGIFile*>  _files;     
00345         bool                   _header;    
00346         base::Log             *_log;       
00347         base::String           _ssibase;   
00348         base::String           _program;   
00349     };
00350 }
00351 
00352 extern void ssiCallback(const base::String &var);
00353 
00354 #endif