Skip to content

Commit b6d54e4

Browse files
committed
Removed application level file caching in prepartion for convertion to memory mapped io
1 parent 75c0137 commit b6d54e4

9 files changed

Lines changed: 36 additions & 82 deletions

File tree

‎src/Client.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
limitations under the License.
1717
*/
1818

19-
#ifndef CLIENT_H_
20-
#define CLIENT_H_
19+
#ifndef _CLIENT_H_
20+
#define _CLIENT_H_
2121

2222
#include <netinet/in.h>
2323
#include <arpa/inet.h>

‎src/HTTPMessage.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
limitations under the License.
1717
*/
1818

19-
#ifndef _HTTPMessage_h
20-
#define _HTTPMessage_h
19+
#ifndef _HTTPMESSAGE_H_
20+
#define _HTTPMESSAGE_H_
2121

2222
#include <map>
2323
#include <string>

‎src/HTTPRequest.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
limitations under the License.
1717
*/
1818

19-
#ifndef _HTTPRequest_h
20-
#define _HTTPRequest_h
19+
#ifndef _HTTPREQUEST_H_
20+
#define _HTTPREQUEST_H_
2121

2222
#include "HTTPMessage.h"
2323

‎src/HTTPServer.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ void HTTPServer::handleGet(Client* cl, HTTPRequest* req, ResourceHost* resHost)
380380

381381
sendResponse(cl, res, dc);
382382
delete res;
383+
delete r;
383384
} else { // Not found
384385
sendStatusResponse(cl, Status(NOT_FOUND));
385386
}

‎src/HTTPServer.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
limitations under the License.
1717
*/
1818

19-
#ifndef _HTTPSERVER_H
20-
#define _HTTPSERVER_H
19+
#ifndef _HTTPSERVER_H_
20+
#define _HTTPSERVER_H_
2121

2222
#include <unordered_map>
2323
#include <vector>

‎src/Resource.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ Resource::Resource(std::string loc, bool dir) {
2222
location = loc;
2323
directory = dir;
2424
mimeType = "";
25-
encoding = "";
26-
language = "";
27-
md5 = "";
2825
size = 0;
2926
data = NULL;
3027
}
3128

3229
Resource::~Resource() {
33-
if(data != NULL)
30+
if(data != NULL) {
3431
delete data;
32+
data = NULL;
33+
}
3534
}
35+

‎src/Resource.h‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
limitations under the License.
1717
*/
1818

19-
#ifndef _RESOURCE_H
20-
#define _RESOURCE_H
19+
#ifndef _RESOURCE_H_
20+
#define _RESOURCE_H_
2121

2222
#include <string>
2323

@@ -29,10 +29,7 @@ class Resource {
2929
byte* data; // File data
3030
unsigned int size;
3131
std::string mimeType;
32-
std::string encoding;
33-
std::string language;
3432
std::string location; // Disk path location within the server
35-
std::string md5;
3633
bool directory;
3734

3835
public:
@@ -56,14 +53,6 @@ class Resource {
5653
return mimeType;
5754
}
5855

59-
std::string getEncoding() {
60-
return encoding;
61-
}
62-
63-
std::string getLanguage() {
64-
return language;
65-
}
66-
6756
std::string getLocation() {
6857
return location;
6958
}

‎src/ResourceHost.cpp‎

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ ResourceHost::ResourceHost(std::string base) {
2626
}
2727

2828
ResourceHost::~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
}

‎src/ResourceHost.h‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,28 @@ class ResourceHost {
4848
#define STR_PAIR(K,V) std::pair<std::string, std::string>(K,V)
4949
#include "MimeTypes.inc"
5050
};
51-
52-
// Map to track resources only in the memory cache (not on disk)
53-
std::unordered_map<std::string, Resource*> cacheMap;
5451

5552
private:
5653
// Returns a MIME type string given an extension
5754
std::string lookupMimeType(std::string ext);
5855

59-
// Reset's the cache by deleteing all resources from memory and clearing the map
60-
void clearCache();
61-
62-
// Loads a file from the FS into the cache as a Resource
63-
Resource* loadFile(std::string path, struct stat sb);
56+
// Read a file from the FS and into a Resource object
57+
Resource* readFile(std::string path, struct stat sb);
6458

65-
// Loads a directory list or index from FS into the cache
66-
Resource* loadDirectory(std::string path, struct stat sb);
59+
// Reads a directory list or index from FS into a Resource object
60+
Resource* readDirectory(std::string path, struct stat sb);
61+
62+
// Provide a string rep of the directory listing based on URI
63+
std::string generateDirList(std::string dirPath);
6764

6865
public:
6966
ResourceHost(std::string base);
7067
~ResourceHost();
71-
72-
// Provide a string rep of the directory listing
73-
std::string listDirectory(std::string dirPath);
7468

75-
// Write a resource to the cache and file system
69+
// Write a resource to the file system
7670
void putResource(Resource* res, bool writeToDisk);
7771

78-
// Returns a Resource in the cache or file system if it exists
72+
// Returns a Resource based on URI
7973
Resource* getResource(std::string uri);
8074
};
8175

0 commit comments

Comments
 (0)