add uclient_get_url_filename helper function
authorFelix Fietkau <nbd@openwrt.org>
Tue, 6 May 2014 13:03:21 +0000 (15:03 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Tue, 6 May 2014 13:03:21 +0000 (15:03 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
uclient-fetch.c
uclient-utils.c
uclient-utils.h

index 9672e07..8dcb97b 100644 (file)
@@ -25,6 +25,7 @@
 #include <libubox/blobmsg.h>
 
 #include "uclient.h"
 #include <libubox/blobmsg.h>
 
 #include "uclient.h"
+#include "uclient-utils.h"
 
 #ifdef __APPLE__
 #define LIB_EXT "dylib"
 
 #ifdef __APPLE__
 #define LIB_EXT "dylib"
@@ -42,10 +43,9 @@ static int error_ret;
 
 static int open_output_file(const char *path, bool create)
 {
 
 static int open_output_file(const char *path, bool create)
 {
-       const char *str;
        char *filename;
        char *filename;
-       int len;
        int flags = O_WRONLY;
        int flags = O_WRONLY;
+       int ret;
 
        if (create)
                flags |= O_CREAT;
 
        if (create)
                flags |= O_CREAT;
@@ -61,27 +61,11 @@ static int open_output_file(const char *path, bool create)
        if (create)
                flags |= O_EXCL;
 
        if (create)
                flags |= O_EXCL;
 
-       len = strcspn(path, ";&");
-       while (len > 0 && path[len - 1] == '/')
-               len--;
-
-       for (str = path + len - 1; str >= path; str--) {
-               if (*str == '/')
-                       break;
-       }
-
-       str++;
-       len -= str - path;
-
-       if (len > 0) {
-               filename = alloca(len + 1);
-               strncpy(filename, str, len);
-               filename[len] = 0;
-       } else {
-               filename = "index.html";
-       }
+       filename = uclient_get_url_filename(path, "index.html");
+       ret = open(filename, flags, 0644);
+       free(filename);
 
 
-       return open(filename, flags, 0644);
+       return ret;
 }
 
 static void request_done(struct uclient *cl)
 }
 
 static void request_done(struct uclient *cl)
index 93a6880..a375eea 100644 (file)
@@ -160,3 +160,25 @@ void http_digest_calculate_response(char *dest, const struct http_digest_data *d
        http_create_hash(dest, h_a2_strings, ARRAY_SIZE(h_a2_strings));
        http_create_hash(dest, resp_strings, ARRAY_SIZE(resp_strings));
 }
        http_create_hash(dest, h_a2_strings, ARRAY_SIZE(h_a2_strings));
        http_create_hash(dest, resp_strings, ARRAY_SIZE(resp_strings));
 }
+
+char *uclient_get_url_filename(const char *url, const char *default_name)
+{
+       const char *str;
+       int len = strcspn(url, ";&");
+
+       while (len > 0 && url[len - 1] == '/')
+               len--;
+
+       for (str = url + len - 1; str >= url; str--) {
+               if (*str == '/')
+                       break;
+       }
+
+       str++;
+       len -= str - url;
+
+       if (len > 0)
+               return strncpy(calloc(1, len + 1), str, len);
+
+       return strdup(default_name);
+}
index 3c52eff..a4f975d 100644 (file)
@@ -44,4 +44,6 @@ int uclient_urldecode(const char *in, char *out, bool decode_plus);
 void http_digest_calculate_auth_hash(char *dest, const char *user, const char *realm, const char *password);
 void http_digest_calculate_response(char *dest, const struct http_digest_data *data);
 
 void http_digest_calculate_auth_hash(char *dest, const char *user, const char *realm, const char *password);
 void http_digest_calculate_response(char *dest, const struct http_digest_data *data);
 
+char *uclient_get_url_filename(const char *url, const char *default_name);
+
 #endif
 #endif