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 /* DcRequest.h: interface for the CdcRequest class. */ 00021 00022 /** 00023 * This class holds the request that arrived from the client. The user builds a 00024 * request object with a pointer to CdcSocket as a parameter to the Constructor. 00025 * To get the next request the user calls <b>ReadNextRequest</b>. Then the user 00026 * can access each data field (read only). In order to send the "100 Continue" 00027 * message after reading the first <u>line</u> of the request, call to 00028 * <b>ReadFirstLineFirst</b> send the message and then call to 00029 * <b>ReadNextRequest</b>. 00030 * 00031 * <p> 00032 * The Request object may hold more then one request in it's buffer, at one time. 00033 * The next call to <b>ReadNextRequest</b> will load the next request into the 00034 * object (and delete the request from the buffer). 00035 * 00036 * <p> 00037 * <b> Use just one CdcRequest object for each connection with a client. </b> 00038 * 00039 * <p> 00040 * Note: there is a different between 'Request Message (Expression)' - The 00041 * entire message from the client, and 'Request Line' - The first line in the 00042 * request message (method URL version). 00043 * 00044 * <p> 00045 * CdcRequest implements the interface IdcRequest. The functions that implements 00046 * IdcRequest, work with STL string and not CdcString, because scripting engines 00047 * doesn't know CdcString. In addition some of the interface functions have a 00048 * little different names from the implemented class functions, and they call 00049 * the "real" class function in-line. That is to solve the ambiguity problem of 00050 * working with STL string and CdcString in the same class. 00051 * 00052 * @author Erez Bibi 00053 * @version 1.1 00054 */ 00055 00056 #ifndef __CDC_REQUEST 00057 #define __CDC_REQUEST 00058 00059 #include "Interfaces/BufferHolder.h" 00060 #include "Interfaces/DcRequest.h" 00061 #include "DcGlobals.h" 00062 #include "DcParameters.h" 00063 #include "DcExceptionParse.h" 00064 #include _GET_INCLUDE_PATH (DcSocket.h) 00065 00066 class CdcRequest : public IdcRequest 00067 { 00068 private: 00069 /** The method of the request. */ 00070 CdcString sMethod; 00071 /** The URL of the request (without the variables). */ 00072 CdcString sURL; 00073 /** The full URL as it received from client. */ 00074 CdcString sFullURL; 00075 /** The HTTP version of the request. */ 00076 CdcString sVersion; 00077 /** The Host name of this request. */ 00078 CdcString sHost; 00079 /** The host name as it was in the request (Alias). */ 00080 CdcString sReqHost; 00081 /** The request protocol. */ 00082 CdcString sProtocol; 00083 /** A map of all the fields name-value in the request. */ 00084 t_map mHeaderFields; 00085 /** A map of all the variables name-value of the request (GET POST Cookies). */ 00086 t_map mVariables; 00087 00088 /** The message received from the socket. */ 00089 CdcString sBuffer; 00090 00091 /** A pointer to a CdcSocket object. */ 00092 CdcSocket* pSocket; 00093 00094 /** Help variable for GetRequestVarsNamesList (I have to return a reference). */ 00095 string sVarList; 00096 00097 /* Functions. */ 00098 00099 /** 00100 * This function parses the first line in the request, and puts each element 00101 * into its variable. 00102 * <p> 00103 * @param line The line to parse, as a string. 00104 * @throw (CdcException) 'Bad Format Exception' if the function cannot parse 00105 * the line. 00106 */ 00107 void ParseRequsetLine (CdcString& line) throw (CdcException); 00108 00109 /** 00110 * This function parses the query part of the first line in the request, and 00111 * puts the name of each variable and it's value into the Variables map. 00112 * <p> 00113 * @param line The line to parse, as a string. 00114 * @param limit The separation char between Name-Value pairs. 00115 * @throw (CdcException) 'Bad Format Exception' if the function cannot parse 00116 * the line. 00117 */ 00118 void ParseQueryLine (CdcString& line, char limit = '&') throw (CdcException); 00119 00120 /** 00121 * This function parses a header line in the request, and puts the name and 00122 * the value into the Header Fields map. 00123 * <p> 00124 * @param line The line to parse, as a string. 00125 * @throw (CdcException) 'Bad Format Exception' if the function cannot parse 00126 * the line. 00127 */ 00128 void ParseHeaderLine (CdcString& line) throw (CdcException); 00129 00130 /** 00131 * Parse the request string buffer and extract the different fields out of 00132 * it. 00133 * <p> 00134 * @param msg The message to parse, as a string. 00135 * @throw (CdcException) 'Bad Format Exception' if the function cannot parse 00136 * the message. 00137 */ 00138 void ParseHeader (CdcString& msg) throw (CdcException); 00139 00140 00141 /** 00142 * Extract the cookies values from the cookies header field. It puts the 00143 * values in the variables map. 00144 * <p> 00145 * @param line The line to parse, as a string. 00146 */ 00147 void ParseCookiesHeader (CdcString& line); 00148 00149 00150 /** 00151 * Find the host for this request by the Host: header. 00152 * <p> 00153 * This server can serve several hosts. each host is mapped to a list of 00154 * names (Aliases) like: IP address or domain name. HTTP 1.0 requests will 00155 * be directed to a host called "Default-Host". The rest of the hosts have 00156 * unique names. Each host have it's own root folder. The hosts are defined 00157 * in the parameters file. 00158 */ 00159 void FindHost () throw (CdcException); 00160 00161 00162 /** 00163 * This function reads and parse the message body in a chunk request. 00164 * (untested function) 00165 * <p> 00166 * @param msg_body Reference to a string that will get the Request body part. 00167 */ 00168 void DoChunkRequest (CdcString& msg_body) throw (CdcException); 00169 00170 00171 /** 00172 * This function erases all the old request fields. 00173 */ 00174 void ResetRequestFields (); 00175 00176 /** 00177 * This function decodes the string <b>str</b> from a string that is coded 00178 * by a URI code, to a regular string. 00179 * <p> 00180 * @param str Reference to a coded string. 00181 */ 00182 void URIDecode (CdcString& str); 00183 00184 /** 00185 * This function sends the "100 Continue" response if appropriate. 00186 */ 00187 virtual void Handle100Continue (); 00188 00189 /* The folwoing functions helps to handle the different between the GetXXX 00190 functions of IdcRequest, and the GetXXX functions of this class. */ 00191 00192 const string& __GetHeaderField (const CdcString& name) const; 00193 const string& __GetVariable (const CdcString& name) const; 00194 00195 public: 00196 /** 00197 * Constructor - Builds an empty object, and attaches a CdcSocket pointer to 00198 * it. 00199 * <p> 00200 * @param psock A pointer to a CdcSocket object. 00201 */ 00202 CdcRequest (CdcSocket* psock); 00203 00204 /** 00205 * Destructor - Does nothing. 00206 */ 00207 virtual ~CdcRequest() {}; 00208 00209 /** 00210 * Get a new request from the client. the request may be read from the 00211 * socket, or it may be already in the buffer, from a previous reads from 00212 * this socket. After calling this function the object will hold the next 00213 * request from the client. 00214 * <p> 00215 * @throw (CdcException) Exceptions from the private parsing functions, or 00216 * from the CdcSocket object. 00217 */ 00218 void ReadNextRequest (); 00219 00220 00221 /* Gets functions for the request fields. */ 00222 00223 /** 00224 * Returns the Method in the request. 00225 * <p> 00226 * @return (CdcString) A copy of the method string. 00227 */ 00228 CdcString GetMethod () const { return sMethod; } 00229 00230 /** 00231 * Returns the URL in the request. 00232 * <p> 00233 * @return (CdcString) A copy of the URL string (w/o query part). 00234 */ 00235 CdcString GetURL () const { return sURL; } 00236 00237 /** 00238 * Returns the full URL as it was received from the client. 00239 * <p> 00240 * @return (CdcString) A copy of the full URL string. 00241 */ 00242 CdcString GetFullURL () const { return sFullURL; } 00243 00244 /** 00245 * Returns the Version in the request. 00246 * <p> 00247 * @return (CdcString) A copy of the version string. 00248 */ 00249 CdcString GetVersion () const { return sVersion; } 00250 00251 /** 00252 * Returns the Host of the request. 00253 * <p> 00254 * @return (CdcString) A copy of the host string. 00255 */ 00256 CdcString GetHost () const { return sHost; } 00257 00258 /** 00259 * Returns the alias name for the host as it is in the request. 00260 * <p> 00261 * @return (CdcString) A copy of the alias host string. 00262 */ 00263 CdcString GetAliasHost () const { return sReqHost; } 00264 00265 /** 00266 * Erase a request header field. I need it for the logout process. 00267 * <p> 00268 * @param name The name of the header field as string. 00269 */ 00270 void CdcRequest::EraseHeaderField (const CdcString& name); 00271 00272 /** IdcRequest GetRequestHeader. */ 00273 virtual BufferHolder GetRequestHeader (BufferHolder name) const 00274 { return BufferHolder (__GetHeaderField (CdcString (name)).c_str ()); } 00275 /** This class GetHeaderField. */ 00276 CdcString GetHeaderField (const CdcString& name) const 00277 { return __GetHeaderField (name); } 00278 00279 /** IdcRequest GetRequestVariable */ 00280 virtual BufferHolder GetRequestVariable (BufferHolder name) const 00281 { return BufferHolder (__GetVariable (CdcString (name)).c_str ()); } 00282 /** This class GetVariable */ 00283 CdcString GetVariable (const CdcString& name) const 00284 { return __GetVariable (name); } 00285 00286 /** 00287 * SetRequestVariable is an implementation of Idcrequest::SetRequestVariable 00288 */ 00289 virtual void SetRequestVariable (BufferHolder name, BufferHolder val) 00290 { SetVariable (CdcString (name), CdcString (val)); } 00291 void SetVariable (const CdcString& name, const CdcString& val) 00292 { mVariables [name] = val; } 00293 00294 /** 00295 * This is an implementation of IdcRequest::GetRequestVarsNamesList 00296 */ 00297 virtual BufferHolder GetRequestVarsNamesList (BufferHolder sep = BufferHolder ("|")); 00298 }; 00299 00300 #endif /* __CDC_REQUEST */ 00301