00001 /* 00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net) 00003 This file is part of Beesnest. 00004 00005 Beesnest is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 Beesnest is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Beesnest; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 /* DcResponse.h: interface for the CdcResponse class. */ 00021 00022 00023 #ifndef __CDC_RESPONSE 00024 #define __CDC_RESPONSE 00025 00026 #include "Interfaces/BufferHolder.h" 00027 #include "Interfaces/DcResponse.h" 00028 #include "DcGlobals.h" 00029 #include "DcBuffer.h" 00030 #include "DcBuffersList.h" 00031 #include "DcParameters.h" 00032 #include _GET_INCLUDE_PATH (DcSocket.h) 00033 #include "DcHttpTime.h" 00034 00035 #define BN_SET_STATUS "STATUS_CODE" 00036 00037 /** 00038 * This class holds the response the server is sending back to the client. 00039 * The response has header fields and body (the HTML file). the user can set the 00040 * headers and the data, and then send it by the CdcSocket object attached to 00041 * this object in the Constructor. 00042 * 00043 * <p> 00044 * CdcResponse implements the interface IdcResponse. The functions that 00045 * implement IdcResponse, work with STL string and not CdcString, because 00046 * scripting engines don't know CdcString. In addition some of the interface 00047 * functions have a little different names from the implemented class functions, 00048 * and they call the "real" class function in-line. That is to solve the 00049 * ambiguity problem of working with STL string and CdcString in the same class. 00050 * 00051 * <p> 00052 * @author Erez Bibi 00053 * @version 1.1 00054 */ 00055 class CdcResponse : public IdcResponse 00056 { 00057 private: 00058 /** The HTTP version (should be left at default value). */ 00059 CdcString sVersion; 00060 /** The status code number of the response. */ 00061 int nStatusCode; 00062 /** The status phrase string of the response (filled automatically). */ 00063 CdcString sStatusPhrase; 00064 /** A map of all the fields name-value in the response. */ 00065 t_map mHeaderFields; 00066 /** A map of all the cookies in the response. */ 00067 t_map mCookies; 00068 /** The response body as reference to a Buffers-List. */ 00069 CdcBuffersList& aBody; 00070 00071 /** A pointer to a CdcSocket object. */ 00072 CdcSocket* pSocket; 00073 00074 /** A flag to signal that the header part of the response was already sent. */ 00075 bool isHeaderSent; 00076 00077 /* Functions */ 00078 00079 /** 00080 * This function returns a status phrase from a status code. 00081 * <p> 00082 * @param code The status code. 00083 * @return (CdcString) The phrase as a string, or empty string if it cannot 00084 * find a proper phrase. 00085 */ 00086 CdcString GetStatusPhrase (int code) const; 00087 00088 00089 /** 00090 * This function puts the response header in to a buffer. 00091 * <p> 00092 * @param buff The buffer to fill with the response header. 00093 * @return (int) the length of the buffer. 00094 */ 00095 int HeaderToBuffer (CdcBuffer& buff) const; 00096 00097 /** 00098 * This function sends the response header. It will send the header just 00099 * once. 00100 * <p> 00101 * @return (int) The length of the header sent (0 if header has already been 00102 * sent). 00103 */ 00104 int SendHeader () throw (CdcException); 00105 00106 public: 00107 /** 00108 * Constructor - Builds an empty object with default values for the header 00109 * fields, and attaches a CdcSocket object to it. 00110 * <p> 00111 * @param psock A pointer to CdcSocket object. 00112 */ 00113 CdcResponse (CdcSocket* psock, CdcBuffersList& bflst); 00114 00115 /** 00116 * Destructor - Does nothing. 00117 */ 00118 virtual ~CdcResponse() {}; 00119 00120 /** 00121 * Send the response to the client. It sends the header and all body buffers. 00122 * If a call to Flush has already been made, it calls Flush again to send 00123 * the rest of the data as chunks, and then sends the end of chunk response 00124 * string. 00125 * <p> 00126 * @return (int) The length of the response that was actually sent. 00127 * @throw (CdcException) Exceptions from the CdcSocket object. 00128 */ 00129 int SendResponse () throw (CdcException); 00130 00131 /** 00132 * This function implements IdcResponse::Flush. It sends the part of the 00133 * buffers list that is already in, as a chunk response. It sends the 00134 * headers just once, and set isHeadersSent to True. If 'buff' is not empty, 00135 * the function sends it as well. After sending everything it calls Clear. 00136 * <p> 00137 * This function implements a Chunk response sending protocol. It is 00138 * possible to call Flush (for number of times) and then call CdcResponse 00139 * regular Send in the same session. 00140 * <p> 00141 * @throw (int) This function will throw an integer value if the socket 00142 * generate an exception. This is for the script engine. 00143 */ 00144 virtual int Flush (BufferHolder buff) throw (int); 00145 00146 /* Sets function to the response fields and data. */ 00147 00148 /** 00149 * Set the HTTP version for this response (you should not use this 00150 * function typically). 00151 * <p> 00152 * @param vrs The HTTP version as string. 00153 */ 00154 void SetVersion (const CdcString& vrs); 00155 00156 /** 00157 * Set the status code, and phrase for this response. The function 00158 * takes the status phrase from a private list. 00159 * <p> 00160 * @param code The status code. 00161 */ 00162 void SetStatus (int code); 00163 00164 /** 00165 * Return the status phrase. 00166 */ 00167 CdcString GetStatus () const 00168 { return sStatusPhrase; } 00169 00170 /** 00171 * Set one of the header fields value. 00172 * Implementation of IdcResponse::SetResponseHeader 00173 */ 00174 void SetHeaderField (const CdcString& name, const CdcString& value); 00175 virtual void SetResponseHeader (BufferHolder name, BufferHolder value) 00176 { SetHeaderField (CdcString (name), CdcString (value)); } 00177 00178 /** 00179 * Returns one of the response fields value, or empty string if cannot find 00180 * the header field name. 00181 * <p> 00182 * @param name The name of the field, as a string. 00183 * @return (CdcString) A copy of the value of this field string. 00184 */ 00185 CdcString GetHeaderField (CdcString& name); 00186 00187 /** 00188 * Set a cookie in the cookies collection. 00189 * Implementation of IdcResponse::SetCookie 00190 */ 00191 void SetCookie (const CdcString& name, const CdcString& val, 00192 int days = 1, const CdcString& path = "/"); 00193 virtual void SetResponseCookie (BufferHolder name, BufferHolder val, 00194 int days = 1, BufferHolder path = BufferHolder ("/")) 00195 { SetCookie (CdcString (name), CdcString (val), days, CdcString (path)); } 00196 00197 /** 00198 * Restart clears all the data from the response except of the connected 00199 * socket. So it will be ready for building a new response to the same 00200 * client. This is for the Keep-Alive feature. After calling Restart, the 00201 * class will send the response headers again. 00202 */ 00203 void Restart (); 00204 00205 /** 00206 * This function implements IdcResponse::Clear. it erases all data from the 00207 * response object, including the parameter buffer pointer, but it does not 00208 * set isHeaderSent flag to false. 00209 */ 00210 virtual void Clear (); 00211 }; 00212 00213 #endif /* __CDC_RESPONSE */