src/server/safs/proxy.c

changeset 696
27e42da5050f
parent 694
a5aa94800b59
child 699
d794871da099
--- a/src/server/safs/proxy.c	Sun Feb 22 09:24:41 2026 +0100
+++ b/src/server/safs/proxy.c	Sun Feb 22 09:33:48 2026 +0100
@@ -34,47 +34,9 @@
 #include <string.h>
 
 #include "../util/pblock.h"
+#include "../util/util.h"
 #include "../proxy/httpclient.h"
 
-// Gets the uri part from an http request line
-static cxstring get_uri_from_clfreq(const char *clfreq) {
-    cxstring uri = { NULL, 0 };
-    
-    const char *begin = NULL;
-    const char *str = clfreq;
-    for(;*str != '\0';str++) {
-        if(*str < 33) {
-            if(begin) {
-                str++;
-                break;
-            }
-        } else {
-            if(!begin) {
-                begin = str;
-            }
-        }
-    }
-    
-    begin = NULL;
-    for(;*str != '\0';str++) {
-        if(*str > 32) {
-            if(!begin) {
-                begin = str;
-            }
-        } else {
-            if(begin) {
-                break;
-            }
-        }
-    }
-    
-    if(begin && *str != '\0') {
-        return cx_strn(begin, str-begin);
-    }
-    
-    return uri;
-    
-}
 
 typedef struct ProxyRequest {
     Session *sn;
@@ -177,11 +139,19 @@
 int http_reverse_proxy_service(pblock *param, Session *sn, Request *rq) {
     EventHandler *ev = sn->ev;
     const char *method = pblock_findkeyval(pb_key_method, rq->reqpb);
-    const char *clfreq = pblock_findkeyval(pb_key_clf_request, rq->reqpb);
+    const char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
+    const char *query = pblock_findkeyval(pb_key_query, rq->reqpb);
     
-    cxstring uri = get_uri_from_clfreq(clfreq);
-    if(uri.length == 0) {
-        return REQ_ABORTED;
+    cxmutstr new_uri = CX_NULLSTR;
+    size_t uri_len = strlen(uri);
+    size_t query_len = query ? strlen(query) : 0;
+    size_t new_uri_alloc = ((uri_len + query_len) * 3) + 2;
+    new_uri.ptr = pool_malloc(sn->pool, new_uri_alloc);
+    new_uri.length = util_uri_escape_s(new_uri.ptr, new_uri_alloc, uri);
+    if(new_uri.length > 0 && query_len > 0) {
+        new_uri.ptr[new_uri.length] = '?';
+        memcpy(new_uri.ptr + new_uri.length + 1, query, query_len + 1);
+        new_uri.length = new_uri.length + 1 + query_len;
     }
     
     // remove some response headers, that were previously set by ObjectType
@@ -215,7 +185,7 @@
         return REQ_ABORTED;
     }
     
-    if(http_client_set_uri_len(client, uri.ptr, uri.length)) {
+    if(http_client_set_uri_len(client, new_uri.ptr, new_uri.length)) {
         http_client_free(client);
         return REQ_ABORTED;
     }
@@ -293,43 +263,3 @@
     return REQ_PROCESSING;
 }
 
-
-
-/* --------------------------------- Tests --------------------------------- */
-
-static CX_TEST(test_safs_proxy_get_uri_from_clfreq) {
-    CX_TEST_DO {
-        cxstring ret;
-        
-        ret = get_uri_from_clfreq("GET /uri HTTP/1.1");
-        CX_TEST_ASSERT(!cx_strcmp(ret, "/uri"));
-        ret = get_uri_from_clfreq("G / HTTP/1.1");
-        CX_TEST_ASSERT(!cx_strcmp(ret, "/"));
-        ret = get_uri_from_clfreq("POST   /test%20/path HTTP/1.1");
-        CX_TEST_ASSERT(!cx_strcmp(ret, "/test%20/path"));
-        ret = get_uri_from_clfreq("   GET /leading_space HTTP/1.1");
-        CX_TEST_ASSERT(!cx_strcmp(ret, "/leading_space"));
-        ret = get_uri_from_clfreq("   PROPFIND   /space2 HTTP/1.1");
-        CX_TEST_ASSERT(!cx_strcmp(ret, "/space2"));
-        ret = get_uri_from_clfreq("HEAD /trailing_space     HTTP/1.1");
-        CX_TEST_ASSERT(!cx_strcmp(ret, "/trailing_space"));
-        ret = get_uri_from_clfreq("   GET   /space3     HTTP/1.1   ");
-        CX_TEST_ASSERT(!cx_strcmp(ret, "/space3"));
-        
-        // fail test
-        ret = get_uri_from_clfreq("");
-        CX_TEST_ASSERT(ret.ptr == NULL);
-        ret = get_uri_from_clfreq("  ");
-        CX_TEST_ASSERT(ret.ptr == NULL);
-        ret = get_uri_from_clfreq("GET");
-        CX_TEST_ASSERT(ret.ptr == NULL);
-        ret = get_uri_from_clfreq("GET /path");
-        CX_TEST_ASSERT(ret.ptr == NULL);
-        ret = get_uri_from_clfreq(" /path2/ ");
-        CX_TEST_ASSERT(ret.ptr == NULL);
-    }
-}
-
-void http_reverse_proxy_add_tests(CxTestSuite *suite) {
-    cx_test_register(suite, test_safs_proxy_get_uri_from_clfreq);
-}

mercurial