Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/tntnet/framework/common/tnt/httpreply.h

00001 /*
00002  * Copyright (C) 2003-2005 Tommi Maekitalo
00003  * 
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * As a special exception, you may use this file as part of a free
00010  * software library without restriction. Specifically, if other files
00011  * instantiate templates or use macros or inline functions from this
00012  * file, or you compile this file and link it with other files to
00013  * produce an executable, this file does not by itself cause the
00014  * resulting executable to be covered by the GNU General Public
00015  * License. This exception does not however invalidate any other
00016  * reasons why the executable file might be covered by the GNU Library
00017  * General Public License.
00018  * 
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00027  */
00028 
00029 
00030 #ifndef TNT_HTTPREPLY_H
00031 #define TNT_HTTPREPLY_H
00032 
00033 #include <tnt/httpmessage.h>
00034 #include <tnt/htmlescostream.h>
00035 #include <tnt/urlescostream.h>
00036 #include <tnt/encoding.h>
00037 #include <tnt/http.h>
00038 #include <sstream>
00039 
00040 namespace tnt
00041 {
00042   class Savepoint;
00043 
00045   class HttpReply : public HttpMessage
00046   {
00047       friend class Savepoint;
00048 
00049       std::ostream& socket;
00050       std::ostringstream outstream;
00051       std::ostream* current_outstream;
00052       HtmlEscOstream safe_outstream;
00053       UrlEscOstream url_outstream;
00054 
00055       Encoding acceptEncoding;
00056 
00057       unsigned keepAliveCounter;
00058       static unsigned keepAliveTimeout;
00059       static unsigned minCompressSize;
00060       static std::string defaultContentType;
00061 
00062       bool sendStatusLine;
00063       bool headRequest;
00064 
00065       void send(unsigned ret, const char* msg, bool ready) const;
00066 
00067     public:
00068       explicit HttpReply(std::ostream& s, bool sendStatusLine = true);
00069 
00070       static bool tryCompress(std::string& body);
00071 
00072       void setContentType(const char* t)            { setHeader(httpheader::contentType, t); }
00073       void setContentType(const std::string& t)     { setHeader(httpheader::contentType, t); }
00074       const char* getContentType() const            { return getHeader(httpheader::contentType); }
00075 
00076       void setHeadRequest(bool sw = true)           { headRequest = sw; }
00077 
00079       unsigned redirect(const std::string& newLocation);
00081       unsigned notAuthorized(const std::string& realm);
00083       unsigned notAuthorised(const std::string& realm) { return notAuthorized(realm); }
00084 
00085       void sendReply(unsigned ret, const char* msg = "OK");
00086       void sendReply(unsigned ret, const std::string& msg)
00087         { sendReply(ret, msg.c_str()); }
00088 
00090       std::ostream& out()   { return *current_outstream; }
00092       std::ostream& sout()  { return safe_outstream; }
00094       std::ostream& uout()  { return url_outstream; }
00095       void resetContent()   { outstream.str(std::string()); }
00096 
00097       void setContentLengthHeader(size_t size);
00098       void setKeepAliveHeader();
00099 
00100       virtual void setDirectMode(unsigned ret = HTTP_OK, const char* msg = "OK");
00101       virtual void setDirectModeNoFlush();
00102       virtual bool isDirectMode() const
00103         { return current_outstream == &socket; }
00104       std::string::size_type getContentSize() const
00105         { return outstream.str().size(); }
00106       std::ostream& getDirectStream()   { return socket; }
00107 
00108       void setMd5Sum();
00109 
00110       void setCookie(const std::string& name, const Cookie& value);
00111       void setCookie(const std::string& name, const std::string& value, unsigned seconds)
00112         { setCookie(name, Cookie(value, seconds)); }
00113       void setCookies(const Cookies& c)
00114         { httpcookies = c; }
00115       void clearCookie(const std::string& name);
00116       void clearCookie(const std::string& name, const Cookie& c)
00117         { httpcookies.clearCookie(name, c); }
00118       bool hasCookies() const
00119         { return httpcookies.hasCookies(); }
00120       const Cookies& getCookies() const
00121         { return httpcookies; }
00122 
00123       void setKeepAliveCounter(unsigned c)          { keepAliveCounter = c; }
00124       unsigned getKeepAliveCounter() const          { return keepAliveCounter; }
00125 
00126       static void setKeepAliveTimeout(unsigned ms)  { keepAliveTimeout = ms; }
00127       static unsigned getKeepAliveTimeout()         { return keepAliveTimeout; }
00128 
00129       static void setMinCompressSize(unsigned s)    { minCompressSize = s; }
00130       static unsigned getMinCompressSize()          { return minCompressSize; }
00131 
00132       static void setDefaultContentType(const std::string& ct) { defaultContentType = ct; }
00133       static const std::string& getDefaultContentType()        { return defaultContentType; }
00134 
00135       void setAcceptEncoding(const Encoding& enc)    { acceptEncoding = enc; }
00136 
00137       bool keepAlive() const;
00138 
00139       void setLocale(const std::locale& loc)    { out().imbue(loc); sout().imbue(loc); }
00140   };
00141 }
00142 
00143 #endif // TNT_HTTPREPLY_H
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6