httpreply.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2005 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 
30 #ifndef TNT_HTTPREPLY_H
31 #define TNT_HTTPREPLY_H
32 
33 #include <tnt/httpmessage.h>
34 #include <tnt/http.h>
35 #include <iosfwd>
36 
37 namespace tnt
38 {
39  class Savepoint;
40  class Encoding;
41 
43  class HttpReply : public HttpMessage
44  {
45  struct Impl;
46  Impl* impl;
47  std::ostream* current_outstream;
48  std::ostream* safe_outstream;
49  std::ostream* url_outstream;
50 
51  void send(unsigned ret, const char* msg, bool ready) const;
52 
53  public:
54  explicit HttpReply(std::ostream& s, bool sendStatusLine = true);
55  ~HttpReply();
56 
57  static bool tryCompress(std::string& body);
58  static void postRunCleanup();
59 
60  void setContentType(const char* t) { setHeader(httpheader::contentType, t); }
61  void setContentType(const std::string& t) { setHeader(httpheader::contentType, t); }
62  const char* getContentType() const { return getHeader(httpheader::contentType); }
63 
64  void setHeadRequest(bool sw = true);
65 
67  void clearSession();
69  bool isClearSession() const;
70 
72  unsigned redirect(const std::string& newLocation);
74  unsigned notAuthorized(const std::string& realm);
76  unsigned notAuthorised(const std::string& realm) { return notAuthorized(realm); }
77 
78  void sendReply(unsigned ret, const char* msg = "OK");
79  void sendReply(unsigned ret, const std::string& msg)
80  { sendReply(ret, msg.c_str()); }
81 
83  std::ostream& out() { return *current_outstream; }
85  std::ostream& sout() { return *safe_outstream; }
87  std::ostream& uout() { return *url_outstream; }
88  void resetContent();
89  void rollbackContent(unsigned size);
90 
91  void setContentLengthHeader(size_t size);
92  void setKeepAliveHeader();
93 
94  virtual void setDirectMode(unsigned ret = HTTP_OK, const char* msg = "OK");
95  virtual void setDirectModeNoFlush();
96  virtual bool isDirectMode() const;
97  std::string::size_type getContentSize() const;
98  std::ostream& getDirectStream();
99 
100  void setMd5Sum();
101 
102  void setCookie(const std::string& name, const Cookie& value);
103  void setCookie(const std::string& name, const std::string& value, unsigned seconds)
104  { setCookie(name, Cookie(value, seconds)); }
105  void setCookies(const Cookies& c)
106  { httpcookies = c; }
107  void clearCookie(const std::string& name);
108  void clearCookie(const std::string& name, const Cookie& c)
109  { httpcookies.clearCookie(name, c); }
110  bool hasCookies() const
111  { return httpcookies.hasCookies(); }
112  const Cookies& getCookies() const
113  { return httpcookies; }
114 
115  void setKeepAliveCounter(unsigned c);
116  unsigned getKeepAliveCounter() const;
117 
118  void setAcceptEncoding(const Encoding& enc);
119 
120  bool keepAlive() const;
121 
122  void setLocale(const std::locale& loc) { out().imbue(loc); sout().imbue(loc); }
123  };
124 }
125 
126 #endif // TNT_HTTPREPLY_H