handler.cpp 9.7 KB

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