Explorar el Código

more sample code

hewei.it hace 5 años
padre
commit
8f0d6b3ed6
Se han modificado 2 ficheros con 69 adiciones y 59 borrados
  1. 64 54
      examples/httpd/handler.h
  2. 5 5
      examples/httpd/router.h

+ 64 - 54
examples/httpd/handler.h

@@ -6,24 +6,24 @@
 class Handler {
 public:
     // preprocessor => handler => postprocessor
-    static int preprocessor(HttpRequest* req, HttpResponse* res) {
+    static int preprocessor(HttpRequest* req, HttpResponse* resp) {
         // printf("%s:%d\n", req->client_addr.ip.c_str(), req->client_addr.port);
         // printf("%s\n", req->Dump(true, true).c_str());
         // if (req->content_type != APPLICATION_JSON) {
-        //     return response_status(res, HTTP_STATUS_BAD_REQUEST);
+        //     return response_status(resp, HTTP_STATUS_BAD_REQUEST);
         // }
         req->ParseBody();
-        res->content_type = APPLICATION_JSON;
+        resp->content_type = APPLICATION_JSON;
 #if 0
         // authentication sample code
         if (strcmp(req->path.c_str(), "/login") != 0) {
             string token = req->GetHeader("token");
             if (token.empty()) {
-                response_status(res, 10011, "Miss token");
+                response_status(resp, 10011, "Miss token");
                 return HTTP_STATUS_UNAUTHORIZED;
             }
             else if (strcmp(token.c_str(), "abcdefg") != 0) {
-                response_status(res, 10012, "Token wrong");
+                response_status(resp, 10012, "Token wrong");
                 return HTTP_STATUS_UNAUTHORIZED;
             }
             return 0;
@@ -32,12 +32,12 @@ public:
         return 0;
     }
 
-    static int postprocessor(HttpRequest* req, HttpResponse* res) {
-        // printf("%s\n", res->Dump(true, true).c_str());
+    static int postprocessor(HttpRequest* req, HttpResponse* resp) {
+        // printf("%s\n", resp->Dump(true, true).c_str());
         return 0;
     }
 
-    static int sleep(HttpRequest* req, HttpResponse* res) {
+    static int sleep(HttpRequest* req, HttpResponse* resp) {
         time_t start_time = time(NULL);
         std::string strTime = req->GetParam("t");
         if (!strTime.empty()) {
@@ -47,50 +47,60 @@ public:
             }
         }
         time_t end_time = time(NULL);
-        res->Set("start_time", start_time);
-        res->Set("end_time", end_time);
-        response_status(res, 0, "OK");
+        resp->Set("start_time", start_time);
+        resp->Set("end_time", end_time);
+        response_status(resp, 0, "OK");
         return 200;
     }
 
-    static int query(HttpRequest* req, HttpResponse* res) {
+    static int query(HttpRequest* req, HttpResponse* resp) {
         // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
         // ?query => HttpRequest::query_params
         for (auto& param : req->query_params) {
-            res->Set(param.first.c_str(), param.second);
+            resp->Set(param.first.c_str(), param.second);
         }
-        response_status(res, 0, "OK");
+        response_status(resp, 0, "OK");
         return 200;
     }
 
-    static int kv(HttpRequest* req, HttpResponse* res) {
+    static int kv(HttpRequest* req, HttpResponse* resp) {
         if (req->content_type != APPLICATION_URLENCODED) {
-            return response_status(res, HTTP_STATUS_BAD_REQUEST);
+            return response_status(resp, HTTP_STATUS_BAD_REQUEST);
         }
-        res->content_type = APPLICATION_URLENCODED;
-        res->kv = req->kv;
+        resp->content_type = APPLICATION_URLENCODED;
+        resp->kv = req->kv;
+        resp->kv["int"] = hv::to_string(123);
+        resp->kv["float"] = hv::to_string(3.14);
+        resp->kv["string"] = "hello";
         return 200;
     }
 
-    static int json(HttpRequest* req, HttpResponse* res) {
+    static int json(HttpRequest* req, HttpResponse* resp) {
         if (req->content_type != APPLICATION_JSON) {
-            return response_status(res, HTTP_STATUS_BAD_REQUEST);
+            return response_status(resp, HTTP_STATUS_BAD_REQUEST);
         }
-        res->content_type = APPLICATION_JSON;
-        res->json = req->json;
+        resp->content_type = APPLICATION_JSON;
+        resp->json = req->json;
+        resp->json["int"] = 123;
+        resp->json["float"] = 3.14;
+        resp->json["string"] = "hello";
         return 200;
     }
 
-    static int form(HttpRequest* req, HttpResponse* res) {
+    static int form(HttpRequest* req, HttpResponse* resp) {
         if (req->content_type != MULTIPART_FORM_DATA) {
-            return response_status(res, HTTP_STATUS_BAD_REQUEST);
+            return response_status(resp, HTTP_STATUS_BAD_REQUEST);
         }
-        res->content_type = MULTIPART_FORM_DATA;
-        res->form = req->form;
+        resp->content_type = MULTIPART_FORM_DATA;
+        resp->form = req->form;
+        resp->form["int"] = 123;
+        resp->form["float"] = 3.14;
+        resp->form["float"] = "hello";
+        // resp->form["file"] = FormData("test.jpg");
         return 200;
     }
 
-    static int test(HttpRequest* req, HttpResponse* res) {
+    static int test(HttpRequest* req, HttpResponse* resp) {
         // bool b = req->Get<bool>("bool");
         // int64_t n = req->Get<int64_t>("int");
         // double f = req->Get<double>("float");
@@ -99,65 +109,65 @@ public:
         double f = req->GetFloat("float");
         string str = req->GetString("string");
 
-        res->content_type = req->content_type;
-        res->Set("bool", b);
-        res->Set("int", n);
-        res->Set("float", f);
-        res->Set("string", str);
-        response_status(res, 0, "OK");
+        resp->content_type = req->content_type;
+        resp->Set("bool", b);
+        resp->Set("int", n);
+        resp->Set("float", f);
+        resp->Set("string", str);
+        response_status(resp, 0, "OK");
         return 200;
     }
 
-    static int grpc(HttpRequest* req, HttpResponse* res) {
+    static int grpc(HttpRequest* req, HttpResponse* resp) {
         if (req->content_type != APPLICATION_GRPC) {
-            return response_status(res, HTTP_STATUS_BAD_REQUEST);
+            return response_status(resp, HTTP_STATUS_BAD_REQUEST);
         }
         // parse protobuf
         // ParseFromString(req->body);
-        // res->content_type = APPLICATION_GRPC;
+        // resp->content_type = APPLICATION_GRPC;
         // serailize protobuf
-        // res->body = SerializeAsString(xxx);
-        response_status(res, 0, "OK");
+        // resp->body = SerializeAsString(xxx);
+        response_status(resp, 0, "OK");
         return 200;
     }
 
-    static int restful(HttpRequest* req, HttpResponse* res) {
+    static int restful(HttpRequest* req, HttpResponse* resp) {
         // RESTful /:field/ => HttpRequest::query_params
         // path=/group/:group_name/user/:user_id
         // string group_name = req->GetParam("group_name");
         // string user_id = req->GetParam("user_id");
         for (auto& param : req->query_params) {
-            res->Set(param.first.c_str(), param.second);
+            resp->Set(param.first.c_str(), param.second);
         }
-        response_status(res, 0, "OK");
+        response_status(resp, 0, "OK");
         return 200;
     }
 
-    static int login(HttpRequest* req, HttpResponse* res) {
+    static int login(HttpRequest* req, HttpResponse* resp) {
         string username = req->GetString("username");
         string password = req->GetString("password");
         if (username.empty() || password.empty()) {
-            response_status(res, 10001, "Miss username or password");
+            response_status(resp, 10001, "Miss username or password");
             return HTTP_STATUS_BAD_REQUEST;
         }
         else if (strcmp(username.c_str(), "admin") != 0) {
-            response_status(res, 10002, "Username not exist");
+            response_status(resp, 10002, "Username not exist");
             return HTTP_STATUS_BAD_REQUEST;
         }
         else if (strcmp(password.c_str(), "123456") != 0) {
-            response_status(res, 10003, "Password wrong");
+            response_status(resp, 10003, "Password wrong");
             return HTTP_STATUS_BAD_REQUEST;
         }
         else {
-            res->Set("token", "abcdefg");
-            response_status(res, 0, "OK");
+            resp->Set("token", "abcdefg");
+            response_status(resp, 0, "OK");
             return HTTP_STATUS_OK;
         }
     }
 
-    static int upload(HttpRequest* req, HttpResponse* res) {
+    static int upload(HttpRequest* req, HttpResponse* resp) {
         if (req->content_type != MULTIPART_FORM_DATA) {
-            return response_status(res, HTTP_STATUS_BAD_REQUEST);
+            return response_status(resp, HTTP_STATUS_BAD_REQUEST);
         }
         FormData file = req->form["file"];
         string filepath("html/uploads/");
@@ -167,16 +177,16 @@ public:
             fwrite(file.content.data(), 1, file.content.size(), fp);
             fclose(fp);
         }
-        response_status(res, 0, "OK");
+        response_status(resp, 0, "OK");
         return 200;
     }
 
 private:
-    static int response_status(HttpResponse* res, int code = 200, const char* message = NULL) {
-        res->Set("code", code);
+    static int response_status(HttpResponse* resp, int code = 200, const char* message = NULL) {
+        resp->Set("code", code);
         if (message == NULL) message = http_status_str((enum http_status)code);
-        res->Set("message", message);
-        res->DumpBody();
+        resp->Set("message", message);
+        resp->DumpBody();
         return code;
     }
 };

+ 5 - 5
examples/httpd/router.h

@@ -13,15 +13,15 @@ public:
         http.postprocessor = Handler::postprocessor;
 
         // curl -v http://ip:port/ping
-        http.GET("/ping", [](HttpRequest* req, HttpResponse* res) {
-            res->body = "PONG";
+        http.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
+            resp->body = "pong";
             return 200;
         });
 
         // curl -v http://ip:port/echo -d "hello,world!"
-        http.POST("/echo", [](HttpRequest* req, HttpResponse* res) {
-            res->content_type = req->content_type;
-            res->body = req->body;
+        http.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
+            resp->content_type = req->content_type;
+            resp->body = req->body;
             return 200;
         });