handler.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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->GetUrlEncoded();
  168. resp->SetUrlEncoded("int", 123);
  169. resp->SetUrlEncoded("float", 3.14);
  170. resp->SetUrlEncoded("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->GetJson();
  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->GetForm();
  190. resp->SetFormData("int", 123);
  191. resp->SetFormData("float", 3.14);
  192. resp->SetFormData("string", "hello");
  193. // resp->SetFormFile("file", "test.jpg");
  194. return 200;
  195. }
  196. int Handler::grpc(HttpRequest* req, HttpResponse* resp) {
  197. if (req->content_type != APPLICATION_GRPC) {
  198. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  199. }
  200. // parse protobuf
  201. // ParseFromString(req->body);
  202. // resp->content_type = APPLICATION_GRPC;
  203. // serailize protobuf
  204. // resp->body = SerializeAsString(xxx);
  205. response_status(resp, 0, "OK");
  206. return 200;
  207. }
  208. int Handler::test(const HttpContextPtr& ctx) {
  209. ctx->setContentType(ctx->type());
  210. ctx->set("bool", ctx->get<bool>("bool"));
  211. ctx->set("int", ctx->get<int>("int"));
  212. ctx->set("float", ctx->get<float>("float"));
  213. ctx->set("string", ctx->get("string"));
  214. response_status(ctx, 0, "OK");
  215. return 200;
  216. }
  217. int Handler::restful(const HttpContextPtr& ctx) {
  218. // RESTful /:field/ => HttpRequest::query_params
  219. // path=/group/:group_name/user/:user_id
  220. std::string group_name = ctx->param("group_name");
  221. std::string user_id = ctx->param("user_id");
  222. ctx->set("group_name", group_name);
  223. ctx->set("user_id", user_id);
  224. response_status(ctx, 0, "OK");
  225. return 200;
  226. }
  227. int Handler::login(const HttpContextPtr& ctx) {
  228. std::string username = ctx->get("username");
  229. std::string password = ctx->get("password");
  230. if (username.empty() || password.empty()) {
  231. response_status(ctx, 10001, "Miss username or password");
  232. return HTTP_STATUS_BAD_REQUEST;
  233. }
  234. else if (strcmp(username.c_str(), "admin") != 0) {
  235. response_status(ctx, 10002, "Username not exist");
  236. return HTTP_STATUS_BAD_REQUEST;
  237. }
  238. else if (strcmp(password.c_str(), "123456") != 0) {
  239. response_status(ctx, 10003, "Password wrong");
  240. return HTTP_STATUS_BAD_REQUEST;
  241. }
  242. else {
  243. ctx->set("token", "abcdefg");
  244. response_status(ctx, 0, "OK");
  245. return HTTP_STATUS_OK;
  246. }
  247. }
  248. int Handler::upload(const HttpContextPtr& ctx) {
  249. int status_code = 200;
  250. if (ctx->is(MULTIPART_FORM_DATA)) {
  251. status_code = ctx->request->SaveFormFile("file", "html/uploads/");
  252. } else {
  253. status_code = ctx->request->SaveFile("html/uploads/upload.txt");
  254. }
  255. return response_status(ctx, status_code);
  256. }