• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/DcConnectionThread.h

Go to the documentation of this file.
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 /* DcConnectionThread.h: interface for the CdcConnectionThread class. */
00021 
00022 #ifndef __CDC_CONNECTION_THREAD
00023 #define __CDC_CONNECTION_THREAD
00024 
00025 #include "DcBuffer.h"
00026 #include "DcBuffersList.h"
00027 #include "DcEnginesBank.h"
00028 #include "DcGlobals.h"
00029 #include "DcHttpTime.h"
00030 #include "DcLoadController.h"
00031 #include "DcLoginManager.h"
00032 #include "DcRequest.h"
00033 #include "DcResponse.h"
00034 #include "DcRetrieveFile.h"
00035 #include "Interfaces/BufferHolder.h"
00036 #include "Interfaces/DcAdaptersSet.h"
00037 #include "Interfaces/DcEngine.h"
00038 #include _GET_INCLUDE_PATH (DcMutex.h)
00039 #include _GET_INCLUDE_PATH (DcSocket.h)
00040 #include _GET_INCLUDE_PATH (DcThread.h)
00041 
00042 /**
00043  * CdcConnectionThread is a class that runs in a separate thread, and manages
00044  * most of the job of this server. It gets a CdcSocket object in the constructor,
00045  * reads the request, gets the proper response file(s), process the file(s), and
00046  * returns it to the client. It builds a proper header for the response and stay
00047  * connected (waits for another request) if necessary. </p>
00048  *
00049  * <p>
00050  * In order to run the separate thread (and do the job this class suppose to do)
00051  * the user should call <b>CdcConnectionThread::DcRun</b> (from the new thread).
00052  * <p>
00053  * @author Erez Bibi
00054  * @version 1.0
00055  */
00056 class CdcConnectionThread
00057 {
00058 private:
00059     /** The pointer to the thread object. */
00060     CdcThread oThread;
00061 
00062     /** The pointer to the CdcSocket object. */
00063     CdcSocket* pSocket;
00064 
00065     /** The buffer list to create and send. (must be declared before oResponse). */
00066     CdcBuffersList oBuffList;
00067 
00068     /** A request object. */
00069     CdcRequest oRequest;
00070 
00071     /** A response object. */
00072     CdcResponse oResponse;
00073 
00074     /** The static thread counter. */
00075     static volatile int nThreadCounter;
00076 
00077     /** The thread number. */
00078     int nThreadNumber;
00079 
00080     /** String to hold the logging information. */
00081     CdcString sLog;
00082 
00083     /** The last error (exception) happened. */
00084     CdcString sLastError;
00085 
00086     /** Response buffer size limit. */
00087     int nResSizeLimit;
00088     /** Response time limit [sec]. */
00089     int nResTimeLimit;
00090 
00091     /** Help variable to know if need to execute script on a buffer. */
00092     bool isExecScript;
00093 
00094     /** Pointer to a user status object. */
00095     CdcUserStatus* pUserStatus;
00096 
00097     /** Set of URLs that has been included. To prevent infinite includes loop. */
00098     typedef set <CdcString> t_inc_set;
00099     t_inc_set oIncSet;
00100 
00101     /**
00102      * This function reads and parses the next request from a socket.
00103      * <p>
00104      * @return (bool) True if the request is valid.
00105      */
00106     bool ReadRequest ();
00107 
00108     /**
00109      * This function checks if the client will accept the response file.
00110      * <p>
00111      * @param type The type of the file.
00112      * @return (bool) True if the client will accept this response.
00113      */
00114     bool CheckAccept (CdcString type);
00115 
00116     /**
00117      * This function handle an if-modified condition on the request.
00118      * <p>
00119      * @param file Reference to a CdcRetrieveFile object.
00120      * @return (bool) True if the request should be fulfilled.
00121      */
00122     bool DoIfModified (const CdcRetrieveFile& file);
00123 
00124     /**
00125      * This function handle an if-unmodified condition on the request.
00126      * <p>
00127      * @param file Reference to a CdcRetrieveFile object.
00128      * @return (bool) True if the request should be fulfilled.
00129      */
00130     bool DoIfUnmodified (const CdcRetrieveFile& file);
00131 
00132     /**
00133      * This function builds a normal response (200 - OK).
00134      * <p>
00135      * @param file Reference to a CdcRetrieveFile object.
00136      * @return (bool) always True.
00137      */
00138     bool BuildNormalResponse (CdcRetrieveFile& file);
00139 
00140     /**
00141      * This is the function that deals with including files and with login
00142      * instructions. It builds the buffer list to send to the client.
00143      * It starts with the file the client requested, and add all the included
00144      * files to the list. Each file it adds to the list, it checks for a login
00145      * instruction in it. If there is one, it execute the login process (that
00146      * can result in a new file been sent to the client, or not).
00147      * <p>
00148      * This is a recursive function. It maintains a set of file names that
00149      * currently in the include list, so it won't create an infinite loop (but
00150      * it can include the same file twice if it is not one inside the other).
00151      * <p>
00152      * @param file Reference to the current file to add to the list.
00153      * @param list Reference to the list that is been created.
00154      * @param check_allow This is true if we still need to check for login. once
00155      *  the function determines the login status, it will stop checking.
00156      * @return (bool) The returned value is just for the inner recursive calls,
00157      *  If it is False the function returns all the way up immediately.
00158      */
00159     bool BuildBuffersList (const CdcRetrieveFile& file, CdcBuffersList& list,
00160                            bool& check_allow);
00161 
00162     /**
00163      * This function executes an SSI instruction. It's been called while the
00164      * system builds the returned buffer list (before it executes any script).
00165      * Most common task will be to "include" another file. It can also "echo"
00166      * or "set" a value of the Request variables. "set" is also capable of
00167      * setting the response size limit, and the response time limit. use
00168      * "BNI_SizeLimit" and "BNI_TimeLimit". BNI stands for "Beesnest Inner
00169      * (variable)", it is different from the "BN_" variables which are Request
00170      * variables.
00171      * <p>
00172      * The format of supported "Server Side Includes" by Beesnest is: <br>
00173      * &lt;!--#set var="VarName" value="Value" --&gt; <br>
00174      * &lt;!--#echo var="VarName" --&gt; <br>
00175      * &lt;!--#include file="..." --&gt; <br>
00176      * &lt;!--#include virtual="..." --&gt; <br>
00177      * <p>
00178      * @param inner The inner string that in the SSI instruction.
00179      * @param file Reference to the current file (just to solve virtual URLs in
00180      *  #include instruction).
00181      * @param isInclude [out] The function sets this flag to true if the
00182      * returned value is a new URL to include.
00183      * @return (CdcString) The result of the SSI instruction or an empty string.
00184      */
00185     CdcString ExecuteSSI (const CdcString& inner, const CdcRetrieveFile& file,
00186                           bool& isInclude);
00187 
00188     /**
00189      * This is an help function for ExecuteSSI. It extracts the URL from an
00190      * #include instruction, and if it is a virtual URL, it solves it.
00191      * <p>
00192      * @param inner The inner string that in the #include instruction.
00193      * @param file Reference to the current file (just to solve virtual URLs.
00194      * @return (CdcString) The absolute URL to include.
00195      */
00196     CdcString ExtractURL (const CdcString& inner, const CdcRetrieveFile& file);
00197 
00198     /**
00199      * This is an help function to BuildBuffersList. It looks in the parameters
00200      * file if the URL requires login, and call the Login Manager if it does.
00201      * <p>
00202      * This function may clear the buffers list and change it, if the Login
00203      * Manager needs to send other (login / deny) file to the client.
00204      * <p>
00205      * @param url Reference to the URL to check.
00206      * @param list Reference to the buffers list.
00207      * @return (int) A code that tells BuildBuffersList what is going on.
00208      *  DC_LM_NONE - No access limit was found (but need to keep checking
00209      *  because an include file may have access limit).
00210      *  DC_LM_OK - Client is logged in successfully (No need to check for login
00211      *  any more).
00212      *  DC_LM_SEND - need to send new file to client (BuildBuffersList will exit).
00213      */
00214     int CheckAllow (const CdcString& url, CdcBuffersList& list);
00215 
00216     /**
00217      * This (very important) function takes the buffers list of the main file
00218      * with all the includes, goes over it and executes each script. It builds
00219      * oBuffList (the buffers list of the Response object), while going over
00220      * the source buffers. If a script will call Response.Flush, it will
00221      * send just the buffers that already in oBuffList.
00222      * <p>
00223      * @param bufflist Reference to the source buffers list.
00224      */
00225     void ExecuteScript (CdcBuffersList& bufflist);
00226 
00227     /**
00228      * This is an help function to ExecuteScript. It executes all the
00229      * &lt;bnscript&gt; section in a (part of a) buffer. This function has a
00230      * lot of arguments.
00231      * <p>
00232      * @param eb Reference to the engines bank.
00233      * @param pbuff Constant pointer of the current buffer to execute.
00234      * @param start_time The time when we started to work on the buffers list.
00235      * @param offset The offset in pbuff (hence "part of a").
00236      * @param len The relevant length of pbuff.
00237      * @return True if everything want right.
00238      */
00239     bool ExecuteBufferSections (CdcEnginesBank& eb, const CdcBuffer* pbuff,
00240         int start_time, int offset, int len);
00241 
00242     /**
00243      * This is another help function to ExecuteScript (and to
00244      * ExecuteBufferSections). It executes one script section, using a known
00245      * script engine. This function has even more arguments.
00246      * <p>
00247      * @param eb Reference to the engines bank.
00248      * @param eng_name The name of the script engine to use.
00249      * @param pbuff Const pointer of the current buffer to execute.
00250      * @param start_time The time when we started to work on the buffers list.
00251      * @param offset The offset in pbuff.
00252      * @param len The relevant length of pbuff.
00253      * @return True if everything want right.
00254      */
00255     bool ExecuteSection (CdcEnginesBank& eb, const CdcString eng_name,
00256         const CdcBuffer* pbuff, int start_time, int offset, int len);
00257 
00258 
00259     /**
00260      * Write the log information to the log string.
00261      */
00262     void FillInLog ();
00263 
00264 public:
00265     /**
00266      * Constructor - This is the only constructor for this class. It gets a
00267      * pointer to a CdcSocket object (this object is connected to the client).
00268      * <p>
00269      * @param psoc A pointer to an active CdcSocket object.
00270      */
00271     CdcConnectionThread ( CdcSocket* psoc) throw (CdcException);
00272 
00273     /**
00274      * Destructor - The Destructor stops the thread if it runs, and calls the
00275      * Destructor of CdcSocket.
00276      */
00277     virtual ~CdcConnectionThread ();
00278 
00279     /**
00280      * This is a global friend function. This function runs as a separate thread,
00281      * has access to all private members of the class and does the job of this
00282      * thread.
00283      */
00284     friend int DcRun (void* pParam);
00285 };
00286 
00287 #endif /* __CDC_CONNECTION_THREAD */

Generated on Mon Oct 11 2010 16:23:23 for Beesnest by  doxygen 1.7.2