-rw-r--r-- | lib/util.cc | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/util.cc b/lib/util.cc index 54d6535..b7bc437 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -132,64 +132,83 @@ namespace opkele { | |||
132 | ) != 6 | 132 | ) != 6 |
133 | ) && ( | 133 | ) && ( |
134 | sscanf( | 134 | sscanf( |
135 | w.c_str(), | 135 | w.c_str(), |
136 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | 136 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
137 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 137 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
138 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, | 138 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, |
139 | &fraction | 139 | &fraction |
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 |
188 | * - remove empty and default ports | 207 | * - remove empty and default ports |
189 | * - if there's no path component, add '/' | 208 | * - if there's no path component, add '/' |
190 | */ | 209 | */ |
191 | string rfc_3986_normalize_uri(const string& uri) { | 210 | string rfc_3986_normalize_uri(const string& uri) { |
192 | static const char *whitespace = " \t\r\n"; | 211 | static const char *whitespace = " \t\r\n"; |
193 | string rv; | 212 | string rv; |
194 | string::size_type ns = uri.find_first_not_of(whitespace); | 213 | string::size_type ns = uri.find_first_not_of(whitespace); |
195 | if(ns==string::npos) | 214 | if(ns==string::npos) |