00001 #ifndef DV_HTTPHEAD_H 00002 #define DV_HTTPHEAD_H 00003 // $Id: httphead.h,v 1.10 2003/08/16 08:30:34 dvermeir Exp $ 00004 00005 #include <sys/types.h> 00006 #include <iostream> 00007 #include <string> 00008 #include <list> 00009 #include <map> 00010 00011 namespace Dv { namespace Cgi { 00012 /** A class representing the header part of a HTTP response. 00013 * 00014 * The class makes it convenient to set up standard headers and 00015 * cookies for the response: 00016 * <ul> 00017 * <li> Dv::Cgi::HttpHeader::nocache() generates header lines assuring 00018 * that a browser will not keep the response pagein its cache. 00019 * <li> Dv::Cgi::HttpHeader::cookie() sets the a cookie 00020 * <li> Dv::Cgi::HttpHeader::content_type() sets the content-type 00021 * of the page. 00022 * <li> Dv::Cgi::HttpHeader::location() redirects the browser to a url 00023 * <li> Dv::Cgi::HttpHeader::refresh() redirects the browser after a 00024 * certain period 00025 * </ul> 00026 */ 00027 class HttpHeader { 00028 public: 00029 /** Constructor 00030 * @param content type, default is "text/plain" 00031 * @param debug debug flag 00032 */ 00033 HttpHeader(const std::string& content="text/plain", bool debug=false); 00034 /** Type of data member that contains headers (except cookies) */ 00035 typedef std::map<std::string,std::string> header_map; 00036 /** Iterator type that allows access to header strings (except 00037 * cookies). */ 00038 typedef header_map::iterator iterator; 00039 /** Const Iterator type that allows access to header strings (except 00040 * cookies). */ 00041 typedef header_map::const_iterator const_iterator; 00042 00043 /** @return current debug status */ 00044 bool debug() const { return debug_; } 00045 /** @return const_iterator pointing to first header string */ 00046 const_iterator begin() const { return headers_.begin(); } 00047 /** @return const_iterator pointing past last header string */ 00048 const_iterator end() const { return headers_.end(); } 00049 /** @return iterator pointing to first header string */ 00050 iterator begin() { return headers_.begin(); } 00051 /** @return iterator pointing past last header string */ 00052 iterator end() { return headers_.end(); } 00053 /** @return number of header strings, excluding cookies. */ 00054 size_t size() const { return headers_.size(); } 00055 00056 /** Add header strings that prevent browser from caching the page. */ 00057 HttpHeader& nocache(); 00058 /** Set cookie name = value for path. 00059 * Providing an empty value will remove the cookie. 00060 * @param name of cookie 00061 * @param value of cookie (default is "", i.e. delete) 00062 * @param path of cookie (default is "/") 00063 * @return reference to *this. 00064 */ 00065 HttpHeader& cookie(const std::string& name, const std::string& value="", 00066 const std::string& path="/"); 00067 /** Add header strings to set content type. 00068 * @param value content type, e.g. "text/html" 00069 * @return reference to *this. 00070 */ 00071 HttpHeader& content_type(const std::string& value); 00072 /** Add header string to redirect browser to a given url. 00073 * @param value url to redirect to 00074 * @return reference to *this. 00075 */ 00076 HttpHeader& location(const std::string& value); 00077 /** Add header string to refresh browser 00078 * @param duration in seconds that browser will wait before refresh, 00079 * default is 0 00080 * @param url which will be accessed upon refresh, 00081 * default is same url 00082 * @return reference to *this. 00083 */ 00084 HttpHeader& refresh(size_t duration=0, const std::string& url=""); 00085 00086 /** General header add function. 00087 * @param name first part of header line, e.g. "Content-type: ". 00088 * @param value rest of header line, e.g."text/html". 00089 * @return reference to *this. 00090 * Example 00091 * @code 00092 * httpheader.add("Content-type: ","text/html"); 00093 * @endcode 00094 * @warning 00095 * Any previous value of "name" is replaced by the new value. 00096 */ 00097 HttpHeader& add(const std::string& name, const std::string& value); 00098 /** Remove any header with a given name. 00099 * @param name first part of header to remove 00100 * @return reference to *this. 00101 * @sa Dv::Cgi::HttpHeader::add 00102 */ 00103 HttpHeader& erase(const std::string& name); 00104 /** Remove all header strings. 00105 * @return reference to *this. 00106 */ 00107 HttpHeader& clear() { headers_.clear(); return *this; } 00108 00109 /** Output header strings and cookie definitions. 00110 * @param os to write headers to 00111 * @param header to write 00112 * @return reference to os 00113 */ 00114 friend std::ostream& operator<<(std::ostream& os, const HttpHeader& header); 00115 private: 00116 bool debug_; 00117 header_map headers_; 00118 typedef std::map<std::string,std::pair<std::string,std::string> > cookie_map; 00119 cookie_map cookies_; 00120 }; 00121 } } 00122 #endif
dvcgi-0.5.14 | [22 January, 2006] |