handler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #ifndef HV_HTTPD_HANDLER_H
  2. #define HV_HTTPD_HANDLER_H
  3. #include <thread> // import std::thread
  4. #include <chrono> // import std::chrono
  5. #include "hbase.h"
  6. #include "htime.h"
  7. #include "hfile.h"
  8. #include "hstring.h"
  9. #include "EventLoop.h" // import setTimeout, setInterval
  10. #include "HttpService.h"
  11. class Handler {
  12. public:
  13. // preprocessor => api_handlers => postprocessor
  14. static int preprocessor(HttpRequest* req, HttpResponse* resp) {
  15. // printf("%s:%d\n", req->client_addr.ip.c_str(), req->client_addr.port);
  16. // printf("%s\n", req->Dump(true, true).c_str());
  17. // if (req->content_type != APPLICATION_JSON) {
  18. // return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  19. // }
  20. req->ParseBody();
  21. resp->content_type = APPLICATION_JSON;
  22. // cors
  23. resp->headers["Access-Control-Allow-Origin"] = "*";
  24. if (req->method == HTTP_OPTIONS) {
  25. resp->headers["Access-Control-Allow-Origin"] = req->GetHeader("Origin", "*");
  26. resp->headers["Access-Control-Allow-Methods"] = req->GetHeader("Access-Control-Request-Method", "OPTIONS, HEAD, GET, POST, PUT, DELETE, PATCH");
  27. resp->headers["Access-Control-Allow-Headers"] = req->GetHeader("Access-Control-Request-Headers", "Content-Type");
  28. return HTTP_STATUS_NO_CONTENT;
  29. }
  30. #if 0
  31. // authentication sample code
  32. if (strcmp(req->path.c_str(), "/login") != 0) {
  33. string token = req->GetHeader("token");
  34. if (token.empty()) {
  35. response_status(resp, 10011, "Miss token");
  36. return HTTP_STATUS_UNAUTHORIZED;
  37. }
  38. else if (strcmp(token.c_str(), "abcdefg") != 0) {
  39. response_status(resp, 10012, "Token wrong");
  40. return HTTP_STATUS_UNAUTHORIZED;
  41. }
  42. return HTTP_STATUS_UNFINISHED;
  43. }
  44. #endif
  45. return HTTP_STATUS_UNFINISHED;
  46. }
  47. static int postprocessor(HttpRequest* req, HttpResponse* resp) {
  48. // printf("%s\n", resp->Dump(true, true).c_str());
  49. return resp->status_code;
  50. }
  51. static int errorHandler(const HttpContextPtr& ctx) {
  52. int error_code = ctx->response->status_code;
  53. return response_status(ctx, error_code);
  54. }
  55. static int largeFileHandler(const HttpContextPtr& ctx) {
  56. std::thread([ctx](){
  57. ctx->writer->Begin();
  58. std::string filepath = ctx->service->document_root + ctx->request->Path();
  59. HFile file;
  60. if (file.open(filepath.c_str(), "rb") != 0) {
  61. ctx->writer->WriteStatus(HTTP_STATUS_NOT_FOUND);
  62. ctx->writer->WriteHeader("Content-Type", "text/html");
  63. ctx->writer->WriteBody("<center><h1>404 Not Found</h1></center>");
  64. ctx->writer->End();
  65. return;
  66. }
  67. http_content_type content_type = CONTENT_TYPE_NONE;
  68. const char* suffix = hv_suffixname(filepath.c_str());
  69. if (suffix) {
  70. content_type = http_content_type_enum_by_suffix(suffix);
  71. }
  72. if (content_type == CONTENT_TYPE_NONE || content_type == CONTENT_TYPE_UNDEFINED) {
  73. content_type = APPLICATION_OCTET_STREAM;
  74. }
  75. size_t filesize = file.size();
  76. ctx->writer->WriteHeader("Content-Type", http_content_type_str(content_type));
  77. ctx->writer->WriteHeader("Content-Length", filesize);
  78. // ctx->writer->WriteHeader("Transfer-Encoding", "chunked");
  79. ctx->writer->EndHeaders();
  80. char* buf = NULL;
  81. int len = 4096; // 4K
  82. SAFE_ALLOC(buf, len);
  83. size_t total_readbytes = 0;
  84. int last_progress = 0;
  85. auto start_time = std::chrono::steady_clock::now();
  86. auto end_time = start_time;
  87. while (total_readbytes < filesize) {
  88. size_t readbytes = file.read(buf, len);
  89. if (readbytes <= 0) {
  90. ctx->writer->close();
  91. break;
  92. }
  93. if (ctx->writer->WriteBody(buf, readbytes) < 0) {
  94. break;
  95. }
  96. total_readbytes += readbytes;
  97. int cur_progress = total_readbytes * 100 / filesize;
  98. if (cur_progress > last_progress) {
  99. // printf("<< %s progress: %ld/%ld = %d%%\n",
  100. // ctx->request->path.c_str(), (long)total_readbytes, (long)filesize, (int)cur_progress);
  101. last_progress = cur_progress;
  102. }
  103. end_time += std::chrono::milliseconds(len / 1024); // 1KB/ms = 1MB/s = 8Mbps
  104. std::this_thread::sleep_until(end_time);
  105. }
  106. ctx->writer->End();
  107. SAFE_FREE(buf);
  108. // auto elapsed_time = std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time);
  109. // printf("<< %s taked %ds\n", ctx->request->path.c_str(), (int)elapsed_time.count());
  110. }).detach();
  111. return HTTP_STATUS_UNFINISHED;
  112. }
  113. static int sleep(const HttpContextPtr& ctx) {
  114. ctx->set("start_ms", gettimeofday_ms());
  115. std::string strTime = ctx->param("t", "1000");
  116. if (!strTime.empty()) {
  117. int ms = atoi(strTime.c_str());
  118. if (ms > 0) {
  119. hv_delay(ms);
  120. }
  121. }
  122. ctx->set("end_ms", gettimeofday_ms());
  123. response_status(ctx, 0, "OK");
  124. return 200;
  125. }
  126. static int setTimeout(const HttpContextPtr& ctx) {
  127. ctx->set("start_ms", gettimeofday_ms());
  128. std::string strTime = ctx->param("t", "1000");
  129. if (!strTime.empty()) {
  130. int ms = atoi(strTime.c_str());
  131. if (ms > 0) {
  132. hv::setTimeout(ms, [ctx](hv::TimerID timerID){
  133. ctx->set("end_ms", gettimeofday_ms());
  134. response_status(ctx, 0, "OK");
  135. ctx->send();
  136. });
  137. }
  138. }
  139. return HTTP_STATUS_UNFINISHED;
  140. }
  141. static int query(const HttpContextPtr& ctx) {
  142. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  143. // ?query => HttpRequest::query_params
  144. for (auto& param : ctx->params()) {
  145. ctx->set(param.first.c_str(), param.second);
  146. }
  147. response_status(ctx, 0, "OK");
  148. return 200;
  149. }
  150. static int kv(HttpRequest* req, HttpResponse* resp) {
  151. if (req->content_type != APPLICATION_URLENCODED) {
  152. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  153. }
  154. resp->content_type = APPLICATION_URLENCODED;
  155. resp->kv = req->kv;
  156. resp->kv["int"] = hv::to_string(123);
  157. resp->kv["float"] = hv::to_string(3.14);
  158. resp->kv["string"] = "hello";
  159. return 200;
  160. }
  161. static int json(HttpRequest* req, HttpResponse* resp) {
  162. if (req->content_type != APPLICATION_JSON) {
  163. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  164. }
  165. resp->content_type = APPLICATION_JSON;
  166. resp->json = req->json;
  167. resp->json["int"] = 123;
  168. resp->json["float"] = 3.14;
  169. resp->json["string"] = "hello";
  170. return 200;
  171. }
  172. static int form(HttpRequest* req, HttpResponse* resp) {
  173. if (req->content_type != MULTIPART_FORM_DATA) {
  174. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  175. }
  176. resp->content_type = MULTIPART_FORM_DATA;
  177. resp->form = req->form;
  178. resp->form["int"] = 123;
  179. resp->form["float"] = 3.14;
  180. resp->form["string"] = "hello";
  181. // resp->form["file"] = FormData(NULL, "test.jpg");
  182. // resp->UploadFormFile("file", "test.jpg");
  183. return 200;
  184. }
  185. static int grpc(HttpRequest* req, HttpResponse* resp) {
  186. if (req->content_type != APPLICATION_GRPC) {
  187. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  188. }
  189. // parse protobuf
  190. // ParseFromString(req->body);
  191. // resp->content_type = APPLICATION_GRPC;
  192. // serailize protobuf
  193. // resp->body = SerializeAsString(xxx);
  194. response_status(resp, 0, "OK");
  195. return 200;
  196. }
  197. static int test(const HttpContextPtr& ctx) {
  198. ctx->setContentType(ctx->type());
  199. ctx->set("bool", ctx->get<bool>("bool"));
  200. ctx->set("int", ctx->get<int>("int"));
  201. ctx->set("float", ctx->get<float>("float"));
  202. ctx->set("string", ctx->get("string"));
  203. response_status(ctx, 0, "OK");
  204. return 200;
  205. }
  206. static int restful(const HttpContextPtr& ctx) {
  207. // RESTful /:field/ => HttpRequest::query_params
  208. // path=/group/:group_name/user/:user_id
  209. std::string group_name = ctx->param("group_name");
  210. std::string user_id = ctx->param("user_id");
  211. ctx->set("group_name", group_name);
  212. ctx->set("user_id", user_id);
  213. response_status(ctx, 0, "OK");
  214. return 200;
  215. }
  216. static int login(const HttpContextPtr& ctx) {
  217. string username = ctx->get("username");
  218. string password = ctx->get("password");
  219. if (username.empty() || password.empty()) {
  220. response_status(ctx, 10001, "Miss username or password");
  221. return HTTP_STATUS_BAD_REQUEST;
  222. }
  223. else if (strcmp(username.c_str(), "admin") != 0) {
  224. response_status(ctx, 10002, "Username not exist");
  225. return HTTP_STATUS_BAD_REQUEST;
  226. }
  227. else if (strcmp(password.c_str(), "123456") != 0) {
  228. response_status(ctx, 10003, "Password wrong");
  229. return HTTP_STATUS_BAD_REQUEST;
  230. }
  231. else {
  232. ctx->set("token", "abcdefg");
  233. response_status(ctx, 0, "OK");
  234. return HTTP_STATUS_OK;
  235. }
  236. }
  237. static int upload(HttpRequest* req, HttpResponse* resp) {
  238. // return resp->SaveFormFile("file", "html/uploads/test.jpg");
  239. if (req->content_type != MULTIPART_FORM_DATA) {
  240. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  241. }
  242. const FormData& file = req->form["file"];
  243. if (file.content.empty()) {
  244. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  245. }
  246. string filepath("html/uploads/");
  247. filepath += file.filename;
  248. FILE* fp = fopen(filepath.c_str(), "wb");
  249. if (fp) {
  250. fwrite(file.content.data(), 1, file.content.size(), fp);
  251. fclose(fp);
  252. }
  253. response_status(resp, 0, "OK");
  254. return 200;
  255. }
  256. private:
  257. static int response_status(HttpResponse* resp, int code = 200, const char* message = NULL) {
  258. if (message == NULL) message = http_status_str((enum http_status)code);
  259. resp->Set("code", code);
  260. resp->Set("message", message);
  261. return code;
  262. }
  263. static int response_status(const HttpContextPtr& ctx, int code = 200, const char* message = NULL) {
  264. if (message == NULL) message = http_status_str((enum http_status)code);
  265. ctx->set("code", code);
  266. ctx->set("message", message);
  267. return code;
  268. }
  269. };
  270. #endif // HV_HTTPD_HANDLER_H