@@ -26,7 +26,6 @@ ResourceHost::ResourceHost(std::string base) {
2626}
2727
2828ResourceHost::~ResourceHost () {
29- clearCache ();
3029}
3130
3231/* *
@@ -44,14 +43,14 @@ std::string ResourceHost::lookupMimeType(std::string ext) {
4443}
4544
4645/* *
47- * Load File
48- * Read a file from disk and load it into the memory cache
46+ * Read File
47+ * Read a file from disk and return the appropriate Resource object
4948 *
5049 * @param path Full disk path of the file
5150 * @param sb Filled in stat struct
5251 * @return Return's the resource object upon successful load
5352 */
54- Resource* ResourceHost::loadFile (std::string path, struct stat sb) {
53+ Resource* ResourceHost::readFile (std::string path, struct stat sb) {
5554 // Make sure the webserver USER owns the file
5655 if (!(sb.st_mode & S_IRWXU ))
5756 return NULL ;
@@ -84,21 +83,18 @@ Resource* ResourceHost::loadFile(std::string path, struct stat sb) {
8483 res->setMimeType (lookupMimeType (res->getExtension ()));
8584 res->setData (fdata, len);
8685
87- // Insert the resource into the map
88- cacheMap.insert (std::pair<std::string, Resource*>(res->getLocation (), res));
89-
9086 return res;
9187}
9288
9389/* *
94- * Load Directory
95- * Read a directory (list or index) from disk and load it into the memory cache
90+ * Read Directory
91+ * Read a directory (list or index) from disk into a Resource object
9692 *
9793 * @param path Full disk path of the file
9894 * @param sb Filled in stat struct
9995 * @return Return's the resource object upon successful load
10096 */
101- Resource* ResourceHost::loadDirectory (std::string path, struct stat sb) {
97+ Resource* ResourceHost::readDirectory (std::string path, struct stat sb) {
10298 Resource* res = NULL ;
10399 // Make the path end with a / (for consistency) if it doesnt already
104100 if (path.empty () || path[path.length ()-1 ] != ' /' )
@@ -112,15 +108,15 @@ Resource* ResourceHost::loadDirectory(std::string path, struct stat sb) {
112108 loadIndex = path + validIndexes[i];
113109 // Found a suitable index file to load and return to the client
114110 if (stat (loadIndex.c_str (), &sidx) != -1 )
115- return loadFile (loadIndex.c_str (), sidx);
111+ return readFile (loadIndex.c_str (), sidx);
116112 }
117113
118114 // Make sure the webserver USER owns the directory
119115 if (!(sb.st_mode & S_IRWXU ))
120116 return NULL ;
121117
122118 // Generate an HTML directory listing
123- std::string listing = listDirectory (path);
119+ std::string listing = generateDirList (path);
124120
125121 unsigned int slen = listing.length ();
126122 char * sdata = new char [slen];
@@ -129,30 +125,16 @@ Resource* ResourceHost::loadDirectory(std::string path, struct stat sb) {
129125 res = new Resource (path, true );
130126 res->setData ((byte*)sdata, slen);
131127
132- // Cache the listing
133- cacheMap.insert (std::pair<std::string, Resource*>(res->getLocation (), res));
134-
135128 return res;
136129}
137130
138- /* *
139- * Dump and delete all resources in the cache, then clear it out
140- */
141- void ResourceHost::clearCache () {
142- // Cleanup all Resource objects
143- for (auto & x : cacheMap)
144- delete x.second ;
145-
146- cacheMap.clear ();
147- }
148-
149131/* *
150132 * Return an HTML directory listing provided by the relative path dirPath
151133 *
152134 * @param path Full disk path of the file
153135 * @return String representation of the directory. Blank string if invalid directory
154136 */
155- std::string ResourceHost::listDirectory (std::string path) {
137+ std::string ResourceHost::generateDirList (std::string path) {
156138 // Get just the relative uri from the entire path by stripping out the baseDiskPath from the beginning
157139 size_t uri_pos = path.find_last_of (baseDiskPath);
158140 std::string uri = " ?" ;
@@ -192,7 +174,6 @@ std::string ResourceHost::listDirectory(std::string path) {
192174
193175/* *
194176 * Retrieve a resource from the File system
195- * The memory cache will be checked before going out to disk
196177 *
197178 * @param uri The URI sent in the request
198179 * @return NULL if unable to load the resource. Resource object
@@ -203,17 +184,6 @@ Resource* ResourceHost::getResource(std::string uri) {
203184
204185 std::string path = baseDiskPath + uri;
205186 Resource* res = NULL ;
206-
207- // Check the cache first:
208- std::unordered_map<std::string, Resource*>::const_iterator it;
209- it = cacheMap.find (path);
210- // If it isn't the element past the end (end()), then a resource was found
211- if (it != cacheMap.end ()) {
212- res = it->second ;
213- return res;
214- }
215-
216- // Not in cache, check the disk
217187
218188 // Gather info about the resource with stat: determine if it's a directory or file, check if its owned by group/user, modify times
219189 struct stat sb;
@@ -222,11 +192,11 @@ Resource* ResourceHost::getResource(std::string uri) {
222192
223193 // Determine file type
224194 if (sb.st_mode & S_IFDIR ) { // Directory
225- // Load a directory list or index into memory from FS
226- res = loadDirectory (path, sb);
195+ // Read a directory list or index into memory from FS
196+ res = readDirectory (path, sb);
227197 } else if (sb.st_mode & S_IFREG ) { // Regular file
228198 // Attempt to load the file into memory from the FS
229- res = loadFile (path, sb);
199+ res = readFile (path, sb);
230200 } else { // Something else..device, socket, symlink
231201 return NULL ;
232202 }
0 commit comments