FileCache.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef HW_FILE_CACHE_H_
  2. #define HW_FILE_CACHE_H_
  3. #include <map>
  4. #include <string>
  5. #include "hbuf.h"
  6. #include "hfile.h"
  7. #include "hstring.h"
  8. #include "hscope.h"
  9. #include "hdir.h"
  10. #include "md5.h"
  11. #include "HttpRequest.h" // for get_content_type_str_by_suffix
  12. #include "http_page.h"
  13. #define HTTP_HEADER_MAX_LENGTH 1024 // 1k
  14. typedef struct file_cache_s {
  15. //std::string filepath;
  16. struct stat st;
  17. time_t open_time;
  18. time_t stat_time;
  19. uint32_t stat_cnt;
  20. HBuf buf; // http_header + file_content
  21. hbuf_t filebuf;
  22. hbuf_t httpbuf;
  23. char last_modified[64];
  24. char etag[64];
  25. const char* content_type;
  26. file_cache_s() {
  27. stat_cnt = 0;
  28. content_type = NULL;
  29. }
  30. void resize_buf(int filesize) {
  31. buf.resize(HTTP_HEADER_MAX_LENGTH + filesize);
  32. filebuf.base = buf.base + HTTP_HEADER_MAX_LENGTH;
  33. filebuf.len = filesize;
  34. }
  35. void prepend_header(const char* header, int len) {
  36. if (len > HTTP_HEADER_MAX_LENGTH) return;
  37. httpbuf.base = filebuf.base - len;
  38. httpbuf.len = len + filebuf.len;
  39. memcpy(httpbuf.base, header, len);
  40. }
  41. } file_cache_t;
  42. // filepath => file_cache_t
  43. typedef std::map<std::string, file_cache_t*> FileCacheMap;
  44. #define DEFAULT_FILE_STAT_INTERVAL 10 // s
  45. #define DEFAULT_FILE_CACHED_TIME 60 // s
  46. class FileCache {
  47. public:
  48. int file_stat_interval;
  49. int file_cached_time;
  50. FileCacheMap cached_files;
  51. FileCache() {
  52. file_stat_interval = DEFAULT_FILE_STAT_INTERVAL;
  53. file_cached_time = DEFAULT_FILE_CACHED_TIME;
  54. }
  55. ~FileCache() {
  56. for (auto& pair : cached_files) {
  57. delete pair.second;
  58. }
  59. cached_files.clear();
  60. }
  61. file_cache_t* Open(const char* filepath, void* ctx) {
  62. file_cache_t* fc = Get(filepath);
  63. bool modified = false;
  64. if (fc) {
  65. time_t tt;
  66. time(&tt);
  67. if (tt - fc->stat_time > file_stat_interval) {
  68. time_t mtime = fc->st.st_mtime;
  69. stat(filepath, &fc->st);
  70. fc->stat_time = tt;
  71. fc->stat_cnt++;
  72. if (mtime != fc->st.st_mtime) {
  73. modified = true;
  74. fc->stat_cnt = 1;
  75. }
  76. }
  77. }
  78. if (fc == NULL || modified) {
  79. int fd = open(filepath, O_RDONLY);
  80. if (fd < 0) {
  81. return NULL;
  82. }
  83. ScopeCleanup _(close, fd);
  84. if (fc == NULL) {
  85. struct stat st;
  86. fstat(fd, &st);
  87. if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)) {
  88. return NULL;
  89. }
  90. fc = new file_cache_t;
  91. //fc->filepath = filepath;
  92. fc->st = st;
  93. time(&fc->open_time);
  94. fc->stat_time = fc->open_time;
  95. fc->stat_cnt = 1;
  96. cached_files[filepath] = fc;
  97. }
  98. if (S_ISREG(fc->st.st_mode)) {
  99. // FILE
  100. fc->resize_buf(fc->st.st_size);
  101. read(fd, fc->filebuf.base, fc->filebuf.len);
  102. const char* suffix = strrchr(filepath, '.');
  103. if (suffix) {
  104. fc->content_type = http_content_type_str_by_suffix(++suffix);
  105. }
  106. }
  107. else if (S_ISDIR(fc->st.st_mode)) {
  108. // DIR
  109. std::string page;
  110. make_index_of_page(filepath, page, (const char*)ctx);
  111. fc->resize_buf(page.size());
  112. memcpy(fc->filebuf.base, page.c_str(), page.size());
  113. fc->content_type = http_content_type_str(TEXT_HTML);
  114. }
  115. time_t tt = fc->st.st_mtime;
  116. strftime(fc->last_modified, sizeof(fc->last_modified), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&tt));
  117. MD5_CTX md5_ctx;
  118. MD5Init(&md5_ctx);
  119. MD5Update(&md5_ctx, (unsigned char*)fc->filebuf.base, fc->filebuf.len);
  120. unsigned char digital[16];
  121. MD5Final(digital, &md5_ctx);
  122. char* md5 = fc->etag;
  123. for (int i = 0; i < 16; ++i) {
  124. sprintf(md5, "%02x", digital[i]);
  125. md5 += 2;
  126. }
  127. fc->etag[32] = '\0';
  128. }
  129. return fc;
  130. }
  131. int Close(const char* filepath) {
  132. auto iter = cached_files.find(filepath);
  133. if (iter != cached_files.end()) {
  134. delete iter->second;
  135. iter = cached_files.erase(iter);
  136. return 0;
  137. }
  138. return -1;
  139. }
  140. protected:
  141. file_cache_t* Get(const char* filepath) {
  142. auto iter = cached_files.find(filepath);
  143. if (iter != cached_files.end()) {
  144. return iter->second;
  145. }
  146. return NULL;
  147. }
  148. };
  149. #endif // HW_FILE_CACHE_H_