00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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