10 #ifndef __PION_FILESERVICE_HEADER__
11 #define __PION_FILESERVICE_HEADER__
13 #include <boost/shared_ptr.hpp>
14 #include <boost/functional/hash.hpp>
15 #include <boost/filesystem/path.hpp>
16 #include <boost/thread/once.hpp>
17 #include <boost/thread/mutex.hpp>
18 #include <boost/shared_array.hpp>
19 #include <pion/config.hpp>
20 #include <pion/logger.hpp>
21 #include <pion/hash_map.hpp>
22 #include <pion/http/plugin_service.hpp>
23 #include <pion/http/request.hpp>
24 #include <pion/http/response_writer.hpp>
25 #include <pion/http/server.hpp>
45 char *content,
unsigned long size,
46 std::time_t modified,
const std::string& mime)
134 public boost::enable_shared_from_this<DiskFileSender>,
135 private boost::noncopyable
146 static inline boost::shared_ptr<DiskFileSender>
148 pion::http::request_ptr& http_request_ptr,
149 pion::tcp::connection_ptr& tcp_conn,
150 unsigned long max_chunk_size = 0)
152 return boost::shared_ptr<DiskFileSender>(
new DiskFileSender(file, http_request_ptr,
153 tcp_conn, max_chunk_size));
182 pion::http::request_ptr& http_request_ptr,
183 pion::tcp::connection_ptr& tcp_conn,
184 unsigned long max_chunk_size);
192 void handle_write(
const boost::system::error_code& write_error,
193 std::size_t bytes_written);
206 pion::http::response_writer_ptr m_writer;
209 boost::filesystem::ifstream m_file_stream;
212 boost::shared_array<char> m_content_buf;
219 unsigned long m_max_chunk_size;
222 unsigned long m_file_bytes_to_send;
225 unsigned long m_bytes_sent;
229 typedef boost::shared_ptr<DiskFileSender> DiskFileSenderPtr;
254 virtual void set_option(
const std::string& name,
const std::string& value);
257 virtual void operator()(pion::http::request_ptr& http_request_ptr,
258 pion::tcp::connection_ptr& tcp_conn);
261 virtual void start(
void);
264 virtual void stop(
void);
276 typedef PION_HASH_MAP<std::string, DiskFile, PION_HASH_STRING >
CacheMap;
279 typedef PION_HASH_MAP<std::string, std::string, PION_HASH_STRING >
MIMETypeMap;
298 std::pair<CacheMap::iterator, bool>
300 const boost::filesystem::path& file_path,
301 const bool placeholder);
309 static std::string
findMIMEType(
const std::string& file_name);
311 void sendNotFoundResponse(pion::http::request_ptr& http_request_ptr,
312 pion::tcp::connection_ptr& tcp_conn);
321 static void createMIMETypes(
void);
325 static const std::string DEFAULT_MIME_TYPE;
328 static const unsigned int DEFAULT_CACHE_SETTING;
331 static const unsigned int DEFAULT_SCAN_SETTING;
334 static const unsigned long DEFAULT_MAX_CACHE_SIZE;
337 static const unsigned long DEFAULT_MAX_CHUNK_SIZE;
340 static boost::once_flag m_mime_types_init_flag;
343 static MIMETypeMap * m_mime_types_ptr;
347 boost::filesystem::path m_directory;
350 boost::filesystem::path m_file;
353 CacheMap m_cache_map;
356 boost::mutex m_cache_mutex;
364 unsigned int m_cache_setting;
373 unsigned int m_scan_setting;
379 unsigned long m_max_cache_size;
386 unsigned long m_max_chunk_size;
virtual void stop(void)
called when the web service's server is stopping
void scanDirectory(const boost::filesystem::path &dir_path)
DiskFile(void)
default constructor
std::streamsize m_file_size
size of the file's content
DiskFileSender(DiskFile &file, pion::http::request_ptr &http_request_ptr, pion::tcp::connection_ptr &tcp_conn, unsigned long max_chunk_size)
const boost::filesystem::path & getFilePath(void) const
return path to the cached file
std::pair< CacheMap::iterator, bool > addCacheEntry(const std::string &relative_path, const boost::filesystem::path &file_path, const bool placeholder)
virtual ~DiskFileSender()
default virtual destructor
void setFilePath(const boost::filesystem::path &p)
sets the path to the cached file
void setMimeType(const std::string &t)
sets the mime type for the cached file
std::string m_last_modified_string
timestamp that the cached file was last modified (string format)
void read(void)
reads content from disk into file_content buffer (may throw)
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
void update(void)
updates the file_size and last_modified timestamp to disk
virtual void set_option(const std::string &name, const std::string &value)
void handle_write(const boost::system::error_code &write_error, std::size_t bytes_written)
unsigned long getFileSize(void) const
returns size of the file's content
std::time_t getLastModified(void) const
returns timestamp that the cached file was last modified (0 = cache disabled)
DiskFile(const DiskFile &f)
copy constructor
void set_logger(logger log_ptr)
sets the logger to be used
logger m_logger
primary logging interface used by this class
logger m_logger
primary logging interface used by this class
void resetFileContent(unsigned long n=0)
resets the size of the file content buffer
logger get_logger(void)
returns the logger currently in use
PION_HASH_MAP< std::string, DiskFile, PION_HASH_STRING > CacheMap
data type for map of file names to cache entries
boost::filesystem::path m_file_path
path to the cached file
virtual void start(void)
called when the web service's server is starting
void set_logger(logger log_ptr)
sets the logger to be used
std::time_t m_last_modified
timestamp that the cached file was last modified (0 = cache disabled)
logger get_logger(void)
returns the logger currently in use
const std::string & getMimeType(void) const
returns mime type for the cached file
const std::string & getLastModifiedString(void) const
returns timestamp that the cached file was last modified (string format)
char * getFileContent(void)
returns content of the cached file
virtual void operator()(pion::http::request_ptr &http_request_ptr, pion::tcp::connection_ptr &tcp_conn)
handles requests for FileService
static std::string findMIMEType(const std::string &file_name)
static boost::shared_ptr< DiskFileSender > create(DiskFile &file, pion::http::request_ptr &http_request_ptr, pion::tcp::connection_ptr &tcp_conn, unsigned long max_chunk_size=0)
void appendFilePath(const std::string &p)
appends to the path of the cached file
boost::shared_array< char > m_file_content
content of the cached file
PION_HASH_MAP< std::string, std::string, PION_HASH_STRING > MIMETypeMap
data type for map of file extensions to MIME types
bool hasFileContent(void) const
returns true if there is cached file content
std::string m_mime_type
mime type for the cached file