File httpfs-URL of Package httpfs
# HG changeset patch
# User Michal Suchánek <msuchanek@suse.de>
# Date 1676322135 -3600
# Mon Feb 13 22:02:15 2023 +0100
# Node ID 995982d79044d301e8f39a81928185c9a1dd869d
# Parent 068c0fd6d16b7339a05e43231fb5166d825e781a
Add URL encoding https://sourceforge.net/p/httpfs/patches/2/
diff -r 068c0fd6d16b -r 995982d79044 httpfs2.c
--- a/httpfs2.c Mon May 30 22:49:38 2022 +0300
+++ b/httpfs2.c Mon Feb 13 22:02:15 2023 +0100
@@ -45,6 +45,8 @@
#include <stddef.h>
#include <inttypes.h>
+#define ENCODE_MODE_PATH 1
+
#ifdef USE_THREAD
#include <pthread.h>
static pthread_key_t url_key;
@@ -694,10 +696,67 @@
errno = e;
}
-static char * url_encode(char * path) {
- return strdup(path); /*FIXME encode*/
+
+/*
+ * VERSION 2
+ *
+ * Behavior - never encodes reserved charecters if ENCODE_MODE_PATH is used.
+ * - never encodes allowd characters.
+ * - never encodes a-zA-Z0-9
+ *
+ * required - define ENCODE_MODE_PATH
+ * use - url_encode("some/path",ENCODE_MODE_PATH)
+ *
+ *
+ * url_encode - Returns a pointer to url encoded character string accordning to rules:
+ * Returned pointer needs to be freed by user.
+ * params - path : path to be encoded
+ * - mode : mode to use while encoding. Added ENCODE_MODE_PATH, use it
+ * to encode paths. It will not convert reserved '/' characters.
+ *
+ * contributed by Arek Bochinski http://zenebo.com/word
+ */
+static char * url_encode(char * path, int mode)
+{
+ int i, j = 0;
+ char * hex_table = "0123456789ABCDEF";
+ char * allowed = "$-_.+!*'(),";
+ char * reserved = ";/?:@=&";
+ unsigned char c;
+ size_t len = strlen(path);
+ char * dest = malloc(3 * len + 1);
+
+ if (!dest)
+ return dest;
+
+ for(i = 0; i < len; i++)
+ {
+ c = (unsigned char) path[i];
+
+ if(
+ ((c >= 'a') && (c <= 'z')) ||
+ ((c >= 'A') && (c <= 'Z')) ||
+ ((c >= '0') && (c <= '9')) ||
+ (strchr(allowed, c) != NULL) ||
+ ((mode == ENCODE_MODE_PATH) && (strchr(reserved, c) != NULL))
+ )
+ {
+ dest[i + j] = path[i];
+ }
+ else
+ {
+ dest[i + j] = '%';
+ dest[i + j + 1] = hex_table[c / 16];
+ dest[i + j + 2] = hex_table[c % 16];
+
+ j += 2;
+ }
+ }
+
+ return dest;
}
+
/*
* functions for handling struct_url
*/
@@ -813,7 +872,7 @@
if(res->path)
free(res->path);
if(strchr(url, path_start))
- res->path = url_encode(strchr(url, path_start));
+ res->path = url_encode(strchr(url, path_start), ENCODE_MODE_PATH);
else{
path_start = 0;
res->path = strdup("/");