| 1 |
/* |
| 2 |
* $LynxId: HTParse.c,v 1.102 2025/01/06 15:18:39 tom Exp $ |
| 3 |
* |
| 4 |
* Parse HyperText Document Address HTParse.c |
| 5 |
* ================================ |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <HTUtils.h> |
| 9 |
#include <HTParse.h> |
| 10 |
|
| 11 |
#include <LYUtils.h> |
| 12 |
#include <LYLeaks.h> |
| 13 |
#include <LYStrings.h> |
| 14 |
#include <LYCharUtils.h> |
| 15 |
#include <LYGlobalDefs.h> |
| 16 |
|
| 17 |
#ifdef HAVE_ALLOCA_H |
| 18 |
#include <alloca.h> |
| 19 |
#else |
| 20 |
#ifdef __MINGW32__ |
| 21 |
#include <malloc.h> |
| 22 |
#endif /* __MINGW32__ */ |
| 23 |
#endif |
| 24 |
|
| 25 |
#ifdef USE_IDN2 |
| 26 |
#include <idn2.h> |
| 27 |
#define FreeIdna(out) idn2_free(out) |
| 28 |
#elif defined(USE_IDNA) |
| 29 |
#include <idna.h> |
| 30 |
#include <idn-free.h> |
| 31 |
#define FreeIdna(out) idn_free(out) |
| 32 |
#define IDN2_OK IDNA_SUCCESS |
| 33 |
#endif |
| 34 |
|
| 35 |
#define HEX_ESCAPE '%' |
| 36 |
|
| 37 |
struct struct_parts { |
| 38 |
char *access; |
| 39 |
char *host; |
| 40 |
char *absolute; |
| 41 |
char *relative; |
| 42 |
char *search; /* treated normally as part of path */ |
| 43 |
char *anchor; |
| 44 |
}; |
| 45 |
|
| 46 |
#if 0 /* for debugging */ |
| 47 |
static void show_parts(const char *name, struct struct_parts *parts, int line) |
| 48 |
{ |
| 49 |
if (TRACE) { |
| 50 |
CTRACE((tfp, "struct_parts(%s) %s@%d\n", name, __FILE__, line)); |
| 51 |
CTRACE((tfp, " access '%s'\n", NONNULL(parts->access))); |
| 52 |
CTRACE((tfp, " host '%s'\n", NONNULL(parts->host))); |
| 53 |
CTRACE((tfp, " absolute '%s'\n", NONNULL(parts->absolute))); |
| 54 |
CTRACE((tfp, " relative '%s'\n", NONNULL(parts->relative))); |
| 55 |
CTRACE((tfp, " search '%s'\n", NONNULL(parts->search))); |
| 56 |
CTRACE((tfp, " anchor '%s'\n", NONNULL(parts->anchor))); |
| 57 |
} |
| 58 |
} |
| 59 |
#define SHOW_PARTS(name) show_parts(#name, &name, __LINE__) |
| 60 |
#else |
| 61 |
#define SHOW_PARTS(name) /* nothing */ |
| 62 |
#endif |
| 63 |
|
| 64 |
/* Strip white space off a string. HTStrip() |
| 65 |
* ------------------------------- |
| 66 |
* |
| 67 |
* On exit, |
| 68 |
* Return value points to first non-white character, or to 0 if none. |
| 69 |
* All trailing white space is OVERWRITTEN with zero. |
| 70 |
*/ |
| 71 |
char *HTStrip(char *s) |
| 72 |
{ |
| 73 |
#define SPACE(c) ((c == ' ') || (c == '\t') || (c == '\n')) |
| 74 |
char *p; |
| 75 |
|
| 76 |
for (p = s; *p; p++) { /* Find end of string */ |
| 77 |
; |
| 78 |
} |
| 79 |
for (p--; p >= s; p--) { |
| 80 |
if (SPACE(*p)) |
| 81 |
*p = '\0'; /* Zap trailing blanks */ |
| 82 |
else |
| 83 |
break; |
| 84 |
} |
| 85 |
while (SPACE(*s)) |
| 86 |
s++; /* Strip leading blanks */ |
| 87 |
return s; |
| 88 |
} |
| 89 |
|
| 90 |
/* Scan a filename for its constituents. scan() |
| 91 |
* ------------------------------------- |
| 92 |
* |
| 93 |
* On entry, |
| 94 |
* name points to a document name which may be incomplete. |
| 95 |
* On exit, |
| 96 |
* absolute or relative may be nonzero (but not both). |
| 97 |
* host, anchor and access may be nonzero if they were specified. |
| 98 |
* Any which are nonzero point to zero terminated strings. |
| 99 |
*/ |
| 100 |
static void scan(char *name, |
| 101 |
struct struct_parts *parts) |
| 102 |
{ |
| 103 |
char *after_access; |
| 104 |
char *p; |
| 105 |
|
| 106 |
parts->access = NULL; |
| 107 |
parts->host = NULL; |
| 108 |
parts->absolute = NULL; |
| 109 |
parts->relative = NULL; |
| 110 |
parts->search = NULL; /* normally not used - kw */ |
| 111 |
parts->anchor = NULL; |
| 112 |
|
| 113 |
/* |
| 114 |
* Scan left-to-right for a scheme (access). |
| 115 |
*/ |
| 116 |
after_access = name; |
| 117 |
for (p = name; *p; p++) { |
| 118 |
if (*p == ':') { |
| 119 |
*p = '\0'; |
| 120 |
parts->access = name; /* Access name has been specified */ |
| 121 |
after_access = (p + 1); |
| 122 |
break; |
| 123 |
} |
| 124 |
if (*p == '/' || *p == '#' || *p == ';' || *p == '?') |
| 125 |
break; |
| 126 |
} |
| 127 |
|
| 128 |
/* |
| 129 |
* Scan left-to-right for a fragment (anchor). |
| 130 |
*/ |
| 131 |
for (p = after_access; *p; p++) { |
| 132 |
if (*p == '#') { |
| 133 |
parts->anchor = (p + 1); |
| 134 |
*p = '\0'; /* terminate the rest */ |
| 135 |
break; /* leave things after first # alone - kw */ |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/* |
| 140 |
* Scan left-to-right for a host or absolute path. |
| 141 |
*/ |
| 142 |
p = after_access; |
| 143 |
if (*p == '/') { |
| 144 |
if (p[1] == '/') { |
| 145 |
parts->host = (p + 2); /* host has been specified */ |
| 146 |
*p = '\0'; /* Terminate access */ |
| 147 |
p = StrChr(parts->host, '/'); /* look for end of host name if any */ |
| 148 |
if (p != NULL) { |
| 149 |
*p = '\0'; /* Terminate host */ |
| 150 |
parts->absolute = (p + 1); /* Root has been found */ |
| 151 |
} else { |
| 152 |
p = StrChr(parts->host, '?'); |
| 153 |
if (p != NULL) { |
| 154 |
*p = '\0'; /* Terminate host */ |
| 155 |
parts->search = (p + 1); |
| 156 |
} |
| 157 |
} |
| 158 |
} else { |
| 159 |
parts->absolute = (p + 1); /* Root found but no host */ |
| 160 |
} |
| 161 |
} else { |
| 162 |
parts->relative = (*after_access) ? |
| 163 |
after_access : NULL; /* NULL for "" */ |
| 164 |
} |
| 165 |
|
| 166 |
/* |
| 167 |
* Check schemes that commonly have unescaped hashes. |
| 168 |
*/ |
| 169 |
if (parts->access && parts->anchor && |
| 170 |
/* optimize */ StrChr("lnsdLNSD", *parts->access) != NULL) { |
| 171 |
if ((!parts->host && strcasecomp(parts->access, "lynxcgi")) || |
| 172 |
!strcasecomp(parts->access, "nntp") || |
| 173 |
!strcasecomp(parts->access, "snews") || |
| 174 |
!strcasecomp(parts->access, "news") || |
| 175 |
!strcasecomp(parts->access, "data")) { |
| 176 |
/* |
| 177 |
* Access specified but no host and not a lynxcgi URL, so the |
| 178 |
* anchor may not really be one, e.g., news:j462#[email protected], or |
| 179 |
* it's an nntp or snews URL, or news URL with a host. Restore the |
| 180 |
* '#' in the address. |
| 181 |
*/ |
| 182 |
/* but only if we have found a path component of which this will |
| 183 |
* become part. - kw */ |
| 184 |
if (parts->relative || parts->absolute) { |
| 185 |
*(parts->anchor - 1) = '#'; |
| 186 |
parts->anchor = NULL; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} /*scan */ |
| 191 |
|
| 192 |
#if defined(HAVE_ALLOCA) && !defined(LY_FIND_LEAKS) |
| 193 |
#define LYalloca(x) alloca((size_t)(x)) |
| 194 |
#define LYalloca_free(x) {} |
| 195 |
#else |
| 196 |
#define LYalloca(x) malloc((size_t)(x)) |
| 197 |
#define LYalloca_free(x) free((void *)(x)) |
| 198 |
#endif |
| 199 |
|
| 200 |
static char *strchr_or_end(char *string, int ch) |
| 201 |
{ |
| 202 |
char *result = StrChr(string, ch); |
| 203 |
|
| 204 |
if (result == NULL) { |
| 205 |
result = string + strlen(string); |
| 206 |
} |
| 207 |
return result; |
| 208 |
} |
| 209 |
|
| 210 |
/* |
| 211 |
* Given a host specification that may end with a port number, e.g., |
| 212 |
* foobar:123 |
| 213 |
* point to the ':' which begins the ":port" to make it simple to handle the |
| 214 |
* substring. |
| 215 |
* |
| 216 |
* If no port is found (or a syntax error), return null. |
| 217 |
*/ |
| 218 |
char *HTParsePort(char *host, int *portp) |
| 219 |
{ |
| 220 |
int brackets = 0; |
| 221 |
char *result = NULL; |
| 222 |
|
| 223 |
*portp = 0; |
| 224 |
if (host != NULL) { |
| 225 |
while (*host != '\0' && result == NULL) { |
| 226 |
switch (*host++) { |
| 227 |
case ':': |
| 228 |
if (brackets == 0 && isdigit(UCH(*host))) { |
| 229 |
char *next = NULL; |
| 230 |
|
| 231 |
*portp = (int) strtol(host, &next, 10); |
| 232 |
if (next != NULL && next != host && *next == '\0') { |
| 233 |
result = (host - 1); |
| 234 |
CTRACE((tfp, "HTParsePort %d\n", *portp)); |
| 235 |
} |
| 236 |
} |
| 237 |
break; |
| 238 |
case '[': /* for ipv6 */ |
| 239 |
++brackets; |
| 240 |
break; |
| 241 |
case ']': /* for ipv6 */ |
| 242 |
--brackets; |
| 243 |
break; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
return result; |
| 248 |
} |
| 249 |
|
| 250 |
#if defined(USE_IDNA) || defined(USE_IDN2) |
| 251 |
static int hex_decode(int ch) |
| 252 |
{ |
| 253 |
int result = -1; |
| 254 |
|
| 255 |
if (ch >= '0' && ch <= '9') |
| 256 |
result = (ch - '0'); |
| 257 |
else if (ch >= 'a' && ch <= 'f') |
| 258 |
result = (ch - 'a') + 10; |
| 259 |
else if (ch >= 'A' && ch <= 'F') |
| 260 |
result = (ch - 'A') + 10; |
| 261 |
return result; |
| 262 |
} |
| 263 |
|
| 264 |
/* |
| 265 |
* Convert in-place the given hostname to IDNA form. That requires up to 64 |
| 266 |
* characters, and we've allowed for that, with MIN_PARSE. |
| 267 |
*/ |
| 268 |
static void convert_to_idna(char *host) |
| 269 |
{ |
| 270 |
size_t length = strlen(host); |
| 271 |
char *endhost = host + length; |
| 272 |
char *buffer = malloc(length + 1); |
| 273 |
char *params = malloc(length + 1); |
| 274 |
char *output = NULL; |
| 275 |
char *src, *dst; |
| 276 |
int code; |
| 277 |
int hi, lo; |
| 278 |
|
| 279 |
if (buffer != NULL && params != NULL) { |
| 280 |
code = TRUE; |
| 281 |
*params = '\0'; |
| 282 |
for (dst = buffer, src = host; src < endhost; ++dst) { |
| 283 |
int ch = *src++; |
| 284 |
|
| 285 |
if (RFC_3986_GEN_DELIMS(ch)) { |
| 286 |
strcpy(params, src - 1); |
| 287 |
*dst = '\0'; |
| 288 |
break; |
| 289 |
} else if (ch == HEX_ESCAPE) { |
| 290 |
if ((src + 1) < endhost |
| 291 |
&& (hi = hex_decode(src[0])) >= 0 |
| 292 |
&& (lo = hex_decode(src[1])) >= 0) { |
| 293 |
|
| 294 |
*dst = (char) ((hi << 4) | lo); |
| 295 |
src += 2; |
| 296 |
} else { |
| 297 |
CTRACE((tfp, "convert_to_idna: `%s' is malformed\n", host)); |
| 298 |
code = FALSE; |
| 299 |
break; |
| 300 |
} |
| 301 |
} else { |
| 302 |
*dst = (char) ch; |
| 303 |
} |
| 304 |
} |
| 305 |
if (code) { |
| 306 |
*dst = '\0'; |
| 307 |
#ifdef USE_IDN2 |
| 308 |
#if (!defined(IDN2_VERSION_NUMBER) || IDN2_VERSION_NUMBER < 0x02000003) |
| 309 |
/* |
| 310 |
* Older libidn2 mishandles STD3, stripping underscores. |
| 311 |
*/ |
| 312 |
if (strchr(buffer, '_') != NULL) { |
| 313 |
code = -1; |
| 314 |
} else |
| 315 |
#endif |
| 316 |
switch (LYidnaMode) { |
| 317 |
case LYidna2003: |
| 318 |
code = idn2_to_ascii_8z(buffer, &output, IDN2_TRANSITIONAL); |
| 319 |
break; |
| 320 |
case LYidna2008: |
| 321 |
/* IDNA2008 rules without the TR46 amendments */ |
| 322 |
code = idn2_to_ascii_8z(buffer, &output, 0); |
| 323 |
break; |
| 324 |
case LYidnaTR46: |
| 325 |
code = idn2_to_ascii_8z(buffer, &output, IDN2_NONTRANSITIONAL |
| 326 |
| IDN2_NFC_INPUT); |
| 327 |
break; |
| 328 |
case LYidnaCompat: |
| 329 |
/* IDNA2008 */ |
| 330 |
code = idn2_to_ascii_8z(buffer, &output, IDN2_NONTRANSITIONAL |
| 331 |
| IDN2_NFC_INPUT); |
| 332 |
if (code == IDN2_DISALLOWED) { |
| 333 |
/* IDNA2003 - compatible */ |
| 334 |
code = idn2_to_ascii_8z(buffer, &output, IDN2_TRANSITIONAL); |
| 335 |
} |
| 336 |
break; |
| 337 |
} |
| 338 |
#else |
| 339 |
code = idna_to_ascii_8z(buffer, &output, IDNA_USE_STD3_ASCII_RULES); |
| 340 |
#endif |
| 341 |
if (code == IDN2_OK) { |
| 342 |
CTRACE((tfp, "convert_to_idna: `%s' -> `%s': OK\n", buffer, output)); |
| 343 |
strcpy(host, output); |
| 344 |
strcat(host, params); |
| 345 |
} else { |
| 346 |
CTRACE((tfp, "convert_to_idna: `%s': %s\n", |
| 347 |
buffer, |
| 348 |
idna_strerror((Idna_rc) code))); |
| 349 |
} |
| 350 |
if (output) |
| 351 |
FreeIdna(output); |
| 352 |
} |
| 353 |
} |
| 354 |
free(buffer); |
| 355 |
free(params); |
| 356 |
} |
| 357 |
#define MIN_PARSE 80 |
| 358 |
#else |
| 359 |
#define MIN_PARSE 8 |
| 360 |
#endif |
| 361 |
|
| 362 |
/* Parse a Name relative to another name. HTParse() |
| 363 |
* -------------------------------------- |
| 364 |
* |
| 365 |
* This returns those parts of a name which are given (and requested) |
| 366 |
* substituting bits from the related name where necessary. |
| 367 |
* |
| 368 |
* Originally based on RFC 1808, some details in RFC 3986 are used. |
| 369 |
* |
| 370 |
* On entry, |
| 371 |
* aName A filename given |
| 372 |
* relatedName A name relative to which aName is to be parsed |
| 373 |
* wanted A mask for the bits which are wanted. |
| 374 |
* |
| 375 |
* On exit, |
| 376 |
* returns A pointer to a malloc'd string which MUST BE FREED |
| 377 |
*/ |
| 378 |
char *HTParse(const char *aName, |
| 379 |
const char *relatedName, |
| 380 |
int wanted) |
| 381 |
{ |
| 382 |
char *result = NULL; |
| 383 |
char *tail = NULL; /* a pointer to the end of the 'result' string */ |
| 384 |
char *return_value = NULL; |
| 385 |
size_t len, len1, len2; |
| 386 |
size_t need; |
| 387 |
char *name = NULL; |
| 388 |
char *rel = NULL; |
| 389 |
char *p, *q; |
| 390 |
char *acc_method; |
| 391 |
struct struct_parts given, related; |
| 392 |
|
| 393 |
CTRACE((tfp, "HTParse: aName:`%s'\n", aName)); |
| 394 |
CTRACE((tfp, " relatedName:`%s'\n", relatedName)); |
| 395 |
|
| 396 |
if (wanted & (PARSE_STRICTPATH | PARSE_QUERY)) { /* if detail wanted... */ |
| 397 |
if ((wanted & (PARSE_STRICTPATH | PARSE_QUERY)) |
| 398 |
== (PARSE_STRICTPATH | PARSE_QUERY)) /* if strictpath AND query */ |
| 399 |
wanted |= PARSE_PATH; /* then treat as if PARSE_PATH wanted */ |
| 400 |
if (wanted & PARSE_PATH) /* if PARSE_PATH wanted */ |
| 401 |
wanted &= ~(PARSE_STRICTPATH | PARSE_QUERY); /* ignore details */ |
| 402 |
} |
| 403 |
/* *INDENT-OFF* */ |
| 404 |
CTRACE((tfp, " want:%s%s%s%s%s%s%s\n", |
| 405 |
wanted & PARSE_PUNCTUATION ? " punc" : "", |
| 406 |
wanted & PARSE_ANCHOR ? " anchor" : "", |
| 407 |
wanted & PARSE_PATH ? " path" : "", |
| 408 |
wanted & PARSE_HOST ? " host" : "", |
| 409 |
wanted & PARSE_ACCESS ? " access" : "", |
| 410 |
wanted & PARSE_STRICTPATH ? " PATH" : "", |
| 411 |
wanted & PARSE_QUERY ? " QUERY" : "")); |
| 412 |
/* *INDENT-ON* */ |
| 413 |
|
| 414 |
/* |
| 415 |
* Allocate the temporary string. Optimized. |
| 416 |
*/ |
| 417 |
len1 = strlen(aName) + 1; |
| 418 |
len2 = strlen(relatedName) + 1; |
| 419 |
len = len1 + len2 + MIN_PARSE; /* Lots of space: more than enough */ |
| 420 |
need = (len * 2 + len1 + len2); |
| 421 |
|
| 422 |
if ((int) need < (int) len1 || |
| 423 |
(int) need < (int) len2) { |
| 424 |
CTRACE((tfp, "HTParse: overflow\n")); |
| 425 |
return StrAllocCopy(return_value, ""); |
| 426 |
} |
| 427 |
|
| 428 |
result = tail = (char *) LYalloca(need); |
| 429 |
if (result == NULL) { |
| 430 |
outofmem(__FILE__, "HTParse"); |
| 431 |
} |
| 432 |
*result = '\0'; |
| 433 |
name = result + len; |
| 434 |
rel = name + len1; |
| 435 |
|
| 436 |
/* |
| 437 |
* Make working copy of the input string to cut up. |
| 438 |
*/ |
| 439 |
MemCpy(name, aName, len1); |
| 440 |
|
| 441 |
/* |
| 442 |
* Cut up the string into URL fields. |
| 443 |
*/ |
| 444 |
scan(name, &given); |
| 445 |
SHOW_PARTS(given); |
| 446 |
|
| 447 |
/* |
| 448 |
* Now related string. |
| 449 |
*/ |
| 450 |
if ((given.access && given.host && given.absolute) || !*relatedName) { |
| 451 |
/* |
| 452 |
* Inherit nothing! |
| 453 |
*/ |
| 454 |
related.access = NULL; |
| 455 |
related.host = NULL; |
| 456 |
related.absolute = NULL; |
| 457 |
related.relative = NULL; |
| 458 |
related.search = NULL; |
| 459 |
related.anchor = NULL; |
| 460 |
} else { |
| 461 |
MemCpy(rel, relatedName, len2); |
| 462 |
scan(rel, &related); |
| 463 |
} |
| 464 |
SHOW_PARTS(related); |
| 465 |
|
| 466 |
/* |
| 467 |
* Handle the scheme (access) field. |
| 468 |
*/ |
| 469 |
if (given.access && given.host && !given.relative && !given.absolute) { |
| 470 |
if (!strcmp(given.access, "http") || |
| 471 |
!strcmp(given.access, "https") || |
| 472 |
!strcmp(given.access, "ftp")) { |
| 473 |
|
| 474 |
/* |
| 475 |
* Assume root. |
| 476 |
*/ |
| 477 |
given.absolute = empty_string; |
| 478 |
} |
| 479 |
} |
| 480 |
acc_method = given.access ? given.access : related.access; |
| 481 |
if (wanted & PARSE_ACCESS) { |
| 482 |
if (acc_method) { |
| 483 |
strcpy(tail, acc_method); |
| 484 |
tail += strlen(tail); |
| 485 |
if (wanted & PARSE_PUNCTUATION) { |
| 486 |
*tail++ = ':'; |
| 487 |
*tail = '\0'; |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
/* |
| 493 |
* If different schemes, inherit nothing. |
| 494 |
* |
| 495 |
* We'll try complying with RFC 1808 and the Fielding draft, and inherit |
| 496 |
* nothing if both schemes are given, rather than only when they differ, |
| 497 |
* except for file URLs - FM |
| 498 |
* |
| 499 |
* After trying it for a while, it's still premature, IHMO, to go along |
| 500 |
* with it, so this is back to inheriting for identical schemes whether or |
| 501 |
* not they are "file". If you want to try it again yourself, uncomment |
| 502 |
* the strcasecomp() below. - FM |
| 503 |
*/ |
| 504 |
if ((given.access && related.access) && |
| 505 |
( /* strcasecomp(given.access, "file") || */ |
| 506 |
strcmp(given.access, related.access))) { |
| 507 |
related.host = NULL; |
| 508 |
related.absolute = NULL; |
| 509 |
related.relative = NULL; |
| 510 |
related.search = NULL; |
| 511 |
related.anchor = NULL; |
| 512 |
} |
| 513 |
|
| 514 |
/* |
| 515 |
* Handle the host field. |
| 516 |
*/ |
| 517 |
if (wanted & PARSE_HOST) { |
| 518 |
if (given.host || related.host) { |
| 519 |
if (wanted & PARSE_PUNCTUATION) { |
| 520 |
*tail++ = '/'; |
| 521 |
*tail++ = '/'; |
| 522 |
} |
| 523 |
strcpy(tail, given.host ? given.host : related.host); |
| 524 |
/* |
| 525 |
* Ignore default port numbers, and trailing dots on FQDNs, which |
| 526 |
* will only cause identical addresses to look different. (related |
| 527 |
* is already a clean url). |
| 528 |
*/ |
| 529 |
{ |
| 530 |
char *p2, *h; |
| 531 |
int portnumber; |
| 532 |
int gen_delims = 0; |
| 533 |
|
| 534 |
if ((p2 = HTSkipToAt(result, &gen_delims)) != NULL |
| 535 |
&& gen_delims == 0) { |
| 536 |
tail = (p2 + 1); |
| 537 |
} |
| 538 |
p2 = HTParsePort(result, &portnumber); |
| 539 |
if (p2 != NULL && acc_method != NULL) { |
| 540 |
/* |
| 541 |
* Port specified. |
| 542 |
*/ |
| 543 |
#define ACC_METHOD(a,b) (!strcmp(acc_method, a) && (portnumber == b)) |
| 544 |
if (ACC_METHOD("http", 80) || |
| 545 |
ACC_METHOD("https", 443) || |
| 546 |
ACC_METHOD("gopher", 70) || |
| 547 |
ACC_METHOD("ftp", 21) || |
| 548 |
ACC_METHOD("wais", 210) || |
| 549 |
ACC_METHOD("nntp", 119) || |
| 550 |
ACC_METHOD("news", 119) || |
| 551 |
ACC_METHOD("newspost", 119) || |
| 552 |
ACC_METHOD("newsreply", 119) || |
| 553 |
ACC_METHOD("snews", 563) || |
| 554 |
ACC_METHOD("snewspost", 563) || |
| 555 |
ACC_METHOD("snewsreply", 563) || |
| 556 |
ACC_METHOD("finger", 79) || |
| 557 |
ACC_METHOD("telnet", 23) || |
| 558 |
ACC_METHOD("tn3270", 23) || |
| 559 |
ACC_METHOD("rlogin", 513) || |
| 560 |
ACC_METHOD("cso", 105)) |
| 561 |
*p2 = '\0'; /* It is the default: ignore it */ |
| 562 |
} |
| 563 |
if (p2 == NULL) { |
| 564 |
int len3 = (int) strlen(tail); |
| 565 |
|
| 566 |
if (len3 > 0) { |
| 567 |
h = tail + len3 - 1; /* last char of hostname */ |
| 568 |
if (*h == '.') |
| 569 |
*h = '\0'; /* chop final . */ |
| 570 |
} |
| 571 |
} else if (p2 != result) { |
| 572 |
h = p2; |
| 573 |
h--; /* End of hostname */ |
| 574 |
if (*h == '.') { |
| 575 |
/* |
| 576 |
* Slide p2 over h. |
| 577 |
*/ |
| 578 |
while (*p2 != '\0') |
| 579 |
*h++ = *p2++; |
| 580 |
*h = '\0'; /* terminate */ |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
#if defined(USE_IDNA) || defined(USE_IDN2) |
| 585 |
/* |
| 586 |
* Depending on locale-support, we could have a literal UTF-8 |
| 587 |
* string as a host name, or a URL-encoded form of that. |
| 588 |
*/ |
| 589 |
convert_to_idna(tail); |
| 590 |
#endif |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
/* |
| 595 |
* Trim any blanks from the result so far - there's no excuse for blanks |
| 596 |
* in a hostname. Also update the tail here. |
| 597 |
*/ |
| 598 |
tail = LYRemoveBlanks(result); |
| 599 |
|
| 600 |
/* |
| 601 |
* If host in given or related was ended directly with a '?' (no slash), |
| 602 |
* fake the search part into absolute. This is the only case search is |
| 603 |
* returned from scan. A host must have been present. this restores the |
| 604 |
* '?' at which the host part had been truncated in scan, we have to do |
| 605 |
* this after host part handling is done. - kw |
| 606 |
*/ |
| 607 |
if (given.search && *(given.search - 1) == '\0') { |
| 608 |
given.absolute = given.search - 1; |
| 609 |
given.absolute[0] = '?'; |
| 610 |
} else if (related.search && !related.absolute && |
| 611 |
*(related.search - 1) == '\0') { |
| 612 |
related.absolute = related.search - 1; |
| 613 |
related.absolute[0] = '?'; |
| 614 |
} |
| 615 |
|
| 616 |
/* |
| 617 |
* If different hosts, inherit no path. |
| 618 |
*/ |
| 619 |
if (given.host && related.host) |
| 620 |
if (strcmp(given.host, related.host) != 0) { |
| 621 |
related.absolute = NULL; |
| 622 |
related.relative = NULL; |
| 623 |
related.anchor = NULL; |
| 624 |
} |
| 625 |
|
| 626 |
/* |
| 627 |
* Handle the path. |
| 628 |
*/ |
| 629 |
if (wanted & (PARSE_PATH | PARSE_STRICTPATH | PARSE_QUERY)) { |
| 630 |
int want_detail = (wanted & (PARSE_STRICTPATH | PARSE_QUERY)); |
| 631 |
|
| 632 |
if (acc_method && !given.absolute && given.relative) { |
| 633 |
/* |
| 634 |
* Treat all given nntp or snews paths, or given paths for news |
| 635 |
* URLs with a host, as absolute. |
| 636 |
*/ |
| 637 |
switch (*acc_method) { |
| 638 |
case 'N': |
| 639 |
case 'n': |
| 640 |
if (!strcasecomp(acc_method, "nntp") || |
| 641 |
(!strcasecomp(acc_method, "news") && |
| 642 |
!strncasecomp(result, "news://", 7))) { |
| 643 |
given.absolute = given.relative; |
| 644 |
given.relative = NULL; |
| 645 |
} |
| 646 |
break; |
| 647 |
case 'S': |
| 648 |
case 's': |
| 649 |
if (!strcasecomp(acc_method, "snews")) { |
| 650 |
given.absolute = given.relative; |
| 651 |
given.relative = NULL; |
| 652 |
} |
| 653 |
break; |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
if (given.absolute) { /* All is given */ |
| 658 |
char *base = tail; |
| 659 |
|
| 660 |
if (wanted & PARSE_PUNCTUATION) |
| 661 |
*tail++ = '/'; |
| 662 |
strcpy(tail, given.absolute); |
| 663 |
HTSimplify(base, TRUE); |
| 664 |
CTRACE((tfp, "HTParse: (ABS)\n")); |
| 665 |
} else if (related.absolute) { /* Adopt path not name */ |
| 666 |
char *base = tail; |
| 667 |
|
| 668 |
*tail++ = '/'; |
| 669 |
strcpy(tail, related.absolute); |
| 670 |
if (given.relative) { |
| 671 |
/* RFC 1808 part 4 step 5 (if URL path is empty) */ |
| 672 |
/* a) if given has params, add/replace that */ |
| 673 |
if (given.relative[0] == ';') { |
| 674 |
strcpy(strchr_or_end(tail, ';'), given.relative); |
| 675 |
} |
| 676 |
/* b) if given has query, add/replace that */ |
| 677 |
else if (given.relative[0] == '?') { |
| 678 |
strcpy(strchr_or_end(tail, '?'), given.relative); |
| 679 |
} |
| 680 |
/* otherwise fall through to RFC 1808 part 4 step 6 */ |
| 681 |
else { |
| 682 |
p = StrChr(tail, '?'); /* Search part? */ |
| 683 |
if (p == NULL) |
| 684 |
p = (tail + strlen(tail) - 1); |
| 685 |
for (; *p != '/'; p--) ; /* last / */ |
| 686 |
p[1] = '\0'; /* Remove filename */ |
| 687 |
strcat(p, given.relative); /* Add given one */ |
| 688 |
} |
| 689 |
HTSimplify(base, FALSE); |
| 690 |
if (*base == '\0') |
| 691 |
strcpy(base, "/"); |
| 692 |
} else { |
| 693 |
HTSimplify(base, TRUE); |
| 694 |
} |
| 695 |
if (base[0] == '/' && base[1] == '/') { |
| 696 |
char *pz; |
| 697 |
|
| 698 |
for (pz = base; (pz[0] = pz[1]) != '\0'; ++pz) ; |
| 699 |
} |
| 700 |
CTRACE((tfp, "HTParse: (Related-ABS)\n")); |
| 701 |
} else if (given.relative) { |
| 702 |
strcpy(tail, given.relative); /* what we've got */ |
| 703 |
HTSimplify(tail, FALSE); |
| 704 |
CTRACE((tfp, "HTParse: (REL)\n")); |
| 705 |
} else if (related.relative) { |
| 706 |
strcpy(tail, related.relative); |
| 707 |
HTSimplify(tail, FALSE); |
| 708 |
CTRACE((tfp, "HTParse: (Related-REL)\n")); |
| 709 |
} else { /* No inheritance */ |
| 710 |
if (!isLYNXCGI(aName) && |
| 711 |
!isLYNXEXEC(aName) && |
| 712 |
!isLYNXPROG(aName)) { |
| 713 |
*tail++ = '/'; |
| 714 |
*tail = '\0'; |
| 715 |
} else { |
| 716 |
HTSimplify(tail, FALSE); |
| 717 |
} |
| 718 |
if (!strcmp(result, "news:/")) |
| 719 |
result[5] = '*'; |
| 720 |
CTRACE((tfp, "HTParse: (No inheritance)\n")); |
| 721 |
} |
| 722 |
if (want_detail) { |
| 723 |
p = StrChr(tail, '?'); /* Search part? */ |
| 724 |
if (p) { |
| 725 |
if (PARSE_STRICTPATH) { |
| 726 |
*p = '\0'; |
| 727 |
} else { |
| 728 |
if (!(wanted & PARSE_PUNCTUATION)) |
| 729 |
p++; |
| 730 |
do { |
| 731 |
*tail++ = *p; |
| 732 |
} while (*p++); |
| 733 |
} |
| 734 |
} else { |
| 735 |
if (wanted & PARSE_QUERY) |
| 736 |
*tail = '\0'; |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
/* |
| 742 |
* Handle the fragment (anchor). Never inherit. |
| 743 |
*/ |
| 744 |
if (wanted & PARSE_ANCHOR) { |
| 745 |
if (given.anchor && *given.anchor) { |
| 746 |
tail += strlen(tail); |
| 747 |
if (wanted & PARSE_PUNCTUATION) |
| 748 |
*tail++ = '#'; |
| 749 |
strcpy(tail, given.anchor); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
/* |
| 754 |
* If there are any blanks remaining in the string, escape them as needed. |
| 755 |
* See the discussion in LYLegitimizeHREF() for example. |
| 756 |
*/ |
| 757 |
if ((p = StrChr(result, ' ')) != NULL) { |
| 758 |
switch (is_url(result)) { |
| 759 |
case UNKNOWN_URL_TYPE: |
| 760 |
CTRACE((tfp, "HTParse: ignore:`%s'\n", result)); |
| 761 |
break; |
| 762 |
case LYNXEXEC_URL_TYPE: |
| 763 |
case LYNXPROG_URL_TYPE: |
| 764 |
case LYNXCGI_URL_TYPE: |
| 765 |
case LYNXPRINT_URL_TYPE: |
| 766 |
case LYNXHIST_URL_TYPE: |
| 767 |
case LYNXDOWNLOAD_URL_TYPE: |
| 768 |
case LYNXKEYMAP_URL_TYPE: |
| 769 |
case LYNXIMGMAP_URL_TYPE: |
| 770 |
case LYNXCOOKIE_URL_TYPE: |
| 771 |
case LYNXCACHE_URL_TYPE: |
| 772 |
case LYNXDIRED_URL_TYPE: |
| 773 |
case LYNXOPTIONS_URL_TYPE: |
| 774 |
case LYNXCFG_URL_TYPE: |
| 775 |
case LYNXCOMPILE_OPTS_URL_TYPE: |
| 776 |
case LYNXMESSAGES_URL_TYPE: |
| 777 |
CTRACE((tfp, "HTParse: spaces:`%s'\n", result)); |
| 778 |
break; |
| 779 |
case NOT_A_URL_TYPE: |
| 780 |
default: |
| 781 |
CTRACE((tfp, "HTParse: encode:`%s'\n", result)); |
| 782 |
do { |
| 783 |
q = p + strlen(p) + 2; |
| 784 |
|
| 785 |
while (q != p + 1) { |
| 786 |
q[0] = q[-2]; |
| 787 |
--q; |
| 788 |
} |
| 789 |
p[0] = HEX_ESCAPE; |
| 790 |
p[1] = '2'; |
| 791 |
p[2] = '0'; |
| 792 |
} while ((p = StrChr(result, ' ')) != NULL); |
| 793 |
break; |
| 794 |
} |
| 795 |
} |
| 796 |
CTRACE((tfp, "HTParse: result:`%s'\n", result)); |
| 797 |
|
| 798 |
need = strlen(result); |
| 799 |
if (need > (size_t) max_uri_size) { |
| 800 |
CTRACE((tfp, "HTParse too-long address (have %ld vs limit %d)\n", |
| 801 |
(long) need, max_uri_size)); |
| 802 |
StrAllocCopy(return_value, ""); |
| 803 |
} else { |
| 804 |
StrAllocCopy(return_value, result); |
| 805 |
} |
| 806 |
LYalloca_free(result); |
| 807 |
|
| 808 |
/* FIXME: could be optimized using HTParse() internals */ |
| 809 |
if (*relatedName && |
| 810 |
((wanted & PARSE_ALL_WITHOUT_ANCHOR) == PARSE_ALL_WITHOUT_ANCHOR)) { |
| 811 |
/* |
| 812 |
* Check whether to fill in localhost. - FM |
| 813 |
*/ |
| 814 |
LYFillLocalFileURL(&return_value, relatedName); |
| 815 |
CTRACE((tfp, "pass LYFillLocalFile:`%s'\n", return_value)); |
| 816 |
} |
| 817 |
|
| 818 |
return return_value; /* exactly the right length */ |
| 819 |
} |
| 820 |
|
| 821 |
/* HTParseAnchor(), fast HTParse() specialization |
| 822 |
* ---------------------------------------------- |
| 823 |
* |
| 824 |
* On exit, |
| 825 |
* returns A pointer within input string (probably to its end '\0') |
| 826 |
*/ |
| 827 |
const char *HTParseAnchor(const char *aName) |
| 828 |
{ |
| 829 |
const char *p = aName; |
| 830 |
|
| 831 |
for (; *p && *p != '#'; p++) { |
| 832 |
; |
| 833 |
} |
| 834 |
if (*p == '#') { |
| 835 |
/* the safe way based on HTParse() - |
| 836 |
* keeping in mind scan() peculiarities on schemes: |
| 837 |
*/ |
| 838 |
struct struct_parts given; |
| 839 |
size_t need = ((unsigned) ((p - aName) + (int) strlen(p) + 1)); |
| 840 |
char *name; |
| 841 |
|
| 842 |
if (need > (size_t) max_uri_size) { |
| 843 |
p += strlen(p); |
| 844 |
} else { |
| 845 |
name = (char *) LYalloca(need); |
| 846 |
|
| 847 |
if (name == NULL) { |
| 848 |
outofmem(__FILE__, "HTParseAnchor"); |
| 849 |
} |
| 850 |
strcpy(name, aName); |
| 851 |
scan(name, &given); |
| 852 |
LYalloca_free(name); |
| 853 |
|
| 854 |
p++; /*next to '#' */ |
| 855 |
if (given.anchor == NULL) { |
| 856 |
for (; *p; p++) /*scroll to end '\0' */ |
| 857 |
; |
| 858 |
} |
| 859 |
} |
| 860 |
} |
| 861 |
return p; |
| 862 |
} |
| 863 |
|
| 864 |
/* Simplify a filename. HTSimplify() |
| 865 |
* -------------------- |
| 866 |
* |
| 867 |
* A unix-style file is allowed to contain the sequence xxx/../ which may |
| 868 |
* be replaced by "" , and the sequence "/./" which may be replaced by "/". |
| 869 |
* Simplification helps us recognize duplicate filenames. |
| 870 |
* |
| 871 |
* RFC 3986 section 5.2.4 says to do this whether or not the path was relative. |
| 872 |
*/ |
| 873 |
void HTSimplify(char *filename, BOOL absolute) |
| 874 |
{ |
| 875 |
#define MY_FMT "HTParse HTSimplify\t(%s)" |
| 876 |
#ifdef NO_LYNX_TRACE |
| 877 |
#define debug_at(at) /* nothing */ |
| 878 |
#define atln "?" |
| 879 |
#else |
| 880 |
const char *atln; |
| 881 |
|
| 882 |
#define debug_at(at) atln = at |
| 883 |
#endif |
| 884 |
char *mark; |
| 885 |
char *p; |
| 886 |
size_t limit; |
| 887 |
|
| 888 |
CTRACE2(TRACE_HTPARSE, |
| 889 |
(tfp, MY_FMT " %s\n", |
| 890 |
filename, |
| 891 |
absolute ? "ABS" : "REL")); |
| 892 |
|
| 893 |
if (LYIsPathSep(*filename) && !absolute) |
| 894 |
++filename; |
| 895 |
mark = filename; |
| 896 |
limit = strlen(filename); |
| 897 |
|
| 898 |
for (p = filename; *p; ++p) { |
| 899 |
if (*p == '?' || *p == '#') { |
| 900 |
limit = (size_t) (p - filename); |
| 901 |
break; |
| 902 |
} |
| 903 |
} |
| 904 |
while ((limit != 0) && (*filename != '\0')) { |
| 905 |
size_t trim = 0; |
| 906 |
size_t skip = 0; |
| 907 |
size_t last = 0; |
| 908 |
|
| 909 |
debug_at("?"); |
| 910 |
p = filename; |
| 911 |
if (limit >= 2 && !memcmp(p, "./", 2)) { /* 2A */ |
| 912 |
debug_at("2A"); |
| 913 |
trim = 2; |
| 914 |
} else if (limit >= 3 && !memcmp(p, "../", 3)) { |
| 915 |
debug_at("2A2"); |
| 916 |
trim = 3; |
| 917 |
} else if (limit >= 3 && !memcmp(p, "/./", 3)) { /* 2B */ |
| 918 |
debug_at("2B"); |
| 919 |
trim = 2; |
| 920 |
skip = 1; |
| 921 |
} else if (limit == 2 && !memcmp(p, "/.", 2)) { |
| 922 |
debug_at("2B2"); |
| 923 |
trim = 1; |
| 924 |
skip = 1; |
| 925 |
} else if (limit >= 4 && !memcmp(p, "/../", 4)) { /* 2C */ |
| 926 |
debug_at("2C"); |
| 927 |
trim = 3; |
| 928 |
skip = 1; |
| 929 |
last = 1; |
| 930 |
} else if (limit == 3 && !memcmp(p, "/..", 3)) { |
| 931 |
debug_at("2C2"); |
| 932 |
trim = 2; |
| 933 |
skip = 1; |
| 934 |
last = 1; |
| 935 |
} else if (limit == 2 && !memcmp(p, "..", 2)) { /* 2D */ |
| 936 |
debug_at("2D"); |
| 937 |
trim = 2; |
| 938 |
} else if (limit == 1 && !memcmp(p, ".", 1)) { |
| 939 |
debug_at("2D2"); |
| 940 |
trim = 1; |
| 941 |
} |
| 942 |
if (trim) { |
| 943 |
CTRACE2(TRACE_HTPARSE, |
| 944 |
(tfp, MY_FMT " trim %lu/%lu (%.*s) '%.*s' @%s\n", |
| 945 |
mark, (unsigned long) trim, (unsigned long) limit, |
| 946 |
(int) trim, p + skip, (int) limit, p, atln)); |
| 947 |
} |
| 948 |
if (last) { |
| 949 |
char *prior = filename; |
| 950 |
|
| 951 |
if (prior != mark) { |
| 952 |
--prior; |
| 953 |
while (prior != mark && *prior != '/') { |
| 954 |
--prior; |
| 955 |
} |
| 956 |
} |
| 957 |
if (prior != filename) { |
| 958 |
trim += (size_t) (filename - prior); |
| 959 |
limit += (size_t) (filename - prior); |
| 960 |
filename = prior; |
| 961 |
CTRACE2(TRACE_HTPARSE, |
| 962 |
(tfp, MY_FMT " TRIM %lu/%lu (%.*s)\n", |
| 963 |
mark, (unsigned long) trim, (unsigned long) limit, |
| 964 |
(int) trim, filename + skip)); |
| 965 |
} |
| 966 |
} |
| 967 |
if (trim) { |
| 968 |
limit -= trim; |
| 969 |
for (p = filename;; ++p) { |
| 970 |
if ((p[0] = p[trim]) == '\0') { |
| 971 |
break; |
| 972 |
} |
| 973 |
if (skip) { |
| 974 |
p[0] = '/'; |
| 975 |
skip = 0; |
| 976 |
} |
| 977 |
} |
| 978 |
CTRACE2(TRACE_HTPARSE, |
| 979 |
(tfp, MY_FMT " loop %lu\n", mark, (unsigned long) limit)); |
| 980 |
} else { |
| 981 |
if (*filename == '/') { |
| 982 |
++filename; |
| 983 |
--limit; |
| 984 |
} |
| 985 |
while ((limit != 0) && (*filename != '/')) { |
| 986 |
++filename; |
| 987 |
--limit; |
| 988 |
} |
| 989 |
} |
| 990 |
} |
| 991 |
CTRACE2(TRACE_HTPARSE, (tfp, MY_FMT " done\n", mark)); |
| 992 |
#undef MY_FMT |
| 993 |
} |
| 994 |
|
| 995 |
/* Make Relative Name. HTRelative() |
| 996 |
* ------------------- |
| 997 |
* |
| 998 |
* This function creates and returns a string which gives an expression of |
| 999 |
* one address as related to another. Where there is no relation, an absolute |
| 1000 |
* address is returned. |
| 1001 |
* |
| 1002 |
* On entry, |
| 1003 |
* Both names must be absolute, fully qualified names of nodes |
| 1004 |
* (no anchor bits) |
| 1005 |
* |
| 1006 |
* On exit, |
| 1007 |
* The return result points to a newly allocated name which, if |
| 1008 |
* parsed by HTParse relative to relatedName, will yield aName. |
| 1009 |
* The caller is responsible for freeing the resulting name later. |
| 1010 |
* |
| 1011 |
*/ |
| 1012 |
char *HTRelative(const char *aName, |
| 1013 |
const char *relatedName) |
| 1014 |
{ |
| 1015 |
char *result = NULL; |
| 1016 |
const char *p = aName; |
| 1017 |
const char *q = relatedName; |
| 1018 |
const char *after_access = NULL; |
| 1019 |
const char *path = NULL; |
| 1020 |
const char *last_slash = NULL; |
| 1021 |
int slashes = 0; |
| 1022 |
|
| 1023 |
for (; *p; p++, q++) { /* Find extent of match */ |
| 1024 |
if (*p != *q) |
| 1025 |
break; |
| 1026 |
if (*p == ':') |
| 1027 |
after_access = p + 1; |
| 1028 |
if (*p == '/') { |
| 1029 |
last_slash = p; |
| 1030 |
slashes++; |
| 1031 |
if (slashes == 3) |
| 1032 |
path = p; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
/* q, p point to the first non-matching character or zero */ |
| 1037 |
|
| 1038 |
if (!after_access) { /* Different access */ |
| 1039 |
StrAllocCopy(result, aName); |
| 1040 |
} else if (slashes < 3) { /* Different nodes */ |
| 1041 |
StrAllocCopy(result, after_access); |
| 1042 |
} else if (slashes == 3) { /* Same node, different path */ |
| 1043 |
StrAllocCopy(result, path); |
| 1044 |
} else { /* Some path in common */ |
| 1045 |
unsigned levels = 0; |
| 1046 |
|
| 1047 |
for (; *q && (*q != '#'); q++) |
| 1048 |
if (*q == '/') |
| 1049 |
levels++; |
| 1050 |
result = typecallocn(char, 3 * levels + strlen(last_slash) + 1); |
| 1051 |
|
| 1052 |
if (result == NULL) |
| 1053 |
outofmem(__FILE__, "HTRelative"); |
| 1054 |
|
| 1055 |
result[0] = '\0'; |
| 1056 |
for (; levels; levels--) |
| 1057 |
strcat(result, "../"); |
| 1058 |
strcat(result, last_slash + 1); |
| 1059 |
} |
| 1060 |
CTRACE((tfp, |
| 1061 |
"HTparse: `%s' expressed relative to\n `%s' is\n `%s'.\n", |
| 1062 |
aName, relatedName, result)); |
| 1063 |
return result; |
| 1064 |
} |
| 1065 |
|
| 1066 |
#define AlloCopy(next,base,extra) \ |
| 1067 |
typecallocn(char, ((next - base) + ((int) extra))) |
| 1068 |
|
| 1069 |
/* Escape undesirable characters using % HTEscape() |
| 1070 |
* ------------------------------------- |
| 1071 |
* |
| 1072 |
* This function takes a pointer to a string in which |
| 1073 |
* some characters may be unacceptable unescaped. |
| 1074 |
* It returns a string which has these characters |
| 1075 |
* represented by a '%' character followed by two hex digits. |
| 1076 |
* |
| 1077 |
* Unlike HTUnEscape(), this routine returns a calloc'd string. |
| 1078 |
*/ |
| 1079 |
/* *INDENT-OFF* */ |
| 1080 |
static const unsigned char isAcceptable[96] = |
| 1081 |
|
| 1082 |
/* Bit 0 xalpha -- see HTFile.h |
| 1083 |
* Bit 1 xpalpha -- as xalpha but with plus. |
| 1084 |
* Bit 2 ... path -- as xpalphas but with / |
| 1085 |
*/ |
| 1086 |
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ |
| 1087 |
{ 0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4, /* 2x !"#$%&'()*+,-./ */ |
| 1088 |
7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */ |
| 1089 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */ |
| 1090 |
7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */ |
| 1091 |
0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */ |
| 1092 |
7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 }; /* 7X pqrstuvwxyz{|}~ DEL */ |
| 1093 |
/* *INDENT-ON* */ |
| 1094 |
|
| 1095 |
static const char *hex = "0123456789ABCDEF"; |
| 1096 |
|
| 1097 |
#define ACCEPTABLE(a) ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask)) |
| 1098 |
|
| 1099 |
char *HTEscape(const char *str, |
| 1100 |
unsigned mask) |
| 1101 |
{ |
| 1102 |
const char *p; |
| 1103 |
char *q; |
| 1104 |
char *result; |
| 1105 |
size_t unacceptable = 0; |
| 1106 |
|
| 1107 |
for (p = str; *p; p++) |
| 1108 |
if (!ACCEPTABLE(UCH(TOASCII(*p)))) |
| 1109 |
unacceptable++; |
| 1110 |
result = AlloCopy(p, str, (unacceptable * 2) + 1); |
| 1111 |
|
| 1112 |
if (result == NULL) |
| 1113 |
outofmem(__FILE__, "HTEscape"); |
| 1114 |
|
| 1115 |
for (q = result, p = str; *p; p++) { |
| 1116 |
unsigned char a = UCH(TOASCII(*p)); |
| 1117 |
|
| 1118 |
if (!ACCEPTABLE(a)) { |
| 1119 |
*q++ = HEX_ESCAPE; /* Means hex coming */ |
| 1120 |
*q++ = hex[a >> 4]; |
| 1121 |
*q++ = hex[a & 15]; |
| 1122 |
} else |
| 1123 |
*q++ = *p; |
| 1124 |
} |
| 1125 |
*q = '\0'; /* Terminate */ |
| 1126 |
return result; |
| 1127 |
} |
| 1128 |
|
| 1129 |
/* Escape unsafe characters using % HTEscapeUnsafe() |
| 1130 |
* -------------------------------- |
| 1131 |
* |
| 1132 |
* This function takes a pointer to a string in which |
| 1133 |
* some characters may be that may be unsafe are unescaped. |
| 1134 |
* It returns a string which has these characters |
| 1135 |
* represented by a '%' character followed by two hex digits. |
| 1136 |
* |
| 1137 |
* Unlike HTUnEscape(), this routine returns a malloc'd string. |
| 1138 |
*/ |
| 1139 |
#define UNSAFE(ch) (((ch) <= 32) || ((ch) >= 127)) |
| 1140 |
|
| 1141 |
char *HTEscapeUnsafe(const char *str) |
| 1142 |
{ |
| 1143 |
const char *p; |
| 1144 |
char *q; |
| 1145 |
char *result; |
| 1146 |
size_t unacceptable = 0; |
| 1147 |
|
| 1148 |
for (p = str; *p; p++) |
| 1149 |
if (UNSAFE(UCH(TOASCII(*p)))) |
| 1150 |
unacceptable++; |
| 1151 |
result = AlloCopy(p, str, (unacceptable * 2) + 1); |
| 1152 |
|
| 1153 |
if (result == NULL) |
| 1154 |
outofmem(__FILE__, "HTEscapeUnsafe"); |
| 1155 |
|
| 1156 |
for (q = result, p = str; *p; p++) { |
| 1157 |
unsigned char a = UCH(TOASCII(*p)); |
| 1158 |
|
| 1159 |
if (UNSAFE(a)) { |
| 1160 |
*q++ = HEX_ESCAPE; /* Means hex coming */ |
| 1161 |
*q++ = hex[a >> 4]; |
| 1162 |
*q++ = hex[a & 15]; |
| 1163 |
} else |
| 1164 |
*q++ = *p; |
| 1165 |
} |
| 1166 |
*q = '\0'; /* Terminate */ |
| 1167 |
return result; |
| 1168 |
} |
| 1169 |
|
| 1170 |
/* Escape undesirable characters using % but space to +. HTEscapeSP() |
| 1171 |
* ----------------------------------------------------- |
| 1172 |
* |
| 1173 |
* This function takes a pointer to a string in which |
| 1174 |
* some characters may be unacceptable unescaped. |
| 1175 |
* It returns a string which has these characters |
| 1176 |
* represented by a '%' character followed by two hex digits, |
| 1177 |
* except that spaces are converted to '+' instead of %2B. |
| 1178 |
* |
| 1179 |
* Unlike HTUnEscape(), this routine returns a calloced string. |
| 1180 |
*/ |
| 1181 |
char *HTEscapeSP(const char *str, |
| 1182 |
unsigned mask) |
| 1183 |
{ |
| 1184 |
const char *p; |
| 1185 |
char *q; |
| 1186 |
char *result; |
| 1187 |
size_t unacceptable = 0; |
| 1188 |
|
| 1189 |
for (p = str; *p; p++) |
| 1190 |
if (!(*p == ' ' || ACCEPTABLE(UCH(TOASCII(*p))))) |
| 1191 |
unacceptable++; |
| 1192 |
result = AlloCopy(p, str, (unacceptable * 2) + 1); |
| 1193 |
|
| 1194 |
if (result == NULL) |
| 1195 |
outofmem(__FILE__, "HTEscape"); |
| 1196 |
|
| 1197 |
for (q = result, p = str; *p; p++) { |
| 1198 |
unsigned char a = UCH(TOASCII(*p)); |
| 1199 |
|
| 1200 |
if (a == 32) { |
| 1201 |
*q++ = '+'; |
| 1202 |
} else if (!ACCEPTABLE(a)) { |
| 1203 |
*q++ = HEX_ESCAPE; /* Means hex coming */ |
| 1204 |
*q++ = hex[a >> 4]; |
| 1205 |
*q++ = hex[a & 15]; |
| 1206 |
} else { |
| 1207 |
*q++ = *p; |
| 1208 |
} |
| 1209 |
} |
| 1210 |
*q = '\0'; /* Terminate */ |
| 1211 |
return result; |
| 1212 |
} |
| 1213 |
|
| 1214 |
/* Decode %xx escaped characters. HTUnEscape() |
| 1215 |
* ------------------------------ |
| 1216 |
* |
| 1217 |
* This function takes a pointer to a string in which some |
| 1218 |
* characters may have been encoded in %xy form, where xy is |
| 1219 |
* the ASCII hex code for character 16x+y. |
| 1220 |
* The string is converted in place, as it will never grow. |
| 1221 |
*/ |
| 1222 |
static char from_hex(int c) |
| 1223 |
{ |
| 1224 |
return (char) (c >= '0' && c <= '9' ? c - '0' |
| 1225 |
: c >= 'A' && c <= 'F' ? c - 'A' + 10 |
| 1226 |
: c - 'a' + 10); /* accept small letters just in case */ |
| 1227 |
} |
| 1228 |
|
| 1229 |
char *HTUnEscape(char *str) |
| 1230 |
{ |
| 1231 |
char *p = str; |
| 1232 |
char *q = str; |
| 1233 |
|
| 1234 |
if (!(p && *p)) |
| 1235 |
return str; |
| 1236 |
|
| 1237 |
while (*p != '\0') { |
| 1238 |
if (*p == HEX_ESCAPE && |
| 1239 |
/* |
| 1240 |
* Tests shouldn't be needed, but better safe than sorry. |
| 1241 |
*/ |
| 1242 |
p[1] && p[2] && |
| 1243 |
isxdigit(UCH(p[1])) && |
| 1244 |
isxdigit(UCH(p[2]))) { |
| 1245 |
p++; |
| 1246 |
if (*p) |
| 1247 |
*q = (char) (from_hex(*p++) * 16); |
| 1248 |
if (*p) { |
| 1249 |
/* |
| 1250 |
* Careful! FROMASCII() may evaluate its arg more than once! |
| 1251 |
*/ |
| 1252 |
/* S/390 -- gil -- 0221 */ |
| 1253 |
*q = (char) (*q + from_hex(*p++)); |
| 1254 |
} |
| 1255 |
*q = FROMASCII(*q); |
| 1256 |
q++; |
| 1257 |
} else { |
| 1258 |
*q++ = *p++; |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
*q = '\0'; |
| 1263 |
return str; |
| 1264 |
|
| 1265 |
} /* HTUnEscape */ |
| 1266 |
|
| 1267 |
/* Decode some %xx escaped characters. HTUnEscapeSome() |
| 1268 |
* ----------------------------------- Klaus Weide |
| 1269 |
* ([email protected]) |
| 1270 |
* This function takes a pointer to a string in which some |
| 1271 |
* characters may have been encoded in %xy form, where xy is |
| 1272 |
* the ASCII hex code for character 16x+y, and a pointer to |
| 1273 |
* a second string containing one or more characters which |
| 1274 |
* should be unescaped if escaped in the first string. |
| 1275 |
* The first string is converted in place, as it will never grow. |
| 1276 |
*/ |
| 1277 |
char *HTUnEscapeSome(char *str, |
| 1278 |
const char *do_trans) |
| 1279 |
{ |
| 1280 |
char *p = str; |
| 1281 |
char *q = str; |
| 1282 |
char testcode; |
| 1283 |
|
| 1284 |
if (p == NULL || *p == '\0' || do_trans == NULL || *do_trans == '\0') |
| 1285 |
return str; |
| 1286 |
|
| 1287 |
while (*p != '\0') { |
| 1288 |
if (*p == HEX_ESCAPE && |
| 1289 |
p[1] && p[2] && /* tests shouldn't be needed, but.. */ |
| 1290 |
isxdigit(UCH(p[1])) && |
| 1291 |
isxdigit(UCH(p[2])) && |
| 1292 |
(testcode = (char) FROMASCII(from_hex(p[1]) * 16 + |
| 1293 |
from_hex(p[2]))) && /* %00 no good */ |
| 1294 |
StrChr(do_trans, testcode)) { /* it's one of the ones we want */ |
| 1295 |
*q++ = testcode; |
| 1296 |
p += 3; |
| 1297 |
} else { |
| 1298 |
*q++ = *p++; |
| 1299 |
} |
| 1300 |
} |
| 1301 |
|
| 1302 |
*q = '\0'; |
| 1303 |
return str; |
| 1304 |
|
| 1305 |
} /* HTUnEscapeSome */ |
| 1306 |
/* *INDENT-OFF* */ |
| 1307 |
static const unsigned char crfc[96] = |
| 1308 |
|
| 1309 |
/* Bit 0 xalpha -- need "quoting" |
| 1310 |
* Bit 1 xpalpha -- need \escape if quoted |
| 1311 |
*/ |
| 1312 |
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ |
| 1313 |
{ 1,0,3,0,0,0,0,0,1,1,0,0,1,0,1,0, /* 2x !"#$%&'()*+,-./ */ |
| 1314 |
0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,0, /* 3x 0123456789:;<=>? */ |
| 1315 |
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 4x @ABCDEFGHIJKLMNO */ |
| 1316 |
0,0,0,0,0,0,0,0,0,0,0,1,2,1,0,0, /* 5X PQRSTUVWXYZ[\]^_ */ |
| 1317 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 6x `abcdefghijklmno */ |
| 1318 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3 }; /* 7X pqrstuvwxyz{|}~ DEL */ |
| 1319 |
/* *INDENT-ON* */ |
| 1320 |
|
| 1321 |
#define ASCII_TAB '\011' |
| 1322 |
#define ASCII_LF '\012' |
| 1323 |
#define ASCII_CR '\015' |
| 1324 |
#define ASCII_SPC '\040' |
| 1325 |
#define ASCII_BAK '\134' |
| 1326 |
|
| 1327 |
/* |
| 1328 |
* Turn a string which is not a RFC 822 token into a quoted-string. - KW |
| 1329 |
* The "quoted" parameter tells whether we need the beginning/ending quote |
| 1330 |
* marks. If not, the caller will provide them -TD |
| 1331 |
*/ |
| 1332 |
void HTMake822Word(char **str, |
| 1333 |
int quoted) |
| 1334 |
{ |
| 1335 |
const char *p; |
| 1336 |
char *q; |
| 1337 |
char *result; |
| 1338 |
unsigned char a; |
| 1339 |
unsigned added = 0; |
| 1340 |
|
| 1341 |
if (isEmpty(*str)) { |
| 1342 |
StrAllocCopy(*str, quoted ? "\"\"" : ""); |
| 1343 |
return; |
| 1344 |
} |
| 1345 |
for (p = *str; *p; p++) { |
| 1346 |
a = UCH(TOASCII(*p)); /* S/390 -- gil -- 0240 */ |
| 1347 |
if (a < 32 || a >= 128 || |
| 1348 |
((crfc[a - 32]) & 1)) { |
| 1349 |
if (!added) |
| 1350 |
added = 2; |
| 1351 |
if (a >= 160 || a == '\t') |
| 1352 |
continue; |
| 1353 |
if (a == '\r' || a == '\n') |
| 1354 |
added += 2; |
| 1355 |
else if ((a & 127) < 32 || ((crfc[a - 32]) & 2)) |
| 1356 |
added++; |
| 1357 |
} |
| 1358 |
} |
| 1359 |
if (!added) |
| 1360 |
return; |
| 1361 |
result = AlloCopy(p, *str, added + 1); |
| 1362 |
if (result == NULL) |
| 1363 |
outofmem(__FILE__, "HTMake822Word"); |
| 1364 |
|
| 1365 |
q = result; |
| 1366 |
if (quoted) |
| 1367 |
*q++ = '"'; |
| 1368 |
/* |
| 1369 |
* Having converted the character to ASCII, we can't use symbolic |
| 1370 |
* escape codes, since they're in the host character set, which |
| 1371 |
* is not necessarily ASCII. Thus we use octal escape codes instead. |
| 1372 |
* -- gil (Paul Gilmartin) <[email protected]> |
| 1373 |
*/ |
| 1374 |
/* S/390 -- gil -- 0268 */ |
| 1375 |
for (p = *str; *p; p++) { |
| 1376 |
a = UCH(TOASCII(*p)); |
| 1377 |
if ((a != ASCII_TAB) && |
| 1378 |
((a & 127) < ASCII_SPC || |
| 1379 |
(a < 128 && ((crfc[a - 32]) & 2)))) |
| 1380 |
*q++ = ASCII_BAK; |
| 1381 |
*q++ = *p; |
| 1382 |
if (a == ASCII_LF || |
| 1383 |
(a == ASCII_CR && (TOASCII(*(p + 1)) != ASCII_LF))) |
| 1384 |
*q++ = ' '; |
| 1385 |
} |
| 1386 |
if (quoted) |
| 1387 |
*q++ = '"'; |
| 1388 |
*q = '\0'; /* Terminate */ |
| 1389 |
FREE(*str); |
| 1390 |
*str = result; |
| 1391 |
} |