1
0

FileCache.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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) ||
  88. (S_ISDIR(st.st_mode) &&
  89. filepath[strlen(filepath)-1] == '/')) {
  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. else {
  99. return NULL;
  100. }
  101. }
  102. if (S_ISREG(fc->st.st_mode)) {
  103. // FILE
  104. fc->resize_buf(fc->st.st_size);
  105. read(fd, fc->filebuf.base, fc->filebuf.len);
  106. const char* suffix = strrchr(filepath, '.');
  107. if (suffix) {
  108. fc->content_type = http_content_type_str_by_suffix(++suffix);
  109. }
  110. }
  111. else if (S_ISDIR(fc->st.st_mode)) {
  112. // DIR
  113. std::string page;
  114. make_index_of_page(filepath, page, (const char*)ctx);
  115. fc->resize_buf(page.size());
  116. memcpy(fc->filebuf.base, page.c_str(), page.size());
  117. fc->content_type = http_content_type_str(TEXT_HTML);
  118. }
  119. time_t tt = fc->st.st_mtime;
  120. strftime(fc->last_modified, sizeof(fc->last_modified), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&tt));
  121. MD5_CTX md5_ctx;
  122. MD5Init(&md5_ctx);
  123. MD5Update(&md5_ctx, (unsigned char*)fc->filebuf.base, fc->filebuf.len);
  124. unsigned char digital[16];
  125. MD5Final(digital, &md5_ctx);
  126. char* md5 = fc->etag;
  127. for (int i = 0; i < 16; ++i) {
  128. sprintf(md5, "%02x", digital[i]);
  129. md5 += 2;
  130. }
  131. fc->etag[32] = '\0';
  132. }
  133. return fc;
  134. }
  135. int Close(const char* filepath) {
  136. auto iter = cached_files.find(filepath);
  137. if (iter != cached_files.end()) {
  138. delete iter->second;
  139. iter = cached_files.erase(iter);
  140. return 0;
  141. }
  142. return -1;
  143. }
  144. protected:
  145. file_cache_t* Get(const char* filepath) {
  146. auto iter = cached_files.find(filepath);
  147. if (iter != cached_files.end()) {
  148. return iter->second;
  149. }
  150. return NULL;
  151. }
  152. };
  153. #endif // HW_FILE_CACHE_H_