1
0
Эх сурвалжийг харах

Add HttpService::enableForwardProxy

ithewei 3 жил өмнө
parent
commit
8923734f62

+ 18 - 6
http/server/HttpHandler.cpp

@@ -232,18 +232,26 @@ void HttpHandler::onHeadersComplete() {
             }
         };
     } else {
-        if (!proxy && service->proxies.size() != 0) {
+        if (proxy) {
+            // forward proxy
+            if (service->enable_forward_proxy) {
+                proxyConnect(pReq->url);
+            } else {
+                proxy = 0;
+                resp->status_code = HTTP_STATUS_FORBIDDEN;
+                hlogw("Forbidden to forward proxy %s", pReq->url.c_str());
+            }
+        }
+        else if (service->proxies.size() != 0) {
             // reverse proxy
             std::string proxy_url = service->GetProxyUrl(pReq->path.c_str());
             if (!proxy_url.empty()) {
                 proxy = 1;
                 pReq->url = proxy_url;
+                proxyConnect(pReq->url);
             }
         }
-
-        if (proxy) {
-            proxyConnect(pReq->url);
-        } else {
+        else {
             // TODO: rewrite
         }
     }
@@ -311,13 +319,17 @@ int HttpHandler::proxyConnect(const std::string& strUrl) {
 
 int HttpHandler::HandleHttpRequest() {
     // preprocessor -> processor -> postprocessor
-    int status_code = HTTP_STATUS_OK;
     HttpRequest* pReq = req.get();
     HttpResponse* pResp = resp.get();
 
     // NOTE: Not all users want to parse body, we comment it out.
     // pReq->ParseBody();
 
+    int status_code = pResp->status_code;
+    if (status_code != HTTP_STATUS_OK) {
+        goto postprocessor;
+    }
+
 preprocessor:
     state = HANDLE_BEGIN;
     if (service->preprocessor) {

+ 5 - 0
http/server/HttpService.h

@@ -144,6 +144,7 @@ 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 enable_forward_proxy   :1;
 
     HttpService() {
         // base_url = DEFAULT_BASE_URL;
@@ -162,6 +163,7 @@ 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
+        enable_forward_proxy = 0;
     }
 
     void AddApi(const char* path, http_method method, const http_handler& handler);
@@ -175,6 +177,9 @@ struct HV_EXPORT HttpService {
     // @retval / => /var/www/html/index.html
     std::string GetStaticFilepath(const char* path);
 
+    // forward proxy
+    void enableForwardProxy() { enable_forward_proxy = 1; }
+    // reverse proxy
     // Proxy("/api/v1/", "http://www.httpbin.org/");
     void Proxy(const char* path, const char* url);
     // @retval /api/v1/test => http://www.httpbin.org/test