New function for unescaping percent-escaped characters in URLs
authorZdeněk Šustr <sustr4@cesnet.cz>
Tue, 7 Aug 2012 15:27:43 +0000 (15:27 +0000)
committerZdeněk Šustr <sustr4@cesnet.cz>
Tue, 7 Aug 2012 15:27:43 +0000 (15:27 +0000)
org.glite.lbjp-common.trio/interface/escape.h
org.glite.lbjp-common.trio/src/escape.c

index 3524e7d..f48445c 100644 (file)
@@ -83,4 +83,13 @@ char *glite_lbu_EscapeSQL(const char *);
 
 char *glite_lbu_EscapeJSON(const char *in);
 
+/*!
+ * \fn char *glite_lbu_UnescapeURL(const char *str)
+ * \param str          a string to unescape
+ * \return             new (allocated) unescaped string
+ * \brief in given string unescape all percent-encoded characters
+ */
+
+char *glite_lbu_UnescapeURL(const char *);
+
 #endif /* __EDG_WORKLOAD_LOGGING_COMMON_ESCAPE_H__ */
index 3077aa7..4e81b5b 100644 (file)
@@ -310,3 +310,29 @@ char *glite_lbu_EscapeJSON(const char *in) {
 
        return out;
 }
+
+char *glite_lbu_UnescapeURL(const char *in) {
+       char *out;
+       char *spec = in;
+       char *reserved;
+       unsigned int val;
+
+       if(!in) return NULL;
+       out = (char*)calloc(strlen(in), sizeof(char));
+
+       strncpy(out, spec, strcspn(spec, "%")); // Copy the first part of the string up to the first '%'
+
+       while ((spec = strchr(spec, '%'))) {
+               if(sscanf(spec+1, "%02X", &val)) { //Treat as percent-escaped hex if the format is right
+                       asprintf(&reserved,"%c", val);
+                       strcat(out, reserved);
+                       free(reserved);
+                       spec = spec + 3;
+               }
+               else spec = spec + 1; //Otherwise just skip the '%' sign. This should not happen. This increment is here just to prevent cycle lockups in case of browser malfunction.
+               strncpy(out+strlen(out), spec, strcspn(spec, "%"));
+       }
+
+       return(out);
+}
+