merge

Thu, 15 Oct 2015 17:45:00 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 15 Oct 2015 17:45:00 +0200
changeset 175
9e14d920a7d0
parent 172
cd9f1e772fd0 (current diff)
parent 174
e7e56c56d126 (diff)
child 176
747f3796eddd

merge

--- a/libidav/utils.c	Thu Oct 15 17:44:41 2015 +0200
+++ b/libidav/utils.c	Thu Oct 15 17:45:00 2015 +0200
@@ -286,65 +286,40 @@
 static size_t util_header_callback(char *buffer, size_t size,
         size_t nitems, void *data) {
     
-    char *duped; // local variable for string duplicates
+    sstr_t sbuffer = sstrn(buffer, size*nitems);
     
     UcxMap *map = (UcxMap*) data;
     
     // if we get a status line, clear the map and exit
-    if(size*nitems >= 5 && !strncmp("HTTP/", buffer, 5)) {
+    if(sstrprefix(sbuffer, S("HTTP/"))) {
         ucx_map_free_content(map, free);
         ucx_map_clear(map);
         return size*nitems;
     }
     
     // if we get the terminating CRLF, just exit
-    if (size*nitems == 2 && !strncmp("\r\n", buffer, 2)) {
+    if(!sstrcmp(sbuffer, S("\r\n"))) {
         return 2;
     }
     
+    sstr_t key = sbuffer;
+    sstr_t value = sstrchr(sbuffer, ':');
     
-    // get header key
-    size_t s = 0;
-    do {
-        s++;
-    } while(buffer[s] != ':');
-    char *value = buffer+s+1;
-    
-    // remove trailing spaces
-    while(isspace(buffer[s-1])) {
-        s--;
+    if(value.length == 0) {
+        return 0; // invalid header line
     }
     
-    // save key as all lower case
-    duped = malloc(s);
-    for(size_t i = 0;i<s;i++) {
-        duped[i] = tolower(buffer[i]);
-    }
-    UcxKey key = ucx_key(duped, s);
+    key.length = value.ptr - key.ptr;
+    value.ptr++; value.length--;
     
-    // get trimmed header value
-    while(isspace(*value)) {
-        value++;
-    }
-    s = size*nitems - (value - buffer);
-    while(isspace(value[s-1])) {
-        s--;
-    }
+    key = sstrlower(sstrtrim(key));
+    value = sstrdup(sstrtrim(value));
+        
+    ucx_map_sstr_put(map, key, value.ptr);
     
-    // copy header value and append NULL byte (curl does not provide one)
-    duped = malloc(s+1);
-    memcpy(duped, value, s+1);
-    duped[s] = '\0';
+    free(key.ptr);
     
-    char *oldval = ucx_map_get(map, key);
-    if(oldval) {
-        free(oldval);
-    }
-    
-    ucx_map_put(map, key, duped);
-    free(key.data); // ucx_map_put made a copy, so we free this memory
-    
-    return nitems * size;    
+    return sbuffer.length;
 }
 
 void util_capture_header(CURL *handle, UcxMap* map) {
--- a/ucx/string.c	Thu Oct 15 17:44:41 2015 +0200
+++ b/ucx/string.c	Thu Oct 15 17:45:00 2015 +0200
@@ -339,3 +339,35 @@
             suffix.ptr, suffix.length) == 0;
     }
 }
+
+sstr_t sstrlower(sstr_t string) {
+    sstr_t ret = sstrdup(string);
+    for (size_t i = 0; i < ret.length ; i++) {
+        ret.ptr[i] = tolower(ret.ptr[i]);
+    }
+    return ret;
+}
+
+sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string) {
+    sstr_t ret = sstrdup_a(allocator, string);
+    for (size_t i = 0; i < ret.length ; i++) {
+        ret.ptr[i] = tolower(ret.ptr[i]);
+    }
+    return ret;
+}
+
+sstr_t sstrupper(sstr_t string) {
+    sstr_t ret = sstrdup(string);
+    for (size_t i = 0; i < ret.length ; i++) {
+        ret.ptr[i] = toupper(ret.ptr[i]);
+    }
+    return ret;
+}
+
+sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string) {
+    sstr_t ret = sstrdup_a(allocator, string);
+    for (size_t i = 0; i < ret.length ; i++) {
+        ret.ptr[i] = toupper(ret.ptr[i]);
+    }
+    return ret;
+}
--- a/ucx/string.h	Thu Oct 15 17:44:41 2015 +0200
+++ b/ucx/string.h	Thu Oct 15 17:45:00 2015 +0200
@@ -383,6 +383,56 @@
  */
 int sstrsuffix(sstr_t string, sstr_t suffix);
 
+/**
+ * Returns a lower case version of a string.
+ * 
+ * This function creates a duplicate of the input string, first. See the
+ * documentation of sstrdup() for the implications.
+ * 
+ * @param string the input string
+ * @return the resulting lower case string
+ * @see sstrdup()
+ */
+sstr_t sstrlower(sstr_t string);
+
+/**
+ * Returns a lower case version of a string.
+ * 
+ * This function creates a duplicate of the input string, first. See the
+ * documentation of sstrdup_a() for the implications.
+ * 
+ * @param allocator the allocator used for duplicating the string
+ * @param string the input string
+ * @return the resulting lower case string
+ * @see sstrdup_a()
+ */
+sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string);
+
+/**
+ * Returns a upper case version of a string.
+ * 
+ * This function creates a duplicate of the input string, first. See the
+ * documentation of sstrdup() for the implications.
+ * 
+ * @param string the input string
+ * @return the resulting upper case string
+ * @see sstrdup()
+ */
+sstr_t sstrupper(sstr_t string);
+
+/**
+ * Returns a upper case version of a string.
+ * 
+ * This function creates a duplicate of the input string, first. See the
+ * documentation of sstrdup_a() for the implications.
+ * 
+ * @param allocator the allocator used for duplicating the string
+ * @param string the input string
+ * @return the resulting upper case string
+ * @see sstrdup_a()
+ */
+sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string);
+
 #ifdef	__cplusplus
 }
 #endif

mercurial