Przeglądaj źródła

support RESTful both /:field/ and /{field}/

hewei.it 4 lat temu
rodzic
commit
d0174564d5
2 zmienionych plików z 12 dodań i 5 usunięć
  1. 1 0
      examples/httpd/router.h
  2. 11 5
      http/server/HttpService.cpp

+ 1 - 0
examples/httpd/router.h

@@ -113,6 +113,7 @@ public:
         // RESTful API: /group/:group_name/user/:user_id
         // curl -v -X DELETE http://ip:port/group/test/user/123
         router.Delete("/group/:group_name/user/:user_id", Handler::restful);
+        // router.Delete("/group/{group_name}/user/{user_id}", Handler::restful);
 
         // bin/curl -v localhost:8080/upload -F "file=@LICENSE"
         router.POST("/upload", Handler::upload);

+ 11 - 5
http/server/HttpService.cpp

@@ -84,19 +84,25 @@ int HttpService::GetApi(HttpRequest* req, http_sync_handler* handler, http_async
                 }
                 break;
             } else if (*kp == *vp) {
-                if (kp[0] == '/' && kp[1] == ':') {
+                if (kp[0] == '/' &&
+                   (kp[1] == ':' || kp[1] == '{')) {
                     // RESTful /:field/
+                    // RESTful /{field}/
                     kp += 2;
                     ks = kp;
                     while (*kp && *kp != '/') {++kp;}
                     vp += 1;
                     vs = vp;
                     while (*vp && *vp != '/') {++vp;}
-                    params[std::string(ks, kp-ks)] = std::string(vs, vp-vs);
-                } else {
-                    ++kp;
-                    ++vp;
+                    int klen = kp - ks;
+                    if (*(ks-1) == '{' && *(kp-1) == '}') {
+                        --klen;
+                    }
+                    params[std::string(ks, klen)] = std::string(vs, vp-vs);
+                    continue;
                 }
+                ++kp;
+                ++vp;
             } else {
                 match = false;
                 break;