Explorar el Código

Add HttpService::AllowCORS

ithewei hace 3 años
padre
commit
9837308a9b

+ 2 - 0
etc/httpd.conf

@@ -30,6 +30,8 @@ home_page = index.html
 #error_page = error.html
 index_of = /downloads/
 limit_rate = 500 # KB/s
+cors = yes
+forward_proxy = no
 
 # SSL/TLS
 ssl_certificate = cert/server.crt

+ 0 - 9
examples/httpd/handler.cpp

@@ -21,15 +21,6 @@ int Handler::preprocessor(HttpRequest* req, HttpResponse* resp) {
     }
 #endif
 
-    // cors
-    resp->headers["Access-Control-Allow-Origin"] = "*";
-    if (req->method == HTTP_OPTIONS) {
-        resp->headers["Access-Control-Allow-Origin"] = req->GetHeader("Origin", "*");
-        resp->headers["Access-Control-Allow-Methods"] = req->GetHeader("Access-Control-Request-Method", "OPTIONS, HEAD, GET, POST, PUT, DELETE, PATCH");
-        resp->headers["Access-Control-Allow-Headers"] = req->GetHeader("Access-Control-Request-Headers", "Content-Type");
-        return HTTP_STATUS_NO_CONTENT;
-    }
-
     // Unified verification request Content-Type?
     // if (req->content_type != APPLICATION_JSON) {
     //     return response_status(resp, HTTP_STATUS_BAD_REQUEST);

+ 7 - 0
examples/httpd/httpd.cpp

@@ -175,6 +175,13 @@ int parse_confile(const char* confile) {
     if (str.size() != 0) {
         g_http_service.limit_rate = atoi(str.c_str());
     }
+    // cors
+    if (ini.Get<bool>("cors")) {
+        g_http_service.AllowCORS();
+    }
+    if (ini.Get<bool>("forward_proxy")) {
+        g_http_service.EnableForwardProxy();
+    }
     // ssl
     if (g_http_server.https_port > 0) {
         std::string crt_file = ini.GetValue("ssl_certificate");

+ 9 - 0
http/server/HttpHandler.cpp

@@ -332,6 +332,15 @@ int HttpHandler::HandleHttpRequest() {
 
 preprocessor:
     state = HANDLE_BEGIN;
+    if (service->allow_cors) {
+        resp->headers["Access-Control-Allow-Origin"] = req->GetHeader("Origin", "*");
+        if (req->method == HTTP_OPTIONS) {
+            resp->headers["Access-Control-Allow-Methods"] = req->GetHeader("Access-Control-Request-Method", "OPTIONS, HEAD, GET, POST, PUT, DELETE, PATCH");
+            resp->headers["Access-Control-Allow-Headers"] = req->GetHeader("Access-Control-Request-Headers", "Content-Type");
+            status_code = HTTP_STATUS_NO_CONTENT;
+            goto postprocessor;
+        }
+    }
     if (service->preprocessor) {
         status_code = customHttpHandler(service->preprocessor);
         if (status_code != 0) {

+ 8 - 1
http/server/HttpService.h

@@ -144,6 +144,8 @@ struct HV_EXPORT HttpService {
      * @client  bin/wget http://127.0.0.1:8080/downloads/test.zip
      */
     int limit_rate; // limit send rate, unit: KB/s
+
+    unsigned allow_cors             :1;
     unsigned enable_forward_proxy   :1;
 
     HttpService() {
@@ -163,6 +165,8 @@ struct HV_EXPORT HttpService {
         file_cache_stat_interval = DEFAULT_FILE_CACHE_STAT_INTERVAL;
         file_cache_expired_time = DEFAULT_FILE_CACHE_EXPIRED_TIME;
         limit_rate = -1; // unlimited
+
+        allow_cors = 0;
         enable_forward_proxy = 0;
     }
 
@@ -177,8 +181,11 @@ struct HV_EXPORT HttpService {
     // @retval / => /var/www/html/index.html
     std::string GetStaticFilepath(const char* path);
 
+    // CORS
+    void AllowCORS() { allow_cors = 1; }
+
     // forward proxy
-    void enableForwardProxy() { enable_forward_proxy = 1; }
+    void EnableForwardProxy() { enable_forward_proxy = 1; }
     // reverse proxy
     // Proxy("/api/v1/", "http://www.httpbin.org/");
     void Proxy(const char* path, const char* url);