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/PionLogger.hpp>
20 #include <pion/PionException.hpp>
21 #include <pion/PionHashMap.hpp>
22 #include <pion/net/WebService.hpp>
23 #include <pion/net/HTTPRequest.hpp>
24 #include <pion/net/HTTPResponseWriter.hpp>
25 #include <pion/net/HTTPServer.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::net::HTTPRequestPtr& request,
149 pion::net::TCPConnectionPtr& tcp_conn,
150 unsigned long max_chunk_size = 0)
152 return boost::shared_ptr<DiskFileSender>(
new DiskFileSender(file, request,
153 tcp_conn, max_chunk_size));
182 pion::net::HTTPRequestPtr& request,
183 pion::net::TCPConnectionPtr& tcp_conn,
184 unsigned long max_chunk_size);
192 void handleWrite(
const boost::system::error_code& write_error,
193 std::size_t bytes_written);
206 pion::net::HTTPResponseWriterPtr 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;
251 :
PionException(
"FileService option is not a directory: ", dir) {}
265 :
PionException(
"FileService option is not a file: ", file) {}
272 :
PionException(
"FileService invalid value for cache option: ", value) {}
279 :
PionException(
"FileService invalid value for scan option: ", value) {}
286 :
PionException(
"FileService invalid value for " + option +
" option: ", value) {}
293 :
PionException(
"FileService unable to read file: ", value) {}
300 :
PionException(
"FileService has an undefined response: ", value) {}
318 virtual void setOption(
const std::string& name,
const std::string& value);
321 virtual void operator()(pion::net::HTTPRequestPtr& request,
322 pion::net::TCPConnectionPtr& tcp_conn);
325 virtual void start(
void);
328 virtual void stop(
void);
340 typedef PION_HASH_MAP<std::string, DiskFile, PION_HASH_STRING >
CacheMap;
343 typedef PION_HASH_MAP<std::string, std::string, PION_HASH_STRING >
MIMETypeMap;
362 std::pair<CacheMap::iterator, bool>
364 const boost::filesystem::path& file_path,
365 const bool placeholder);
373 static std::string
findMIMEType(
const std::string& file_name);
375 void sendNotFoundResponse(pion::net::HTTPRequestPtr& http_request,
376 pion::net::TCPConnectionPtr& tcp_conn);
385 static void createMIMETypes(
void);
389 static const std::string DEFAULT_MIME_TYPE;
392 static const unsigned int DEFAULT_CACHE_SETTING;
395 static const unsigned int DEFAULT_SCAN_SETTING;
398 static const unsigned long DEFAULT_MAX_CACHE_SIZE;
401 static const unsigned long DEFAULT_MAX_CHUNK_SIZE;
404 static boost::once_flag m_mime_types_init_flag;
407 static MIMETypeMap * m_mime_types_ptr;
411 boost::filesystem::path m_directory;
414 boost::filesystem::path m_file;
417 CacheMap m_cache_map;
420 boost::mutex m_cache_mutex;
428 unsigned int m_cache_setting;
437 unsigned int m_scan_setting;
443 unsigned long m_max_cache_size;
450 unsigned long m_max_chunk_size;
virtual void stop(void)
called when the web service's server is stopping
exception thrown if the cache option is set to an invalid value
void scanDirectory(const boost::filesystem::path &dir_path)
DiskFile(void)
default constructor
std::streamsize m_file_size
size of the file's content
exception thrown if an option is set to an invalid value
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
exception thrown if the directory configured is not found
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
exception thrown if the file configured is not found
PionLogger m_logger
primary logging interface used by this class
exception thrown if we are unable to read a file from disk
unsigned long getFileSize(void) const
returns size of the file's content
PionLogger m_logger
primary logging interface used by this class
virtual void setOption(const std::string &name, const std::string &value)
exception thrown if we do not know how to respond (should never happen)
std::time_t getLastModified(void) const
returns timestamp that the cached file was last modified (0 = cache disabled)
DiskFile(const DiskFile &f)
copy constructor
virtual void operator()(pion::net::HTTPRequestPtr &request, pion::net::TCPConnectionPtr &tcp_conn)
handles requests for FileService
PionLogger getLogger(void)
returns the logger currently in use
void setLogger(PionLogger log_ptr)
sets the logger to be used
void resetFileContent(unsigned long n=0)
resets the size of the file content buffer
static boost::shared_ptr< DiskFileSender > create(DiskFile &file, pion::net::HTTPRequestPtr &request, pion::net::TCPConnectionPtr &tcp_conn, unsigned long max_chunk_size=0)
void setLogger(PionLogger log_ptr)
sets the logger to be used
PION_HASH_MAP< std::string, DiskFile, PION_HASH_STRING > CacheMap
data type for map of file names to cache entries
exception thrown if the file configuration option is not a file
boost::filesystem::path m_file_path
path to the cached file
virtual void start(void)
called when the web service's server is starting
std::time_t m_last_modified
timestamp that the cached file was last modified (0 = cache disabled)
exception thrown if the directory configuration option is not a directory
void handleWrite(const boost::system::error_code &write_error, std::size_t bytes_written)
the following enables use of the lock-free cache
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
static std::string findMIMEType(const std::string &file_name)
PionLogger getLogger(void)
returns the logger currently in use
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
exception thrown if the scan option is set to an invalid value
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
DiskFileSender(DiskFile &file, pion::net::HTTPRequestPtr &request, pion::net::TCPConnectionPtr &tcp_conn, unsigned long max_chunk_size)