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  private:
46  struct Impl;
47  Impl* _impl;
48  std::ostream* _currentOutstream;
49  std::ostream* _safeOutstream;
50  std::ostream* _urlOutstream;
51 
52  void sendHttpStatus(std::ostream& hsocket, unsigned ret, const char* msg) const;
53  void sendHttpHeaders(std::ostream& hsocket) const;
54  void send(unsigned ret, const char* msg, bool ready) const;
55 
56  public:
57  explicit HttpReply(std::ostream& s, bool sendStatusLine = true);
58  ~HttpReply();
59 
60  static bool tryCompress(std::string& body);
61  static void postRunCleanup();
62 
63  void setContentType(const char* t) { setHeader(httpheader::contentType, t); }
64  void setContentType(const std::string& t) { setHeader(httpheader::contentType, t); }
65  const char* getContentType() const { return getHeader(httpheader::contentType); }
66 
67  void setHeadRequest(bool sw = true);
68 
70  void clearSession();
71 
73  bool isClearSession() const;
74 
75  enum Redirect { permanently = HTTP_MOVED_PERMANENTLY, temporarily = HTTP_TEMPORARY_REDIRECT };
77 
81  unsigned redirect(const std::string& newLocation, Redirect type = temporarily);
82  unsigned redirectTemporary(const std::string& newLocation)
83  { return redirect(newLocation, temporarily); }
84  unsigned redirectPermantently(const std::string& newLocation)
85  { return redirect(newLocation, permanently); }
87 
92  unsigned notAuthorized(const std::string& realm);
93 
94  // TODO: Why does this exist?
96  unsigned notAuthorised(const std::string& realm) { return notAuthorized(realm); }
97 
98  void sendReply(unsigned ret, const char* msg = "OK");
99  void sendReply(unsigned ret, const std::string& msg)
100  { sendReply(ret, msg.c_str()); }
101 
103  std::ostream& out() { return *_currentOutstream; }
104 
106  std::ostream& sout() { return *_safeOutstream; }
107 
109  std::ostream& uout() { return *_urlOutstream; }
110 
111  void resetContent();
112  void rollbackContent(unsigned size);
113 
114  void setContentLengthHeader(size_t size);
115  void setKeepAliveHeader();
116  void setMaxAgeHeader(unsigned seconds);
117 
118  virtual void setDirectMode(unsigned ret = HTTP_OK, const char* msg = "OK");
119  virtual void setDirectModeNoFlush();
120  virtual bool isDirectMode() const;
121  std::string::size_type getContentSize() const;
122  unsigned chunkedBytesWritten() const;
123  std::ostream& getDirectStream();
124 
138  void setChunkedEncoding(unsigned ret = HTTP_OK, const char* msg = 0);
139 
141  bool isChunkedEncoding() const;
142 
143  // TODO: Documentation revision
150  void setMd5Sum();
151 
152  void setCookie(const std::string& name, const Cookie& value);
153  void setCookie(const std::string& name, const std::string& value, unsigned seconds)
154  { setCookie(name, Cookie(value, seconds)); }
155  void setCookies(const Cookies& c)
156  { httpcookies = c; }
157  void clearCookie(const std::string& name);
158  void clearCookie(const std::string& name, const Cookie& c)
159  { httpcookies.clearCookie(name, c); }
160  bool hasCookies() const
161  { return httpcookies.hasCookies(); }
162  const Cookies& getCookies() const
163  { return httpcookies; }
164 
165  void setKeepAliveCounter(unsigned c);
166  unsigned getKeepAliveCounter() const;
167 
168  void setAcceptEncoding(const Encoding& enc);
169 
170  bool keepAlive() const;
171 
172  void setLocale(const std::locale& loc)
173  { out().imbue(loc); sout().imbue(loc); }
174  };
175 }
176 
177 #endif // TNT_HTTPREPLY_H