handler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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::headerHandler(HttpRequest* req, HttpResponse* resp) {
  10. // printf("%s:%d\n", req->client_addr.ip.c_str(), req->client_addr.port);
  11. #if REDIRECT_HTTP_TO_HTTPS
  12. // 301
  13. if (req->scheme == "http") {
  14. std::string location = hv::asprintf("https://%s:%d%s", req->host.c_str(), 8443, req->path.c_str());
  15. return resp->Redirect(location, HTTP_STATUS_MOVED_PERMANENTLY);
  16. }
  17. #endif
  18. // Unified verification request Content-Type?
  19. // if (req->content_type != APPLICATION_JSON) {
  20. // return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  21. // }
  22. return HTTP_STATUS_NEXT;
  23. }
  24. int Handler::preprocessor(HttpRequest* req, HttpResponse* resp) {
  25. // printf("%s\n", req->Dump(true, true).c_str());
  26. // Deserialize request body to json, form, etc.
  27. req->ParseBody();
  28. // Unified setting response Content-Type?
  29. resp->content_type = APPLICATION_JSON;
  30. return HTTP_STATUS_NEXT;
  31. }
  32. int Handler::postprocessor(HttpRequest* req, HttpResponse* resp) {
  33. // printf("%s\n", resp->Dump(true, true).c_str());
  34. return resp->status_code;
  35. }
  36. int Handler::errorHandler(const HttpContextPtr& ctx) {
  37. int error_code = ctx->response->status_code;
  38. return response_status(ctx, error_code);
  39. }
  40. int Handler::Authorization(HttpRequest* req, HttpResponse* resp) {
  41. // authentication sample code
  42. if (strcmp(req->path.c_str(), "/login") == 0) {
  43. return HTTP_STATUS_NEXT;
  44. }
  45. std::string token = req->GetHeader("Authorization");
  46. if (token.empty()) {
  47. response_status(resp, 10011, "Miss Authorization header!");
  48. return HTTP_STATUS_UNAUTHORIZED;
  49. }
  50. else if (strcmp(token.c_str(), "abcdefg") != 0) {
  51. response_status(resp, 10012, "Authorization failed!");
  52. return HTTP_STATUS_UNAUTHORIZED;
  53. }
  54. return HTTP_STATUS_NEXT;
  55. }
  56. int Handler::sleep(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
  57. writer->WriteHeader("X-Response-tid", hv_gettid());
  58. unsigned long long start_ms = gettimeofday_ms();
  59. writer->response->Set("start_ms", start_ms);
  60. std::string strTime = req->GetParam("t", "1000");
  61. if (!strTime.empty()) {
  62. int ms = atoi(strTime.c_str());
  63. if (ms > 0) {
  64. hv_delay(ms);
  65. }
  66. }
  67. unsigned long long end_ms = gettimeofday_ms();
  68. writer->response->Set("end_ms", end_ms);
  69. writer->response->Set("cost_ms", end_ms - start_ms);
  70. response_status(writer, 0, "OK");
  71. return 200;
  72. }
  73. int Handler::setTimeout(const HttpContextPtr& ctx) {
  74. unsigned long long start_ms = gettimeofday_ms();
  75. ctx->set("start_ms", start_ms);
  76. std::string strTime = ctx->param("t", "1000");
  77. if (!strTime.empty()) {
  78. int ms = atoi(strTime.c_str());
  79. if (ms > 0) {
  80. hv::setTimeout(ms, [ctx, start_ms](hv::TimerID timerID){
  81. unsigned long long end_ms = gettimeofday_ms();
  82. ctx->set("end_ms", end_ms);
  83. ctx->set("cost_ms", end_ms - start_ms);
  84. response_status(ctx, 0, "OK");
  85. });
  86. }
  87. }
  88. return HTTP_STATUS_UNFINISHED;
  89. }
  90. int Handler::query(const HttpContextPtr& ctx) {
  91. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  92. // ?query => HttpRequest::query_params
  93. for (auto& param : ctx->params()) {
  94. ctx->set(param.first.c_str(), param.second);
  95. }
  96. response_status(ctx, 0, "OK");
  97. return 200;
  98. }
  99. int Handler::kv(HttpRequest* req, HttpResponse* resp) {
  100. if (req->content_type != APPLICATION_URLENCODED) {
  101. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  102. }
  103. resp->content_type = APPLICATION_URLENCODED;
  104. resp->kv = req->GetUrlEncoded();
  105. resp->SetUrlEncoded("int", 123);
  106. resp->SetUrlEncoded("float", 3.14);
  107. resp->SetUrlEncoded("string", "hello");
  108. return 200;
  109. }
  110. int Handler::json(HttpRequest* req, HttpResponse* resp) {
  111. if (req->content_type != APPLICATION_JSON) {
  112. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  113. }
  114. resp->content_type = APPLICATION_JSON;
  115. resp->json = req->GetJson();
  116. resp->json["int"] = 123;
  117. resp->json["float"] = 3.14;
  118. resp->json["string"] = "hello";
  119. return 200;
  120. }
  121. int Handler::form(HttpRequest* req, HttpResponse* resp) {
  122. if (req->content_type != MULTIPART_FORM_DATA) {
  123. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  124. }
  125. resp->content_type = MULTIPART_FORM_DATA;
  126. resp->form = req->GetForm();
  127. resp->SetFormData("int", 123);
  128. resp->SetFormData("float", 3.14);
  129. resp->SetFormData("string", "hello");
  130. // resp->SetFormFile("file", "test.jpg");
  131. return 200;
  132. }
  133. int Handler::grpc(HttpRequest* req, HttpResponse* resp) {
  134. if (req->content_type != APPLICATION_GRPC) {
  135. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  136. }
  137. // parse protobuf
  138. // ParseFromString(req->body);
  139. // resp->content_type = APPLICATION_GRPC;
  140. // serailize protobuf
  141. // resp->body = SerializeAsString(xxx);
  142. response_status(resp, 0, "OK");
  143. return 200;
  144. }
  145. int Handler::test(const HttpContextPtr& ctx) {
  146. ctx->setContentType(ctx->type());
  147. ctx->set("bool", ctx->get<bool>("bool"));
  148. ctx->set("int", ctx->get<int>("int"));
  149. ctx->set("float", ctx->get<float>("float"));
  150. ctx->set("string", ctx->get("string"));
  151. response_status(ctx, 0, "OK");
  152. return 200;
  153. }
  154. int Handler::restful(const HttpContextPtr& ctx) {
  155. // RESTful /:field/ => HttpRequest::query_params
  156. // path=/group/:group_name/user/:user_id
  157. std::string group_name = ctx->param("group_name");
  158. std::string user_id = ctx->param("user_id");
  159. ctx->set("group_name", group_name);
  160. ctx->set("user_id", user_id);
  161. response_status(ctx, 0, "OK");
  162. return 200;
  163. }
  164. int Handler::login(const HttpContextPtr& ctx) {
  165. std::string username = ctx->get("username");
  166. std::string password = ctx->get("password");
  167. if (username.empty() || password.empty()) {
  168. response_status(ctx, 10001, "Miss username or password");
  169. return HTTP_STATUS_BAD_REQUEST;
  170. }
  171. else if (strcmp(username.c_str(), "admin") != 0) {
  172. response_status(ctx, 10002, "Username not exist");
  173. return HTTP_STATUS_BAD_REQUEST;
  174. }
  175. else if (strcmp(password.c_str(), "123456") != 0) {
  176. response_status(ctx, 10003, "Password wrong");
  177. return HTTP_STATUS_BAD_REQUEST;
  178. }
  179. else {
  180. ctx->set("token", "abcdefg");
  181. response_status(ctx, 0, "OK");
  182. return HTTP_STATUS_OK;
  183. }
  184. }
  185. int Handler::upload(const HttpContextPtr& ctx) {
  186. int status_code = 200;
  187. std::string save_path = "html/uploads/";
  188. if (ctx->is(MULTIPART_FORM_DATA)) {
  189. status_code = ctx->request->SaveFormFile("file", save_path.c_str());
  190. } else {
  191. std::string filename = ctx->param("filename", "unnamed.txt");
  192. std::string filepath = save_path + filename;
  193. status_code = ctx->request->SaveFile(filepath.c_str());
  194. }
  195. return response_status(ctx, status_code);
  196. }
  197. int Handler::recvLargeFile(const HttpContextPtr& ctx, http_parser_state state, const char* data, size_t size) {
  198. // printf("recvLargeFile state=%d\n", (int)state);
  199. int status_code = HTTP_STATUS_UNFINISHED;
  200. HFile* file = (HFile*)ctx->userdata;
  201. switch (state) {
  202. case HP_HEADERS_COMPLETE:
  203. {
  204. if (ctx->is(MULTIPART_FORM_DATA)) {
  205. // NOTE: You can use multipart_parser if you want to use multipart/form-data.
  206. ctx->close();
  207. return HTTP_STATUS_BAD_REQUEST;
  208. }
  209. std::string save_path = "html/uploads/";
  210. std::string filename = ctx->param("filename", "unnamed.txt");
  211. std::string filepath = save_path + filename;
  212. file = new HFile;
  213. if (file->open(filepath.c_str(), "wb") != 0) {
  214. ctx->close();
  215. return HTTP_STATUS_INTERNAL_SERVER_ERROR;
  216. }
  217. ctx->userdata = file;
  218. }
  219. break;
  220. case HP_BODY:
  221. {
  222. if (file && data && size) {
  223. if (file->write(data, size) != size) {
  224. ctx->close();
  225. return HTTP_STATUS_INTERNAL_SERVER_ERROR;
  226. }
  227. }
  228. }
  229. break;
  230. case HP_MESSAGE_COMPLETE:
  231. {
  232. status_code = HTTP_STATUS_OK;
  233. ctx->setContentType(APPLICATION_JSON);
  234. response_status(ctx, status_code);
  235. if (file) {
  236. delete file;
  237. ctx->userdata = NULL;
  238. }
  239. }
  240. break;
  241. case HP_ERROR:
  242. {
  243. if (file) {
  244. file->remove();
  245. delete file;
  246. ctx->userdata = NULL;
  247. }
  248. }
  249. break;
  250. default:
  251. break;
  252. }
  253. return status_code;
  254. }
  255. int Handler::sendLargeFile(const HttpContextPtr& ctx) {
  256. std::thread([ctx](){
  257. ctx->writer->Begin();
  258. std::string filepath = ctx->service->document_root + ctx->request->Path();
  259. HFile file;
  260. if (file.open(filepath.c_str(), "rb") != 0) {
  261. ctx->writer->WriteStatus(HTTP_STATUS_NOT_FOUND);
  262. ctx->writer->WriteHeader("Content-Type", "text/html");
  263. ctx->writer->WriteBody("<center><h1>404 Not Found</h1></center>");
  264. ctx->writer->End();
  265. return;
  266. }
  267. http_content_type content_type = CONTENT_TYPE_NONE;
  268. const char* suffix = hv_suffixname(filepath.c_str());
  269. if (suffix) {
  270. content_type = http_content_type_enum_by_suffix(suffix);
  271. }
  272. if (content_type == CONTENT_TYPE_NONE || content_type == CONTENT_TYPE_UNDEFINED) {
  273. content_type = APPLICATION_OCTET_STREAM;
  274. }
  275. size_t filesize = file.size();
  276. ctx->writer->WriteHeader("Content-Type", http_content_type_str(content_type));
  277. #if USE_TRANSFER_ENCODING_CHUNKED
  278. ctx->writer->WriteHeader("Transfer-Encoding", "chunked");
  279. #else
  280. ctx->writer->WriteHeader("Content-Length", filesize);
  281. #endif
  282. ctx->writer->EndHeaders();
  283. char* buf = NULL;
  284. int len = 40960; // 40K
  285. SAFE_ALLOC(buf, len);
  286. size_t total_readbytes = 0;
  287. int last_progress = 0;
  288. int sleep_ms_per_send = 0;
  289. if (ctx->service->limit_rate <= 0) {
  290. // unlimited
  291. } else {
  292. sleep_ms_per_send = len * 1000 / 1024 / ctx->service->limit_rate;
  293. }
  294. if (sleep_ms_per_send == 0) sleep_ms_per_send = 1;
  295. int sleep_ms = sleep_ms_per_send;
  296. auto start_time = std::chrono::steady_clock::now();
  297. auto end_time = start_time;
  298. while (total_readbytes < filesize) {
  299. if (!ctx->writer->isConnected()) {
  300. break;
  301. }
  302. if (!ctx->writer->isWriteComplete()) {
  303. hv_delay(1);
  304. continue;
  305. }
  306. size_t readbytes = file.read(buf, len);
  307. if (readbytes <= 0) {
  308. // read file error!
  309. ctx->writer->close();
  310. break;
  311. }
  312. int nwrite = ctx->writer->WriteBody(buf, readbytes);
  313. if (nwrite < 0) {
  314. // disconnected!
  315. break;
  316. }
  317. total_readbytes += readbytes;
  318. int cur_progress = total_readbytes * 100 / filesize;
  319. if (cur_progress > last_progress) {
  320. // printf("<< %s progress: %ld/%ld = %d%%\n",
  321. // ctx->request->path.c_str(), (long)total_readbytes, (long)filesize, (int)cur_progress);
  322. last_progress = cur_progress;
  323. }
  324. end_time += std::chrono::milliseconds(sleep_ms);
  325. std::this_thread::sleep_until(end_time);
  326. }
  327. ctx->writer->End();
  328. SAFE_FREE(buf);
  329. // auto elapsed_time = std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time);
  330. // printf("<< %s taked %ds\n", ctx->request->path.c_str(), (int)elapsed_time.count());
  331. }).detach();
  332. return HTTP_STATUS_UNFINISHED;
  333. }
  334. int Handler::sse(const HttpContextPtr& ctx) {
  335. // SSEvent(message) every 1s
  336. hv::setInterval(1000, [ctx](hv::TimerID timerID) {
  337. if (ctx->writer->isConnected()) {
  338. char szTime[DATETIME_FMT_BUFLEN] = {0};
  339. datetime_t now = datetime_now();
  340. datetime_fmt(&now, szTime);
  341. ctx->writer->SSEvent(szTime);
  342. } else {
  343. hv::killTimer(timerID);
  344. }
  345. });
  346. return HTTP_STATUS_UNFINISHED;
  347. }