author | Michael Krelin <hacker@klever.net> | 2008-02-02 10:50:48 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-02-02 10:50:48 (UTC) |
commit | b7ce9a84f0775eb24f0a27d3816bf57b774a2927 (patch) (unidiff) | |
tree | 77d3bf4830e9d45824bc466c7e1b5279f3ff1111 | |
parent | f29ad6501686e5f43b536258b86d12683c711f02 (diff) | |
download | libopkele-b7ce9a84f0775eb24f0a27d3816bf57b774a2927.zip libopkele-b7ce9a84f0775eb24f0a27d3816bf57b774a2927.tar.gz libopkele-b7ce9a84f0775eb24f0a27d3816bf57b774a2927.tar.bz2 |
added util::attr_escape
escaping string for inclusion into x(ht)ml attributes
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | include/opkele/util.h | 7 | ||||
-rw-r--r-- | lib/util.cc | 19 |
2 files changed, 26 insertions, 0 deletions
diff --git a/include/opkele/util.h b/include/opkele/util.h index 6f3ddf6..719f951 100644 --- a/include/opkele/util.h +++ b/include/opkele/util.h | |||
@@ -79,48 +79,55 @@ namespace opkele { | |||
79 | /** | 79 | /** |
80 | * Convert internal time representation to w3c format | 80 | * Convert internal time representation to w3c format |
81 | * @param t internal representation | 81 | * @param t internal representation |
82 | * @return w3c time | 82 | * @return w3c time |
83 | * @throw failed_conversion in case of error | 83 | * @throw failed_conversion in case of error |
84 | */ | 84 | */ |
85 | string time_to_w3c(time_t t); | 85 | string time_to_w3c(time_t t); |
86 | /** | 86 | /** |
87 | * Convert W3C time representation to internal time_t | 87 | * Convert W3C time representation to internal time_t |
88 | * @param w w3c representation | 88 | * @param w w3c representation |
89 | * @return converted time | 89 | * @return converted time |
90 | * @throw failed_conversion in case of error | 90 | * @throw failed_conversion in case of error |
91 | */ | 91 | */ |
92 | time_t w3c_to_time(const string& w); | 92 | time_t w3c_to_time(const string& w); |
93 | 93 | ||
94 | /** | 94 | /** |
95 | * Encode string to the representation suitable for using in URL. | 95 | * Encode string to the representation suitable for using in URL. |
96 | * @param str string to encode | 96 | * @param str string to encode |
97 | * @return encoded string | 97 | * @return encoded string |
98 | * @throw failed_conversion in case of failure | 98 | * @throw failed_conversion in case of failure |
99 | */ | 99 | */ |
100 | string url_encode(const string& str); | 100 | string url_encode(const string& str); |
101 | 101 | ||
102 | /** | 102 | /** |
103 | * Make string suitable for using as x(ht)ml attribute. | ||
104 | * @param str string to escape | ||
105 | * @return escaped string | ||
106 | */ | ||
107 | string attr_escape(const string& str); | ||
108 | |||
109 | /** | ||
103 | * Convert number to string | 110 | * Convert number to string |
104 | * @param l number | 111 | * @param l number |
105 | * @return string representation | 112 | * @return string representation |
106 | * @throw failed_conversion in case of failure | 113 | * @throw failed_conversion in case of failure |
107 | */ | 114 | */ |
108 | string long_to_string(long l); | 115 | string long_to_string(long l); |
109 | /** | 116 | /** |
110 | * Convert string to number | 117 | * Convert string to number |
111 | * @param s string, containing the number | 118 | * @param s string, containing the number |
112 | * @return the number | 119 | * @return the number |
113 | * @throw failed_conversion in case of failure | 120 | * @throw failed_conversion in case of failure |
114 | */ | 121 | */ |
115 | long string_to_long(const string& s); | 122 | long string_to_long(const string& s); |
116 | 123 | ||
117 | /** | 124 | /** |
118 | * Encode binary data using base64. | 125 | * Encode binary data using base64. |
119 | * @param data pointer to binary data | 126 | * @param data pointer to binary data |
120 | * @param length length of data | 127 | * @param length length of data |
121 | * @return encoded data | 128 | * @return encoded data |
122 | */ | 129 | */ |
123 | string encode_base64(const void *data,size_t length); | 130 | string encode_base64(const void *data,size_t length); |
124 | /** | 131 | /** |
125 | * Decode binary data from base64 representation. | 132 | * Decode binary data from base64 representation. |
126 | * @param data base64-encoded data | 133 | * @param data base64-encoded data |
diff --git a/lib/util.cc b/lib/util.cc index 54d6535..b7bc437 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -140,48 +140,67 @@ namespace opkele { | |||
140 | ) != 7 | 140 | ) != 7 |
141 | ) ) | 141 | ) ) |
142 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 142 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
143 | tm_t.tm_mon--; | 143 | tm_t.tm_mon--; |
144 | tm_t.tm_year-=1900; | 144 | tm_t.tm_year-=1900; |
145 | time_t rv = mktime(&tm_t); | 145 | time_t rv = mktime(&tm_t); |
146 | if(rv==(time_t)-1) | 146 | if(rv==(time_t)-1) |
147 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 147 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
148 | return rv-timezone; | 148 | return rv-timezone; |
149 | } | 149 | } |
150 | 150 | ||
151 | /* | 151 | /* |
152 | * | 152 | * |
153 | */ | 153 | */ |
154 | 154 | ||
155 | string url_encode(const string& str) { | 155 | string url_encode(const string& str) { |
156 | char * t = curl_escape(str.c_str(),str.length()); | 156 | char * t = curl_escape(str.c_str(),str.length()); |
157 | if(!t) | 157 | if(!t) |
158 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); | 158 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); |
159 | string rv(t); | 159 | string rv(t); |
160 | curl_free(t); | 160 | curl_free(t); |
161 | return rv; | 161 | return rv; |
162 | } | 162 | } |
163 | 163 | ||
164 | string attr_escape(const string& str) { | ||
165 | static const char *unsafechars = "<>&\n\"'"; | ||
166 | string rv; | ||
167 | string::size_type p=0; | ||
168 | while(true) { | ||
169 | string::size_type us = str.find_first_of(unsafechars,p); | ||
170 | if(us==string::npos) { | ||
171 | if(p!=str.length()) | ||
172 | rv.append(str,p,str.length()-p); | ||
173 | return rv; | ||
174 | } | ||
175 | rv.append(str,p,us-p); | ||
176 | rv += "&#"; | ||
177 | rv += long_to_string((long)str[us]); | ||
178 | rv += ';'; | ||
179 | p = us+1; | ||
180 | } | ||
181 | } | ||
182 | |||
164 | string long_to_string(long l) { | 183 | string long_to_string(long l) { |
165 | char rv[32]; | 184 | char rv[32]; |
166 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 185 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
167 | if(r<0 || r>=(int)sizeof(rv)) | 186 | if(r<0 || r>=(int)sizeof(rv)) |
168 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 187 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
169 | return rv; | 188 | return rv; |
170 | } | 189 | } |
171 | 190 | ||
172 | long string_to_long(const string& s) { | 191 | long string_to_long(const string& s) { |
173 | char *endptr = 0; | 192 | char *endptr = 0; |
174 | long rv = strtol(s.c_str(),&endptr,10); | 193 | long rv = strtol(s.c_str(),&endptr,10); |
175 | if((!endptr) || endptr==s.c_str()) | 194 | if((!endptr) || endptr==s.c_str()) |
176 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 195 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
177 | return rv; | 196 | return rv; |
178 | } | 197 | } |
179 | 198 | ||
180 | /* | 199 | /* |
181 | * Normalize URL according to the rules, described in rfc 3986, section 6 | 200 | * Normalize URL according to the rules, described in rfc 3986, section 6 |
182 | * | 201 | * |
183 | * - uppercase hex triplets (e.g. %ab -> %AB) | 202 | * - uppercase hex triplets (e.g. %ab -> %AB) |
184 | * - lowercase scheme and host | 203 | * - lowercase scheme and host |
185 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, | 204 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, |
186 | * that is - [:alpha:][:digit:]._~- | 205 | * that is - [:alpha:][:digit:]._~- |
187 | * - remove dot segments | 206 | * - remove dot segments |