FileService.hpp
// ---------------------------------------------------------------------
// pion: a Boost C++ framework for building lightweight HTTP interfaces
// ---------------------------------------------------------------------
// Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//
#ifndef __PION_FILESERVICE_HEADER__
#define __PION_FILESERVICE_HEADER__
#include <boost/shared_ptr.hpp>
#include <boost/functional/hash.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/thread/once.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/shared_array.hpp>
#include <pion/config.hpp>
#include <pion/logger.hpp>
#include <pion/hash_map.hpp>
#include <pion/http/plugin_service.hpp>
#include <pion/http/request.hpp>
#include <pion/http/response_writer.hpp>
#include <pion/http/server.hpp>
#include <string>
#include <map>
namespace pion { // begin namespace pion
namespace plugins { // begin namespace plugins
class DiskFile {
public:
DiskFile(void)
: m_file_size(0), m_last_modified(0) {}
DiskFile(const boost::filesystem::path& path,
char *content, unsigned long size,
std::time_t modified, const std::string& mime)
: m_file_path(path), m_file_content(content), m_file_size(size),
m_last_modified(modified), m_mime_type(mime)
{}
DiskFile(const DiskFile& f)
: m_file_path(f.m_file_path), m_file_content(f.m_file_content),
m_file_size(f.m_file_size), m_last_modified(f.m_last_modified),
m_last_modified_string(f.m_last_modified_string), m_mime_type(f.m_mime_type)
{}
void update(void);
void read(void);
bool checkUpdated(void);
inline const boost::filesystem::path& getFilePath(void) const { return m_file_path; }
inline char *getFileContent(void) { return m_file_content.get(); }
inline bool hasFileContent(void) const { return static_cast<bool>(m_file_content); }
inline unsigned long getFileSize(void) const { return m_file_size; }
inline std::time_t getLastModified(void) const { return m_last_modified; }
inline const std::string& getLastModifiedString(void) const { return m_last_modified_string; }
inline const std::string& getMimeType(void) const { return m_mime_type; }
inline void setFilePath(const boost::filesystem::path& p) { m_file_path = p; }
inline void appendFilePath(const std::string& p) { m_file_path /= p; }
inline void setMimeType(const std::string& t) { m_mime_type = t; }
inline void resetFileContent(unsigned long n = 0) {
if (n == 0) m_file_content.reset();
else m_file_content.reset(new char[n]);
}
protected:
boost::filesystem::path m_file_path;
boost::shared_array<char> m_file_content;
std::streamsize m_file_size;
std::time_t m_last_modified;
std::string m_last_modified_string;
std::string m_mime_type;
};
class DiskFileSender :
public boost::enable_shared_from_this<DiskFileSender>,
private boost::noncopyable
{
public:
static inline boost::shared_ptr<DiskFileSender>
create(DiskFile& file,
const pion::http::request_ptr& http_request_ptr,
const pion::tcp::connection_ptr& tcp_conn,
unsigned long max_chunk_size = 0)
{
return boost::shared_ptr<DiskFileSender>(new DiskFileSender(file, http_request_ptr,
tcp_conn, max_chunk_size));
}
virtual ~DiskFileSender() {}
void send(void);
inline void set_logger(logger log_ptr) { m_logger = log_ptr; }
inline logger get_logger(void) { return m_logger; }
protected:
DiskFileSender(DiskFile& file,
const pion::http::request_ptr& http_request_ptr,
const pion::tcp::connection_ptr& tcp_conn,
unsigned long max_chunk_size);
void handle_write(const boost::system::error_code& write_error,
std::size_t bytes_written);
logger m_logger;
private:
DiskFile m_disk_file;
pion::http::response_writer_ptr m_writer;
boost::filesystem::ifstream m_file_stream;
boost::shared_array<char> m_content_buf;
unsigned long m_max_chunk_size;
unsigned long m_file_bytes_to_send;
unsigned long m_bytes_sent;
};
typedef boost::shared_ptr<DiskFileSender> DiskFileSenderPtr;
class FileService :
public pion::http::plugin_service
{
public:
// default constructor and destructor
FileService(void);
virtual ~FileService() {}
virtual void set_option(const std::string& name, const std::string& value);
virtual void operator()(const pion::http::request_ptr& http_request_ptr,
const pion::tcp::connection_ptr& tcp_conn);
virtual void start(void);
virtual void stop(void);
inline void set_logger(logger log_ptr) { m_logger = log_ptr; }
inline logger get_logger(void) { return m_logger; }
protected:
typedef PION_HASH_MAP<std::string, DiskFile, PION_HASH_STRING > CacheMap;
typedef PION_HASH_MAP<std::string, std::string, PION_HASH_STRING > MIMETypeMap;
void scanDirectory(const boost::filesystem::path& dir_path);
std::pair<CacheMap::iterator, bool>
addCacheEntry(const std::string& relative_path,
const boost::filesystem::path& file_path,
const bool placeholder);
static std::string findMIMEType(const std::string& file_name);
void sendNotFoundResponse(const pion::http::request_ptr& http_request_ptr,
const pion::tcp::connection_ptr& tcp_conn);
logger m_logger;
private:
static void createMIMETypes(void);
static const std::string DEFAULT_MIME_TYPE;
static const unsigned int DEFAULT_CACHE_SETTING;
static const unsigned int DEFAULT_SCAN_SETTING;
static const unsigned long DEFAULT_MAX_CACHE_SIZE;
static const unsigned long DEFAULT_MAX_CHUNK_SIZE;
static boost::once_flag m_mime_types_init_flag;
static MIMETypeMap * m_mime_types_ptr;
boost::filesystem::path m_directory;
boost::filesystem::path m_file;
CacheMap m_cache_map;
boost::mutex m_cache_mutex;
unsigned int m_cache_setting;
unsigned int m_scan_setting;
unsigned long m_max_cache_size;
unsigned long m_max_chunk_size;
bool m_writable;
};
} // end namespace plugins
} // end namespace pion
#endif
pion::http::plugin_service
Definition: plugin_service.hpp:29
pion::plugins::DiskFile::DiskFile
DiskFile(void)
default constructor
Definition: FileService.hpp:40
pion::plugins::DiskFile::m_file_size
std::streamsize m_file_size
size of the file&#39;s content
Definition: FileService.hpp:117
pion::plugins::DiskFileSender::~DiskFileSender
virtual ~DiskFileSender()
default virtual destructor
Definition: FileService.hpp:157
pion::plugins::DiskFile::setFilePath
void setFilePath(const boost::filesystem::path &p)
sets the path to the cached file
Definition: FileService.hpp:93
pion::plugins::DiskFile::setMimeType
void setMimeType(const std::string &t)
sets the mime type for the cached file
Definition: FileService.hpp:99
pion::plugins::DiskFile::m_last_modified_string
std::string m_last_modified_string
timestamp that the cached file was last modified (string format)
Definition: FileService.hpp:123
pion::plugins::DiskFile::read
void read(void)
reads content from disk into file_content buffer (may throw)
Definition: FileService.cpp:767
pion::plugins::DiskFile::DiskFile
DiskFile(const boost::filesystem::path &path, char *content, unsigned long size, std::time_t modified, const std::string &mime)
used to construct new disk file objects
Definition: FileService.hpp:44
pion::plugins::DiskFile::update
void update(void)
updates the file_size and last_modified timestamp to disk
Definition: FileService.cpp:759
pion::plugins::DiskFile::getLastModified
std::time_t getLastModified(void) const
returns timestamp that the cached file was last modified (0 = cache disabled)
Definition: FileService.hpp:84
pion::plugins::DiskFile::getFilePath
const boost::filesystem::path & getFilePath(void) const
return path to the cached file
Definition: FileService.hpp:72
pion::plugins::DiskFileSender
Definition: FileService.hpp:133
pion::plugins::DiskFile::getLastModifiedString
const std::string & getLastModifiedString(void) const
returns timestamp that the cached file was last modified (string format)
Definition: FileService.hpp:87
pion::plugins::DiskFile::DiskFile
DiskFile(const DiskFile &f)
copy constructor
Definition: FileService.hpp:52
pion::plugins::DiskFile::getMimeType
const std::string & getMimeType(void) const
returns mime type for the cached file
Definition: FileService.hpp:90
pion::plugins::DiskFileSender::set_logger
void set_logger(logger log_ptr)
sets the logger to be used
Definition: FileService.hpp:165
pion::plugins::DiskFileSender::m_logger
logger m_logger
primary logging interface used by this class
Definition: FileService.hpp:197
pion::plugins::FileService::m_logger
logger m_logger
primary logging interface used by this class
Definition: FileService.hpp:315
pion::plugins::FileService
Definition: FileService.hpp:235
pion::plugins::DiskFile::resetFileContent
void resetFileContent(unsigned long n=0)
resets the size of the file content buffer
Definition: FileService.hpp:102
pion::plugins::DiskFile::getFileSize
unsigned long getFileSize(void) const
returns size of the file&#39;s content
Definition: FileService.hpp:81
pion::plugins::DiskFileSender::get_logger
logger get_logger(void)
returns the logger currently in use
Definition: FileService.hpp:168
pion::logger
Definition: logger.hpp:238
pion::plugins::FileService::CacheMap
PION_HASH_MAP< std::string, DiskFile, PION_HASH_STRING > CacheMap
data type for map of file names to cache entries
Definition: FileService.hpp:276
pion::plugins::DiskFile::m_file_path
boost::filesystem::path m_file_path
path to the cached file
Definition: FileService.hpp:111
pion::plugins::FileService::set_logger
void set_logger(logger log_ptr)
sets the logger to be used
Definition: FileService.hpp:267
pion::plugins::DiskFile::m_last_modified
std::time_t m_last_modified
timestamp that the cached file was last modified (0 = cache disabled)
Definition: FileService.hpp:120
pion
Definition: admin_rights.hpp:19
pion::plugins::FileService::get_logger
logger get_logger(void)
returns the logger currently in use
Definition: FileService.hpp:270
pion::plugins::DiskFile::hasFileContent
bool hasFileContent(void) const
returns true if there is cached file content
Definition: FileService.hpp:78
pion::plugins::DiskFileSender::create
static boost::shared_ptr< DiskFileSender > create(DiskFile &file, const pion::http::request_ptr &http_request_ptr, const pion::tcp::connection_ptr &tcp_conn, unsigned long max_chunk_size=0)
Definition: FileService.hpp:147
pion::plugins::DiskFile::getFileContent
char * getFileContent(void)
returns content of the cached file
Definition: FileService.hpp:75
pion::plugins::DiskFile::appendFilePath
void appendFilePath(const std::string &p)
appends to the path of the cached file
Definition: FileService.hpp:96
pion::plugins::DiskFile::m_file_content
boost::shared_array< char > m_file_content
content of the cached file
Definition: FileService.hpp:114
pion::plugins::FileService::MIMETypeMap
PION_HASH_MAP< std::string, std::string, PION_HASH_STRING > MIMETypeMap
data type for map of file extensions to MIME types
Definition: FileService.hpp:279
pion::plugins::DiskFile
Definition: FileService.hpp:37
pion::plugins::DiskFile::checkUpdated
bool checkUpdated(void)
Definition: FileService.cpp:787
pion::plugins::DiskFile::m_mime_type
std::string m_mime_type
mime type for the cached file
Definition: FileService.hpp:126