1
0

http_api_test.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef HTTP_API_TEST_H_
  2. #define HTTP_API_TEST_H_
  3. #include "HttpServer.h"
  4. // XXX(path, method, handler)
  5. #define HTTP_API_MAP(XXX) \
  6. XXX("/login", POST, http_api_login) \
  7. XXX("/hello", GET, http_api_hello) \
  8. XXX("/query", GET, http_api_query) \
  9. XXX("/echo", POST, http_api_echo) \
  10. XXX("/kv", POST, http_api_kv) \
  11. XXX("/json", POST, http_api_json) \
  12. XXX("/form", POST, http_api_form) \
  13. XXX("/upload", POST, http_api_upload) \
  14. XXX("/grpc", POST, http_api_grpc) \
  15. \
  16. XXX("/test", POST, http_api_test) \
  17. XXX("/group/:group_name/user/:user_id", DELETE, http_api_restful) \
  18. inline void response_status(HttpResponse* res, int code, const char* message) {
  19. res->Set("code", code);
  20. res->Set("message", message);
  21. }
  22. inline int http_api_preprocessor(HttpRequest* req, HttpResponse* res) {
  23. //printf("%s:%d\n", req->client_addr.ip.c_str(), req->client_addr.port);
  24. //printf("%s\n", req->Dump(true, true).c_str());
  25. req->ParseBody();
  26. res->content_type = APPLICATION_JSON;
  27. #if 0
  28. // authentication sample code
  29. if (strcmp(req->path.c_str(), DEFAULT_BASE_URL "/login") != 0) {
  30. string token = req->GetHeader("token");
  31. if (token.empty()) {
  32. response_status(res, 10011, "Miss token");
  33. res->DumpBody();
  34. return HTTP_STATUS_UNAUTHORIZED;
  35. }
  36. else if (strcmp(token.c_str(), "abcdefg") != 0) {
  37. response_status(res, 10012, "Token wrong");
  38. res->DumpBody();
  39. return HTTP_STATUS_UNAUTHORIZED;
  40. }
  41. return 0;
  42. }
  43. #endif
  44. return 0;
  45. }
  46. inline int http_api_postprocessor(HttpRequest* req, HttpResponse* res) {
  47. res->DumpBody();
  48. //printf("%s\n", res->Dump(true, true).c_str());
  49. return 0;
  50. }
  51. inline int http_api_login(HttpRequest* req, HttpResponse* res) {
  52. int ret = 0;
  53. string username = req->GetString("username");
  54. string password = req->GetString("password");
  55. if (username.empty() || password.empty()) {
  56. response_status(res, 10001, "Miss username or password");
  57. ret = HTTP_STATUS_BAD_REQUEST;
  58. }
  59. else if (strcmp(username.c_str(), "admin") != 0) {
  60. response_status(res, 10002, "Username not exist");
  61. ret = HTTP_STATUS_BAD_REQUEST;
  62. }
  63. else if (strcmp(password.c_str(), "123456") != 0) {
  64. response_status(res, 10003, "Password wrong");
  65. ret = HTTP_STATUS_BAD_REQUEST;
  66. }
  67. else {
  68. res->Set("token", "abcdefg");
  69. response_status(res, 0, "Login succeed.");
  70. ret = HTTP_STATUS_OK;
  71. }
  72. res->DumpBody();
  73. return ret;
  74. }
  75. inline int http_api_hello(HttpRequest* req, HttpResponse* res) {
  76. res->content_type = TEXT_PLAIN;
  77. res->body = "hello";
  78. return 0;
  79. }
  80. inline int http_api_query(HttpRequest* req, HttpResponse* res) {
  81. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  82. // ?query => HttpRequest::query_params
  83. for (auto& param : req->query_params) {
  84. res->Set(param.first.c_str(), param.second);
  85. }
  86. response_status(res, 0, "Query completed.");
  87. return 0;
  88. }
  89. inline int http_api_echo(HttpRequest* req, HttpResponse* res) {
  90. res->content_type = req->content_type;
  91. res->body = req->body;
  92. return 0;
  93. }
  94. inline int http_api_kv(HttpRequest*req, HttpResponse* res) {
  95. if (req->content_type != APPLICATION_URLENCODED) {
  96. return HTTP_STATUS_BAD_REQUEST;
  97. }
  98. res->content_type = APPLICATION_URLENCODED;
  99. res->kv = req->kv;
  100. return 0;
  101. }
  102. inline int http_api_json(HttpRequest* req, HttpResponse* res) {
  103. if (req->content_type != APPLICATION_JSON) {
  104. return HTTP_STATUS_BAD_REQUEST;
  105. }
  106. res->content_type = APPLICATION_JSON;
  107. res->json = req->json;
  108. return 0;
  109. }
  110. inline int http_api_form(HttpRequest* req, HttpResponse* res) {
  111. if (req->content_type != MULTIPART_FORM_DATA) {
  112. return HTTP_STATUS_BAD_REQUEST;
  113. }
  114. res->content_type = MULTIPART_FORM_DATA;
  115. res->form = req->form;
  116. return 0;
  117. }
  118. inline int http_api_upload(HttpRequest* req, HttpResponse* res) {
  119. if (req->content_type != MULTIPART_FORM_DATA) {
  120. return HTTP_STATUS_BAD_REQUEST;
  121. }
  122. FormData file = req->form["file"];
  123. string filepath("html/uploads/");
  124. filepath += file.filename;
  125. FILE* fp = fopen(filepath.c_str(), "w");
  126. if (fp) {
  127. hlogi("Save as %s", filepath.c_str());
  128. fwrite(file.content.data(), 1, file.content.size(), fp);
  129. fclose(fp);
  130. }
  131. response_status(res, 0, "OK");
  132. return 0;
  133. }
  134. inline int http_api_grpc(HttpRequest* req, HttpResponse* res) {
  135. if (req->content_type != APPLICATION_GRPC) {
  136. return HTTP_STATUS_BAD_REQUEST;
  137. }
  138. // parse protobuf: ParseFromString
  139. // req->body;
  140. // res->content_type = APPLICATION_GRPC;
  141. // serailize protobuf: SerializeAsString
  142. // res->body;
  143. return 0;
  144. }
  145. inline int http_api_test(HttpRequest* req, HttpResponse* res) {
  146. string str = req->GetString("string");
  147. //int64_t n = req->Get<int64_t>("int");
  148. //double f = req->Get<double>("float");
  149. //bool b = req->Get<bool>("bool");
  150. int64_t n = req->GetInt("int");
  151. double f = req->GetFloat("float");
  152. bool b = req->GetBool("bool");
  153. res->content_type = req->content_type;
  154. res->Set("string", str);
  155. res->Set("int", n);
  156. res->Set("float", f);
  157. res->Set("bool", b);
  158. response_status(res, 0, "OK");
  159. return 0;
  160. }
  161. inline int http_api_restful(HttpRequest* req, HttpResponse* res) {
  162. // RESTful /:field/ => HttpRequest::query_params
  163. // path=/group/:group_name/user/:user_id
  164. //string group_name = req->GetParam("group_name");
  165. //string user_id = req->GetParam("user_id");
  166. for (auto& param : req->query_params) {
  167. res->Set(param.first.c_str(), param.second);
  168. }
  169. response_status(res, 0, "Operation completed.");
  170. return 0;
  171. }
  172. #endif // HTTP_API_TEST_H_