From a01e72e651112ddd3fb7777fb4eaa09e05bbc18a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zden=C4=9Bk=20=C5=A0ustr?= Date: Tue, 7 Aug 2012 15:27:43 +0000 Subject: [PATCH] New function for unescaping percent-escaped characters in URLs --- org.glite.lbjp-common.trio/interface/escape.h | 9 +++++++++ org.glite.lbjp-common.trio/src/escape.c | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/org.glite.lbjp-common.trio/interface/escape.h b/org.glite.lbjp-common.trio/interface/escape.h index 3524e7d..f48445c 100644 --- a/org.glite.lbjp-common.trio/interface/escape.h +++ b/org.glite.lbjp-common.trio/interface/escape.h @@ -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__ */ diff --git a/org.glite.lbjp-common.trio/src/escape.c b/org.glite.lbjp-common.trio/src/escape.c index 3077aa7..4e81b5b 100644 --- a/org.glite.lbjp-common.trio/src/escape.c +++ b/org.glite.lbjp-common.trio/src/escape.c @@ -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); +} + -- 1.8.2.3