handler.h 10 KB

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