Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe4eeda
commit 23dd6fc
Showing
14 changed files
with
217 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,139 @@ | ||
#include "Config.h" | ||
#include "Logger.h" | ||
#include "Utils.h" | ||
#include <stdio.h> | ||
#include <errno.h> | ||
|
||
void Config::import(const string& filename) | ||
using std::string; | ||
using std::vector; | ||
using std::pair; | ||
|
||
string Config::S_KEY_ACCESS_FILE_PATH = "AccessLogFile"; | ||
string Config::S_KEY_ERROR_FILE_PATH = "ErrorLogFile"; | ||
string Config::S_KEY_RUN_AS_USERNAME = "RunAsUserName"; | ||
string Config::S_KEY_BASE_PATH = "BasePath"; | ||
string Config::S_KEY_GONE_PATH = "GonePath"; | ||
|
||
size_t Config::S_READ_SIZE = 4096; | ||
string Config::S_WHITESPACE = "\r\n\t "; | ||
|
||
Config::Config(const string& filename) : | ||
m_configFile(filename) | ||
{ | ||
import(); | ||
} | ||
|
||
void Config::import() | ||
{ | ||
|
||
FILE* file = fopen(filename.c_str(), "r"); | ||
FILE* file = fopen(m_configFile.c_str(), "r"); | ||
if (file == NULL) | ||
{ | ||
m_logger.logError("Config file read error(" + Utils::llToString(errno) + "): " + strerror(errno)); | ||
return; | ||
} | ||
|
||
char buffer[S_READ_SIZE]; | ||
bool doneReading = false; | ||
size_t remaining = 0; | ||
while (!doneReading) | ||
{ | ||
size_t readSize = S_READ_SIZE - remaining; | ||
size_t bytesRead = fread(buffer + remaining, 1, readSize, file); | ||
if (bytesRead < readSize) | ||
{ | ||
doneReading = true; | ||
} | ||
|
||
// parse what we've read so far. | ||
vector<string> lines; | ||
char* start = buffer; | ||
char* end = buffer; | ||
while (end < (buffer + bytesRead + remaining)) | ||
{ | ||
// Skip to a useful character. | ||
if ((*start == '\n') || (*start == '\r')) | ||
{ | ||
start++; | ||
end++; | ||
continue; | ||
} | ||
|
||
end++; | ||
if ((*end == '\n') || (*end == '\r')) | ||
{ | ||
lines.push_back(string(start, end - start)); | ||
start = end; | ||
} | ||
} | ||
for (size_t i = 0; i < lines.size(); i++) | ||
{ | ||
parseLine(lines[i]); | ||
} | ||
remaining = end - start; | ||
memmove(buffer, start, remaining); | ||
} | ||
// We still have one line in the buffer if it didn't have a terminating newline. | ||
parseLine(string(buffer, remaining)); | ||
|
||
int fileError = ferror(file); | ||
if (fileError) | ||
{ | ||
// Log? | ||
} | ||
fclose(file); | ||
return; | ||
} | ||
|
||
void Config::parseLine(string line) | ||
{ | ||
size_t front = line.find_first_not_of(S_WHITESPACE); | ||
if (front == line.npos) | ||
{ | ||
return; | ||
} | ||
line = line.substr(front); | ||
line = line.substr(0, line.find_first_of("#")); | ||
line = line.substr(0, line.find_last_not_of(S_WHITESPACE) + 1); | ||
if (line.empty()) | ||
{ | ||
return; | ||
} | ||
|
||
// Now leading/trailing whitespace and comments are removed. | ||
|
||
string key; | ||
string value; | ||
size_t ws = line.find_first_of(S_WHITESPACE); | ||
if (ws == line.npos) | ||
{ | ||
key = line; | ||
} | ||
else | ||
{ | ||
key = line.substr(0, ws); | ||
value = line.substr(line.find_first_not_of(S_WHITESPACE, ws)); | ||
} | ||
|
||
if ((key == S_KEY_ACCESS_FILE_PATH) || | ||
(key == S_KEY_ERROR_FILE_PATH) || | ||
(key == S_KEY_RUN_AS_USERNAME) || | ||
(key == S_KEY_BASE_PATH) | ||
) | ||
{ | ||
m_configValues.erase(key); | ||
m_configValues.insert(pair<string, string>(key, value)); | ||
} | ||
else if(key == S_KEY_GONE_PATH) | ||
{ | ||
m_configValues.insert(pair<string, string>(key, value)); | ||
} | ||
} | ||
|
||
string Config::getString(const string& key) const | ||
{ | ||
std::multimap<std::string, std::string>::const_iterator it = m_configValues.find(key); | ||
if (it == m_configValues.end()) | ||
{ | ||
return ""; | ||
} | ||
return it->second; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef CONFIG_H | ||
#define CONFIG_H | ||
#include <string> | ||
#include <vector> | ||
#include <map> | ||
|
||
class Config | ||
{ | ||
|
||
public: | ||
|
||
static std::string S_KEY_ACCESS_FILE_PATH; | ||
static std::string S_KEY_ERROR_FILE_PATH; | ||
static std::string S_KEY_RUN_AS_USERNAME; | ||
static std::string S_KEY_BASE_PATH; | ||
static std::string S_KEY_GONE_PATH; | ||
|
||
Config(const std::string& filename); | ||
|
||
std::string getString(const std::string& key) const; | ||
|
||
private: | ||
void import(); | ||
void parseLine(std::string line); | ||
|
||
static size_t S_READ_SIZE; | ||
static std::string S_WHITESPACE; | ||
|
||
std::string m_configFile; | ||
std::multimap<std::string, std::string> m_configValues; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
#ifndef FILENAME_RESOLVER_H | ||
#define FILENAME_RESOLVER_H | ||
|
||
#include "Config.h" | ||
#include <string> | ||
#include <time.h> | ||
|
||
class FilenameResolver | ||
{ | ||
public: | ||
|
||
// point this at the 'documentRoot' of your website. Do not include a trailing '/' | ||
static const std::string S_BASE_PATH; | ||
|
||
// Based on the path given, attempts to resolve that to a file on disk. If possible, returns a string represnting the | ||
// actual file to read. If not possible, returns an empty string. | ||
static std::string resolve(const std::string& webPath, std::string& redirectPath, time_t* mtime); | ||
static std::string resolve(const std::string& webPath, const Config& config, | ||
std::string& redirectPath, time_t* mtime); | ||
|
||
// Gets a content-type from a filename. Doesn't do any fancy inspection of the actual file, just looks at filename | ||
// extension. | ||
static std::string getContentType(const std::string& filename); | ||
|
||
private: | ||
static std::string resolveDir(const std::string& dirPath, time_t* mtime); | ||
static std::string findNewestInDir(const std::string& webPath); | ||
static std::string resolveDir(const std::string& dirPath, const Config& config, time_t* mtime); | ||
static std::string findNewestInDir(const std::string& webPath, const Config& config); | ||
|
||
std::string m_basePath; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.