file_service.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef _RPC_FILE_SERVICE_H_
  2. #define _RPC_FILE_SERVICE_H_
  3. #include <sys/stat.h>
  4. #include <string>
  5. #include <unordered_map>
  6. #include "workflow/Workflow.h"
  7. #include "workflow/HttpMessage.h"
  8. #include "workflow/HttpUtil.h"
  9. #include "workflow/WFHttpServer.h"
  10. #include "workflow/WFFacilities.h"
  11. // This is a simple exmaple for file service
  12. class FileService
  13. {
  14. public:
  15. using ErrorPageMap = std::unordered_map<int, std::string>;
  16. void process(WFHttpTask *server_task);
  17. void pread_callback(WFFileIOTask *task);
  18. protected:
  19. struct ModuleCtx
  20. {
  21. protocol::HttpResponse *resp;
  22. void *buf;
  23. bool is_error_set;
  24. ModuleCtx(protocol::HttpResponse * resp) :
  25. resp(resp),
  26. buf(NULL),
  27. is_error_set(false)
  28. {
  29. }
  30. };
  31. private:
  32. WFModuleTask *create_module(WFHttpTask *task, const std::string& path);
  33. SubTask *create_file_task(const std::string& path, struct ModuleCtx *ctx);
  34. SubTask *create_error_task(int code, struct ModuleCtx *ctx);
  35. public:
  36. FileService(std::string root, const ErrorPageMap& error_page) :
  37. root(std::move(root)),
  38. error_page(error_page)
  39. {
  40. if (this->root.empty())
  41. root = "./";
  42. else if (this->root.at(this->root.length() - 1) != '/')
  43. root += "/";
  44. }
  45. private:
  46. std::string root;
  47. const ErrorPageMap& error_page;
  48. };
  49. #endif