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 /* DcFileAttrib.h: interface for the CdcFileAttrib class. */ 00021 00022 #ifndef __CDC_FILE_ATTRIB 00023 #define __CDC_FILE_ATTRIB 00024 00025 #include "DcGlobals.h" 00026 #include "DcParameters.h" 00027 #include "DcException.h" 00028 #include "DcExceptionFile.h" 00029 #include _GET_INCLUDE_PATH (DcFileUtil.h) 00030 #include _GET_INCLUDE_PATH (DcMutex.h) 00031 00032 /** 00033 * This class holds the file attributes of a given file. The class DOES NOT open 00034 * the file. It gets the OS file attribute from CdcFileUtil. and Beesnest file 00035 * extension settings from CdcParameters. 00036 * <p> 00037 * The class gets a URL and a host name in the constructor. After constructing 00038 * the object, it is a read only object. 00039 * <p> 00040 * This class uses reference counting. It gets created only once, for a given 00041 * thread and URL. But then it gets passed to many objects (mostly 00042 * CdcBuffresList). Unlike other reference counting classes, here the object is 00043 * read only, so there is no Copy On Write. The last object that detach from 00044 * this class, deletes it. 00045 * <p> 00046 * @author Erez Bibi 00047 * @version 1.1 00048 */ 00049 class CdcFileAttrib 00050 { 00051 private: 00052 class CdcFileAttribInnerData 00053 { 00054 public: 00055 /** The URL string. */ 00056 CdcString sURL; 00057 /** The full path + file name */ 00058 CdcString sFullName; 00059 /** The extension of the file. */ 00060 CdcString sFileExtention; 00061 /** flag for "file found". */ 00062 bool isFound; 00063 /** flag for redirect. */ 00064 bool isRedirect; 00065 /** File status object. */ 00066 CdcFileUtil oFileStatus; 00067 }; 00068 00069 /** Pointer to the reference counter. */ 00070 int* pRefCount; 00071 /** Pointer to a dynamically allocated inner data structure (cannot be NULL). */ 00072 CdcFileAttribInnerData* pInnerData; 00073 00074 00075 /** 00076 * This function makes the shallow copy of other <b>InnerData</b> into 00077 * <b>this</b>, and increases by one the reference counter. 00078 * <p> 00079 * @param fa Reference to the FileAttrib to copy. 00080 */ 00081 void Attach (const CdcFileAttrib& fa); 00082 00083 /** 00084 * This function releases this object from pointing to the InnerData, 00085 * and decreases the reference counter by one. If the reference 00086 * counter is zero. it deletes the InnerData and the counter. 00087 */ 00088 void Detach (); 00089 00090 public: 00091 00092 CdcFileAttrib (const CdcString& u, const CdcString& host, bool inc); 00093 00094 /* Reference counting functions. */ 00095 00096 /** 00097 * Empty constructor. 00098 */ 00099 CdcFileAttrib () 00100 :pRefCount (NULL), pInnerData (NULL) {} 00101 00102 /** 00103 * Copy constructor - Shallow copy one FileAttrib into the other. 00104 * <p> 00105 * @param fa The FileAttrib to copy. 00106 */ 00107 CdcFileAttrib (const CdcFileAttrib& fa) { Attach (fa); } 00108 00109 /** 00110 * Destructor - Free the dynamic allocated memory. 00111 */ 00112 virtual ~CdcFileAttrib() { Detach (); } 00113 00114 /** 00115 * Assignment operator - Shallow copy one FileAttrib into the other. 00116 * <p> 00117 * @param fa The FileAttrib to copy. 00118 * @return (CdcFileAttrib&) A reference to the FileAttrib (this). 00119 */ 00120 const CdcFileAttrib& operator= (const CdcFileAttrib& fa); 00121 00122 /* Attributes functions. */ 00123 00124 /** 00125 * This function returns True if it can find the file under the root 00126 * directory of the server, otherwise it returns False. 00127 * <p> 00128 * @return (See description). 00129 */ 00130 bool IsFileFound () const 00131 { if (pInnerData) return pInnerData->isFound; 00132 else return false; } 00133 00134 /** 00135 * This function returns True if the URL needs to be redirected, otherwise 00136 * it returns False. 00137 * For now it only redirects if the URL is of a folder and doesn't ends with 00138 * '/'. 00139 * Note that if Redirect is True, FileFound will be False. 00140 * <p> 00141 * @return (See description). 00142 */ 00143 bool IsRedirect () const 00144 { if (pInnerData) return pInnerData->isRedirect; 00145 else return false; } 00146 00147 /** 00148 * Returns True if need to execute script on this file. 00149 */ 00150 bool IsExecScript () const 00151 { if (pInnerData) return CdcParameters::GetParam ("Server->File-Types->" + 00152 pInnerData->sFileExtention + "->Exec") == "yes"; 00153 else return false; } 00154 00155 00156 /** 00157 * Returns True if file cannot be cached in file cacher. 00158 */ 00159 bool IsNotToCache () const 00160 { if (pInnerData) return CdcParameters::GetParam ( "Server->File-Types->" + 00161 pInnerData->sFileExtention + "->Do-Not-Cache") == "yes"; 00162 else return false; } 00163 00164 00165 /** 00166 * Returns the file URL. 00167 */ 00168 CdcString GetURL () const 00169 { if (pInnerData) return pInnerData->sURL; 00170 else return EMPTY_STR; } 00171 00172 /** 00173 * Returns the file full path and name. 00174 */ 00175 CdcString GetFullName () const 00176 { if (pInnerData) return pInnerData->sFullName; 00177 else return EMPTY_STR; } 00178 00179 00180 /** 00181 * Returns the file extension as it is in the URL, in lower case letters. 00182 * <p> 00183 * @return (CdcString). 00184 */ 00185 CdcString GetFileExtention () const 00186 { if (pInnerData) return pInnerData->sFileExtention; 00187 else return EMPTY_STR; } 00188 00189 /** 00190 * Returns the description of the type of the file, to send to client 00191 * <p> 00192 * @return (CdcString). 00193 */ 00194 CdcString GetClientFileType () const 00195 { if (pInnerData) return CdcParameters::GetParam ("Server->File-Types->" + 00196 pInnerData->sFileExtention + "->Client-Type"); 00197 else return EMPTY_STR; } 00198 00199 /** 00200 * Returns the file script engine associate with this file type, or empty 00201 * string if none. 00202 */ 00203 CdcString GetScriptEngine () const 00204 { if (pInnerData) return CdcParameters::GetParam ( "Server->File-Types->" + 00205 pInnerData->sFileExtention + "->ScriptEngine"); 00206 else return EMPTY_STR; } 00207 00208 /** 00209 * This function returns the time this file was last modified (GMT). 00210 * <p> 00211 * @return (CdcHttpTime) The last time this file was modified. 00212 * @throws (CdcException) FILE NOT FOUND if it cannot find the file under 00213 * the root directory of the server 00214 */ 00215 CdcHttpTime GetLastModified () const throw (CdcException) 00216 { if (pInnerData) return pInnerData->oFileStatus.GetFileLastModified (true); 00217 else return CdcHttpTime (); } 00218 00219 /** 00220 * Return the size of the file, without opening it. 00221 * <p> 00222 * @return (int) the size of the file in bytes. 00223 * @throws (CdcException) FILE NOT FOUND if it cannot find the file under 00224 * the root directory of the server 00225 */ 00226 int GetFileSize () const throw (CdcException) 00227 { if (pInnerData) return pInnerData->oFileStatus.GetFileSize (); 00228 else return 0; } 00229 }; 00230 00231 #endif /* __CDC_FILE_ATTRIB */