src/server/safs/service.c

changeset 415
d938228c382e
parent 346
784b24381bed
child 428
ab58e46b50a5
--- a/src/server/safs/service.c	Wed Nov 02 19:19:01 2022 +0100
+++ b/src/server/safs/service.c	Sun Nov 06 15:53:32 2022 +0100
@@ -38,8 +38,9 @@
 #include "../daemon/vfs.h"
 
 #include "../util/strbuf.h"
-#include <ucx/string.h>
-#include <ucx/utils.h>
+#include <cx/string.h>
+#include <cx/utils.h>
+#include <cx/printf.h>
 
 #include <errno.h>
 
@@ -112,14 +113,14 @@
 static HttpRange* parse_range(Session *sn, char *header, int *status) {
     *status = PROTOCOL_OK;
     
-    sstr_t range = sstrtrim(sstr(header));
-    if(!sstrprefix(range, S("bytes="))) {
+    cxstring range = cx_strtrim(cx_str(header));
+    if(!cx_strprefix(range, (cxstring)CX_STR("bytes="))) {
         // unknown range unit - ignore range header
         return NULL;
     }
     
     // get byte-range-set
-    range = sstrsubs(range, 6);
+    range = cx_strsubs(range, 6);
     if(range.length < 1) {
         return NULL;
     }
@@ -132,7 +133,7 @@
     for(int i=0;i<=range.length;i++) {
         char c = range.ptr[i];
         if(c == '-') {
-            sstr_t num = sstrsubsl(range, start, i-start);
+            cxstring num = cx_strsubsl(range, start, i-start);
             if(num.length == 0) {
                 // empty string before '-' is legal
                 hasbegin = 1;
@@ -152,7 +153,7 @@
                 return NULL;
             }
         } else if(c == ',' || c == '\0') {
-            sstr_t num = sstrsubsl(range, start, i-start);
+            cxstring num = cx_strsubsl(range, start, i-start);
             if(hasbegin) {
                 long long n;
                 if(num.length == 0) {
@@ -495,24 +496,26 @@
 }
 
 struct multi_range_elm {
-    sstr_t header;
+    cxmutstr header;
     off_t  offset;
     off_t  length;
 };
 
 static int send_multi_range(Session *sn, Request *rq, SYS_FILE fd, off_t filelen, HttpRange *range) {
+    CxAllocator *a = pool_allocator(sn->pool);
+    
     pb_param *content_type = pblock_remove("content-type", rq->srvhdrs);
     
     char sep[64];
     int seplen = util_mime_separator(sep);
     
-    sstr_t newct = ucx_sprintf("multipart/byteranges; boundary=%s", sep+4);
+    cxmutstr newct = cx_asprintf_a(a, "multipart/byteranges; boundary=%s", sep+4);
     pblock_kvinsert(
             pb_key_content_type,
             newct.ptr,
             newct.length,
             rq->srvhdrs);
-    free(newct.ptr);
+    cxFree(a, newct.ptr);
     
     // calculate content-length
     off_t response_len = 0;
@@ -524,12 +527,13 @@
         rangeelm = rangeelm->next;
     }
     
-    struct multi_range_elm *r = calloc(nrange, sizeof(struct multi_range_elm));
+    struct multi_range_elm *r = pool_calloc(sn->pool, nrange, sizeof(struct multi_range_elm));
     rangeelm = range;
     int i=0;
     while(rangeelm) {
         range2off(rangeelm, filelen, &(r[i].offset), &(r[i].length));
-        r[i].header = ucx_sprintf(
+        r[i].header = cx_asprintf_a(
+                a,
                 "%s\r\nContent-Type: %s\r\nContent-Range: bytes %lld-%lld/%lld\r\n\r\n",
                 sep,
                 content_type->value,
@@ -565,7 +569,7 @@
     }
     net_printf(sn->csd, "%s--\r\n", sep);
     
-    free(r);
+    pool_free(sn->pool, r);
     return 0;
 }
 
@@ -616,7 +620,7 @@
                     (long long)length,
                     rq->srvhdrs);
             
-            sstr_t content_range = ucx_sprintf(
+            cxmutstr content_range = cx_asprintf(
                     "%lld-%lld/%lld",
                     (long long)offset,
                     (long long)offset+length - 1,
@@ -696,7 +700,7 @@
     char *path = pblock_findkeyval(pb_key_path, rq->vars);
     char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
 
-    sstr_t r_uri = sstr(uri);
+    cxstring r_uri = cx_str(uri);
 
     // open the file
     VFSContext *vfs = vfs_request_context(sn, rq);
@@ -717,7 +721,7 @@
     // list directory
     VFS_ENTRY f;
     while(vfs_readdir(dir, &f)) {
-        sstr_t filename = sstr(f.name);
+        cxstring filename = cx_str(f.name);
 
         sbuf_puts(out, "<a href=\"");
         sbuf_append(out, r_uri);

mercurial