pion-net
4.0.9
|
00001 // ------------------------------------------------------------------ 00002 // pion-net: a C++ framework for building lightweight HTTP interfaces 00003 // ------------------------------------------------------------------ 00004 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com) 00005 // 00006 // Distributed under the Boost Software License, Version 1.0. 00007 // See http://www.boost.org/LICENSE_1_0.txt 00008 // 00009 00010 #ifndef __PION_HTTPRESPONSE_HEADER__ 00011 #define __PION_HTTPRESPONSE_HEADER__ 00012 00013 #include <boost/shared_ptr.hpp> 00014 #include <boost/lexical_cast.hpp> 00015 #include <pion/PionConfig.hpp> 00016 #include <pion/net/HTTPMessage.hpp> 00017 #include <pion/net/HTTPRequest.hpp> 00018 00019 00020 namespace pion { // begin namespace pion 00021 namespace net { // begin namespace net (Pion Network Library) 00022 00023 00027 class HTTPResponse 00028 : public HTTPMessage 00029 { 00030 public: 00031 00037 HTTPResponse(const HTTPRequest& http_request) 00038 : m_status_code(RESPONSE_CODE_OK), 00039 m_status_message(RESPONSE_MESSAGE_OK) 00040 { 00041 updateRequestInfo(http_request); 00042 } 00043 00049 HTTPResponse(const std::string& request_method) 00050 : m_status_code(RESPONSE_CODE_OK), m_status_message(RESPONSE_MESSAGE_OK), 00051 m_request_method(request_method) 00052 {} 00053 00055 HTTPResponse(const HTTPResponse& http_response) 00056 : HTTPMessage(http_response), 00057 m_status_code(http_response.m_status_code), 00058 m_status_message(http_response.m_status_message), 00059 m_request_method(http_response.m_request_method) 00060 {} 00061 00064 HTTPResponse(void) 00065 : m_status_code(RESPONSE_CODE_OK), 00066 m_status_message(RESPONSE_MESSAGE_OK) 00067 {} 00068 00070 virtual ~HTTPResponse() {} 00071 00073 virtual void clear(void) { 00074 HTTPMessage::clear(); 00075 m_status_code = RESPONSE_CODE_OK; 00076 m_status_message = RESPONSE_MESSAGE_OK; 00077 m_request_method.clear(); 00078 } 00079 00081 virtual bool isContentLengthImplied(void) const { 00082 return (m_request_method == REQUEST_METHOD_HEAD // HEAD responses have no content 00083 || (m_status_code >= 100 && m_status_code <= 199) // 1xx responses have no content 00084 || m_status_code == 204 || m_status_code == 205 // no content & reset content responses 00085 || m_status_code == 304 // not modified responses have no content 00086 ); 00087 } 00088 00095 inline void updateRequestInfo(const HTTPRequest& http_request) { 00096 m_request_method = http_request.getMethod(); 00097 if (http_request.getVersionMajor() == 1 && http_request.getVersionMinor() >= 1) 00098 setChunksSupported(true); 00099 } 00100 00102 inline void setStatusCode(unsigned int n) { 00103 m_status_code = n; 00104 clearFirstLine(); 00105 } 00106 00108 inline void setStatusMessage(const std::string& msg) { 00109 m_status_message = msg; 00110 clearFirstLine(); 00111 } 00112 00114 inline unsigned int getStatusCode(void) const { return m_status_code; } 00115 00117 inline const std::string& getStatusMessage(void) const { return m_status_message; } 00118 00119 00127 inline void setCookie(const std::string& name, const std::string& value) { 00128 std::string set_cookie_header(make_set_cookie_header(name, value, "/")); 00129 addHeader(HEADER_SET_COOKIE, set_cookie_header); 00130 } 00131 00140 inline void setCookie(const std::string& name, const std::string& value, 00141 const std::string& path) 00142 { 00143 std::string set_cookie_header(make_set_cookie_header(name, value, path)); 00144 addHeader(HEADER_SET_COOKIE, set_cookie_header); 00145 } 00146 00155 inline void setCookie(const std::string& name, const std::string& value, 00156 const std::string& path, const unsigned long max_age) 00157 { 00158 std::string set_cookie_header(make_set_cookie_header(name, value, path, true, max_age)); 00159 addHeader(HEADER_SET_COOKIE, set_cookie_header); 00160 } 00161 00169 inline void setCookie(const std::string& name, const std::string& value, 00170 const unsigned long max_age) 00171 { 00172 std::string set_cookie_header(make_set_cookie_header(name, value, "/", true, max_age)); 00173 addHeader(HEADER_SET_COOKIE, set_cookie_header); 00174 } 00175 00177 inline void deleteCookie(const std::string& name) { 00178 std::string set_cookie_header(make_set_cookie_header(name, "", "/", true, 0)); 00179 addHeader(HEADER_SET_COOKIE, set_cookie_header); 00180 } 00181 00183 inline void deleteCookie(const std::string& name, const std::string& path) { 00184 std::string set_cookie_header(make_set_cookie_header(name, "", path, true, 0)); 00185 addHeader(HEADER_SET_COOKIE, set_cookie_header); 00186 } 00187 00189 inline void setLastModified(const unsigned long t) { 00190 changeHeader(HEADER_LAST_MODIFIED, get_date_string(t)); 00191 } 00192 00193 00194 protected: 00195 00197 virtual void updateFirstLine(void) const { 00198 // start out with the HTTP version 00199 m_first_line = getVersionString(); 00200 m_first_line += ' '; 00201 // append the response status code 00202 m_first_line += boost::lexical_cast<std::string>(m_status_code); 00203 m_first_line += ' '; 00204 // append the response status message 00205 m_first_line += m_status_message; 00206 } 00207 00208 00209 private: 00210 00212 unsigned int m_status_code; 00213 00215 std::string m_status_message; 00216 00218 std::string m_request_method; 00219 }; 00220 00221 00223 typedef boost::shared_ptr<HTTPResponse> HTTPResponsePtr; 00224 00225 00226 } // end namespace net 00227 } // end namespace pion 00228 00229 #endif