/src/httrack/src/htsurlport.c
Line | Count | Source |
1 | | /* ------------------------------------------------------------ */ |
2 | | /* |
3 | | HTTrack Website Copier, Offline Browser for Windows and Unix |
4 | | Copyright (C) 1998 Xavier Roche and other contributors |
5 | | |
6 | | SPDX-License-Identifier: GPL-3.0-or-later |
7 | | |
8 | | This program is free software: you can redistribute it and/or modify |
9 | | it under the terms of the GNU General Public License as published by |
10 | | the Free Software Foundation, either version 3 of the License, or |
11 | | (at your option) any later version. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU General Public License |
19 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | | |
21 | | Ethical use: we kindly ask that you NOT use this software to harvest email |
22 | | addresses or to collect any other private information about people. Doing so |
23 | | would dishonor our work and waste the many hours we have spent on it. |
24 | | |
25 | | Please visit our Website: http://www.httrack.com |
26 | | */ |
27 | | |
28 | | /* ------------------------------------------------------------ */ |
29 | | /* File: bounded decimal, TCP port and separated-field parsers, */ |
30 | | /* shared by the engine, htsserver and proxytrack */ |
31 | | /* Author: Xavier Roche */ |
32 | | /* ------------------------------------------------------------ */ |
33 | | |
34 | | #include "htsurlport.h" |
35 | | #include "htsctype.h" |
36 | | #include "htsbuf.h" |
37 | | #include "htssafe.h" |
38 | | |
39 | | #include <string.h> |
40 | | |
41 | 0 | static hts_boolean is_decimal_digit(char c) { return c >= '0' && c <= '9'; } |
42 | | |
43 | 0 | hts_boolean hts_span_is_digits(hts_span s) { |
44 | 0 | size_t i; |
45 | |
|
46 | 0 | if (hts_span_empty(s)) |
47 | 0 | return HTS_FALSE; |
48 | 0 | for (i = 0; i < s.len; i++) { |
49 | 0 | if (!is_decimal_digit(s.p[i])) |
50 | 0 | return HTS_FALSE; |
51 | 0 | } |
52 | 0 | return HTS_TRUE; |
53 | 0 | } |
54 | | |
55 | 0 | hts_boolean hts_span_parse_llint(hts_span s, LLint min, LLint max, LLint *out) { |
56 | 0 | LLint value = 0; |
57 | 0 | size_t i; |
58 | |
|
59 | 0 | if (hts_span_empty(s)) |
60 | 0 | return HTS_FALSE; |
61 | 0 | for (i = 0; i < s.len; i++) { |
62 | 0 | const int digit = s.p[i] - '0'; |
63 | | |
64 | | // Checked before the multiply, so value * 10 + digit never passes max. |
65 | 0 | if (!is_decimal_digit(s.p[i]) || digit > max || value > (max - digit) / 10) |
66 | 0 | return HTS_FALSE; |
67 | 0 | value = value * 10 + digit; |
68 | 0 | } |
69 | 0 | if (value < min) |
70 | 0 | return HTS_FALSE; |
71 | 0 | *out = value; |
72 | 0 | return HTS_TRUE; |
73 | 0 | } |
74 | | |
75 | | hts_boolean hts_parse_llint(const char *s, const char **end, LLint min, |
76 | 0 | LLint max, LLint *out) { |
77 | 0 | const char *p = s; |
78 | |
|
79 | 0 | while (is_decimal_digit(*p)) |
80 | 0 | p++; |
81 | 0 | if (end != NULL) |
82 | 0 | *end = p; |
83 | 0 | return hts_span_parse_llint(hts_span_between(s, p), min, max, out); |
84 | 0 | } |
85 | | |
86 | | hts_scan_result hts_scan_llint(const char **s, LLint min, LLint max, |
87 | 0 | LLint *out) { |
88 | 0 | const char *p = *s; |
89 | 0 | const char *end; |
90 | 0 | LLint ignored; |
91 | |
|
92 | 0 | while (hts_isspace((unsigned char) *p)) |
93 | 0 | p++; |
94 | 0 | if (hts_parse_llint(p, &end, min, max, out)) { |
95 | 0 | *s = end; |
96 | 0 | return HTS_SCAN_OK; |
97 | 0 | } |
98 | 0 | if ((*p == '-' || *p == '+') && is_decimal_digit(p[1])) |
99 | 0 | (void) !hts_parse_llint(p + 1, &end, min, max, |
100 | 0 | &ignored); // only to skip the refused digits |
101 | 0 | if (end == p) |
102 | 0 | return HTS_SCAN_NONE; |
103 | 0 | *s = end; |
104 | 0 | return HTS_SCAN_REFUSED; |
105 | 0 | } |
106 | | |
107 | 0 | hts_boolean hts_parse_url_port(const char *a, int *port) { |
108 | 0 | const char *end; |
109 | 0 | LLint p; |
110 | |
|
111 | 0 | if (!hts_parse_llint(a, &end, 1, 65535, &p) || *end != '\0') |
112 | 0 | return HTS_FALSE; |
113 | 0 | *port = (int) p; |
114 | 0 | return HTS_TRUE; |
115 | 0 | } |
116 | | |
117 | 0 | hts_boolean hts_span_next_in(hts_span *rest, char sep, hts_span *field) { |
118 | 0 | if (hts_span_empty(*rest)) |
119 | 0 | return HTS_FALSE; |
120 | 0 | *field = hts_span_at(rest->p, hts_span_find(*rest, sep)); |
121 | | /* past the SEP, or to the end when there is none */ |
122 | 0 | *rest = hts_span_skip(*rest, field->len + 1); |
123 | 0 | return HTS_TRUE; |
124 | 0 | } |
125 | | |
126 | 0 | static hts_boolean span_in_set(const char *set, char c) { |
127 | 0 | return hts_span_find_any(hts_span_at(&c, 1), set) == 0; |
128 | 0 | } |
129 | | |
130 | 0 | hts_boolean hts_span_empty(hts_span s) { |
131 | 0 | return s.len == 0 ? HTS_TRUE : HTS_FALSE; |
132 | 0 | } |
133 | | |
134 | 0 | hts_span hts_span_trim(hts_span s, const char *left, const char *right) { |
135 | 0 | while (!hts_span_empty(s) && span_in_set(left, s.p[0])) |
136 | 0 | s = hts_span_skip(s, 1); |
137 | 0 | while (!hts_span_empty(s) && span_in_set(right, s.p[s.len - 1])) |
138 | 0 | s.len--; |
139 | 0 | return s; |
140 | 0 | } |
141 | | |
142 | 0 | hts_boolean hts_span_split(hts_span s, char c, hts_span *head, hts_span *tail) { |
143 | 0 | const size_t at = hts_span_find(s, c); |
144 | |
|
145 | 0 | if (at == s.len) |
146 | 0 | return HTS_FALSE; |
147 | 0 | *head = hts_span_at(s.p, at); |
148 | 0 | *tail = hts_span_skip(s, at + 1); |
149 | 0 | return HTS_TRUE; |
150 | 0 | } |
151 | | |
152 | | /* the blanks treathead_opt() skips, which HTS_REALSPACES also lists */ |
153 | 0 | #define LIST_BLANKS " \t\n\v\f\r" |
154 | | |
155 | 0 | hts_boolean hts_span_list_has(hts_span list, char sep, hts_span token) { |
156 | 0 | hts_span item; |
157 | |
|
158 | 0 | while (hts_span_next_in(&list, sep, &item)) { |
159 | 0 | item = hts_span_trim(item, LIST_BLANKS, LIST_BLANKS); |
160 | 0 | if (!hts_span_empty(item) && hts_span_eq_ci(item, token)) |
161 | 0 | return HTS_TRUE; |
162 | 0 | } |
163 | 0 | return HTS_FALSE; |
164 | 0 | } |
165 | | |
166 | | hts_boolean hts_param_next(hts_span *rest, char sep, hts_span *name, |
167 | 0 | hts_span *value) { |
168 | 0 | hts_span field; |
169 | |
|
170 | 0 | if (!hts_span_next_in(rest, sep, &field)) |
171 | 0 | return HTS_FALSE; |
172 | 0 | *value = hts_span_at(NULL, 0); |
173 | 0 | if (!hts_span_split(field, '=', name, value)) |
174 | 0 | *name = field; |
175 | 0 | return HTS_TRUE; |
176 | 0 | } |
177 | | |
178 | 0 | static hts_boolean header_blank(int c) { return c == ' ' || c == '\t'; } |
179 | | |
180 | 0 | void hts_header_split(hts_span line, hts_span *name, hts_span *value) { |
181 | 0 | const size_t colon = |
182 | 0 | header_blank(hts_span_first(line)) ? line.len : hts_span_find(line, ':'); |
183 | |
|
184 | 0 | if (colon == line.len) { |
185 | 0 | *name = *value = hts_span_at(line.p, 0); |
186 | 0 | return; |
187 | 0 | } |
188 | | /* hand-trimmed, because hts_span_trim costs a strchr per byte here */ |
189 | 0 | *name = hts_span_at(line.p, colon); |
190 | 0 | *value = hts_span_skip(line, colon + 1); |
191 | 0 | while (header_blank(hts_span_last(*name))) |
192 | 0 | name->len--; |
193 | 0 | while (header_blank(hts_span_first(*value))) |
194 | 0 | *value = hts_span_skip(*value, 1); |
195 | 0 | while (header_blank(hts_span_last(*value))) |
196 | 0 | value->len--; |
197 | 0 | } |
198 | | |
199 | 0 | hts_boolean hts_header_next(hts_span *rest, hts_header_field *f) { |
200 | 0 | if (!hts_span_next_in(rest, '\n', &f->line)) |
201 | 0 | return HTS_FALSE; |
202 | 0 | if (hts_span_last(f->line) == '\r') |
203 | 0 | f->line.len--; |
204 | 0 | hts_header_split(f->line, &f->name, &f->value); |
205 | 0 | return !hts_span_empty(f->line); |
206 | 0 | } |
207 | | |
208 | 0 | hts_boolean hts_span_copy(hts_span s, char *dst, size_t size) { |
209 | 0 | return hts_buf_put_span_clip(hts_buf_at(dst, size), s); |
210 | 0 | } |
211 | | |
212 | 1.36k | char *hts_span_dup(hts_span s) { |
213 | 1.36k | char *const copy = malloct(s.len + 1); |
214 | | |
215 | 1.36k | if (copy != NULL) { |
216 | 1.36k | memcpy(copy, s.p, s.len); |
217 | 1.36k | copy[s.len] = '\0'; |
218 | 1.36k | } |
219 | 1.36k | return copy; |
220 | 1.36k | } |
221 | | |
222 | 0 | hts_span hts_span_between(const char *from, const char *to) { |
223 | 0 | return hts_span_at(from, (size_t) (to - from)); |
224 | 0 | } |
225 | | |
226 | 0 | hts_boolean hts_span_present(hts_span s) { |
227 | 0 | return s.p != NULL ? HTS_TRUE : HTS_FALSE; |
228 | 0 | } |
229 | | |
230 | | // Compare the bytes of A and B, where a NUL is a byte like any other. |
231 | 0 | static hts_boolean span_bytes_eq(hts_span a, hts_span b, hts_boolean fold) { |
232 | 0 | size_t i; |
233 | |
|
234 | 0 | if (a.len != b.len) |
235 | 0 | return HTS_FALSE; |
236 | 0 | if (a.len == 0) |
237 | 0 | return HTS_TRUE; // either may be the NULL of an absent view field |
238 | 0 | if (!fold) |
239 | 0 | return memcmp(a.p, b.p, a.len) == 0; |
240 | 0 | for (i = 0; i < a.len; i++) |
241 | 0 | if (hts_tolower((unsigned char) a.p[i]) != |
242 | 0 | hts_tolower((unsigned char) b.p[i])) |
243 | 0 | return HTS_FALSE; |
244 | 0 | return HTS_TRUE; |
245 | 0 | } |
246 | | |
247 | 0 | hts_boolean hts_span_eq(hts_span a, hts_span b) { |
248 | 0 | return span_bytes_eq(a, b, HTS_FALSE); |
249 | 0 | } |
250 | | |
251 | 0 | hts_boolean hts_span_eq_ci(hts_span a, hts_span b) { |
252 | 0 | return span_bytes_eq(a, b, HTS_TRUE); |
253 | 0 | } |
254 | | |
255 | 0 | hts_boolean hts_span_starts_with(hts_span s, hts_span prefix) { |
256 | 0 | return s.len >= prefix.len && |
257 | 0 | span_bytes_eq(hts_span_at(s.p, prefix.len), prefix, HTS_FALSE); |
258 | 0 | } |
259 | | |
260 | 0 | hts_boolean hts_span_starts_with_ci(hts_span s, hts_span prefix) { |
261 | 0 | return s.len >= prefix.len && |
262 | 0 | span_bytes_eq(hts_span_at(s.p, prefix.len), prefix, HTS_TRUE); |
263 | 0 | } |
264 | | |
265 | 0 | hts_boolean hts_span_ends_with(hts_span s, hts_span suffix) { |
266 | | // Check the length so the subtraction cannot wrap. |
267 | 0 | return s.len >= suffix.len && |
268 | 0 | hts_span_starts_with(hts_span_skip(s, s.len - suffix.len), suffix); |
269 | 0 | } |
270 | | |
271 | 0 | hts_boolean hts_span_ends_with_ci(hts_span s, hts_span suffix) { |
272 | | // Check the length so the subtraction cannot wrap. |
273 | 0 | return s.len >= suffix.len && |
274 | 0 | hts_span_starts_with_ci(hts_span_skip(s, s.len - suffix.len), suffix); |
275 | 0 | } |
276 | | |
277 | 0 | static int span_index(hts_span s, const char *const *words, hts_boolean fold) { |
278 | 0 | int i; |
279 | |
|
280 | 0 | for (i = 0; words[i] != NULL; i++) { |
281 | 0 | const hts_span w = hts_span_of(words[i]); |
282 | |
|
283 | 0 | if (span_bytes_eq(s, w, fold)) |
284 | 0 | return i; |
285 | 0 | } |
286 | 0 | return -1; |
287 | 0 | } |
288 | | |
289 | 0 | int hts_span_index(hts_span s, const char *const *words) { |
290 | 0 | return span_index(s, words, HTS_FALSE); |
291 | 0 | } |
292 | | |
293 | 0 | int hts_span_index_ci(hts_span s, const char *const *words) { |
294 | 0 | return span_index(s, words, HTS_TRUE); |
295 | 0 | } |
296 | | |
297 | | // Only these two are escapes, so a raw Windows path keeps its backslashes. |
298 | 0 | static hts_boolean param_escape(const char *p) { |
299 | 0 | return p[0] == '\\' && (p[1] == '"' || p[1] == '\\'); |
300 | 0 | } |
301 | | |
302 | | // Skip to the next separator outside a quoted string. |
303 | 0 | static const char *param_skip(const char *p, const char *seps) { |
304 | 0 | hts_boolean quoted = HTS_FALSE; |
305 | |
|
306 | 0 | for (; *p != '\0' && (quoted || !span_in_set(seps, *p)); p++) { |
307 | 0 | if (*p == '"') |
308 | 0 | quoted = !quoted; |
309 | 0 | else if (quoted && param_escape(p)) |
310 | 0 | p++; |
311 | 0 | } |
312 | 0 | return p; |
313 | 0 | } |
314 | | |
315 | | hts_boolean hts_header_param(const char *value, const char *seps, |
316 | 0 | const char *name, char *out, size_t size) { |
317 | 0 | const char *p = value; |
318 | |
|
319 | 0 | if (size == 0) |
320 | 0 | return HTS_FALSE; |
321 | 0 | out[0] = '\0'; |
322 | 0 | while (*p != '\0') { |
323 | 0 | const char *key, *v; |
324 | 0 | size_t key_len, n = 0; |
325 | 0 | hts_boolean match; |
326 | |
|
327 | 0 | while (span_in_set(" \t", *p) || span_in_set(seps, *p)) |
328 | 0 | p++; |
329 | 0 | for (key = p; *p != '\0' && *p != '=' && !span_in_set(seps, *p); p++) |
330 | 0 | ; |
331 | 0 | for (key_len = (size_t) (p - key); |
332 | 0 | key_len != 0 && span_in_set(" \t", key[key_len - 1]); key_len--) |
333 | 0 | ; |
334 | 0 | if (*p != '=') |
335 | 0 | continue; // a field with no value, such as the media type |
336 | 0 | match = hts_span_eq_ci(hts_span_at(key, key_len), hts_span_of(name)); |
337 | 0 | for (p++; span_in_set(" \t", *p); p++) |
338 | 0 | ; |
339 | 0 | if (*p == '"') { |
340 | 0 | for (p++; *p != '\0' && *p != '"'; p++) { |
341 | 0 | if (param_escape(p)) |
342 | 0 | p++; |
343 | 0 | if (match) { |
344 | 0 | if (n >= size - 1) { |
345 | 0 | out[0] = '\0'; |
346 | 0 | return HTS_FALSE; |
347 | 0 | } |
348 | 0 | out[n++] = *p; |
349 | 0 | } |
350 | 0 | } |
351 | 0 | if (match) { |
352 | 0 | out[n] = '\0'; |
353 | 0 | return HTS_TRUE; |
354 | 0 | } |
355 | 0 | if (*p == '"') |
356 | 0 | p = param_skip(p + 1, seps); // junk after the closing quote |
357 | 0 | } else { |
358 | 0 | for (v = p; *p != '\0' && !span_in_set(seps, *p); p++) |
359 | 0 | ; |
360 | 0 | for (n = (size_t) (p - v); n != 0 && span_in_set(" \t", v[n - 1]); n--) |
361 | 0 | ; |
362 | 0 | if (match) |
363 | 0 | return hts_buf_put_span(hts_buf_at(out, size), hts_span_at(v, n)); |
364 | 0 | } |
365 | 0 | } |
366 | 0 | return HTS_FALSE; |
367 | 0 | } |
368 | | |
369 | | static const struct { |
370 | | int code; |
371 | | const char *text; |
372 | | } status_reasons[] = { |
373 | | {100, "Continue"}, |
374 | | {101, "Switching Protocols"}, |
375 | | {200, "OK"}, |
376 | | {201, "Created"}, |
377 | | {202, "Accepted"}, |
378 | | {203, "Non-Authoritative Information"}, |
379 | | {204, "No Content"}, |
380 | | {205, "Reset Content"}, |
381 | | {206, "Partial Content"}, |
382 | | {207, "Multi-Status"}, |
383 | | {300, "Multiple Choices"}, |
384 | | {301, "Moved Permanently"}, |
385 | | {302, "Moved Temporarily"}, |
386 | | {303, "See Other"}, |
387 | | {304, "Not Modified"}, |
388 | | {305, "Use Proxy"}, |
389 | | {306, "Undefined 306 error"}, |
390 | | {307, "Temporary Redirect"}, |
391 | | {308, "Permanent Redirect"}, |
392 | | {400, "Bad Request"}, |
393 | | {401, "Unauthorized"}, |
394 | | {402, "Payment Required"}, |
395 | | {403, "Forbidden"}, |
396 | | {404, "Not Found"}, |
397 | | {405, "Method Not Allowed"}, |
398 | | {406, "Not Acceptable"}, |
399 | | {407, "Proxy Authentication Required"}, |
400 | | {408, "Request Time-out"}, |
401 | | {409, "Conflict"}, |
402 | | {410, "Gone"}, |
403 | | {411, "Length Required"}, |
404 | | {412, "Precondition Failed"}, |
405 | | {413, "Request Entity Too Large"}, |
406 | | {414, "Request-URI Too Large"}, |
407 | | {415, "Unsupported Media Type"}, |
408 | | {416, "Requested Range Not Satisfiable"}, |
409 | | {417, "Expectation Failed"}, |
410 | | {429, "Too Many Requests"}, |
411 | | {451, "Unavailable For Legal Reasons"}, |
412 | | {500, "Internal Server Error"}, |
413 | | {501, "Not Implemented"}, |
414 | | {502, "Bad Gateway"}, |
415 | | {503, "Service Unavailable"}, |
416 | | {504, "Gateway Time-out"}, |
417 | | {505, "HTTP Version Not Supported"}, |
418 | | }; |
419 | | |
420 | 0 | const char *hts_path_ext_dot(const char *path, const char *seps) { |
421 | 0 | size_t i; |
422 | |
|
423 | 0 | for (i = strlen(path); i > 0; i--) { |
424 | 0 | if (path[i - 1] == '.') |
425 | 0 | return &path[i - 1]; |
426 | 0 | if (span_in_set(seps, path[i - 1])) |
427 | 0 | return NULL; |
428 | 0 | } |
429 | 0 | return NULL; |
430 | 0 | } |
431 | | |
432 | 0 | hts_span hts_fil_ext(const char *fil) { |
433 | 0 | const char *const end = fil + strcspn(fil, "?"); |
434 | 0 | const char *dot = end; |
435 | |
|
436 | 0 | while (dot != fil && dot[-1] != '.') |
437 | 0 | dot--; |
438 | 0 | return dot != fil ? hts_span_between(dot, end) : hts_span_at(end, 0); |
439 | 0 | } |
440 | | |
441 | 0 | const char *hts_status_reason(int code) { |
442 | 0 | size_t i; |
443 | |
|
444 | 0 | for (i = 0; i < sizeof(status_reasons) / sizeof(status_reasons[0]); i++) { |
445 | 0 | if (status_reasons[i].code == code) |
446 | 0 | return status_reasons[i].text; |
447 | 0 | } |
448 | 0 | return NULL; |
449 | 0 | } |