| 1 |
/* |
| 2 |
* $LynxId: HTFile.c,v 1.170 2026/04/19 22:46:40 tom Exp $ |
| 3 |
* |
| 4 |
* File Access HTFile.c |
| 5 |
* =========== |
| 6 |
* |
| 7 |
* This is unix-specific code in general, with some VMS bits. |
| 8 |
* These are routines for file access used by browsers. |
| 9 |
* Development of this module for Unix DIRED_SUPPORT in Lynx |
| 10 |
* regrettably has has been conducted in a manner with now |
| 11 |
* creates a major impediment for hopes of adapting Lynx to |
| 12 |
* a newer version of the library. |
| 13 |
* |
| 14 |
* History: |
| 15 |
* Feb 91 Written Tim Berners-Lee CERN/CN |
| 16 |
* Apr 91 vms-vms access included using DECnet syntax |
| 17 |
* 26 Jun 92 (JFG) When running over DECnet, suppressed FTP. |
| 18 |
* Fixed access bug for relative names on VMS. |
| 19 |
* Sep 93 (MD) Access to VMS files allows sharing. |
| 20 |
* 15 Nov 93 (MD) Moved HTVMSname to HTVMSUTILS.C |
| 21 |
* 27 Dec 93 (FM) FTP now works with VMS hosts. |
| 22 |
* FTP path must be Unix-style and cannot include |
| 23 |
* the device or top directory. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <HTUtils.h> |
| 27 |
|
| 28 |
#ifndef VMS |
| 29 |
#if defined(DOSPATH) |
| 30 |
#undef LONG_LIST |
| 31 |
#define LONG_LIST /* Define this for long style unix listings (ls -l), |
| 32 |
the actual style is configurable from lynx.cfg */ |
| 33 |
#endif |
| 34 |
/* #define NO_PARENT_DIR_REFERENCE */ |
| 35 |
/* Define this for no parent links */ |
| 36 |
#endif /* !VMS */ |
| 37 |
|
| 38 |
#if defined(DOSPATH) |
| 39 |
#define HAVE_READDIR 1 |
| 40 |
#define USE_DIRENT |
| 41 |
#endif |
| 42 |
|
| 43 |
#if defined(USE_DOS_DRIVES) |
| 44 |
#include <HTDOS.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#include <HTFile.h> /* Implemented here */ |
| 48 |
|
| 49 |
#ifdef VMS |
| 50 |
#include <stat.h> |
| 51 |
#endif /* VMS */ |
| 52 |
|
| 53 |
#if defined (USE_ZLIB) || defined (USE_BZLIB) |
| 54 |
#include <GridText.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#define MULTI_SUFFIX ".multi" /* Extension for scanning formats */ |
| 58 |
|
| 59 |
#include <HTParse.h> |
| 60 |
#include <HTTCP.h> |
| 61 |
#ifndef DECNET |
| 62 |
#include <HTFTP.h> |
| 63 |
#endif /* !DECNET */ |
| 64 |
#include <HTAnchor.h> |
| 65 |
#include <HTAtom.h> |
| 66 |
#include <HTAAProt.h> |
| 67 |
#include <HTFWriter.h> |
| 68 |
#include <HTInit.h> |
| 69 |
#include <HTBTree.h> |
| 70 |
#include <HTAlert.h> |
| 71 |
#include <HTCJK.h> |
| 72 |
#include <UCDefs.h> |
| 73 |
#include <UCMap.h> |
| 74 |
#include <UCAux.h> |
| 75 |
|
| 76 |
#include <LYexit.h> |
| 77 |
#include <LYCharSets.h> |
| 78 |
#include <LYGlobalDefs.h> |
| 79 |
#include <LYStrings.h> |
| 80 |
#include <LYUtils.h> |
| 81 |
|
| 82 |
#ifdef USE_PRETTYSRC |
| 83 |
# include <LYPrettySrc.h> |
| 84 |
#endif |
| 85 |
|
| 86 |
#include <LYLeaks.h> |
| 87 |
|
| 88 |
typedef struct _HTSuffix { |
| 89 |
char *suffix; |
| 90 |
HTAtom *rep; |
| 91 |
HTAtom *encoding; |
| 92 |
char *desc; |
| 93 |
float quality; |
| 94 |
} HTSuffix; |
| 95 |
|
| 96 |
typedef struct { |
| 97 |
struct stat file_info; |
| 98 |
char sort_tags; |
| 99 |
char file_name[1]; /* on the end of the struct, since its length varies */ |
| 100 |
} DIRED; |
| 101 |
|
| 102 |
#ifndef NGROUPS |
| 103 |
#ifdef NGROUPS_MAX |
| 104 |
#define NGROUPS NGROUPS_MAX |
| 105 |
#else |
| 106 |
#define NGROUPS 32 |
| 107 |
#endif /* NGROUPS_MAX */ |
| 108 |
#endif /* NGROUPS */ |
| 109 |
|
| 110 |
#ifndef GETGROUPS_T |
| 111 |
#define GETGROUPS_T int |
| 112 |
#endif |
| 113 |
|
| 114 |
#include <HTML.h> /* For directory object building */ |
| 115 |
|
| 116 |
#define PUTC(c) (*target->isa->put_character)(target, c) |
| 117 |
#define PUTS(s) (*target->isa->put_string)(target, s) |
| 118 |
#define START(e) (*target->isa->start_element)(target, e, NULL, NULL, -1, NULL) |
| 119 |
#define END(e) (*target->isa->end_element)(target, e, NULL) |
| 120 |
#define MAYBE_END(e) if (HTML_dtd.tags[e].contents != SGML_EMPTY) \ |
| 121 |
(*target->isa->end_element)(target, e, 0) |
| 122 |
#define FREE_TARGET (*target->isa->_free)(target) |
| 123 |
#define ABORT_TARGET (*targetClass._abort)(target, NULL); |
| 124 |
|
| 125 |
struct _HTStructured { |
| 126 |
const HTStructuredClass *isa; |
| 127 |
/* ... */ |
| 128 |
}; |
| 129 |
|
| 130 |
/* |
| 131 |
* Controlling globals. |
| 132 |
*/ |
| 133 |
int HTDirAccess = HT_DIR_OK; |
| 134 |
|
| 135 |
#ifdef DIRED_SUPPORT |
| 136 |
int HTDirReadme = HT_DIR_README_NONE; |
| 137 |
|
| 138 |
#else |
| 139 |
int HTDirReadme = HT_DIR_README_TOP; |
| 140 |
#endif /* DIRED_SUPPORT */ |
| 141 |
|
| 142 |
static const char *HTMountRoot = "/Net/"; /* Where to find mounts */ |
| 143 |
|
| 144 |
#ifdef VMS |
| 145 |
static const char *HTCacheRoot = "/WWW$SCRATCH"; /* Where to cache things */ |
| 146 |
|
| 147 |
#else |
| 148 |
static const char *HTCacheRoot = "/tmp/W3_Cache_"; /* Where to cache things */ |
| 149 |
#endif /* VMS */ |
| 150 |
|
| 151 |
static char s_no_suffix[] = "*"; |
| 152 |
static char s_unknown_suffix[] = "*.*"; |
| 153 |
|
| 154 |
/* |
| 155 |
* Suffix registration. |
| 156 |
*/ |
| 157 |
static HTList *HTSuffixes = NULL; |
| 158 |
|
| 159 |
static HTSuffix no_suffix = |
| 160 |
{ |
| 161 |
s_no_suffix, NULL, NULL, NULL, 1.0 |
| 162 |
}; |
| 163 |
|
| 164 |
static HTSuffix unknown_suffix = |
| 165 |
{ |
| 166 |
s_unknown_suffix, NULL, NULL, NULL, 1.0 |
| 167 |
}; |
| 168 |
|
| 169 |
/* To free up the suffixes at program exit. |
| 170 |
* ---------------------------------------- |
| 171 |
*/ |
| 172 |
#ifdef LY_FIND_LEAKS |
| 173 |
static void free_suffixes(void); |
| 174 |
#endif |
| 175 |
|
| 176 |
#define FindSearch(filename) strchr(filename, '?') |
| 177 |
|
| 178 |
#ifdef LONG_LIST |
| 179 |
static char *FormatStr(char **bufp, |
| 180 |
char *start, |
| 181 |
const char *entry) |
| 182 |
{ |
| 183 |
char fmt[512]; |
| 184 |
|
| 185 |
if (*start) { |
| 186 |
sprintf(fmt, "%%%.*ss", (int) sizeof(fmt) - 3, start); |
| 187 |
HTSprintf0(bufp, HT_FMT("%s", fmt), entry); |
| 188 |
} else if (*bufp && !(entry && *entry)) { |
| 189 |
**bufp = '\0'; |
| 190 |
} else if (entry) { |
| 191 |
StrAllocCopy(*bufp, entry); |
| 192 |
} |
| 193 |
return *bufp; |
| 194 |
} |
| 195 |
|
| 196 |
static char *FormatSize(char **bufp, |
| 197 |
char *start, |
| 198 |
off_t entry) |
| 199 |
{ |
| 200 |
char fmt[512]; |
| 201 |
|
| 202 |
if (*start) { |
| 203 |
sprintf(fmt, "%%%.*s" PRI_off_t, |
| 204 |
(int) sizeof(fmt) - DigitsOf(start) - 3, start); |
| 205 |
|
| 206 |
HTSprintf0(bufp, HT_FMT("%" PRI_off_t, fmt), entry); |
| 207 |
} else { |
| 208 |
sprintf(fmt, "%" PRI_off_t, CAST_off_t (entry)); |
| 209 |
|
| 210 |
StrAllocCopy(*bufp, fmt); |
| 211 |
} |
| 212 |
return *bufp; |
| 213 |
} |
| 214 |
|
| 215 |
static char *FormatNum(char **bufp, |
| 216 |
char *start, |
| 217 |
int entry) |
| 218 |
{ |
| 219 |
char fmt[512]; |
| 220 |
|
| 221 |
if (*start) { |
| 222 |
sprintf(fmt, "%%%.*sd", (int) sizeof(fmt) - 3, start); |
| 223 |
HTSprintf0(bufp, HT_FMT("%d", fmt), entry); |
| 224 |
} else { |
| 225 |
sprintf(fmt, "%d", entry); |
| 226 |
StrAllocCopy(*bufp, fmt); |
| 227 |
} |
| 228 |
return *bufp; |
| 229 |
} |
| 230 |
|
| 231 |
static void LYListFmtParse(const char *fmtstr, |
| 232 |
DIRED * data, |
| 233 |
char *file, |
| 234 |
HTStructured * target, |
| 235 |
char *tail) |
| 236 |
{ |
| 237 |
char c; |
| 238 |
char *s; |
| 239 |
char *end; |
| 240 |
char *start; |
| 241 |
char *str = NULL; |
| 242 |
char *buf = NULL; |
| 243 |
char tmp[LY_MAXPATH]; |
| 244 |
char type; |
| 245 |
|
| 246 |
#ifndef NOUSERS |
| 247 |
const char *name; |
| 248 |
#endif |
| 249 |
time_t now; |
| 250 |
char *datestr; |
| 251 |
|
| 252 |
#ifdef S_IFLNK |
| 253 |
int len; |
| 254 |
#endif |
| 255 |
#define SEC_PER_YEAR (60 * 60 * 24 * 365) |
| 256 |
|
| 257 |
#ifdef _WINDOWS /* 1998/01/06 (Tue) 21:20:53 */ |
| 258 |
static const char *pbits[] = |
| 259 |
{ |
| 260 |
"---", "--x", "-w-", "-wx", |
| 261 |
"r--", "r-x", "rw-", "rwx", |
| 262 |
0}; |
| 263 |
|
| 264 |
#define PBIT(a, n, s) pbits[((a) >> (n)) & 0x7] |
| 265 |
|
| 266 |
#else |
| 267 |
static const char *pbits[] = |
| 268 |
{"---", "--x", "-w-", "-wx", |
| 269 |
"r--", "r-x", "rw-", "rwx", NULL}; |
| 270 |
static const char *psbits[] = |
| 271 |
{"--S", "--s", "-wS", "-ws", |
| 272 |
"r-S", "r-s", "rwS", "rws", NULL}; |
| 273 |
|
| 274 |
#define PBIT(a, n, s) (s) ? psbits[((a) >> (n)) & 0x7] : \ |
| 275 |
pbits[((a) >> (n)) & 0x7] |
| 276 |
#endif |
| 277 |
#if defined(S_ISVTX) && !defined(_WINDOWS) |
| 278 |
static const char *ptbits[] = |
| 279 |
{"--T", "--t", "-wT", "-wt", |
| 280 |
"r-T", "r-t", "rwT", "rwt", NULL}; |
| 281 |
|
| 282 |
#define PTBIT(a, s) (s) ? ptbits[(a) & 0x7] : pbits[(a) & 0x7] |
| 283 |
#else |
| 284 |
#define PTBIT(a, s) PBIT(a, 0, 0) |
| 285 |
#endif |
| 286 |
|
| 287 |
if (data->file_info.st_mode == 0) |
| 288 |
fmtstr = " %a"; /* can't stat so just do anchor */ |
| 289 |
|
| 290 |
StrAllocCopy(str, fmtstr); |
| 291 |
s = str; |
| 292 |
end = str + strlen(str); |
| 293 |
while (*s) { |
| 294 |
start = s; |
| 295 |
while (*s) { |
| 296 |
if (*s == '%') { |
| 297 |
if (*(s + 1) == '%') /* literal % */ |
| 298 |
s++; |
| 299 |
else |
| 300 |
break; |
| 301 |
} |
| 302 |
s++; |
| 303 |
} |
| 304 |
/* s is positioned either at a % or at \0 */ |
| 305 |
*s = '\0'; |
| 306 |
if (s > start) { /* some literal chars. */ |
| 307 |
PUTS(start); |
| 308 |
} |
| 309 |
if (s == end) |
| 310 |
break; |
| 311 |
start = ++s; |
| 312 |
while (isdigit(UCH(*s)) || *s == '.' || *s == '-' || *s == ' ' || |
| 313 |
*s == '#' || *s == '+' || *s == '\'') |
| 314 |
s++; |
| 315 |
c = *s; /* the format char. or \0 */ |
| 316 |
*s = '\0'; |
| 317 |
|
| 318 |
switch (c) { |
| 319 |
case '\0': |
| 320 |
PUTS(start); |
| 321 |
continue; |
| 322 |
|
| 323 |
case 'A': |
| 324 |
case 'a': /* anchor */ |
| 325 |
HTDirEntry(target, tail, data->file_name); |
| 326 |
FormatStr(&buf, start, data->file_name); |
| 327 |
PUTS(buf); |
| 328 |
END(HTML_A); |
| 329 |
*buf = '\0'; |
| 330 |
#ifdef S_IFLNK |
| 331 |
if (c != 'A' && S_ISLNK(data->file_info.st_mode) && |
| 332 |
(len = (int) readlink(file, tmp, sizeof(tmp) - 1)) >= 0) { |
| 333 |
PUTS(" -> "); |
| 334 |
tmp[len] = '\0'; |
| 335 |
PUTS(tmp); |
| 336 |
} |
| 337 |
#endif |
| 338 |
break; |
| 339 |
|
| 340 |
case 'T': /* MIME type */ |
| 341 |
case 't': /* MIME type description */ |
| 342 |
if (S_ISDIR(data->file_info.st_mode)) { |
| 343 |
if (c != 'T') { |
| 344 |
FormatStr(&buf, start, ENTRY_IS_DIRECTORY); |
| 345 |
} else { |
| 346 |
FormatStr(&buf, start, ""); |
| 347 |
} |
| 348 |
} else { |
| 349 |
const char *cp2; |
| 350 |
HTFormat format; |
| 351 |
|
| 352 |
format = HTFileFormat(file, NULL, &cp2); |
| 353 |
|
| 354 |
if (c != 'T') { |
| 355 |
if (cp2 == NULL) { |
| 356 |
if (!StrNCmp(HTAtom_name(format), |
| 357 |
"application", 11)) { |
| 358 |
cp2 = HTAtom_name(format) + 12; |
| 359 |
if (!StrNCmp(cp2, "x-", 2)) |
| 360 |
cp2 += 2; |
| 361 |
} else { |
| 362 |
cp2 = HTAtom_name(format); |
| 363 |
} |
| 364 |
} |
| 365 |
FormatStr(&buf, start, cp2); |
| 366 |
} else { |
| 367 |
FormatStr(&buf, start, HTAtom_name(format)); |
| 368 |
} |
| 369 |
} |
| 370 |
break; |
| 371 |
|
| 372 |
case 'd': /* date */ |
| 373 |
now = time(NULL); |
| 374 |
datestr = ctime(&data->file_info.st_mtime); |
| 375 |
if ((now - data->file_info.st_mtime) < SEC_PER_YEAR / 2) |
| 376 |
/* |
| 377 |
* MMM DD HH:MM |
| 378 |
*/ |
| 379 |
sprintf(tmp, "%.12s", datestr + 4); |
| 380 |
else |
| 381 |
/* |
| 382 |
* MMM DD YYYY |
| 383 |
*/ |
| 384 |
sprintf(tmp, "%.7s %.4s ", datestr + 4, |
| 385 |
datestr + 20); |
| 386 |
FormatStr(&buf, start, tmp); |
| 387 |
break; |
| 388 |
|
| 389 |
case 's': /* size in bytes */ |
| 390 |
FormatSize(&buf, start, data->file_info.st_size); |
| 391 |
break; |
| 392 |
|
| 393 |
case 'K': /* size in Kilobytes but not for directories */ |
| 394 |
if (S_ISDIR(data->file_info.st_mode)) { |
| 395 |
FormatStr(&buf, start, ""); |
| 396 |
StrAllocCat(buf, " "); |
| 397 |
break; |
| 398 |
} |
| 399 |
/* FALL THROUGH */ |
| 400 |
case 'k': /* size in Kilobytes */ |
| 401 |
FormatSize(&buf, start, ((data->file_info.st_size + 1023) / 1024)); |
| 402 |
StrAllocCat(buf, "K"); |
| 403 |
break; |
| 404 |
|
| 405 |
case 'p': /* unix-style permission bits */ |
| 406 |
switch (data->file_info.st_mode & S_IFMT) { |
| 407 |
#if defined(_MSC_VER) && defined(_S_IFIFO) |
| 408 |
case _S_IFIFO: |
| 409 |
type = 'p'; |
| 410 |
break; |
| 411 |
#else |
| 412 |
case S_IFIFO: |
| 413 |
type = 'p'; |
| 414 |
break; |
| 415 |
#endif |
| 416 |
case S_IFCHR: |
| 417 |
type = 'c'; |
| 418 |
break; |
| 419 |
case S_IFDIR: |
| 420 |
type = 'd'; |
| 421 |
break; |
| 422 |
case S_IFREG: |
| 423 |
type = '-'; |
| 424 |
break; |
| 425 |
#ifdef S_IFBLK |
| 426 |
case S_IFBLK: |
| 427 |
type = 'b'; |
| 428 |
break; |
| 429 |
#endif |
| 430 |
#ifdef S_IFLNK |
| 431 |
case S_IFLNK: |
| 432 |
type = 'l'; |
| 433 |
break; |
| 434 |
#endif |
| 435 |
#ifdef S_IFSOCK |
| 436 |
# ifdef S_IFIFO /* some older machines (e.g., apollo) have a conflict */ |
| 437 |
# if S_IFIFO != S_IFSOCK |
| 438 |
case S_IFSOCK: |
| 439 |
type = 's'; |
| 440 |
break; |
| 441 |
# endif |
| 442 |
# else |
| 443 |
case S_IFSOCK: |
| 444 |
type = 's'; |
| 445 |
break; |
| 446 |
# endif |
| 447 |
#endif /* S_IFSOCK */ |
| 448 |
default: |
| 449 |
type = '?'; |
| 450 |
break; |
| 451 |
} |
| 452 |
#ifdef _WINDOWS |
| 453 |
sprintf(tmp, "%c%s", type, |
| 454 |
PBIT(data->file_info.st_mode, 6, data->file_info.st_mode & S_IRWXU)); |
| 455 |
#else |
| 456 |
sprintf(tmp, "%c%s%s%s", type, |
| 457 |
PBIT(data->file_info.st_mode, 6, data->file_info.st_mode & S_ISUID), |
| 458 |
PBIT(data->file_info.st_mode, 3, data->file_info.st_mode & S_ISGID), |
| 459 |
PTBIT(data->file_info.st_mode, data->file_info.st_mode & S_ISVTX)); |
| 460 |
#endif |
| 461 |
FormatStr(&buf, start, tmp); |
| 462 |
break; |
| 463 |
|
| 464 |
case 'o': /* owner */ |
| 465 |
#ifndef NOUSERS |
| 466 |
name = HTAA_UidToName((int) data->file_info.st_uid); |
| 467 |
if (*name) { |
| 468 |
FormatStr(&buf, start, name); |
| 469 |
} else { |
| 470 |
FormatNum(&buf, start, (int) data->file_info.st_uid); |
| 471 |
} |
| 472 |
#endif |
| 473 |
break; |
| 474 |
|
| 475 |
case 'g': /* group */ |
| 476 |
#ifndef NOUSERS |
| 477 |
name = HTAA_GidToName((int) data->file_info.st_gid); |
| 478 |
if (*name) { |
| 479 |
FormatStr(&buf, start, name); |
| 480 |
} else { |
| 481 |
FormatNum(&buf, start, (int) data->file_info.st_gid); |
| 482 |
} |
| 483 |
#endif |
| 484 |
break; |
| 485 |
|
| 486 |
case 'l': /* link count */ |
| 487 |
FormatNum(&buf, start, (int) data->file_info.st_nlink); |
| 488 |
break; |
| 489 |
|
| 490 |
case '%': /* literal % with flags/width */ |
| 491 |
FormatStr(&buf, start, "%"); |
| 492 |
break; |
| 493 |
|
| 494 |
default: |
| 495 |
fprintf(stderr, |
| 496 |
"Unknown format character `%c' in list format\n", c); |
| 497 |
break; |
| 498 |
} |
| 499 |
if (buf) |
| 500 |
PUTS(buf); |
| 501 |
|
| 502 |
s++; |
| 503 |
} |
| 504 |
FREE(buf); |
| 505 |
PUTC('\n'); |
| 506 |
FREE(str); |
| 507 |
} |
| 508 |
#endif /* LONG_LIST */ |
| 509 |
|
| 510 |
/* Define the representation associated with a file suffix. |
| 511 |
* -------------------------------------------------------- |
| 512 |
* |
| 513 |
* Calling this with suffix set to "*" will set the default |
| 514 |
* representation. |
| 515 |
* Calling this with suffix set to "*.*" will set the default |
| 516 |
* representation for unknown suffix files which contain a ".". |
| 517 |
* |
| 518 |
* The encoding parameter can give a trivial (8bit, 7bit, binary) |
| 519 |
* or real (gzip, compress) encoding. |
| 520 |
* |
| 521 |
* If filename suffix is already defined with the same encoding |
| 522 |
* its previous definition is overridden. |
| 523 |
*/ |
| 524 |
void HTSetSuffix5(const char *suffix, |
| 525 |
const char *representation, |
| 526 |
const char *encoding, |
| 527 |
const char *desc, |
| 528 |
double value) |
| 529 |
{ |
| 530 |
HTSuffix *suff; |
| 531 |
BOOL trivial_enc = (BOOL) IsUnityEncStr(encoding); |
| 532 |
|
| 533 |
if (strcmp(suffix, s_no_suffix) == 0) |
| 534 |
suff = &no_suffix; |
| 535 |
else if (strcmp(suffix, s_unknown_suffix) == 0) |
| 536 |
suff = &unknown_suffix; |
| 537 |
else { |
| 538 |
HTList *cur = HTSuffixes; |
| 539 |
|
| 540 |
while (NULL != (suff = (HTSuffix *) HTList_nextObject(cur))) { |
| 541 |
if (suff->suffix && 0 == strcmp(suff->suffix, suffix) && |
| 542 |
((trivial_enc && IsUnityEnc(suff->encoding)) || |
| 543 |
(!trivial_enc && !IsUnityEnc(suff->encoding) && |
| 544 |
strcmp(encoding, HTAtom_name(suff->encoding)) == 0))) |
| 545 |
break; |
| 546 |
} |
| 547 |
if (!suff) { /* Not found -- create a new node */ |
| 548 |
suff = typecalloc(HTSuffix); |
| 549 |
if (suff == NULL) |
| 550 |
outofmem(__FILE__, "HTSetSuffix"); |
| 551 |
|
| 552 |
if (!HTSuffixes) { |
| 553 |
HTSuffixes = HTList_new(); |
| 554 |
#ifdef LY_FIND_LEAKS |
| 555 |
atexit(free_suffixes); |
| 556 |
#endif |
| 557 |
} |
| 558 |
|
| 559 |
HTList_addObject(HTSuffixes, suff); |
| 560 |
|
| 561 |
StrAllocCopy(suff->suffix, suffix); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
if (representation) |
| 566 |
suff->rep = HTAtom_for(representation); |
| 567 |
|
| 568 |
/* |
| 569 |
* Memory leak fixed. |
| 570 |
* 05-28-94 Lynx 2-3-1 Garrett Arch Blythe |
| 571 |
* Invariant code removed. |
| 572 |
*/ |
| 573 |
suff->encoding = HTAtom_for(encoding); |
| 574 |
|
| 575 |
StrAllocCopy(suff->desc, desc); |
| 576 |
|
| 577 |
suff->quality = (float) value; |
| 578 |
} |
| 579 |
|
| 580 |
#ifdef LY_FIND_LEAKS |
| 581 |
/* |
| 582 |
* Purpose: Free all added suffixes. |
| 583 |
* Arguments: void |
| 584 |
* Return Value: void |
| 585 |
* Remarks/Portability/Dependencies/Restrictions: |
| 586 |
* To be used at program exit. |
| 587 |
* Revision History: |
| 588 |
* 05-28-94 created Lynx 2-3-1 Garrett Arch Blythe |
| 589 |
*/ |
| 590 |
static void free_suffixes(void) |
| 591 |
{ |
| 592 |
HTSuffix *suff = NULL; |
| 593 |
|
| 594 |
/* |
| 595 |
* Loop through all suffixes. |
| 596 |
*/ |
| 597 |
while (!HTList_isEmpty(HTSuffixes)) { |
| 598 |
/* |
| 599 |
* Free off each item and its members if need be. |
| 600 |
*/ |
| 601 |
suff = (HTSuffix *) HTList_removeLastObject(HTSuffixes); |
| 602 |
FREE(suff->suffix); |
| 603 |
FREE(suff->desc); |
| 604 |
FREE(suff); |
| 605 |
} |
| 606 |
/* |
| 607 |
* Free off the list itself. |
| 608 |
*/ |
| 609 |
HTList_delete(HTSuffixes); |
| 610 |
HTSuffixes = NULL; |
| 611 |
} |
| 612 |
#endif /* LY_FIND_LEAKS */ |
| 613 |
|
| 614 |
/* Make the cache file name for a W3 document. |
| 615 |
* ------------------------------------------- |
| 616 |
* Make up a suitable name for saving the node in |
| 617 |
* |
| 618 |
* E.g. /tmp/WWW_Cache_news/[email protected] |
| 619 |
* /tmp/WWW_Cache_http/crnvmc/FIND/xx.xxx.xx |
| 620 |
* |
| 621 |
* On exit: |
| 622 |
* Returns a malloc'ed string which must be freed by the caller. |
| 623 |
*/ |
| 624 |
char *HTCacheFileName(const char *name) |
| 625 |
{ |
| 626 |
char *acc_method = HTParse(name, "", PARSE_ACCESS); |
| 627 |
char *host = HTParse(name, "", PARSE_HOST); |
| 628 |
char *path = HTParse(name, "", PARSE_PATH + PARSE_PUNCTUATION); |
| 629 |
char *result = NULL; |
| 630 |
|
| 631 |
HTSprintf0(&result, "%s/WWW/%s/%s%s", HTCacheRoot, acc_method, host, path); |
| 632 |
|
| 633 |
FREE(path); |
| 634 |
FREE(acc_method); |
| 635 |
FREE(host); |
| 636 |
return result; |
| 637 |
} |
| 638 |
|
| 639 |
/* Open a file for write, creating the path. |
| 640 |
* ----------------------------------------- |
| 641 |
*/ |
| 642 |
#ifdef NOT_IMPLEMENTED |
| 643 |
static int HTCreatePath(const char *path) |
| 644 |
{ |
| 645 |
return -1; |
| 646 |
} |
| 647 |
#endif /* NOT_IMPLEMENTED */ |
| 648 |
|
| 649 |
/* Convert filename from URL-path syntax to local path format |
| 650 |
* ---------------------------------------------------------- |
| 651 |
* Input name is assumed to be the URL-path of a local file |
| 652 |
* URL, i.e. what comes after the "file://localhost". |
| 653 |
* '#'-fragments to be treated as such must already be stripped. |
| 654 |
* If expand_all is FALSE, unescape only escaped '/'. - kw |
| 655 |
* |
| 656 |
* On exit: |
| 657 |
* Returns a malloc'ed string which must be freed by the caller. |
| 658 |
*/ |
| 659 |
char *HTURLPath_toFile(const char *name, |
| 660 |
int expand_all, |
| 661 |
int is_remote GCC_UNUSED) |
| 662 |
{ |
| 663 |
char *path = NULL; |
| 664 |
char *result = NULL; |
| 665 |
|
| 666 |
StrAllocCopy(path, name); |
| 667 |
if (expand_all) |
| 668 |
HTUnEscape(path); /* Interpret all % signs */ |
| 669 |
else |
| 670 |
HTUnEscapeSome(path, "/"); /* Interpret % signs for path delims */ |
| 671 |
|
| 672 |
CTRACE((tfp, "URLPath `%s' means path `%s'\n", name, path)); |
| 673 |
#if defined(USE_DOS_DRIVES) |
| 674 |
StrAllocCopy(result, is_remote ? path : HTDOS_name(path)); |
| 675 |
#else |
| 676 |
StrAllocCopy(result, path); |
| 677 |
#endif |
| 678 |
|
| 679 |
FREE(path); |
| 680 |
|
| 681 |
return result; |
| 682 |
} |
| 683 |
/* Convert filenames between local and WWW formats. |
| 684 |
* ------------------------------------------------ |
| 685 |
* Make up a suitable name for saving the node in |
| 686 |
* |
| 687 |
* E.g. $(HOME)/WWW/news/[email protected] |
| 688 |
* $(HOME)/WWW/http/crnvmc/FIND/xx.xxx.xx |
| 689 |
* |
| 690 |
* On exit: |
| 691 |
* Returns a malloc'ed string which must be freed by the caller. |
| 692 |
*/ |
| 693 |
/* NOTE: Don't use this function if you know that the input is a URL path |
| 694 |
rather than a full URL, use HTURLPath_toFile instead. Otherwise |
| 695 |
this function will return the wrong thing for some unusual |
| 696 |
paths (like ones containing "//", possibly escaped). - kw |
| 697 |
*/ |
| 698 |
char *HTnameOfFile_WWW(const char *name, |
| 699 |
int WWW_prefix, |
| 700 |
int expand_all) |
| 701 |
{ |
| 702 |
char *acc_method = HTParse(name, "", PARSE_ACCESS); |
| 703 |
char *host = HTParse(name, "", PARSE_HOST); |
| 704 |
char *path = HTParse(name, "", PARSE_PATH + PARSE_PUNCTUATION); |
| 705 |
const char *home; |
| 706 |
char *result = NULL; |
| 707 |
|
| 708 |
if (expand_all) { |
| 709 |
HTUnEscape(path); /* Interpret all % signs */ |
| 710 |
} else |
| 711 |
HTUnEscapeSome(path, "/"); /* Interpret % signs for path delims */ |
| 712 |
|
| 713 |
if (0 == strcmp(acc_method, "file") /* local file */ |
| 714 |
||!*acc_method) { /* implicitly local? */ |
| 715 |
if ((0 == strcasecomp(host, HTHostName())) || |
| 716 |
(0 == strcasecomp(host, "localhost")) || !*host) { |
| 717 |
CTRACE((tfp, "Node `%s' means path `%s'\n", name, path)); |
| 718 |
StrAllocCopy(result, HTSYS_name(path)); |
| 719 |
} else if (WWW_prefix) { |
| 720 |
HTSprintf0(&result, "%s%s%s", "/Net/", host, path); |
| 721 |
CTRACE((tfp, "Node `%s' means file `%s'\n", name, result)); |
| 722 |
} else { |
| 723 |
StrAllocCopy(result, path); |
| 724 |
} |
| 725 |
} else if (WWW_prefix) { /* other access */ |
| 726 |
#ifdef VMS |
| 727 |
if ((home = LYGetEnv("HOME")) == NULL) |
| 728 |
home = HTCacheRoot; |
| 729 |
else |
| 730 |
home = HTVMS_wwwName(home); |
| 731 |
#else |
| 732 |
#if defined(_WINDOWS) /* 1997/10/16 (Thu) 20:42:51 */ |
| 733 |
home = Home_Dir(); |
| 734 |
#else |
| 735 |
home = LYGetEnv("HOME"); |
| 736 |
#endif |
| 737 |
if (home == NULL) |
| 738 |
home = "/tmp"; |
| 739 |
#endif /* VMS */ |
| 740 |
HTSprintf0(&result, "%s/WWW/%s/%s%s", home, acc_method, host, path); |
| 741 |
} else { |
| 742 |
StrAllocCopy(result, path); |
| 743 |
} |
| 744 |
|
| 745 |
FREE(host); |
| 746 |
FREE(path); |
| 747 |
FREE(acc_method); |
| 748 |
|
| 749 |
CTRACE((tfp, "HTnameOfFile_WWW(%s,%d,%d) = %s\n", |
| 750 |
name, WWW_prefix, expand_all, result)); |
| 751 |
|
| 752 |
return result; |
| 753 |
} |
| 754 |
|
| 755 |
/* Make a WWW name from a full local path name. |
| 756 |
* -------------------------------------------- |
| 757 |
* |
| 758 |
* Bugs: |
| 759 |
* At present, only the names of two network root nodes are hand-coded |
| 760 |
* in and valid for the NeXT only. This should be configurable in |
| 761 |
* the general case. |
| 762 |
*/ |
| 763 |
char *WWW_nameOfFile(const char *name) |
| 764 |
{ |
| 765 |
char *result = NULL; |
| 766 |
|
| 767 |
#ifdef NeXT |
| 768 |
if (0 == StrNCmp("/private/Net/", name, 13)) { |
| 769 |
HTSprintf0(&result, "%s//%s", STR_FILE_URL, name + 13); |
| 770 |
} else |
| 771 |
#endif /* NeXT */ |
| 772 |
if (0 == StrNCmp(HTMountRoot, name, 5)) { |
| 773 |
HTSprintf0(&result, "%s//%s", STR_FILE_URL, name + 5); |
| 774 |
} else { |
| 775 |
HTSprintf0(&result, "%s//%s%s", STR_FILE_URL, HTHostName(), name); |
| 776 |
} |
| 777 |
CTRACE((tfp, "File `%s'\n\tmeans node `%s'\n", name, result)); |
| 778 |
return result; |
| 779 |
} |
| 780 |
|
| 781 |
/* Determine a suitable suffix, given the representation. |
| 782 |
* ------------------------------------------------------ |
| 783 |
* |
| 784 |
* On entry, |
| 785 |
* rep is the atomized MIME style representation |
| 786 |
* enc is an encoding, trivial (8bit, binary, etc.) or gzip etc. |
| 787 |
* |
| 788 |
* On exit: |
| 789 |
* Returns a pointer to a suitable suffix string if one has been |
| 790 |
* found, else "". |
| 791 |
*/ |
| 792 |
const char *HTFileSuffix(HTAtom *rep, |
| 793 |
const char *enc) |
| 794 |
{ |
| 795 |
HTSuffix *suff; |
| 796 |
|
| 797 |
#ifdef FNAMES_8_3 |
| 798 |
HTSuffix *first_found = NULL; |
| 799 |
#endif |
| 800 |
BOOL trivial_enc; |
| 801 |
int n; |
| 802 |
int i; |
| 803 |
|
| 804 |
#define NO_INIT /* don't init anymore since I do it in Lynx at startup */ |
| 805 |
#ifndef NO_INIT |
| 806 |
if (!HTSuffixes) |
| 807 |
HTFileInit(); |
| 808 |
#endif /* !NO_INIT */ |
| 809 |
|
| 810 |
trivial_enc = (BOOL) IsUnityEncStr(enc); |
| 811 |
n = HTList_count(HTSuffixes); |
| 812 |
for (i = 0; i < n; i++) { |
| 813 |
suff = (HTSuffix *) HTList_objectAt(HTSuffixes, i); |
| 814 |
if (suff->rep == rep && |
| 815 |
#if defined(VMS) || defined(FNAMES_8_3) |
| 816 |
/* Don't return a suffix whose first char is a dot, and which |
| 817 |
has more dots or asterisks after that, for |
| 818 |
these systems - kw */ |
| 819 |
(!suff->suffix || !suff->suffix[0] || suff->suffix[0] != '.' || |
| 820 |
(StrChr(suff->suffix + 1, '.') == NULL && |
| 821 |
StrChr(suff->suffix + 1, '*') == NULL)) && |
| 822 |
#endif |
| 823 |
((trivial_enc && IsUnityEnc(suff->encoding)) || |
| 824 |
(!trivial_enc && !IsUnityEnc(suff->encoding) && |
| 825 |
strcmp(enc, HTAtom_name(suff->encoding)) == 0))) { |
| 826 |
#ifdef FNAMES_8_3 |
| 827 |
if (suff->suffix && (strlen(suff->suffix) <= 4)) { |
| 828 |
/* |
| 829 |
* If length of suffix (including dot) is 4 or smaller, return |
| 830 |
* this one even if we found a longer one earlier - kw |
| 831 |
*/ |
| 832 |
return suff->suffix; |
| 833 |
} else if (!first_found) { |
| 834 |
first_found = suff; /* remember this one */ |
| 835 |
} |
| 836 |
#else |
| 837 |
return suff->suffix; /* OK -- found */ |
| 838 |
#endif |
| 839 |
} |
| 840 |
} |
| 841 |
#ifdef FNAMES_8_3 |
| 842 |
if (first_found) |
| 843 |
return first_found->suffix; |
| 844 |
#endif |
| 845 |
return ""; /* Dunno */ |
| 846 |
} |
| 847 |
|
| 848 |
/* |
| 849 |
* Trim version from VMS filenames to avoid confusing comparisons. |
| 850 |
*/ |
| 851 |
#ifdef VMS |
| 852 |
static const char *VMS_trim_version(const char *filename) |
| 853 |
{ |
| 854 |
const char *result = filename; |
| 855 |
const char *version = StrChr(filename, ';'); |
| 856 |
|
| 857 |
if (version != 0) { |
| 858 |
static char *stripped; |
| 859 |
|
| 860 |
StrAllocCopy(stripped, filename); |
| 861 |
stripped[version - filename] = '\0'; |
| 862 |
result = (const char *) stripped; |
| 863 |
} |
| 864 |
return result; |
| 865 |
} |
| 866 |
#define VMS_DEL_VERSION(name) name = VMS_trim_version(name) |
| 867 |
#else |
| 868 |
#define VMS_DEL_VERSION(name) /* nothing */ |
| 869 |
#endif |
| 870 |
|
| 871 |
/* Determine file format from file name. |
| 872 |
* ------------------------------------- |
| 873 |
* |
| 874 |
* This version will return the representation and also set |
| 875 |
* a variable for the encoding. |
| 876 |
* |
| 877 |
* Encoding may be a unity encoding (binary, 8bit, etc.) or |
| 878 |
* a content-coding like gzip, compress. |
| 879 |
* |
| 880 |
* It will handle for example x.txt, x.txt,Z, x.Z |
| 881 |
*/ |
| 882 |
HTFormat HTFileFormat(const char *filename, |
| 883 |
HTAtom **pencoding, |
| 884 |
const char **pdesc) |
| 885 |
{ |
| 886 |
HTSuffix *suff; |
| 887 |
int n; |
| 888 |
int i; |
| 889 |
int lf; |
| 890 |
const char *search; |
| 891 |
|
| 892 |
VMS_DEL_VERSION(filename); |
| 893 |
|
| 894 |
if ((search = FindSearch(filename)) != NULL) { |
| 895 |
char *newname = NULL; |
| 896 |
HTFormat result; |
| 897 |
|
| 898 |
StrAllocCopy(newname, filename); |
| 899 |
newname[((const char *) search) - filename] = '\0'; |
| 900 |
result = HTFileFormat(newname, pencoding, pdesc); |
| 901 |
free(newname); |
| 902 |
return result; |
| 903 |
} |
| 904 |
|
| 905 |
if (pencoding) |
| 906 |
*pencoding = NULL; |
| 907 |
if (pdesc) |
| 908 |
*pdesc = NULL; |
| 909 |
if (LYforce_HTML_mode) { |
| 910 |
if (pencoding) |
| 911 |
*pencoding = WWW_ENC_8BIT; |
| 912 |
return WWW_HTML; |
| 913 |
} |
| 914 |
#ifndef NO_INIT |
| 915 |
if (!HTSuffixes) |
| 916 |
HTFileInit(); |
| 917 |
#endif /* !NO_INIT */ |
| 918 |
lf = (int) strlen(filename); |
| 919 |
n = HTList_count(HTSuffixes); |
| 920 |
for (i = 0; i < n; i++) { |
| 921 |
int ls; |
| 922 |
|
| 923 |
suff = (HTSuffix *) HTList_objectAt(HTSuffixes, i); |
| 924 |
ls = (int) strlen(suff->suffix); |
| 925 |
if ((ls <= lf) && 0 == strcasecomp(suff->suffix, filename + lf - ls)) { |
| 926 |
int j; |
| 927 |
|
| 928 |
if (pencoding) |
| 929 |
*pencoding = suff->encoding; |
| 930 |
if (pdesc) |
| 931 |
*pdesc = suff->desc; |
| 932 |
if (suff->rep) { |
| 933 |
return suff->rep; /* OK -- found */ |
| 934 |
} |
| 935 |
for (j = 0; j < n; j++) { /* Got encoding, need representation */ |
| 936 |
int ls2; |
| 937 |
|
| 938 |
suff = (HTSuffix *) HTList_objectAt(HTSuffixes, j); |
| 939 |
ls2 = (int) strlen(suff->suffix); |
| 940 |
if ((ls + ls2 <= lf) && |
| 941 |
!strncasecomp(suff->suffix, |
| 942 |
filename + lf - ls - ls2, ls2)) { |
| 943 |
if (suff->rep) { |
| 944 |
if (pdesc && !(*pdesc)) |
| 945 |
*pdesc = suff->desc; |
| 946 |
if (pencoding && IsUnityEnc(*pencoding) && |
| 947 |
*pencoding != WWW_ENC_7BIT && |
| 948 |
!IsUnityEnc(suff->encoding)) |
| 949 |
*pencoding = suff->encoding; |
| 950 |
return suff->rep; |
| 951 |
} |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
/* defaults tree */ |
| 959 |
|
| 960 |
suff = (StrChr(filename, '.') |
| 961 |
? (unknown_suffix.rep |
| 962 |
? &unknown_suffix |
| 963 |
: &no_suffix) |
| 964 |
: &no_suffix); |
| 965 |
|
| 966 |
/* |
| 967 |
* Set default encoding unless found with suffix already. |
| 968 |
*/ |
| 969 |
if (pencoding && !*pencoding) { |
| 970 |
*pencoding = (suff->encoding |
| 971 |
? suff->encoding |
| 972 |
: HTAtom_for("binary")); |
| 973 |
} |
| 974 |
return suff->rep ? suff->rep : WWW_BINARY; |
| 975 |
} |
| 976 |
|
| 977 |
/* Revise the file format in relation to the Lynx charset. - FM |
| 978 |
* ------------------------------------------------------- |
| 979 |
* |
| 980 |
* This checks the format associated with an anchor for |
| 981 |
* an extended MIME Content-Type, and if a charset is |
| 982 |
* indicated, sets Lynx up for proper handling in relation |
| 983 |
* to the currently selected character set. - FM |
| 984 |
*/ |
| 985 |
HTFormat HTCharsetFormat(HTFormat format, |
| 986 |
HTParentAnchor *anchor, |
| 987 |
int default_LYhndl) |
| 988 |
{ |
| 989 |
char *cp = NULL, *cp1, *cp2, *cp3 = NULL, *cp4; |
| 990 |
BOOL chartrans_ok = FALSE; |
| 991 |
int chndl = -1; |
| 992 |
const char *format_name = format->name; |
| 993 |
|
| 994 |
FREE(anchor->charset); |
| 995 |
if (format_name == NULL) |
| 996 |
format_name = ""; |
| 997 |
StrAllocCopy(cp, format_name); |
| 998 |
LYLowerCase(cp); |
| 999 |
if (((cp1 = StrChr(cp, ';')) != NULL) && |
| 1000 |
(cp2 = strstr(cp1, "charset")) != NULL) { |
| 1001 |
CTRACE((tfp, "HTCharsetFormat: Extended MIME Content-Type is %s\n", |
| 1002 |
format_name)); |
| 1003 |
cp2 += 7; |
| 1004 |
while (*cp2 == ' ' || *cp2 == '=') |
| 1005 |
cp2++; |
| 1006 |
StrAllocCopy(cp3, cp2); /* copy to mutilate more */ |
| 1007 |
for (cp4 = cp3; (*cp4 != '\0' && *cp4 != '"' && |
| 1008 |
*cp4 != ';' && *cp4 != ':' && |
| 1009 |
!WHITE(*cp4)); cp4++) { |
| 1010 |
; /* do nothing */ |
| 1011 |
} |
| 1012 |
*cp4 = '\0'; |
| 1013 |
cp4 = cp3; |
| 1014 |
chndl = UCGetLYhndl_byMIME(cp3); |
| 1015 |
if (UCCanTranslateFromTo(chndl, current_char_set)) { |
| 1016 |
chartrans_ok = YES; |
| 1017 |
*cp1 = '\0'; |
| 1018 |
format = HTAtom_for(cp); |
| 1019 |
StrAllocCopy(anchor->charset, cp4); |
| 1020 |
HTAnchor_setUCInfoStage(anchor, chndl, |
| 1021 |
UCT_STAGE_MIME, |
| 1022 |
UCT_SETBY_MIME); |
| 1023 |
} else if (chndl < 0) { |
| 1024 |
/* |
| 1025 |
* Got something but we don't recognize it. |
| 1026 |
*/ |
| 1027 |
chndl = UCLYhndl_for_unrec; |
| 1028 |
if (chndl < 0) |
| 1029 |
/* |
| 1030 |
* UCLYhndl_for_unrec not defined :-( fallback to |
| 1031 |
* UCLYhndl_for_unspec which always valid. |
| 1032 |
*/ |
| 1033 |
chndl = UCLYhndl_for_unspec; /* always >= 0 */ |
| 1034 |
if (UCCanTranslateFromTo(chndl, current_char_set)) { |
| 1035 |
chartrans_ok = YES; |
| 1036 |
HTAnchor_setUCInfoStage(anchor, chndl, |
| 1037 |
UCT_STAGE_MIME, |
| 1038 |
UCT_SETBY_DEFAULT); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
if (chartrans_ok) { |
| 1042 |
LYUCcharset *p_in = HTAnchor_getUCInfoStage(anchor, |
| 1043 |
UCT_STAGE_MIME); |
| 1044 |
LYUCcharset *p_out = HTAnchor_setUCInfoStage(anchor, |
| 1045 |
current_char_set, |
| 1046 |
UCT_STAGE_HTEXT, |
| 1047 |
UCT_SETBY_DEFAULT); |
| 1048 |
|
| 1049 |
if (!p_out) { |
| 1050 |
/* |
| 1051 |
* Try again. |
| 1052 |
*/ |
| 1053 |
p_out = HTAnchor_getUCInfoStage(anchor, UCT_STAGE_HTEXT); |
| 1054 |
} |
| 1055 |
if (!strcmp(p_in->MIMEname, "x-transparent")) { |
| 1056 |
HTPassEightBitRaw = TRUE; |
| 1057 |
HTAnchor_setUCInfoStage(anchor, |
| 1058 |
HTAnchor_getUCLYhndl(anchor, |
| 1059 |
UCT_STAGE_HTEXT), |
| 1060 |
UCT_STAGE_MIME, |
| 1061 |
UCT_SETBY_DEFAULT); |
| 1062 |
} |
| 1063 |
if (!strcmp(p_out->MIMEname, "x-transparent")) { |
| 1064 |
HTPassEightBitRaw = TRUE; |
| 1065 |
HTAnchor_setUCInfoStage(anchor, |
| 1066 |
HTAnchor_getUCLYhndl(anchor, |
| 1067 |
UCT_STAGE_MIME), |
| 1068 |
UCT_STAGE_HTEXT, |
| 1069 |
UCT_SETBY_DEFAULT); |
| 1070 |
} |
| 1071 |
if (p_in->enc != UCT_ENC_CJK) { |
| 1072 |
HTCJK = NOCJK; |
| 1073 |
if (!(p_in->codepoints & |
| 1074 |
UCT_CP_SUBSETOF_LAT1) && |
| 1075 |
chndl == current_char_set) { |
| 1076 |
HTPassEightBitRaw = TRUE; |
| 1077 |
} |
| 1078 |
} else if (p_out->enc == UCT_ENC_CJK) { |
| 1079 |
Set_HTCJK(p_in->MIMEname, p_out->MIMEname); |
| 1080 |
} |
| 1081 |
} else { |
| 1082 |
/* |
| 1083 |
* Cannot translate. If according to some heuristic the given |
| 1084 |
* charset and the current display character both are likely to be |
| 1085 |
* like ISO-8859 in structure, pretend we have some kind of match. |
| 1086 |
*/ |
| 1087 |
BOOL given_is_8859 = (BOOL) (!StrNCmp(cp4, "iso-8859-", 9) && |
| 1088 |
isdigit(UCH(cp4[9]))); |
| 1089 |
BOOL given_is_8859like = (BOOL) (given_is_8859 || |
| 1090 |
!StrNCmp(cp4, "windows-", 8) || |
| 1091 |
!StrNCmp(cp4, "cp12", 4) || |
| 1092 |
!StrNCmp(cp4, "cp-12", 5)); |
| 1093 |
BOOL given_and_display_8859like = (BOOL) (given_is_8859like && |
| 1094 |
(strstr(LYchar_set_names[current_char_set], |
| 1095 |
"ISO-8859") || |
| 1096 |
strstr(LYchar_set_names[current_char_set], |
| 1097 |
"windows-"))); |
| 1098 |
|
| 1099 |
if (given_and_display_8859like) { |
| 1100 |
*cp1 = '\0'; |
| 1101 |
format = HTAtom_for(cp); |
| 1102 |
} |
| 1103 |
if (given_is_8859) { |
| 1104 |
cp1 = &cp4[10]; |
| 1105 |
while (*cp1 && |
| 1106 |
isdigit(UCH(*cp1))) |
| 1107 |
cp1++; |
| 1108 |
*cp1 = '\0'; |
| 1109 |
} |
| 1110 |
if (given_and_display_8859like) { |
| 1111 |
StrAllocCopy(anchor->charset, cp4); |
| 1112 |
HTPassEightBitRaw = TRUE; |
| 1113 |
} |
| 1114 |
HTAlert(*cp4 ? cp4 : anchor->charset); |
| 1115 |
} |
| 1116 |
FREE(cp3); |
| 1117 |
} else if (cp1 != NULL) { |
| 1118 |
/* |
| 1119 |
* No charset parameter is present. Ignore all other parameters, as we |
| 1120 |
* do when charset is present. - FM |
| 1121 |
*/ |
| 1122 |
*cp1 = '\0'; |
| 1123 |
format = HTAtom_for(cp); |
| 1124 |
} |
| 1125 |
FREE(cp); |
| 1126 |
|
| 1127 |
/* |
| 1128 |
* Set up defaults, if needed. - FM |
| 1129 |
*/ |
| 1130 |
if (!chartrans_ok && !anchor->charset && default_LYhndl >= 0) { |
| 1131 |
HTAnchor_setUCInfoStage(anchor, default_LYhndl, |
| 1132 |
UCT_STAGE_MIME, |
| 1133 |
UCT_SETBY_DEFAULT); |
| 1134 |
} |
| 1135 |
HTAnchor_copyUCInfoStage(anchor, |
| 1136 |
UCT_STAGE_PARSER, |
| 1137 |
UCT_STAGE_MIME, |
| 1138 |
-1); |
| 1139 |
|
| 1140 |
return format; |
| 1141 |
} |
| 1142 |
|
| 1143 |
/* Get various pieces of meta info from file name. |
| 1144 |
* ----------------------------------------------- |
| 1145 |
* |
| 1146 |
* LYGetFileInfo fills in information that can be determined without |
| 1147 |
* an actual (new) access to the filesystem, based on current suffix |
| 1148 |
* and character set configuration. If the file has been loaded and |
| 1149 |
* parsed before (with the same URL generated here!) and the anchor |
| 1150 |
* is still around, some results may be influenced by that (in |
| 1151 |
* particular, charset info from a META tag - this is not actually |
| 1152 |
* tested!). |
| 1153 |
* The caller should not keep pointers to the returned objects around |
| 1154 |
* for too long, the valid lifetimes vary. In particular, the returned |
| 1155 |
* charset string should be copied if necessary. If return of the |
| 1156 |
* file_anchor is requested, that one can be used to retrieve |
| 1157 |
* additional bits of info that are stored in the anchor object and |
| 1158 |
* are not covered here; as usual, don't keep pointers to the |
| 1159 |
* file_anchor longer than necessary since the object may disappear |
| 1160 |
* through HTuncache_current_document or at the next document load. |
| 1161 |
* - kw |
| 1162 |
*/ |
| 1163 |
void LYGetFileInfo(const char *filename, |
| 1164 |
HTParentAnchor **pfile_anchor, |
| 1165 |
HTFormat *pformat, |
| 1166 |
HTAtom **pencoding, |
| 1167 |
const char **pdesc, |
| 1168 |
const char **pcharset, |
| 1169 |
int *pfile_cs) |
| 1170 |
{ |
| 1171 |
char *Afn; |
| 1172 |
char *Aname = NULL; |
| 1173 |
HTFormat format; |
| 1174 |
HTAtom *myEnc = NULL; |
| 1175 |
HTParentAnchor *file_anchor; |
| 1176 |
const char *file_csname; |
| 1177 |
int file_cs; |
| 1178 |
|
| 1179 |
/* |
| 1180 |
* Convert filename to URL. Note that it is always supposed to be a |
| 1181 |
* filename, not maybe-filename-maybe-URL, so we don't use |
| 1182 |
* LYFillLocalFileURL and LYEnsureAbsoluteURL. - kw |
| 1183 |
*/ |
| 1184 |
Afn = HTEscape(filename, URL_PATH); |
| 1185 |
LYLocalFileToURL(&Aname, Afn); |
| 1186 |
file_anchor = HTAnchor_findSimpleAddress(Aname); |
| 1187 |
|
| 1188 |
format = HTFileFormat(filename, &myEnc, pdesc); |
| 1189 |
format = HTCharsetFormat(format, file_anchor, UCLYhndl_HTFile_for_unspec); |
| 1190 |
file_cs = HTAnchor_getUCLYhndl(file_anchor, UCT_STAGE_MIME); |
| 1191 |
file_csname = file_anchor->charset; |
| 1192 |
if (!file_csname) { |
| 1193 |
if (file_cs >= 0) |
| 1194 |
file_csname = LYCharSet_UC[file_cs].MIMEname; |
| 1195 |
else |
| 1196 |
file_csname = "display character set"; |
| 1197 |
} |
| 1198 |
CTRACE((tfp, "GetFileInfo: '%s' is a%s %s %s file, charset=%s (%d).\n", |
| 1199 |
filename, |
| 1200 |
((myEnc && *HTAtom_name(myEnc) == '8') ? "n" : myEnc ? "" : |
| 1201 |
*HTAtom_name(format) == 'a' ? "n" : ""), |
| 1202 |
myEnc ? HTAtom_name(myEnc) : "", |
| 1203 |
HTAtom_name(format), |
| 1204 |
file_csname, |
| 1205 |
file_cs)); |
| 1206 |
FREE(Afn); |
| 1207 |
FREE(Aname); |
| 1208 |
if (pfile_anchor) |
| 1209 |
*pfile_anchor = file_anchor; |
| 1210 |
if (pformat) |
| 1211 |
*pformat = format; |
| 1212 |
if (pencoding) |
| 1213 |
*pencoding = myEnc; |
| 1214 |
if (pcharset) |
| 1215 |
*pcharset = file_csname; |
| 1216 |
if (pfile_cs) |
| 1217 |
*pfile_cs = file_cs; |
| 1218 |
} |
| 1219 |
|
| 1220 |
/* Determine value from file name. |
| 1221 |
* ------------------------------- |
| 1222 |
* |
| 1223 |
*/ |
| 1224 |
float HTFileValue(const char *filename) |
| 1225 |
{ |
| 1226 |
HTSuffix *suff; |
| 1227 |
int n; |
| 1228 |
int i; |
| 1229 |
int lf = (int) strlen(filename); |
| 1230 |
|
| 1231 |
#ifndef NO_INIT |
| 1232 |
if (!HTSuffixes) |
| 1233 |
HTFileInit(); |
| 1234 |
#endif /* !NO_INIT */ |
| 1235 |
n = HTList_count(HTSuffixes); |
| 1236 |
for (i = 0; i < n; i++) { |
| 1237 |
int ls; |
| 1238 |
|
| 1239 |
suff = (HTSuffix *) HTList_objectAt(HTSuffixes, i); |
| 1240 |
ls = (int) strlen(suff->suffix); |
| 1241 |
if ((ls <= lf) && 0 == strcmp(suff->suffix, filename + lf - ls)) { |
| 1242 |
CTRACE((tfp, "File: Value of %s is %.3f\n", |
| 1243 |
filename, suff->quality)); |
| 1244 |
return suff->quality; /* OK -- found */ |
| 1245 |
} |
| 1246 |
} |
| 1247 |
return (float) 0.3; /* Dunno! */ |
| 1248 |
} |
| 1249 |
|
| 1250 |
/* |
| 1251 |
* Determine compression type from file name, by looking at its suffix. |
| 1252 |
* Sets as side-effect a pointer to the "dot" that begins the suffix. |
| 1253 |
*/ |
| 1254 |
CompressFileType HTCompressFileType(const char *filename, |
| 1255 |
const char *dots, |
| 1256 |
int *rootlen) |
| 1257 |
{ |
| 1258 |
CompressFileType result = cftNone; |
| 1259 |
const char *search; |
| 1260 |
|
| 1261 |
if ((search = FindSearch(filename)) != NULL) { |
| 1262 |
char *newname = NULL; |
| 1263 |
|
| 1264 |
StrAllocCopy(newname, filename); |
| 1265 |
newname[((const char *) search) - filename] = '\0'; |
| 1266 |
result = HTCompressFileType(newname, dots, rootlen); |
| 1267 |
free(newname); |
| 1268 |
} else { |
| 1269 |
size_t len; |
| 1270 |
const char *ftype; |
| 1271 |
|
| 1272 |
VMS_DEL_VERSION(filename); |
| 1273 |
len = strlen(filename); |
| 1274 |
ftype = filename + len; |
| 1275 |
|
| 1276 |
if ((len > 3) |
| 1277 |
&& !strcasecomp((ftype - 2), "br") |
| 1278 |
&& StrChr(dots, ftype[-3]) != NULL) { |
| 1279 |
result = cftBrotli; |
| 1280 |
ftype -= 3; |
| 1281 |
} else if ((len > 4) |
| 1282 |
&& !strcasecomp((ftype - 3), "bz2") |
| 1283 |
&& StrChr(dots, ftype[-4]) != NULL) { |
| 1284 |
result = cftBzip2; |
| 1285 |
ftype -= 4; |
| 1286 |
} else if ((len > 4) |
| 1287 |
&& !strcasecomp((ftype - 3), "zst") |
| 1288 |
&& StrChr(dots, ftype[-4]) != NULL) { |
| 1289 |
result = cftZstd; |
| 1290 |
ftype -= 4; |
| 1291 |
} else if ((len > 3) |
| 1292 |
&& !strcasecomp((ftype - 2), "gz") |
| 1293 |
&& StrChr(dots, ftype[-3]) != NULL) { |
| 1294 |
result = cftGzip; |
| 1295 |
ftype -= 3; |
| 1296 |
} else if ((len > 3) |
| 1297 |
&& !strcasecomp((ftype - 2), "zz") |
| 1298 |
&& StrChr(dots, ftype[-3]) != NULL) { |
| 1299 |
result = cftDeflate; |
| 1300 |
ftype -= 3; |
| 1301 |
} else if ((len > 2) |
| 1302 |
&& !strcmp((ftype - 1), "Z") |
| 1303 |
&& StrChr(dots, ftype[-2]) != NULL) { |
| 1304 |
result = cftCompress; |
| 1305 |
ftype -= 2; |
| 1306 |
} |
| 1307 |
|
| 1308 |
*rootlen = (int) (ftype - filename); |
| 1309 |
|
| 1310 |
CTRACE((tfp, "HTCompressFileType(%s) returns %d:%s\n", |
| 1311 |
filename, (int) result, filename + *rootlen)); |
| 1312 |
} |
| 1313 |
return result; |
| 1314 |
} |
| 1315 |
|
| 1316 |
/* |
| 1317 |
* Determine expected file-suffix from the compression method. |
| 1318 |
*/ |
| 1319 |
const char *HTCompressTypeToSuffix(CompressFileType method) |
| 1320 |
{ |
| 1321 |
const char *result; |
| 1322 |
|
| 1323 |
switch (method) { |
| 1324 |
default: |
| 1325 |
case cftNone: |
| 1326 |
result = ""; |
| 1327 |
break; |
| 1328 |
case cftGzip: |
| 1329 |
result = ".gz"; |
| 1330 |
break; |
| 1331 |
case cftCompress: |
| 1332 |
result = ".Z"; |
| 1333 |
break; |
| 1334 |
case cftBzip2: |
| 1335 |
result = ".bz2"; |
| 1336 |
break; |
| 1337 |
case cftDeflate: |
| 1338 |
result = ".zz"; |
| 1339 |
break; |
| 1340 |
case cftBrotli: |
| 1341 |
result = ".br"; |
| 1342 |
break; |
| 1343 |
case cftZstd: |
| 1344 |
result = ".zst"; |
| 1345 |
break; |
| 1346 |
} |
| 1347 |
return result; |
| 1348 |
} |
| 1349 |
|
| 1350 |
/* |
| 1351 |
* Determine compression encoding from the compression method. |
| 1352 |
*/ |
| 1353 |
const char *HTCompressTypeToEncoding(CompressFileType method) |
| 1354 |
{ |
| 1355 |
const char *result = NULL; |
| 1356 |
|
| 1357 |
switch (method) { |
| 1358 |
default: |
| 1359 |
case cftNone: |
| 1360 |
result = NULL; |
| 1361 |
break; |
| 1362 |
case cftGzip: |
| 1363 |
result = "gzip"; |
| 1364 |
break; |
| 1365 |
case cftCompress: |
| 1366 |
result = "compress"; |
| 1367 |
break; |
| 1368 |
case cftBzip2: |
| 1369 |
result = "bzip2"; |
| 1370 |
break; |
| 1371 |
case cftDeflate: |
| 1372 |
result = "deflate"; |
| 1373 |
break; |
| 1374 |
case cftBrotli: |
| 1375 |
result = "brotli"; |
| 1376 |
break; |
| 1377 |
case cftZstd: |
| 1378 |
result = "zstd"; |
| 1379 |
break; |
| 1380 |
} |
| 1381 |
return result; |
| 1382 |
} |
| 1383 |
|
| 1384 |
/* |
| 1385 |
* Check if the token from "Content-Encoding" corresponds to a compression |
| 1386 |
* type. RFC 2068 (and cut/paste into RFC 2616) lists these: |
| 1387 |
* gzip |
| 1388 |
* compress |
| 1389 |
* deflate |
| 1390 |
* as well as "identity" (but that does nothing). |
| 1391 |
* RFC 8878 gives zstd. |
| 1392 |
*/ |
| 1393 |
CompressFileType HTEncodingToCompressType(const char *coding) |
| 1394 |
{ |
| 1395 |
CompressFileType result = cftNone; |
| 1396 |
|
| 1397 |
if (coding == NULL) { |
| 1398 |
result = cftNone; |
| 1399 |
} else if (!strcasecomp(coding, "deflate") || |
| 1400 |
!strcasecomp(coding, "x-deflate")) { |
| 1401 |
result = cftDeflate; |
| 1402 |
} else if (!strcasecomp(coding, "gzip") || |
| 1403 |
!strcasecomp(coding, "x-gzip")) { |
| 1404 |
result = cftGzip; |
| 1405 |
} else if (!strcasecomp(coding, "compress") || |
| 1406 |
!strcasecomp(coding, "x-compress")) { |
| 1407 |
result = cftCompress; |
| 1408 |
} else if (!strcasecomp(coding, "bzip2") || |
| 1409 |
!strcasecomp(coding, "x-bzip2")) { |
| 1410 |
result = cftBzip2; |
| 1411 |
} else if (!strcasecomp(coding, "br") || /* actual */ |
| 1412 |
!strcasecomp(coding, "x-br") || |
| 1413 |
!strcasecomp(coding, "brotli")) { /* expected */ |
| 1414 |
result = cftBrotli; |
| 1415 |
} else if (!strcasecomp(coding, "zstd")) { |
| 1416 |
result = cftZstd; |
| 1417 |
} |
| 1418 |
return result; |
| 1419 |
} |
| 1420 |
|
| 1421 |
CompressFileType HTContentTypeToCompressType(const char *ct) |
| 1422 |
{ |
| 1423 |
CompressFileType method = cftNone; |
| 1424 |
|
| 1425 |
if (ct == NULL) { |
| 1426 |
method = cftNone; |
| 1427 |
} else if (!strncasecomp(ct, "application/deflate", 19) || |
| 1428 |
!strncasecomp(ct, "application/x-deflate", 21)) { |
| 1429 |
method = cftGzip; |
| 1430 |
} else if (!strncasecomp(ct, "application/gzip", 16) || |
| 1431 |
!strncasecomp(ct, "application/x-gzip", 18)) { |
| 1432 |
method = cftGzip; |
| 1433 |
} else if (!strncasecomp(ct, "application/compress", 20) || |
| 1434 |
!strncasecomp(ct, "application/x-compress", 22)) { |
| 1435 |
method = cftCompress; |
| 1436 |
} else if (!strncasecomp(ct, "application/bzip2", 17) || |
| 1437 |
!strncasecomp(ct, "application/x-bzip2", 19)) { |
| 1438 |
method = cftBzip2; |
| 1439 |
} else if (!strncasecomp(ct, "application/br", 14) || |
| 1440 |
!strncasecomp(ct, "application/x-br", 16) || |
| 1441 |
!strncasecomp(ct, "application/brotli", 18)) { |
| 1442 |
method = cftBrotli; |
| 1443 |
} else if (!strncasecomp(ct, "application/zstd", 16)) { |
| 1444 |
method = cftZstd; |
| 1445 |
} |
| 1446 |
return method; |
| 1447 |
} |
| 1448 |
|
| 1449 |
/* |
| 1450 |
* "Encoding" is jargon for compression-type. Provide a reverse mapping. |
| 1451 |
*/ |
| 1452 |
BOOL IsCompressionFormat(HTAtom *format, CompressFileType check) |
| 1453 |
{ |
| 1454 |
BOOL result = FALSE; |
| 1455 |
|
| 1456 |
switch (check) { |
| 1457 |
case cftDeflate: |
| 1458 |
result = (format == HTAtom_for("application/deflate") || |
| 1459 |
format == HTAtom_for("application/x-deflate")); |
| 1460 |
break; |
| 1461 |
case cftGzip: |
| 1462 |
result = (format == HTAtom_for("application/gzip") || |
| 1463 |
format == HTAtom_for("application/x-gzip")); |
| 1464 |
break; |
| 1465 |
case cftCompress: |
| 1466 |
result = (format == HTAtom_for("application/compress") || |
| 1467 |
format == HTAtom_for("application/x-compress")); |
| 1468 |
break; |
| 1469 |
case cftBzip2: |
| 1470 |
result = (format == HTAtom_for("application/bzip2") || |
| 1471 |
format == HTAtom_for("application/x-bzip2")); |
| 1472 |
break; |
| 1473 |
case cftBrotli: |
| 1474 |
result = (format == HTAtom_for("application/br") || |
| 1475 |
format == HTAtom_for("application/x-br") || |
| 1476 |
format == HTAtom_for("application/brotli")); |
| 1477 |
break; |
| 1478 |
case cftZstd: |
| 1479 |
result = (format == HTAtom_for("application/zstd")); |
| 1480 |
break; |
| 1481 |
case cftNone: |
| 1482 |
break; |
| 1483 |
} |
| 1484 |
return result; |
| 1485 |
} |
| 1486 |
|
| 1487 |
/* |
| 1488 |
* Check the anchor's content_type and content_encoding elements for a gzip or |
| 1489 |
* Unix compressed file -FM, TD |
| 1490 |
*/ |
| 1491 |
CompressFileType HTContentToCompressType(HTParentAnchor *anchor) |
| 1492 |
{ |
| 1493 |
CompressFileType method = cftNone; |
| 1494 |
const char *ct = HTAnchor_content_type(anchor); |
| 1495 |
const char *ce = HTAnchor_content_encoding(anchor); |
| 1496 |
|
| 1497 |
if (ct != NULL) { |
| 1498 |
method = HTContentTypeToCompressType(ct); |
| 1499 |
} else if (ce != NULL) { |
| 1500 |
method = HTEncodingToCompressType(ce); |
| 1501 |
} |
| 1502 |
return method; |
| 1503 |
} |
| 1504 |
|
| 1505 |
/* Determine write access to a file. |
| 1506 |
* --------------------------------- |
| 1507 |
* |
| 1508 |
* On exit: |
| 1509 |
* Returns YES if file can be accessed and can be written to. |
| 1510 |
* |
| 1511 |
* Bugs: |
| 1512 |
* 1. No code for non-unix systems. |
| 1513 |
* 2. Isn't there a quicker way? |
| 1514 |
*/ |
| 1515 |
BOOL HTEditable(const char *filename GCC_UNUSED) |
| 1516 |
{ |
| 1517 |
#ifndef NO_GROUPS |
| 1518 |
GETGROUPS_T groups[NGROUPS]; |
| 1519 |
uid_t myUid; |
| 1520 |
int ngroups; /* The number of groups */ |
| 1521 |
struct stat fileStatus; |
| 1522 |
int i; |
| 1523 |
|
| 1524 |
if (stat(filename, &fileStatus)) /* Get details of filename */ |
| 1525 |
return NO; /* Can't even access file! */ |
| 1526 |
|
| 1527 |
ngroups = getgroups(NGROUPS, groups); /* Groups to which I belong */ |
| 1528 |
myUid = geteuid(); /* Get my user identifier */ |
| 1529 |
|
| 1530 |
if (TRACE) { |
| 1531 |
int i2; |
| 1532 |
|
| 1533 |
fprintf(tfp, |
| 1534 |
"File mode is 0%o, uid=%d, gid=%d. My uid=%d, %d groups (", |
| 1535 |
(unsigned int) fileStatus.st_mode, |
| 1536 |
(int) fileStatus.st_uid, |
| 1537 |
(int) fileStatus.st_gid, |
| 1538 |
(int) myUid, |
| 1539 |
(int) ngroups); |
| 1540 |
for (i2 = 0; i2 < ngroups; i2++) |
| 1541 |
fprintf(tfp, " %d", (int) groups[i2]); |
| 1542 |
fprintf(tfp, ")\n"); |
| 1543 |
} |
| 1544 |
|
| 1545 |
if (fileStatus.st_mode & 0002) /* I can write anyway? */ |
| 1546 |
return YES; |
| 1547 |
|
| 1548 |
if ((fileStatus.st_mode & 0200) /* I can write my own file? */ |
| 1549 |
&&(fileStatus.st_uid == myUid)) |
| 1550 |
return YES; |
| 1551 |
|
| 1552 |
if (fileStatus.st_mode & 0020) /* Group I am in can write? */ |
| 1553 |
{ |
| 1554 |
for (i = 0; i < ngroups; i++) { |
| 1555 |
if (groups[i] == fileStatus.st_gid) |
| 1556 |
return YES; |
| 1557 |
} |
| 1558 |
} |
| 1559 |
CTRACE((tfp, "\tFile is not editable.\n")); |
| 1560 |
#endif /* NO_GROUPS */ |
| 1561 |
return NO; /* If no excuse, can't do */ |
| 1562 |
} |
| 1563 |
|
| 1564 |
/* Make a save stream. |
| 1565 |
* ------------------- |
| 1566 |
* |
| 1567 |
* The stream must be used for writing back the file. |
| 1568 |
* @@@ no backup done |
| 1569 |
*/ |
| 1570 |
HTStream *HTFileSaveStream(HTParentAnchor *anchor) |
| 1571 |
{ |
| 1572 |
const char *addr = anchor->address; |
| 1573 |
char *localname = HTLocalName(addr); |
| 1574 |
FILE *fp = fopen(localname, BIN_W); |
| 1575 |
|
| 1576 |
FREE(localname); |
| 1577 |
if (!fp) |
| 1578 |
return NULL; |
| 1579 |
|
| 1580 |
return HTFWriter_new(fp); |
| 1581 |
} |
| 1582 |
|
| 1583 |
/* Output one directory entry. |
| 1584 |
* --------------------------- |
| 1585 |
*/ |
| 1586 |
void HTDirEntry(HTStructured * target, const char *tail, const char *entry) |
| 1587 |
{ |
| 1588 |
char *relative = NULL; |
| 1589 |
char *stripped = NULL; |
| 1590 |
char *escaped = NULL; |
| 1591 |
int len; |
| 1592 |
|
| 1593 |
if (entry == NULL) |
| 1594 |
entry = ""; |
| 1595 |
StrAllocCopy(escaped, entry); |
| 1596 |
LYTrimPathSep(escaped); |
| 1597 |
if (strcmp(escaped, "..") != 0) { |
| 1598 |
stripped = escaped; |
| 1599 |
escaped = HTEscape(stripped, URL_XPALPHAS); |
| 1600 |
if (((len = (int) strlen(escaped)) > 2) && |
| 1601 |
escaped[(len - 3)] == '%' && |
| 1602 |
escaped[(len - 2)] == '2' && |
| 1603 |
TOUPPER(escaped[(len - 1)]) == 'F') { |
| 1604 |
escaped[(len - 3)] = '\0'; |
| 1605 |
} |
| 1606 |
} |
| 1607 |
|
| 1608 |
if (isEmpty(tail)) { |
| 1609 |
/* |
| 1610 |
* Handle extra slash at end of path. |
| 1611 |
*/ |
| 1612 |
HTStartAnchor(target, NULL, (escaped[0] != '\0' ? escaped : "/")); |
| 1613 |
} else { |
| 1614 |
/* |
| 1615 |
* If empty tail, gives absolute ref below. |
| 1616 |
*/ |
| 1617 |
relative = NULL; |
| 1618 |
HTSprintf0(&relative, "%s%s%s", |
| 1619 |
tail, |
| 1620 |
(*escaped != '\0' ? "/" : ""), |
| 1621 |
escaped); |
| 1622 |
HTStartAnchor(target, NULL, relative); |
| 1623 |
FREE(relative); |
| 1624 |
} |
| 1625 |
FREE(stripped); |
| 1626 |
FREE(escaped); |
| 1627 |
} |
| 1628 |
|
| 1629 |
static BOOL view_structured(HTFormat format_out) |
| 1630 |
{ |
| 1631 |
BOOL result = FALSE; |
| 1632 |
|
| 1633 |
#ifdef USE_PRETTYSRC |
| 1634 |
if (psrc_view |
| 1635 |
|| (format_out == WWW_DUMP)) |
| 1636 |
result = TRUE; |
| 1637 |
#else |
| 1638 |
if (format_out == WWW_SOURCE) |
| 1639 |
result = TRUE; |
| 1640 |
#endif |
| 1641 |
return result; |
| 1642 |
} |
| 1643 |
|
| 1644 |
/* |
| 1645 |
* Write a DOCTYPE to the given stream if we happen to want to see the |
| 1646 |
* source view, or are dumping source. This is not needed when the source |
| 1647 |
* is not visible, since the document is rendered from a HTStructured object. |
| 1648 |
*/ |
| 1649 |
void HTStructured_doctype(HTStructured * target, HTFormat format_out) |
| 1650 |
{ |
| 1651 |
if (view_structured(format_out)) |
| 1652 |
PUTS(LYNX_DOCTYPE "\n"); |
| 1653 |
} |
| 1654 |
|
| 1655 |
void HTStructured_meta(HTStructured * target, HTFormat format_out) |
| 1656 |
{ |
| 1657 |
if (view_structured(format_out)) |
| 1658 |
PUTS("<meta http-equiv=\"Content-Type\" content=\"" STR_HTML |
| 1659 |
"; charset=iso-8859-1\">\n"); |
| 1660 |
} |
| 1661 |
/* Output parent directory entry. |
| 1662 |
* ------------------------------ |
| 1663 |
* |
| 1664 |
* This gives the TITLE and H1 header, and also a link |
| 1665 |
* to the parent directory if appropriate. |
| 1666 |
* |
| 1667 |
* On exit: |
| 1668 |
* Returns TRUE if an "Up to <parent>" link was not created |
| 1669 |
* for a readable local directory because LONG_LIST is defined |
| 1670 |
* and NO_PARENT_DIR_REFERENCE is not defined, so that the |
| 1671 |
* calling function should use LYListFmtParse() to create a link |
| 1672 |
* to the parent directory. Otherwise, it returns FALSE. - FM |
| 1673 |
*/ |
| 1674 |
BOOL HTDirTitles(HTStructured * target, HTParentAnchor *anchor, |
| 1675 |
HTFormat format_out, |
| 1676 |
int tildeIsTop) |
| 1677 |
{ |
| 1678 |
const char *logical = anchor->address; |
| 1679 |
char *path = HTParse(logical, "", PARSE_PATH + PARSE_PUNCTUATION); |
| 1680 |
char *current; |
| 1681 |
char *cp = NULL; |
| 1682 |
BOOL need_parent_link = FALSE; |
| 1683 |
int i; |
| 1684 |
|
| 1685 |
#if defined(USE_DOS_DRIVES) |
| 1686 |
BOOL local_link = (strlen(logical) > 18 |
| 1687 |
&& !strncasecomp(logical, "file://localhost/", 17) |
| 1688 |
&& LYIsDosDrive(logical + 17)); |
| 1689 |
BOOL is_remote = !local_link; |
| 1690 |
|
| 1691 |
#else |
| 1692 |
#define is_remote TRUE |
| 1693 |
#endif |
| 1694 |
|
| 1695 |
/* |
| 1696 |
* Check tildeIsTop for treating home directory as Welcome (assume the |
| 1697 |
* tilde is not followed by a username). - FM |
| 1698 |
*/ |
| 1699 |
if (tildeIsTop && !StrNCmp(path, "/~", 2)) { |
| 1700 |
if (path[2] == '\0') { |
| 1701 |
path[1] = '\0'; |
| 1702 |
} else { |
| 1703 |
for (i = 0; path[(i + 2)]; i++) { |
| 1704 |
path[i] = path[(i + 2)]; |
| 1705 |
} |
| 1706 |
path[i] = '\0'; |
| 1707 |
} |
| 1708 |
} |
| 1709 |
|
| 1710 |
/* |
| 1711 |
* Trim out the ;type= parameter, if present. - FM |
| 1712 |
*/ |
| 1713 |
if ((cp = strrchr(path, ';')) != NULL) { |
| 1714 |
if (!strncasecomp((cp + 1), "type=", 5)) { |
| 1715 |
if (TOUPPER(*(cp + 6)) == 'D' || |
| 1716 |
TOUPPER(*(cp + 6)) == 'A' || |
| 1717 |
TOUPPER(*(cp + 6)) == 'I') |
| 1718 |
*cp = '\0'; |
| 1719 |
} |
| 1720 |
cp = NULL; |
| 1721 |
} |
| 1722 |
current = LYPathLeaf(path); /* last part or "" */ |
| 1723 |
|
| 1724 |
{ |
| 1725 |
char *printable = NULL; |
| 1726 |
|
| 1727 |
#ifdef DIRED_SUPPORT |
| 1728 |
printable = HTURLPath_toFile(((!strncasecomp(path, "/%2F", 4)) /* "//" ? */ |
| 1729 |
? (path + 1) |
| 1730 |
: path), |
| 1731 |
TRUE, |
| 1732 |
is_remote); |
| 1733 |
if (0 == strncasecomp(printable, "/vmsysu:", 8) || |
| 1734 |
0 == strncasecomp(printable, "/anonymou.", 10)) { |
| 1735 |
StrAllocCopy(cp, (printable + 1)); |
| 1736 |
StrAllocCopy(printable, cp); |
| 1737 |
FREE(cp); |
| 1738 |
} |
| 1739 |
#else |
| 1740 |
StrAllocCopy(printable, current); |
| 1741 |
HTUnEscape(printable); |
| 1742 |
#endif /* DIRED_SUPPORT */ |
| 1743 |
|
| 1744 |
HTStructured_doctype(target, format_out); |
| 1745 |
|
| 1746 |
START(HTML_HEAD); |
| 1747 |
PUTC('\n'); |
| 1748 |
START(HTML_TITLE); |
| 1749 |
PUTS(*printable ? printable : WELCOME_MSG); |
| 1750 |
PUTS(SEGMENT_DIRECTORY); |
| 1751 |
END(HTML_TITLE); |
| 1752 |
PUTC('\n'); |
| 1753 |
HTStructured_meta(target, format_out); |
| 1754 |
END(HTML_HEAD); |
| 1755 |
PUTC('\n'); |
| 1756 |
|
| 1757 |
START(HTML_BODY); |
| 1758 |
PUTC('\n'); |
| 1759 |
|
| 1760 |
#ifdef DIRED_SUPPORT |
| 1761 |
START(HTML_H2); |
| 1762 |
PUTS(*printable ? SEGMENT_CURRENT_DIR : ""); |
| 1763 |
PUTS(*printable ? printable : WELCOME_MSG); |
| 1764 |
END(HTML_H2); |
| 1765 |
PUTC('\n'); |
| 1766 |
#else |
| 1767 |
START(HTML_H1); |
| 1768 |
PUTS(*printable ? printable : WELCOME_MSG); |
| 1769 |
END(HTML_H1); |
| 1770 |
PUTC('\n'); |
| 1771 |
#endif /* DIRED_SUPPORT */ |
| 1772 |
if (((0 == strncasecomp(printable, "vmsysu:", 7)) && |
| 1773 |
(cp = StrChr(printable, '.')) != NULL && |
| 1774 |
StrChr(cp, '/') == NULL) || |
| 1775 |
(0 == strncasecomp(printable, "anonymou.", 9) && |
| 1776 |
StrChr(printable, '/') == NULL)) { |
| 1777 |
FREE(printable); |
| 1778 |
FREE(path); |
| 1779 |
return (need_parent_link); |
| 1780 |
} |
| 1781 |
FREE(printable); |
| 1782 |
} |
| 1783 |
|
| 1784 |
#ifndef NO_PARENT_DIR_REFERENCE |
| 1785 |
/* |
| 1786 |
* Make link back to parent directory. |
| 1787 |
*/ |
| 1788 |
if (current - path > 0 |
| 1789 |
&& LYIsPathSep(current[-1]) |
| 1790 |
&& current[0] != '\0') { /* was a slash AND something else too */ |
| 1791 |
char *parent = NULL; |
| 1792 |
char *relative = NULL; |
| 1793 |
|
| 1794 |
current[-1] = '\0'; |
| 1795 |
parent = strrchr(path, '/'); /* penultimate slash */ |
| 1796 |
|
| 1797 |
if ((parent && |
| 1798 |
(!strcmp(parent, "/..") || |
| 1799 |
!strncasecomp(parent, "/%2F", 4))) || |
| 1800 |
!strncasecomp(current, "%2F", 3)) { |
| 1801 |
FREE(path); |
| 1802 |
return (need_parent_link); |
| 1803 |
} |
| 1804 |
|
| 1805 |
relative = NULL; |
| 1806 |
HTSprintf0(&relative, "%s/..", current); |
| 1807 |
|
| 1808 |
#if defined(DOSPATH) || defined(__EMX__) |
| 1809 |
if (local_link) { |
| 1810 |
if (parent != 0 && strlen(parent) == 3) { |
| 1811 |
StrAllocCat(relative, "/."); |
| 1812 |
} |
| 1813 |
} else |
| 1814 |
#endif |
| 1815 |
|
| 1816 |
#if !defined (VMS) |
| 1817 |
{ |
| 1818 |
/* |
| 1819 |
* On Unix, if it's not ftp and the directory cannot be read, don't |
| 1820 |
* put out a link. |
| 1821 |
* |
| 1822 |
* On VMS, this problem is dealt with internally by |
| 1823 |
* HTVMSBrowseDir(). |
| 1824 |
*/ |
| 1825 |
DIR *dp = NULL; |
| 1826 |
|
| 1827 |
if (LYisLocalFile(logical)) { |
| 1828 |
/* |
| 1829 |
* We need an absolute file path for the opendir. We also need |
| 1830 |
* to unescape for this test. Don't worry about %2F now, they |
| 1831 |
* presumably have been dealt with above, and shouldn't appear |
| 1832 |
* for local files anyway... Assume OS / filesystem will just |
| 1833 |
* ignore superfluous slashes. - KW |
| 1834 |
*/ |
| 1835 |
char *fullparentpath = NULL; |
| 1836 |
|
| 1837 |
/* |
| 1838 |
* Path has been shortened above. |
| 1839 |
*/ |
| 1840 |
StrAllocCopy(fullparentpath, *path ? path : "/"); |
| 1841 |
|
| 1842 |
/* |
| 1843 |
* Guard against weirdness. |
| 1844 |
*/ |
| 1845 |
if (0 == strcmp(current, "..")) { |
| 1846 |
StrAllocCat(fullparentpath, "/../.."); |
| 1847 |
} else if (0 == strcmp(current, ".")) { |
| 1848 |
StrAllocCat(fullparentpath, "/.."); |
| 1849 |
} |
| 1850 |
|
| 1851 |
HTUnEscape(fullparentpath); |
| 1852 |
if ((dp = opendir(fullparentpath)) == NULL) { |
| 1853 |
FREE(fullparentpath); |
| 1854 |
FREE(relative); |
| 1855 |
FREE(path); |
| 1856 |
return (need_parent_link); |
| 1857 |
} |
| 1858 |
closedir(dp); |
| 1859 |
FREE(fullparentpath); |
| 1860 |
#ifdef LONG_LIST |
| 1861 |
need_parent_link = TRUE; |
| 1862 |
FREE(path); |
| 1863 |
FREE(relative); |
| 1864 |
return (need_parent_link); |
| 1865 |
#endif /* LONG_LIST */ |
| 1866 |
} |
| 1867 |
} |
| 1868 |
#endif /* !VMS */ |
| 1869 |
HTStartAnchor(target, "", relative); |
| 1870 |
FREE(relative); |
| 1871 |
|
| 1872 |
PUTS(SEGMENT_UP_TO); |
| 1873 |
if (parent) { |
| 1874 |
if ((0 == strcmp(current, ".")) || |
| 1875 |
(0 == strcmp(current, ".."))) { |
| 1876 |
/* |
| 1877 |
* Should not happen, but if it does, at least avoid giving |
| 1878 |
* misleading info. - KW |
| 1879 |
*/ |
| 1880 |
PUTS(".."); |
| 1881 |
} else { |
| 1882 |
char *printable = NULL; |
| 1883 |
|
| 1884 |
StrAllocCopy(printable, parent + 1); |
| 1885 |
HTUnEscape(printable); |
| 1886 |
PUTS(printable); |
| 1887 |
FREE(printable); |
| 1888 |
} |
| 1889 |
} else { |
| 1890 |
PUTC('/'); |
| 1891 |
} |
| 1892 |
END(HTML_A); |
| 1893 |
PUTC('\n'); |
| 1894 |
} |
| 1895 |
#endif /* !NO_PARENT_DIR_REFERENCE */ |
| 1896 |
|
| 1897 |
FREE(path); |
| 1898 |
return (need_parent_link); |
| 1899 |
} |
| 1900 |
|
| 1901 |
#if defined HAVE_READDIR |
| 1902 |
/* Send README file. |
| 1903 |
* ----------------- |
| 1904 |
* |
| 1905 |
* If a README file exists, then it is inserted into the document here. |
| 1906 |
*/ |
| 1907 |
static void do_readme(HTStructured * target, const char *localname) |
| 1908 |
{ |
| 1909 |
FILE *fp; |
| 1910 |
char *readme_file_name = NULL; |
| 1911 |
int ch; |
| 1912 |
|
| 1913 |
HTSprintf0(&readme_file_name, "%s/%s", localname, HT_DIR_README_FILE); |
| 1914 |
|
| 1915 |
fp = fopen(readme_file_name, "r"); |
| 1916 |
|
| 1917 |
if (fp) { |
| 1918 |
START(HTML_PRE); |
| 1919 |
while ((ch = fgetc(fp)) != EOF) { |
| 1920 |
PUTC((char) ch); |
| 1921 |
} |
| 1922 |
END(HTML_PRE); |
| 1923 |
HTDisplayPartial(); |
| 1924 |
fclose(fp); |
| 1925 |
} |
| 1926 |
FREE(readme_file_name); |
| 1927 |
} |
| 1928 |
|
| 1929 |
#define DIRED_BLOK(obj) (((DIRED *)(obj))->sort_tags) |
| 1930 |
#define DIRED_NAME(obj) (((DIRED *)(obj))->file_name) |
| 1931 |
|
| 1932 |
#define NM_cmp(a,b) ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0)) |
| 1933 |
|
| 1934 |
#if defined(LONG_LIST) && defined(DIRED_SUPPORT) |
| 1935 |
static const char *file_type(const char *path) |
| 1936 |
{ |
| 1937 |
const char *type; |
| 1938 |
|
| 1939 |
while (*path == '.') |
| 1940 |
++path; |
| 1941 |
type = StrChr(path, '.'); |
| 1942 |
if (type == NULL) |
| 1943 |
type = ""; |
| 1944 |
return type; |
| 1945 |
} |
| 1946 |
#endif /* LONG_LIST && DIRED_SUPPORT */ |
| 1947 |
|
| 1948 |
static int dired_cmp(void *a, void *b) |
| 1949 |
{ |
| 1950 |
DIRED *p = (DIRED *) a; |
| 1951 |
DIRED *q = (DIRED *) b; |
| 1952 |
int code = p->sort_tags - q->sort_tags; |
| 1953 |
|
| 1954 |
#if defined(LONG_LIST) && defined(DIRED_SUPPORT) |
| 1955 |
if (code == 0) { |
| 1956 |
switch (dir_list_order) { |
| 1957 |
case ORDER_BY_SIZE: |
| 1958 |
code = -NM_cmp(p->file_info.st_size, q->file_info.st_size); |
| 1959 |
break; |
| 1960 |
case ORDER_BY_DATE: |
| 1961 |
code = -NM_cmp(p->file_info.st_mtime, q->file_info.st_mtime); |
| 1962 |
break; |
| 1963 |
case ORDER_BY_MODE: |
| 1964 |
code = NM_cmp(p->file_info.st_mode, q->file_info.st_mode); |
| 1965 |
break; |
| 1966 |
case ORDER_BY_USER: |
| 1967 |
code = NM_cmp(p->file_info.st_uid, q->file_info.st_uid); |
| 1968 |
break; |
| 1969 |
case ORDER_BY_GROUP: |
| 1970 |
code = NM_cmp(p->file_info.st_gid, q->file_info.st_gid); |
| 1971 |
break; |
| 1972 |
case ORDER_BY_TYPE: |
| 1973 |
code = AS_cmp(file_type(p->file_name), file_type(q->file_name)); |
| 1974 |
break; |
| 1975 |
default: |
| 1976 |
code = 0; |
| 1977 |
break; |
| 1978 |
} |
| 1979 |
} |
| 1980 |
#endif /* LONG_LIST && DIRED_SUPPORT */ |
| 1981 |
if (code == 0) |
| 1982 |
code = AS_cmp(p->file_name, q->file_name); |
| 1983 |
#if 0 |
| 1984 |
CTRACE((tfp, "dired_cmp(%d) ->%d\n\t%c:%s (%s)\n\t%c:%s (%s)\n", |
| 1985 |
dir_list_order, |
| 1986 |
code, |
| 1987 |
p->sort_tags, p->file_name, file_type(p->file_name), |
| 1988 |
q->sort_tags, q->file_name, file_type(q->file_name))); |
| 1989 |
#endif |
| 1990 |
return code; |
| 1991 |
} |
| 1992 |
|
| 1993 |
static int print_local_dir(DIR *dp, char *localname, |
| 1994 |
HTParentAnchor *anchor, |
| 1995 |
HTFormat format_out, |
| 1996 |
HTStream *sink) |
| 1997 |
{ |
| 1998 |
HTStructured *target; /* HTML object */ |
| 1999 |
HTBTree *bt; |
| 2000 |
HTStructuredClass targetClass; |
| 2001 |
STRUCT_DIRENT *dirbuf; |
| 2002 |
char *pathname = NULL; |
| 2003 |
char *tail = NULL; |
| 2004 |
const char *p; |
| 2005 |
char *tmpfilename = NULL; |
| 2006 |
BOOL need_parent_link = FALSE; |
| 2007 |
BOOL preformatted = FALSE; |
| 2008 |
int status; |
| 2009 |
struct stat *actual_info; |
| 2010 |
|
| 2011 |
#ifdef DISP_PARTIAL |
| 2012 |
int num_of_entries = 0; /* lines counter */ |
| 2013 |
#endif |
| 2014 |
|
| 2015 |
#ifdef S_IFLNK |
| 2016 |
struct stat link_info; |
| 2017 |
#endif |
| 2018 |
|
| 2019 |
CTRACE((tfp, "print_local_dir() started\n")); |
| 2020 |
|
| 2021 |
pathname = HTParse(anchor->address, "", |
| 2022 |
PARSE_PATH + PARSE_PUNCTUATION); |
| 2023 |
|
| 2024 |
if ((p = strrchr(pathname, '/')) == NULL) |
| 2025 |
p = "/"; |
| 2026 |
StrAllocCopy(tail, (p + 1)); |
| 2027 |
FREE(pathname); |
| 2028 |
|
| 2029 |
if (UCLYhndl_HTFile_for_unspec >= 0) { |
| 2030 |
HTAnchor_setUCInfoStage(anchor, |
| 2031 |
UCLYhndl_HTFile_for_unspec, |
| 2032 |
UCT_STAGE_PARSER, |
| 2033 |
UCT_SETBY_DEFAULT); |
| 2034 |
} |
| 2035 |
|
| 2036 |
target = HTML_new(anchor, format_out, sink); |
| 2037 |
targetClass = *target->isa; /* Copy routine entry points */ |
| 2038 |
|
| 2039 |
/* |
| 2040 |
* The need_parent_link flag will be set if an "Up to <parent>" link was |
| 2041 |
* not created for a readable parent in HTDirTitles() because LONG_LIST is |
| 2042 |
* defined and NO_PARENT_DIR_REFERENCE is not defined so that need we to |
| 2043 |
* create the link via an LYListFmtParse() call. - FM |
| 2044 |
*/ |
| 2045 |
need_parent_link = HTDirTitles(target, anchor, format_out, FALSE); |
| 2046 |
|
| 2047 |
#ifdef DIRED_SUPPORT |
| 2048 |
if (!isLYNXCGI(anchor->address)) { |
| 2049 |
HTAnchor_setFormat(anchor, WWW_DIRED); |
| 2050 |
lynx_edit_mode = TRUE; |
| 2051 |
} |
| 2052 |
#endif /* DIRED_SUPPORT */ |
| 2053 |
if (HTDirReadme == HT_DIR_README_TOP) |
| 2054 |
do_readme(target, localname); |
| 2055 |
|
| 2056 |
bt = HTBTree_new(dired_cmp); |
| 2057 |
|
| 2058 |
_HTProgress(READING_DIRECTORY); |
| 2059 |
status = HT_LOADED; /* assume we don't get interrupted */ |
| 2060 |
while ((dirbuf = readdir(dp)) != NULL) { |
| 2061 |
/* |
| 2062 |
* While there are directory entries to be read... |
| 2063 |
*/ |
| 2064 |
DIRED *data = NULL; |
| 2065 |
|
| 2066 |
#ifdef STRUCT_DIRENT__D_INO |
| 2067 |
if (dirbuf->d_ino == 0) |
| 2068 |
/* |
| 2069 |
* If the entry is not being used, skip it. |
| 2070 |
*/ |
| 2071 |
continue; |
| 2072 |
#endif |
| 2073 |
/* |
| 2074 |
* Skip self, parent if handled in HTDirTitles() or if |
| 2075 |
* NO_PARENT_DIR_REFERENCE is not defined, and any dot files if |
| 2076 |
* no_dotfiles is set or show_dotfiles is not set. - FM |
| 2077 |
*/ |
| 2078 |
if (!strcmp(dirbuf->d_name, ".") /* self */ || |
| 2079 |
(!strcmp(dirbuf->d_name, "..") /* parent */ && |
| 2080 |
need_parent_link == FALSE) || |
| 2081 |
((strcmp(dirbuf->d_name, "..")) && |
| 2082 |
(dirbuf->d_name[0] == '.' && |
| 2083 |
(no_dotfiles || !show_dotfiles)))) |
| 2084 |
continue; |
| 2085 |
|
| 2086 |
StrAllocCopy(tmpfilename, localname); |
| 2087 |
/* |
| 2088 |
* If filename is not root directory, add trailing separator. |
| 2089 |
*/ |
| 2090 |
LYAddPathSep(&tmpfilename); |
| 2091 |
|
| 2092 |
StrAllocCat(tmpfilename, dirbuf->d_name); |
| 2093 |
data = (DIRED *) malloc(sizeof(DIRED) + strlen(dirbuf->d_name) + 4); |
| 2094 |
if (data == NULL) { |
| 2095 |
status = HT_PARTIAL_CONTENT; |
| 2096 |
break; |
| 2097 |
} |
| 2098 |
LYTrimPathSep(tmpfilename); |
| 2099 |
|
| 2100 |
actual_info = &(data->file_info); |
| 2101 |
#ifdef S_IFLNK |
| 2102 |
if (lstat(tmpfilename, actual_info) < 0) { |
| 2103 |
actual_info->st_mode = 0; |
| 2104 |
} else { |
| 2105 |
if (S_ISLNK(actual_info->st_mode)) { |
| 2106 |
actual_info = &link_info; |
| 2107 |
if (stat(tmpfilename, actual_info) < 0) |
| 2108 |
actual_info->st_mode = 0; |
| 2109 |
} |
| 2110 |
} |
| 2111 |
#else |
| 2112 |
if (stat(tmpfilename, actual_info) < 0) |
| 2113 |
actual_info->st_mode = 0; |
| 2114 |
#endif |
| 2115 |
|
| 2116 |
strcpy(data->file_name, dirbuf->d_name); |
| 2117 |
#ifndef DIRED_SUPPORT |
| 2118 |
if (S_ISDIR(actual_info->st_mode)) { |
| 2119 |
data->sort_tags = 'D'; |
| 2120 |
} else { |
| 2121 |
data->sort_tags = 'F'; |
| 2122 |
/* D & F to have first directories, then files */ |
| 2123 |
} |
| 2124 |
#else |
| 2125 |
if (S_ISDIR(actual_info->st_mode)) { |
| 2126 |
if (dir_list_style == MIXED_STYLE) { |
| 2127 |
data->sort_tags = ' '; |
| 2128 |
LYAddPathSep0(data->file_name); |
| 2129 |
} else if (!strcmp(dirbuf->d_name, "..")) { |
| 2130 |
data->sort_tags = 'A'; |
| 2131 |
} else { |
| 2132 |
data->sort_tags = 'D'; |
| 2133 |
} |
| 2134 |
} else if (dir_list_style == MIXED_STYLE) { |
| 2135 |
data->sort_tags = ' '; |
| 2136 |
} else if (dir_list_style == FILES_FIRST) { |
| 2137 |
data->sort_tags = 'C'; |
| 2138 |
/* C & D to have first files, then directories */ |
| 2139 |
} else { |
| 2140 |
data->sort_tags = 'F'; |
| 2141 |
} |
| 2142 |
#endif /* !DIRED_SUPPORT */ |
| 2143 |
/* |
| 2144 |
* Sort dirname in the tree bt. |
| 2145 |
*/ |
| 2146 |
HTBTree_add(bt, data); |
| 2147 |
|
| 2148 |
#ifdef DISP_PARTIAL |
| 2149 |
/* optimize for expensive operation: */ |
| 2150 |
if (num_of_entries % (partial_threshold > 0 ? |
| 2151 |
partial_threshold : display_lines) == 0) { |
| 2152 |
if (HTCheckForInterrupt()) { |
| 2153 |
status = HT_PARTIAL_CONTENT; |
| 2154 |
break; |
| 2155 |
} |
| 2156 |
} |
| 2157 |
num_of_entries++; |
| 2158 |
#endif /* DISP_PARTIAL */ |
| 2159 |
|
| 2160 |
} /* end while directory entries left to read */ |
| 2161 |
|
| 2162 |
if (status != HT_PARTIAL_CONTENT) |
| 2163 |
_HTProgress(OPERATION_OK); |
| 2164 |
else |
| 2165 |
CTRACE((tfp, "Reading the directory interrupted by user\n")); |
| 2166 |
|
| 2167 |
/* |
| 2168 |
* Run through tree printing out in order. |
| 2169 |
*/ |
| 2170 |
{ |
| 2171 |
HTBTElement *next_element = HTBTree_next(bt, NULL); |
| 2172 |
|
| 2173 |
/* pick up the first element of the list */ |
| 2174 |
int num_of_entries_output = 0; /* lines counter */ |
| 2175 |
|
| 2176 |
char state; |
| 2177 |
|
| 2178 |
/* I for initial (.. file), |
| 2179 |
D for directory file, |
| 2180 |
F for file */ |
| 2181 |
|
| 2182 |
#ifdef DIRED_SUPPORT |
| 2183 |
char test; |
| 2184 |
#endif /* DIRED_SUPPORT */ |
| 2185 |
state = 'I'; |
| 2186 |
|
| 2187 |
while (next_element != NULL) { |
| 2188 |
DIRED *entry; |
| 2189 |
|
| 2190 |
#ifndef DISP_PARTIAL |
| 2191 |
if (num_of_entries_output % HTMAX(display_lines, 10) == 0) { |
| 2192 |
if (HTCheckForInterrupt()) { |
| 2193 |
_HTProgress(TRANSFER_INTERRUPTED); |
| 2194 |
status = HT_PARTIAL_CONTENT; |
| 2195 |
break; |
| 2196 |
} |
| 2197 |
} |
| 2198 |
#endif |
| 2199 |
StrAllocCopy(tmpfilename, localname); |
| 2200 |
/* |
| 2201 |
* If filename is not root directory. |
| 2202 |
*/ |
| 2203 |
LYAddPathSep(&tmpfilename); |
| 2204 |
|
| 2205 |
entry = (DIRED *) (HTBTree_object(next_element)); |
| 2206 |
/* |
| 2207 |
* Append the current entry's filename to the path. |
| 2208 |
*/ |
| 2209 |
StrAllocCat(tmpfilename, entry->file_name); |
| 2210 |
HTSimplify(tmpfilename, LYIsPathSep(*tmpfilename)); |
| 2211 |
/* |
| 2212 |
* Output the directory entry. |
| 2213 |
*/ |
| 2214 |
if (strcmp(DIRED_NAME(HTBTree_object(next_element)), "..")) { |
| 2215 |
#ifdef DIRED_SUPPORT |
| 2216 |
test = |
| 2217 |
(char) (DIRED_BLOK(HTBTree_object(next_element)) |
| 2218 |
== 'D' ? 'D' : 'F'); |
| 2219 |
if (state != test) { |
| 2220 |
#ifndef LONG_LIST |
| 2221 |
if (dir_list_style == FILES_FIRST) { |
| 2222 |
if (state == 'F') { |
| 2223 |
END(HTML_DIR); |
| 2224 |
PUTC('\n'); |
| 2225 |
} |
| 2226 |
} else if (dir_list_style != MIXED_STYLE) |
| 2227 |
if (state == 'D') { |
| 2228 |
END(HTML_DIR); |
| 2229 |
PUTC('\n'); |
| 2230 |
} |
| 2231 |
#endif /* !LONG_LIST */ |
| 2232 |
state = |
| 2233 |
(char) (DIRED_BLOK(HTBTree_object(next_element)) |
| 2234 |
== 'D' ? 'D' : 'F'); |
| 2235 |
if (preformatted) { |
| 2236 |
END(HTML_PRE); |
| 2237 |
PUTC('\n'); |
| 2238 |
preformatted = FALSE; |
| 2239 |
} |
| 2240 |
START(HTML_H2); |
| 2241 |
if (dir_list_style != MIXED_STYLE) { |
| 2242 |
START(HTML_EM); |
| 2243 |
PUTS(state == 'D' |
| 2244 |
? LABEL_SUBDIRECTORIES |
| 2245 |
: LABEL_FILES); |
| 2246 |
END(HTML_EM); |
| 2247 |
} |
| 2248 |
END(HTML_H2); |
| 2249 |
PUTC('\n'); |
| 2250 |
#ifndef LONG_LIST |
| 2251 |
START(HTML_DIR); |
| 2252 |
PUTC('\n'); |
| 2253 |
#endif /* !LONG_LIST */ |
| 2254 |
} |
| 2255 |
#else |
| 2256 |
if (state != DIRED_BLOK(HTBTree_object(next_element))) { |
| 2257 |
#ifndef LONG_LIST |
| 2258 |
if (state == 'D') { |
| 2259 |
END(HTML_DIR); |
| 2260 |
PUTC('\n'); |
| 2261 |
} |
| 2262 |
#endif /* !LONG_LIST */ |
| 2263 |
state = |
| 2264 |
(char) (DIRED_BLOK(HTBTree_object(next_element)) |
| 2265 |
== 'D' ? 'D' : 'F'); |
| 2266 |
if (preformatted) { |
| 2267 |
END(HTML_PRE); |
| 2268 |
PUTC('\n'); |
| 2269 |
preformatted = FALSE; |
| 2270 |
} |
| 2271 |
START(HTML_H2); |
| 2272 |
START(HTML_EM); |
| 2273 |
PUTS(state == 'D' |
| 2274 |
? LABEL_SUBDIRECTORIES |
| 2275 |
: LABEL_FILES); |
| 2276 |
END(HTML_EM); |
| 2277 |
END(HTML_H2); |
| 2278 |
PUTC('\n'); |
| 2279 |
#ifndef LONG_LIST |
| 2280 |
START(HTML_DIR); |
| 2281 |
PUTC('\n'); |
| 2282 |
#endif /* !LONG_LIST */ |
| 2283 |
} |
| 2284 |
#endif /* DIRED_SUPPORT */ |
| 2285 |
#ifndef LONG_LIST |
| 2286 |
START(HTML_LI); |
| 2287 |
#endif /* !LONG_LIST */ |
| 2288 |
} |
| 2289 |
if (!preformatted) { |
| 2290 |
START(HTML_PRE); |
| 2291 |
PUTC('\n'); |
| 2292 |
preformatted = TRUE; |
| 2293 |
} |
| 2294 |
#ifdef LONG_LIST |
| 2295 |
LYListFmtParse(list_format, entry, tmpfilename, target, tail); |
| 2296 |
#else |
| 2297 |
HTDirEntry(target, tail, entry->file_name); |
| 2298 |
PUTS(entry->file_name); |
| 2299 |
END(HTML_A); |
| 2300 |
MAYBE_END(HTML_LI); |
| 2301 |
PUTC('\n'); |
| 2302 |
#endif /* LONG_LIST */ |
| 2303 |
|
| 2304 |
next_element = HTBTree_next(bt, next_element); |
| 2305 |
/* pick up the next element of the list; |
| 2306 |
if none, return NULL */ |
| 2307 |
|
| 2308 |
/* optimize for expensive operation: */ |
| 2309 |
#ifdef DISP_PARTIAL |
| 2310 |
if (num_of_entries_output % |
| 2311 |
((partial_threshold > 0) |
| 2312 |
? partial_threshold |
| 2313 |
: display_lines) == 0) { |
| 2314 |
/* num_of_entries, num_of_entries_output... */ |
| 2315 |
HTDisplayPartial(); |
| 2316 |
|
| 2317 |
if (HTCheckForInterrupt()) { |
| 2318 |
_HTProgress(TRANSFER_INTERRUPTED); |
| 2319 |
status = HT_PARTIAL_CONTENT; |
| 2320 |
break; |
| 2321 |
} |
| 2322 |
} |
| 2323 |
num_of_entries_output++; |
| 2324 |
#endif /* DISP_PARTIAL */ |
| 2325 |
|
| 2326 |
} /* end while next_element */ |
| 2327 |
|
| 2328 |
if (status == HT_LOADED) { |
| 2329 |
if (state == 'I') { |
| 2330 |
START(HTML_P); |
| 2331 |
PUTS("Empty Directory"); |
| 2332 |
} |
| 2333 |
#ifndef LONG_LIST |
| 2334 |
else |
| 2335 |
END(HTML_DIR); |
| 2336 |
#endif /* !LONG_LIST */ |
| 2337 |
} |
| 2338 |
} /* end printing out the tree in order */ |
| 2339 |
if (preformatted) { |
| 2340 |
END(HTML_PRE); |
| 2341 |
PUTC('\n'); |
| 2342 |
} |
| 2343 |
END(HTML_BODY); |
| 2344 |
PUTC('\n'); |
| 2345 |
|
| 2346 |
FREE(tmpfilename); |
| 2347 |
FREE(tail); |
| 2348 |
HTBTreeAndObject_free(bt); |
| 2349 |
|
| 2350 |
if (status == HT_LOADED) { |
| 2351 |
if (HTDirReadme == HT_DIR_README_BOTTOM) |
| 2352 |
do_readme(target, localname); |
| 2353 |
FREE_TARGET; |
| 2354 |
} else { |
| 2355 |
ABORT_TARGET; |
| 2356 |
} |
| 2357 |
HTFinishDisplayPartial(); |
| 2358 |
return status; /* document loaded, maybe partial */ |
| 2359 |
} |
| 2360 |
#endif /* HAVE_READDIR */ |
| 2361 |
|
| 2362 |
#ifndef VMS |
| 2363 |
int HTStat(const char *filename, |
| 2364 |
struct stat *data) |
| 2365 |
{ |
| 2366 |
int result = -1; |
| 2367 |
size_t len = strlen(filename); |
| 2368 |
|
| 2369 |
if (len != 0 && LYIsPathSep(filename[len - 1])) { |
| 2370 |
char *temp_name = NULL; |
| 2371 |
|
| 2372 |
HTSprintf0(&temp_name, "%s.", filename); |
| 2373 |
result = HTStat(temp_name, data); |
| 2374 |
FREE(temp_name); |
| 2375 |
} else { |
| 2376 |
result = stat(filename, data); |
| 2377 |
#ifdef _WINDOWS |
| 2378 |
/* |
| 2379 |
* Someone claims that stat() doesn't give the proper result for a |
| 2380 |
* directory on Windows. |
| 2381 |
*/ |
| 2382 |
if (result == -1 |
| 2383 |
&& access(filename, 0) == 0) { |
| 2384 |
data->st_mode = S_IFDIR; |
| 2385 |
result = 0; |
| 2386 |
} |
| 2387 |
#endif |
| 2388 |
} |
| 2389 |
return result; |
| 2390 |
} |
| 2391 |
#endif |
| 2392 |
|
| 2393 |
#if defined(USE_ZLIB) || defined(USE_BZLIB) |
| 2394 |
static BOOL sniffStream(FILE *fp, char *buffer, size_t needed) |
| 2395 |
{ |
| 2396 |
long offset = ftell(fp); |
| 2397 |
BOOL result = FALSE; |
| 2398 |
|
| 2399 |
if (offset >= 0) { |
| 2400 |
if (fread(buffer, sizeof(char), needed, fp) == needed) { |
| 2401 |
result = TRUE; |
| 2402 |
} |
| 2403 |
if (fseek(fp, offset, SEEK_SET) < 0) { |
| 2404 |
CTRACE((tfp, "error seeking in stream\n")); |
| 2405 |
result = FALSE; |
| 2406 |
} |
| 2407 |
} |
| 2408 |
return result; |
| 2409 |
} |
| 2410 |
#endif |
| 2411 |
|
| 2412 |
#ifdef USE_ZLIB |
| 2413 |
static BOOL isGzipStream(FILE *fp) |
| 2414 |
{ |
| 2415 |
char buffer[3]; |
| 2416 |
BOOL result; |
| 2417 |
|
| 2418 |
if (sniffStream(fp, buffer, sizeof(buffer)) |
| 2419 |
&& !MemCmp(buffer, "\037\213", sizeof(buffer) - 1)) { |
| 2420 |
result = TRUE; |
| 2421 |
} else { |
| 2422 |
CTRACE((tfp, "not a gzip-stream\n")); |
| 2423 |
result = FALSE; |
| 2424 |
} |
| 2425 |
return result; |
| 2426 |
} |
| 2427 |
|
| 2428 |
/* |
| 2429 |
* Strictly speaking, DEFLATE has no header bytes. But decode what we can, |
| 2430 |
* (to eliminate the one "reserved" pattern) and provide a trace. See RFC-1951 |
| 2431 |
* discussion of BFINAL and BTYPE. |
| 2432 |
*/ |
| 2433 |
static BOOL isDeflateStream(FILE *fp) |
| 2434 |
{ |
| 2435 |
char buffer[3]; |
| 2436 |
BOOL result = FALSE; |
| 2437 |
|
| 2438 |
if (sniffStream(fp, buffer, sizeof(buffer))) { |
| 2439 |
int bit1 = ((buffer[0] >> 0) & 1); |
| 2440 |
int bit2 = ((buffer[0] >> 1) & 1); |
| 2441 |
int bit3 = ((buffer[0] >> 2) & 1); |
| 2442 |
int btype = ((bit3 << 1) + bit2); |
| 2443 |
|
| 2444 |
if (!MemCmp(buffer, "\170\234", sizeof(buffer) - 1)) { |
| 2445 |
result = TRUE; |
| 2446 |
CTRACE((tfp, "isDeflate: assume zlib-wrapped deflate\n")); |
| 2447 |
} else if (btype == 3) { |
| 2448 |
CTRACE((tfp, "isDeflate: not a deflate-stream\n")); |
| 2449 |
} else { |
| 2450 |
CTRACE((tfp, "isDeflate: %send block, %s compression\n", |
| 2451 |
(bit1 ? "" : "non-"), |
| 2452 |
(btype == 0 |
| 2453 |
? "no" |
| 2454 |
: (btype == 1 |
| 2455 |
? "static Huffman" |
| 2456 |
: "dynamic Huffman")))); |
| 2457 |
result = TRUE; |
| 2458 |
} |
| 2459 |
} |
| 2460 |
return result; |
| 2461 |
} |
| 2462 |
#endif |
| 2463 |
|
| 2464 |
#ifdef USE_BZLIB |
| 2465 |
static BOOL isBzip2Stream(FILE *fp) |
| 2466 |
{ |
| 2467 |
char buffer[6]; |
| 2468 |
BOOL result; |
| 2469 |
|
| 2470 |
if (sniffStream(fp, buffer, sizeof(buffer)) |
| 2471 |
&& !MemCmp(buffer, "BZh", 3) |
| 2472 |
&& isdigit(UCH(buffer[3])) |
| 2473 |
&& isdigit(UCH(buffer[4]))) { |
| 2474 |
result = TRUE; |
| 2475 |
} else { |
| 2476 |
CTRACE((tfp, "not a bzip2-stream\n")); |
| 2477 |
result = FALSE; |
| 2478 |
} |
| 2479 |
return result; |
| 2480 |
} |
| 2481 |
#endif |
| 2482 |
|
| 2483 |
#ifdef VMS |
| 2484 |
#define FOPEN_MODE(bin) "r", "shr=put", "shr=upd" |
| 2485 |
#define DOT_STRING "._-" /* FIXME: should we check if suffix is after ']' or ':' ? */ |
| 2486 |
#else |
| 2487 |
#define FOPEN_MODE(bin) (bin ? BIN_R : "r") |
| 2488 |
#define DOT_STRING "." |
| 2489 |
#endif |
| 2490 |
|
| 2491 |
#ifdef USE_BROTLI |
| 2492 |
static FILE *brotli_open(const char *localname, const char *mode) |
| 2493 |
{ |
| 2494 |
CTRACE((tfp, "brotli_open file=%s, mode=%s\n", localname, mode)); |
| 2495 |
return fopen(localname, mode); |
| 2496 |
} |
| 2497 |
#endif |
| 2498 |
|
| 2499 |
static int decompressAndParse(HTParentAnchor *anchor, |
| 2500 |
HTFormat format_out, |
| 2501 |
HTStream *sink, |
| 2502 |
char *nodename GCC_UNUSED, |
| 2503 |
char *filename, |
| 2504 |
HTAtom *myEncoding, |
| 2505 |
HTFormat format, |
| 2506 |
int *statusp) |
| 2507 |
{ |
| 2508 |
HTAtom *encoding = NULL; |
| 2509 |
|
| 2510 |
#ifdef USE_ZLIB |
| 2511 |
FILE *zzfp = NULL; |
| 2512 |
gzFile gzfp = NULL; |
| 2513 |
#endif /* USE_ZLIB */ |
| 2514 |
#ifdef USE_BZLIB |
| 2515 |
BZFILE *bzfp = NULL; |
| 2516 |
#endif /* USE_BZLIB */ |
| 2517 |
#ifdef USE_BROTLI |
| 2518 |
FILE *brfp = NULL; |
| 2519 |
#endif /* USE_BROTLI */ |
| 2520 |
#ifdef USE_ZSTD |
| 2521 |
FILE *zfp = NULL; |
| 2522 |
#endif |
| 2523 |
#if defined(USE_ZLIB) || defined(USE_BZLIB) ||\ |
| 2524 |
defined(USE_BROTLI) || defined(USE_ZSTD) |
| 2525 |
CompressFileType internal_decompress = cftNone; |
| 2526 |
BOOL failed_decompress = NO; |
| 2527 |
#endif |
| 2528 |
int rootlen = 0; |
| 2529 |
char *localname = filename; |
| 2530 |
int bin; |
| 2531 |
FILE *fp; |
| 2532 |
int result = FALSE; |
| 2533 |
|
| 2534 |
#ifdef VMS |
| 2535 |
/* |
| 2536 |
* Assume that the file is in Unix-style syntax if it contains a '/' after |
| 2537 |
* the leading one. @@ |
| 2538 |
*/ |
| 2539 |
localname = (StrChr(localname + 1, '/') |
| 2540 |
? HTVMS_name(nodename, localname) |
| 2541 |
: localname + 1); |
| 2542 |
#endif /* VMS */ |
| 2543 |
|
| 2544 |
bin = HTCompressFileType(filename, ".", &rootlen) != cftNone; |
| 2545 |
fp = fopen(localname, FOPEN_MODE(bin)); |
| 2546 |
|
| 2547 |
#ifdef VMS |
| 2548 |
/* |
| 2549 |
* If the file wasn't VMS syntax, then perhaps it is Ultrix. |
| 2550 |
*/ |
| 2551 |
if (!fp) { |
| 2552 |
char *ultrixname = 0; |
| 2553 |
|
| 2554 |
CTRACE((tfp, "HTLoadFile: Can't open as %s\n", localname)); |
| 2555 |
HTSprintf0(&ultrixname, "%s::\"%s\"", nodename, filename); |
| 2556 |
fp = fopen(ultrixname, FOPEN_MODE(bin)); |
| 2557 |
if (!fp) { |
| 2558 |
CTRACE((tfp, "HTLoadFile: Can't open as %s\n", ultrixname)); |
| 2559 |
} |
| 2560 |
FREE(ultrixname); |
| 2561 |
} |
| 2562 |
#endif /* VMS */ |
| 2563 |
CTRACE((tfp, "HTLoadFile: Opening `%s' gives %p\n", localname, (void *) fp)); |
| 2564 |
if (fp) { /* Good! */ |
| 2565 |
if (HTEditable(localname)) { |
| 2566 |
HTAtom *put = HTAtom_for("PUT"); |
| 2567 |
HTList *methods = HTAnchor_methods(anchor); |
| 2568 |
|
| 2569 |
if (HTList_indexOf(methods, put) == (-1)) { |
| 2570 |
HTList_addObject(methods, put); |
| 2571 |
} |
| 2572 |
} |
| 2573 |
/* |
| 2574 |
* Fake a Content-Encoding for compressed files. - FM |
| 2575 |
*/ |
| 2576 |
if (!IsUnityEnc(myEncoding)) { |
| 2577 |
/* |
| 2578 |
* We already know from the call to HTFileFormat that |
| 2579 |
* this is a compressed file, no need to look at the filename |
| 2580 |
* again. - kw |
| 2581 |
*/ |
| 2582 |
#if defined(USE_ZLIB) || defined(USE_BZLIB) ||\ |
| 2583 |
defined(USE_BROTLI) || defined(USE_ZSTD) |
| 2584 |
CompressFileType method = HTEncodingToCompressType(HTAtom_name(myEncoding)); |
| 2585 |
#endif |
| 2586 |
|
| 2587 |
#define isDOWNLOAD(m) (strcmp(format_out->name, STR_DOWNLOAD) && (method == m)) |
| 2588 |
#ifdef USE_ZLIB |
| 2589 |
if (isDOWNLOAD(cftGzip)) { |
| 2590 |
if (isGzipStream(fp)) { |
| 2591 |
fclose(fp); |
| 2592 |
fp = NULL; |
| 2593 |
gzfp = gzopen(localname, BIN_R); |
| 2594 |
|
| 2595 |
CTRACE((tfp, "HTLoadFile: gzopen of `%s' gives %p\n", |
| 2596 |
localname, (void *) gzfp)); |
| 2597 |
} |
| 2598 |
internal_decompress = cftGzip; |
| 2599 |
} else if (isDOWNLOAD(cftDeflate)) { |
| 2600 |
if (isDeflateStream(fp)) { |
| 2601 |
zzfp = fp; |
| 2602 |
fp = NULL; |
| 2603 |
|
| 2604 |
CTRACE((tfp, "HTLoadFile: zzopen of `%s' gives %p\n", |
| 2605 |
localname, (void *) zzfp)); |
| 2606 |
} |
| 2607 |
internal_decompress = cftDeflate; |
| 2608 |
} else |
| 2609 |
#endif /* USE_ZLIB */ |
| 2610 |
#ifdef USE_BZLIB |
| 2611 |
if (isDOWNLOAD(cftBzip2)) { |
| 2612 |
if (isBzip2Stream(fp)) { |
| 2613 |
fclose(fp); |
| 2614 |
fp = NULL; |
| 2615 |
bzfp = BZ2_bzopen(localname, BIN_R); |
| 2616 |
|
| 2617 |
CTRACE((tfp, "HTLoadFile: bzopen of `%s' gives %p\n", |
| 2618 |
localname, bzfp)); |
| 2619 |
} |
| 2620 |
internal_decompress = cftBzip2; |
| 2621 |
} else |
| 2622 |
#endif /* USE_BZLIB */ |
| 2623 |
#ifdef USE_BROTLI |
| 2624 |
if (isDOWNLOAD(cftBrotli)) { |
| 2625 |
fclose(fp); |
| 2626 |
fp = NULL; |
| 2627 |
brfp = brotli_open(localname, BIN_R); |
| 2628 |
|
| 2629 |
CTRACE((tfp, "HTLoadFile: brotli_open of `%s' gives %p\n", |
| 2630 |
localname, (void *) brfp)); |
| 2631 |
internal_decompress = cftBrotli; |
| 2632 |
} else |
| 2633 |
#endif /* USE_BROTLI */ |
| 2634 |
#ifdef USE_ZSTD |
| 2635 |
if (isDOWNLOAD(cftZstd)) { |
| 2636 |
fclose(fp); |
| 2637 |
fp = NULL; |
| 2638 |
zfp = fopen(localname, BIN_R); |
| 2639 |
CTRACE((tfp, "HTLoadFile: fopen of `%s' gives %p\n", |
| 2640 |
localname, (void *) zfp)); |
| 2641 |
internal_decompress = cftZstd; |
| 2642 |
} else |
| 2643 |
#endif /* USE_ZSTD */ |
| 2644 |
{ |
| 2645 |
StrAllocCopy(anchor->content_type, format->name); |
| 2646 |
StrAllocCopy(anchor->content_encoding, HTAtom_name(myEncoding)); |
| 2647 |
format = HTAtom_for("www/compressed"); |
| 2648 |
} |
| 2649 |
} else { |
| 2650 |
CompressFileType cft = HTCompressFileType(localname, DOT_STRING, &rootlen); |
| 2651 |
|
| 2652 |
if (cft != cftNone) { |
| 2653 |
char *cp = NULL; |
| 2654 |
|
| 2655 |
StrAllocCopy(cp, localname); |
| 2656 |
cp[rootlen] = '\0'; |
| 2657 |
format = HTFileFormat(cp, &encoding, NULL); |
| 2658 |
FREE(cp); |
| 2659 |
format = HTCharsetFormat(format, anchor, |
| 2660 |
UCLYhndl_HTFile_for_unspec); |
| 2661 |
StrAllocCopy(anchor->content_type, format->name); |
| 2662 |
} |
| 2663 |
|
| 2664 |
switch (cft) { |
| 2665 |
case cftCompress: |
| 2666 |
StrAllocCopy(anchor->content_encoding, "x-compress"); |
| 2667 |
format = HTAtom_for("www/compressed"); |
| 2668 |
break; |
| 2669 |
case cftDeflate: |
| 2670 |
StrAllocCopy(anchor->content_encoding, "x-deflate"); |
| 2671 |
#ifdef USE_ZLIB |
| 2672 |
if (strcmp(format_out->name, STR_DOWNLOAD) != 0) { |
| 2673 |
if (isDeflateStream(fp)) { |
| 2674 |
zzfp = fp; |
| 2675 |
fp = NULL; |
| 2676 |
|
| 2677 |
CTRACE((tfp, "HTLoadFile: zzopen of `%s' gives %p\n", |
| 2678 |
localname, (void *) zzfp)); |
| 2679 |
} |
| 2680 |
internal_decompress = cftDeflate; |
| 2681 |
} |
| 2682 |
#else /* USE_ZLIB */ |
| 2683 |
format = HTAtom_for("www/compressed"); |
| 2684 |
#endif /* USE_ZLIB */ |
| 2685 |
break; |
| 2686 |
case cftGzip: |
| 2687 |
StrAllocCopy(anchor->content_encoding, "x-gzip"); |
| 2688 |
#ifdef USE_ZLIB |
| 2689 |
if (strcmp(format_out->name, STR_DOWNLOAD) != 0) { |
| 2690 |
if (isGzipStream(fp)) { |
| 2691 |
fclose(fp); |
| 2692 |
fp = NULL; |
| 2693 |
gzfp = gzopen(localname, BIN_R); |
| 2694 |
|
| 2695 |
CTRACE((tfp, "HTLoadFile: gzopen of `%s' gives %p\n", |
| 2696 |
localname, (void *) gzfp)); |
| 2697 |
} |
| 2698 |
internal_decompress = cftGzip; |
| 2699 |
} |
| 2700 |
#else /* USE_ZLIB */ |
| 2701 |
format = HTAtom_for("www/compressed"); |
| 2702 |
#endif /* USE_ZLIB */ |
| 2703 |
break; |
| 2704 |
case cftBzip2: |
| 2705 |
StrAllocCopy(anchor->content_encoding, "x-bzip2"); |
| 2706 |
#ifdef USE_BZLIB |
| 2707 |
if (strcmp(format_out->name, STR_DOWNLOAD) != 0) { |
| 2708 |
if (isBzip2Stream(fp)) { |
| 2709 |
fclose(fp); |
| 2710 |
fp = NULL; |
| 2711 |
bzfp = BZ2_bzopen(localname, BIN_R); |
| 2712 |
|
| 2713 |
CTRACE((tfp, "HTLoadFile: bzopen of `%s' gives %p\n", |
| 2714 |
localname, bzfp)); |
| 2715 |
} |
| 2716 |
internal_decompress = cftBzip2; |
| 2717 |
} |
| 2718 |
#else /* USE_BZLIB */ |
| 2719 |
format = HTAtom_for("www/compressed"); |
| 2720 |
#endif /* USE_BZLIB */ |
| 2721 |
break; |
| 2722 |
case cftBrotli: |
| 2723 |
StrAllocCopy(anchor->content_encoding, "x-brotli"); |
| 2724 |
#ifdef USE_BROTLI |
| 2725 |
if (strcmp(format_out->name, STR_DOWNLOAD) != 0) { |
| 2726 |
fclose(fp); |
| 2727 |
fp = NULL; |
| 2728 |
brfp = brotli_open(localname, BIN_R); |
| 2729 |
|
| 2730 |
CTRACE((tfp, "HTLoadFile: brotli_open of `%s' gives %p\n", |
| 2731 |
localname, (void *) brfp)); |
| 2732 |
internal_decompress = cftBrotli; |
| 2733 |
} |
| 2734 |
#else /* USE_BROTLI */ |
| 2735 |
format = HTAtom_for("www/compressed"); |
| 2736 |
#endif /* USE_BROTLI */ |
| 2737 |
break; |
| 2738 |
case cftZstd: |
| 2739 |
StrAllocCopy(anchor->content_encoding, "zstd"); |
| 2740 |
#ifdef USE_ZSTD |
| 2741 |
if (strcmp(format_out->name, STR_DOWNLOAD) != 0) { |
| 2742 |
fclose(fp); |
| 2743 |
fp = NULL; |
| 2744 |
zfp = fopen(localname, BIN_R); |
| 2745 |
CTRACE((tfp, "HTLoadFile: fopen of `%s' gives %p\n", |
| 2746 |
localname, (void *) zfp)); |
| 2747 |
internal_decompress = cftZstd; |
| 2748 |
} |
| 2749 |
#else /* USE_ZSTD */ |
| 2750 |
format = HTAtom_for("www/compressed"); |
| 2751 |
#endif /* USE_ZSTD */ |
| 2752 |
break; |
| 2753 |
case cftNone: |
| 2754 |
break; |
| 2755 |
} |
| 2756 |
} |
| 2757 |
#if defined(USE_ZLIB) || defined(USE_BZLIB) |
| 2758 |
if (internal_decompress != cftNone) { |
| 2759 |
switch (internal_decompress) { |
| 2760 |
#ifdef USE_ZLIB |
| 2761 |
case cftDeflate: |
| 2762 |
failed_decompress = (BOOLEAN) (zzfp == NULL); |
| 2763 |
break; |
| 2764 |
case cftCompress: |
| 2765 |
case cftGzip: |
| 2766 |
failed_decompress = (BOOLEAN) (gzfp == NULL); |
| 2767 |
break; |
| 2768 |
#endif |
| 2769 |
#ifdef USE_BZLIB |
| 2770 |
case cftBzip2: |
| 2771 |
failed_decompress = (BOOLEAN) (bzfp == NULL); |
| 2772 |
break; |
| 2773 |
#endif |
| 2774 |
#ifdef USE_BROTLI |
| 2775 |
case cftBrotli: |
| 2776 |
failed_decompress = (BOOLEAN) (brfp == NULL); |
| 2777 |
break; |
| 2778 |
#endif |
| 2779 |
#ifdef USE_ZSTD |
| 2780 |
case cftZstd: |
| 2781 |
failed_decompress = (BOOLEAN) (zfp == NULL); |
| 2782 |
break; |
| 2783 |
#endif |
| 2784 |
default: |
| 2785 |
failed_decompress = YES; |
| 2786 |
break; |
| 2787 |
} |
| 2788 |
if (failed_decompress) { |
| 2789 |
*statusp = HTLoadError(NULL, |
| 2790 |
-(HT_ERROR), |
| 2791 |
FAILED_OPEN_COMPRESSED_FILE); |
| 2792 |
} else { |
| 2793 |
char *sugfname = NULL; |
| 2794 |
|
| 2795 |
if (anchor->SugFname) { |
| 2796 |
StrAllocCopy(sugfname, anchor->SugFname); |
| 2797 |
} else { |
| 2798 |
char *anchor_path = HTParse(anchor->address, "", |
| 2799 |
PARSE_PATH + PARSE_PUNCTUATION); |
| 2800 |
char *lastslash; |
| 2801 |
|
| 2802 |
HTUnEscape(anchor_path); |
| 2803 |
lastslash = strrchr(anchor_path, '/'); |
| 2804 |
if (lastslash) |
| 2805 |
StrAllocCopy(sugfname, lastslash + 1); |
| 2806 |
FREE(anchor_path); |
| 2807 |
} |
| 2808 |
FREE(anchor->content_encoding); |
| 2809 |
if (sugfname && *sugfname) |
| 2810 |
HTCheckFnameForCompression(&sugfname, anchor, |
| 2811 |
TRUE); |
| 2812 |
if (sugfname && *sugfname) |
| 2813 |
StrAllocCopy(anchor->SugFname, sugfname); |
| 2814 |
FREE(sugfname); |
| 2815 |
#ifdef USE_ZSTD |
| 2816 |
if (zfp) |
| 2817 |
*statusp = HTParseZstdFile(format, format_out, |
| 2818 |
anchor, |
| 2819 |
zfp, sink); |
| 2820 |
#endif |
| 2821 |
#ifdef USE_BROTLI |
| 2822 |
if (brfp) |
| 2823 |
*statusp = HTParseBrFile(format, format_out, |
| 2824 |
anchor, |
| 2825 |
brfp, sink); |
| 2826 |
#endif |
| 2827 |
#ifdef USE_BZLIB |
| 2828 |
if (bzfp) |
| 2829 |
*statusp = HTParseBzFile(format, format_out, |
| 2830 |
anchor, |
| 2831 |
bzfp, sink); |
| 2832 |
#endif |
| 2833 |
#ifdef USE_ZLIB |
| 2834 |
if (gzfp) |
| 2835 |
*statusp = HTParseGzFile(format, format_out, |
| 2836 |
anchor, |
| 2837 |
gzfp, sink); |
| 2838 |
else if (zzfp) |
| 2839 |
*statusp = HTParseZzFile(format, format_out, |
| 2840 |
anchor, |
| 2841 |
zzfp, sink); |
| 2842 |
#endif |
| 2843 |
} |
| 2844 |
} else |
| 2845 |
#endif /* USE_ZLIB || USE_BZLIB */ |
| 2846 |
{ |
| 2847 |
*statusp = HTParseFile(format, format_out, anchor, fp, sink); |
| 2848 |
} |
| 2849 |
if (fp != NULL) { |
| 2850 |
fclose(fp); |
| 2851 |
fp = NULL; |
| 2852 |
} |
| 2853 |
result = TRUE; |
| 2854 |
} /* If successful open */ |
| 2855 |
return result; |
| 2856 |
} |
| 2857 |
|
| 2858 |
/* Load a document. |
| 2859 |
* ---------------- |
| 2860 |
* |
| 2861 |
* On entry: |
| 2862 |
* addr must point to the fully qualified hypertext reference. |
| 2863 |
* This is the physical address of the file |
| 2864 |
* |
| 2865 |
* On exit: |
| 2866 |
* returns <0 Error has occurred. |
| 2867 |
* HTLOADED OK |
| 2868 |
* |
| 2869 |
*/ |
| 2870 |
int HTLoadFile(const char *addr, |
| 2871 |
HTParentAnchor *anchor, |
| 2872 |
HTFormat format_out, |
| 2873 |
HTStream *sink) |
| 2874 |
{ |
| 2875 |
char *filename = NULL; |
| 2876 |
char *acc_method = NULL; |
| 2877 |
HTFormat format; |
| 2878 |
char *nodename = NULL; |
| 2879 |
char *newname = NULL; /* Simplified name of file */ |
| 2880 |
HTAtom *myEncoding = NULL; /* enc of this file, may be gzip etc. */ |
| 2881 |
int status = -1; |
| 2882 |
|
| 2883 |
#ifndef DISABLE_FTP |
| 2884 |
char *ftp_newhost; |
| 2885 |
#endif |
| 2886 |
|
| 2887 |
#ifdef VMS |
| 2888 |
struct stat stat_info; |
| 2889 |
#endif /* VMS */ |
| 2890 |
|
| 2891 |
/* |
| 2892 |
* Reduce the filename to a basic form (hopefully unique!). |
| 2893 |
*/ |
| 2894 |
StrAllocCopy(newname, addr); |
| 2895 |
filename = HTParse(newname, "", PARSE_PATH | PARSE_PUNCTUATION); |
| 2896 |
nodename = HTParse(newname, "", PARSE_HOST); |
| 2897 |
|
| 2898 |
/* |
| 2899 |
* If access is ftp, or file is on another host, invoke ftp now. |
| 2900 |
*/ |
| 2901 |
acc_method = HTParse(newname, "", PARSE_ACCESS); |
| 2902 |
if (strcmp("ftp", acc_method) == 0 || |
| 2903 |
(!LYSameHostname("localhost", nodename) && |
| 2904 |
!LYSameHostname(nodename, HTHostName()))) { |
| 2905 |
status = -1; |
| 2906 |
FREE(newname); |
| 2907 |
FREE(filename); |
| 2908 |
FREE(nodename); |
| 2909 |
FREE(acc_method); |
| 2910 |
#ifndef DISABLE_FTP |
| 2911 |
ftp_newhost = HTParse(addr, "", PARSE_HOST); |
| 2912 |
if (strcmp(ftp_lasthost, ftp_newhost)) |
| 2913 |
ftp_local_passive = ftp_passive; |
| 2914 |
|
| 2915 |
status = HTFTPLoad(addr, anchor, format_out, sink); |
| 2916 |
|
| 2917 |
if (ftp_passive == ftp_local_passive) { |
| 2918 |
if ((status >= 400) || (status < 0)) { |
| 2919 |
ftp_local_passive = (BOOLEAN) !ftp_passive; |
| 2920 |
status = HTFTPLoad(addr, anchor, format_out, sink); |
| 2921 |
} |
| 2922 |
} |
| 2923 |
|
| 2924 |
free(ftp_lasthost); |
| 2925 |
ftp_lasthost = ftp_newhost; |
| 2926 |
#endif /* DISABLE_FTP */ |
| 2927 |
return status; |
| 2928 |
} else { |
| 2929 |
FREE(newname); |
| 2930 |
FREE(acc_method); |
| 2931 |
} |
| 2932 |
#if defined(VMS) || defined(USE_DOS_DRIVES) |
| 2933 |
HTUnEscape(filename); |
| 2934 |
#endif /* VMS */ |
| 2935 |
|
| 2936 |
/* |
| 2937 |
* Determine the format and encoding mapped to any suffix. |
| 2938 |
*/ |
| 2939 |
if (anchor->content_type && anchor->content_encoding) { |
| 2940 |
/* |
| 2941 |
* If content_type and content_encoding are BOTH already set in the |
| 2942 |
* anchor object, we believe it and don't try to derive format and |
| 2943 |
* encoding from the filename. - kw |
| 2944 |
*/ |
| 2945 |
format = HTAtom_for(anchor->content_type); |
| 2946 |
myEncoding = HTAtom_for(anchor->content_encoding); |
| 2947 |
} else { |
| 2948 |
int default_UCLYhndl = UCLYhndl_HTFile_for_unspec; |
| 2949 |
|
| 2950 |
if (force_old_UCLYhndl_on_reload) { |
| 2951 |
force_old_UCLYhndl_on_reload = FALSE; |
| 2952 |
default_UCLYhndl = forced_UCLYhdnl; |
| 2953 |
} |
| 2954 |
|
| 2955 |
format = HTFileFormat(filename, &myEncoding, NULL); |
| 2956 |
|
| 2957 |
/* |
| 2958 |
* Check the format for an extended MIME charset value, and act on it |
| 2959 |
* if present. Otherwise, assume what is indicated by the last |
| 2960 |
* parameter (fallback will effectively be UCLYhndl_for_unspec, by |
| 2961 |
* default ISO-8859-1). - kw |
| 2962 |
*/ |
| 2963 |
format = HTCharsetFormat(format, anchor, default_UCLYhndl); |
| 2964 |
} |
| 2965 |
|
| 2966 |
#ifdef VMS |
| 2967 |
/* |
| 2968 |
* Check to see if the 'filename' is in fact a directory. If it is create |
| 2969 |
* a new hypertext object containing a list of files and subdirectories |
| 2970 |
* contained in the directory. All of these are links to the directories |
| 2971 |
* or files listed. |
| 2972 |
*/ |
| 2973 |
if (HTStat(filename, &stat_info) == -1) { |
| 2974 |
CTRACE((tfp, "HTLoadFile: Can't stat %s\n", filename)); |
| 2975 |
} else { |
| 2976 |
if (S_ISDIR(stat_info.st_mode)) { |
| 2977 |
if (HTDirAccess == HT_DIR_FORBID) { |
| 2978 |
FREE(filename); |
| 2979 |
FREE(nodename); |
| 2980 |
return HTLoadError(sink, 403, DISALLOWED_DIR_SCAN); |
| 2981 |
} |
| 2982 |
|
| 2983 |
if (HTDirAccess == HT_DIR_SELECTIVE) { |
| 2984 |
char *enable_file_name = NULL; |
| 2985 |
|
| 2986 |
HTSprintf0(&enable_file_name, "%s/%s", filename, HT_DIR_ENABLE_FILE); |
| 2987 |
if (HTStat(enable_file_name, &stat_info) == -1) { |
| 2988 |
FREE(filename); |
| 2989 |
FREE(nodename); |
| 2990 |
FREE(enable_file_name); |
| 2991 |
return HTLoadError(sink, 403, DISALLOWED_SELECTIVE_ACCESS); |
| 2992 |
} |
| 2993 |
} |
| 2994 |
|
| 2995 |
FREE(filename); |
| 2996 |
FREE(nodename); |
| 2997 |
return HTVMSBrowseDir(addr, anchor, format_out, sink); |
| 2998 |
} |
| 2999 |
} |
| 3000 |
|
| 3001 |
if (decompressAndParse(anchor, |
| 3002 |
format_out, |
| 3003 |
sink, |
| 3004 |
nodename, |
| 3005 |
filename, |
| 3006 |
myEncoding, |
| 3007 |
format, |
| 3008 |
&status)) { |
| 3009 |
FREE(nodename); |
| 3010 |
FREE(filename); |
| 3011 |
return status; |
| 3012 |
} |
| 3013 |
FREE(filename); |
| 3014 |
|
| 3015 |
#else /* not VMS: */ |
| 3016 |
|
| 3017 |
FREE(filename); |
| 3018 |
|
| 3019 |
/* |
| 3020 |
* For unix, we try to translate the name into the name of a transparently |
| 3021 |
* mounted file. |
| 3022 |
* |
| 3023 |
* Not allowed in secure (HTClientHost) situations. TBL 921019 |
| 3024 |
*/ |
| 3025 |
#ifndef NO_UNIX_IO |
| 3026 |
/* Need protection here for telnet server but not httpd server. */ |
| 3027 |
|
| 3028 |
if (!HTSecure) { /* try local file system */ |
| 3029 |
char *localname = HTLocalName(addr); |
| 3030 |
struct stat dir_info; |
| 3031 |
|
| 3032 |
#ifdef HAVE_READDIR |
| 3033 |
/* |
| 3034 |
* Multiformat handling. |
| 3035 |
* |
| 3036 |
* If needed, scan directory to find a good file. Bug: We don't stat |
| 3037 |
* the file to find the length. |
| 3038 |
*/ |
| 3039 |
if ((strlen(localname) > strlen(MULTI_SUFFIX)) && |
| 3040 |
(0 == strcmp(localname + strlen(localname) - strlen(MULTI_SUFFIX), |
| 3041 |
MULTI_SUFFIX))) { |
| 3042 |
DIR *dp = NULL; |
| 3043 |
BOOL forget_multi = NO; |
| 3044 |
|
| 3045 |
STRUCT_DIRENT *dirbuf; |
| 3046 |
float best = (float) NO_VALUE_FOUND; /* So far best is bad */ |
| 3047 |
HTFormat best_rep = NULL; /* Set when rep found */ |
| 3048 |
HTAtom *best_enc = NULL; |
| 3049 |
char *best_name = NULL; /* Best dir entry so far */ |
| 3050 |
|
| 3051 |
char *base = strrchr(localname, '/'); |
| 3052 |
size_t baselen = 0; |
| 3053 |
|
| 3054 |
if (!base || base == localname) { |
| 3055 |
forget_multi = YES; |
| 3056 |
} else { |
| 3057 |
*base++ = '\0'; /* Just got directory name */ |
| 3058 |
baselen = strlen(base) - strlen(MULTI_SUFFIX); |
| 3059 |
base[baselen] = '\0'; /* Chop off suffix */ |
| 3060 |
|
| 3061 |
dp = opendir(localname); |
| 3062 |
} |
| 3063 |
if (forget_multi || !dp) { |
| 3064 |
FREE(localname); |
| 3065 |
FREE(nodename); |
| 3066 |
return HTLoadError(sink, 500, FAILED_DIR_SCAN); |
| 3067 |
} |
| 3068 |
|
| 3069 |
while ((dirbuf = readdir(dp)) != NULL) { |
| 3070 |
/* |
| 3071 |
* While there are directory entries to be read... |
| 3072 |
*/ |
| 3073 |
#ifdef STRUCT_DIRENT__D_INO |
| 3074 |
if (dirbuf->d_ino == 0) |
| 3075 |
continue; /* if the entry is not being used, skip it */ |
| 3076 |
#endif |
| 3077 |
if (strlen(dirbuf->d_name) > baselen && /* Match? */ |
| 3078 |
!StrNCmp(dirbuf->d_name, base, baselen)) { |
| 3079 |
HTAtom *enc; |
| 3080 |
HTFormat rep = HTFileFormat(dirbuf->d_name, &enc, NULL); |
| 3081 |
float filevalue = HTFileValue(dirbuf->d_name); |
| 3082 |
float value = HTStackValue(rep, format_out, |
| 3083 |
filevalue, |
| 3084 |
0L /* @@@@@@ */ ); |
| 3085 |
|
| 3086 |
if (value <= 0.0) { |
| 3087 |
int rootlen = 0; |
| 3088 |
const char *atomname = NULL; |
| 3089 |
CompressFileType cft = |
| 3090 |
HTCompressFileType(dirbuf->d_name, ".", &rootlen); |
| 3091 |
char *cp = NULL; |
| 3092 |
|
| 3093 |
enc = NULL; |
| 3094 |
if (cft != cftNone) { |
| 3095 |
StrAllocCopy(cp, dirbuf->d_name); |
| 3096 |
cp[rootlen] = '\0'; |
| 3097 |
format = HTFileFormat(cp, NULL, NULL); |
| 3098 |
FREE(cp); |
| 3099 |
value = HTStackValue(format, format_out, |
| 3100 |
filevalue, 0L); |
| 3101 |
} |
| 3102 |
switch (cft) { |
| 3103 |
case cftCompress: |
| 3104 |
atomname = "application/x-compressed"; |
| 3105 |
break; |
| 3106 |
case cftGzip: |
| 3107 |
atomname = "application/x-gzip"; |
| 3108 |
break; |
| 3109 |
case cftDeflate: |
| 3110 |
atomname = "application/x-deflate"; |
| 3111 |
break; |
| 3112 |
case cftBzip2: |
| 3113 |
atomname = "application/x-bzip2"; |
| 3114 |
break; |
| 3115 |
case cftBrotli: |
| 3116 |
atomname = "application/x-brotli"; |
| 3117 |
break; |
| 3118 |
case cftZstd: |
| 3119 |
atomname = "application/zstd"; |
| 3120 |
break; |
| 3121 |
case cftNone: |
| 3122 |
break; |
| 3123 |
} |
| 3124 |
|
| 3125 |
if (atomname != NULL) { |
| 3126 |
value = HTStackValue(format, format_out, |
| 3127 |
filevalue, 0L); |
| 3128 |
if (value <= 0.0) { |
| 3129 |
format = HTAtom_for(atomname); |
| 3130 |
value = HTStackValue(format, format_out, |
| 3131 |
filevalue, 0L); |
| 3132 |
} |
| 3133 |
if (value <= 0.0) { |
| 3134 |
format = HTAtom_for("www/compressed"); |
| 3135 |
value = HTStackValue(format, format_out, |
| 3136 |
filevalue, 0L); |
| 3137 |
} |
| 3138 |
} |
| 3139 |
} |
| 3140 |
if (value < NO_VALUE_FOUND) { |
| 3141 |
CTRACE((tfp, |
| 3142 |
"HTLoadFile: value of presenting %s is %f\n", |
| 3143 |
HTAtom_name(rep), value)); |
| 3144 |
if (value > best) { |
| 3145 |
best_rep = rep; |
| 3146 |
best_enc = enc; |
| 3147 |
best = value; |
| 3148 |
StrAllocCopy(best_name, dirbuf->d_name); |
| 3149 |
} |
| 3150 |
} /* if best so far */ |
| 3151 |
} |
| 3152 |
/* if match */ |
| 3153 |
} /* end while directory entries left to read */ |
| 3154 |
closedir(dp); |
| 3155 |
|
| 3156 |
if (best_rep) { |
| 3157 |
format = best_rep; |
| 3158 |
myEncoding = best_enc; |
| 3159 |
base[-1] = '/'; /* Restore directory name */ |
| 3160 |
base[0] = '\0'; |
| 3161 |
StrAllocCat(localname, best_name); |
| 3162 |
FREE(best_name); |
| 3163 |
} else { /* If not found suitable file */ |
| 3164 |
FREE(localname); |
| 3165 |
FREE(nodename); |
| 3166 |
return HTLoadError(sink, 403, FAILED_NO_REPRESENTATION); |
| 3167 |
} |
| 3168 |
/*NOTREACHED */ |
| 3169 |
} |
| 3170 |
/* if multi suffix */ |
| 3171 |
/* |
| 3172 |
* Check to see if the 'localname' is in fact a directory. If it is |
| 3173 |
* create a new hypertext object containing a list of files and |
| 3174 |
* subdirectories contained in the directory. All of these are links |
| 3175 |
* to the directories or files listed. NB This assumes the existence |
| 3176 |
* of a type 'STRUCT_DIRENT', which will hold the directory entry, and |
| 3177 |
* a type 'DIR' which is used to point to the current directory being |
| 3178 |
* read. |
| 3179 |
*/ |
| 3180 |
#if defined(USE_DOS_DRIVES) |
| 3181 |
if (strlen(localname) == 2 && LYIsDosDrive(localname)) |
| 3182 |
LYAddPathSep(&localname); |
| 3183 |
#endif |
| 3184 |
if (HTStat(localname, &dir_info) == -1) /* get file information */ |
| 3185 |
{ |
| 3186 |
/* if can't read file information */ |
| 3187 |
CTRACE((tfp, "HTLoadFile: can't stat %s\n", localname)); |
| 3188 |
|
| 3189 |
} else { /* Stat was OK */ |
| 3190 |
|
| 3191 |
if (S_ISDIR(dir_info.st_mode)) { |
| 3192 |
/* |
| 3193 |
* If localname is a directory. |
| 3194 |
*/ |
| 3195 |
DIR *dp; |
| 3196 |
struct stat file_info; |
| 3197 |
|
| 3198 |
CTRACE((tfp, "%s is a directory\n", localname)); |
| 3199 |
|
| 3200 |
/* |
| 3201 |
* Check directory access. Selective access means only those |
| 3202 |
* directories containing a marker file can be browsed. |
| 3203 |
*/ |
| 3204 |
if (HTDirAccess == HT_DIR_FORBID) { |
| 3205 |
FREE(localname); |
| 3206 |
FREE(nodename); |
| 3207 |
return HTLoadError(sink, 403, DISALLOWED_DIR_SCAN); |
| 3208 |
} |
| 3209 |
|
| 3210 |
if (HTDirAccess == HT_DIR_SELECTIVE) { |
| 3211 |
char *enable_file_name = NULL; |
| 3212 |
|
| 3213 |
HTSprintf0(&enable_file_name, "%s/%s", localname, HT_DIR_ENABLE_FILE); |
| 3214 |
if (stat(enable_file_name, &file_info) != 0) { |
| 3215 |
FREE(localname); |
| 3216 |
FREE(nodename); |
| 3217 |
FREE(enable_file_name); |
| 3218 |
return HTLoadError(sink, 403, DISALLOWED_SELECTIVE_ACCESS); |
| 3219 |
} |
| 3220 |
} |
| 3221 |
|
| 3222 |
CTRACE((tfp, "Opening directory %s\n", localname)); |
| 3223 |
dp = opendir(localname); |
| 3224 |
if (!dp) { |
| 3225 |
FREE(localname); |
| 3226 |
FREE(nodename); |
| 3227 |
return HTLoadError(sink, 403, FAILED_DIR_UNREADABLE); |
| 3228 |
} |
| 3229 |
|
| 3230 |
/* |
| 3231 |
* Directory access is allowed and possible. |
| 3232 |
*/ |
| 3233 |
|
| 3234 |
status = print_local_dir(dp, localname, |
| 3235 |
anchor, format_out, sink); |
| 3236 |
closedir(dp); |
| 3237 |
FREE(localname); |
| 3238 |
FREE(nodename); |
| 3239 |
return status; /* document loaded, maybe partial */ |
| 3240 |
|
| 3241 |
} |
| 3242 |
/* end if localname is a directory */ |
| 3243 |
if (S_ISREG(dir_info.st_mode)) { |
| 3244 |
#ifdef LONG_MAX |
| 3245 |
if (dir_info.st_size <= LONG_MAX) |
| 3246 |
#endif |
| 3247 |
anchor->content_length = (long) dir_info.st_size; |
| 3248 |
} |
| 3249 |
|
| 3250 |
} /* end if file stat worked */ |
| 3251 |
|
| 3252 |
/* End of directory reading section |
| 3253 |
*/ |
| 3254 |
#endif /* HAVE_READDIR */ |
| 3255 |
if (decompressAndParse(anchor, |
| 3256 |
format_out, |
| 3257 |
sink, |
| 3258 |
nodename, |
| 3259 |
localname, |
| 3260 |
myEncoding, |
| 3261 |
format, |
| 3262 |
&status)) { |
| 3263 |
FREE(nodename); |
| 3264 |
FREE(localname); |
| 3265 |
return status; |
| 3266 |
} |
| 3267 |
FREE(localname); |
| 3268 |
} /* local unix file system */ |
| 3269 |
#endif /* !NO_UNIX_IO */ |
| 3270 |
#endif /* VMS */ |
| 3271 |
|
| 3272 |
#ifndef DECNET |
| 3273 |
/* |
| 3274 |
* Now, as transparently mounted access has failed, we try FTP. |
| 3275 |
*/ |
| 3276 |
{ |
| 3277 |
/* |
| 3278 |
* Deal with case-sensitivity differences on VMS versus Unix. |
| 3279 |
*/ |
| 3280 |
#ifdef VMS |
| 3281 |
if (strcasecomp(nodename, HTHostName()) != 0) |
| 3282 |
#else |
| 3283 |
if (strcmp(nodename, HTHostName()) != 0) |
| 3284 |
#endif /* VMS */ |
| 3285 |
{ |
| 3286 |
status = -1; |
| 3287 |
FREE(nodename); |
| 3288 |
if (StrNCmp(addr, "file://localhost", 16)) { |
| 3289 |
/* never go to ftp site when URL |
| 3290 |
* is file://localhost |
| 3291 |
*/ |
| 3292 |
#ifndef DISABLE_FTP |
| 3293 |
status = HTFTPLoad(addr, anchor, format_out, sink); |
| 3294 |
#endif /* DISABLE_FTP */ |
| 3295 |
} |
| 3296 |
return status; |
| 3297 |
} |
| 3298 |
FREE(nodename); |
| 3299 |
} |
| 3300 |
#endif /* !DECNET */ |
| 3301 |
|
| 3302 |
/* |
| 3303 |
* All attempts have failed. |
| 3304 |
*/ |
| 3305 |
{ |
| 3306 |
CTRACE((tfp, "Can't open `%s', errno=%d\n", addr, SOCKET_ERRNO)); |
| 3307 |
|
| 3308 |
return HTLoadError(sink, 403, FAILED_FILE_UNREADABLE); |
| 3309 |
} |
| 3310 |
} |
| 3311 |
|
| 3312 |
static const char *program_paths[pp_Last]; |
| 3313 |
|
| 3314 |
#if defined(UNIX) || defined(_WINDOWS) |
| 3315 |
#ifndef X_OK |
| 3316 |
#define X_OK 0 |
| 3317 |
#endif |
| 3318 |
static int VerifyProgram(const char *path) |
| 3319 |
{ |
| 3320 |
int result = 0; |
| 3321 |
struct stat info; |
| 3322 |
|
| 3323 |
if (stat(path, &info) == 0) { |
| 3324 |
if (S_ISREG(info.st_mode) && |
| 3325 |
info.st_size != 0 && |
| 3326 |
access(path, X_OK) == 0) { |
| 3327 |
result = 1; |
| 3328 |
} else { |
| 3329 |
result = -1; |
| 3330 |
} |
| 3331 |
} |
| 3332 |
return result; |
| 3333 |
} |
| 3334 |
#endif |
| 3335 |
|
| 3336 |
/* |
| 3337 |
* Given a program number, return its path |
| 3338 |
*/ |
| 3339 |
const char *HTGetProgramPath(ProgramPaths code) |
| 3340 |
{ |
| 3341 |
const char *result = NULL; |
| 3342 |
|
| 3343 |
if (code > ppUnknown && code < pp_Last) { |
| 3344 |
result = program_paths[code]; |
| 3345 |
#if defined(UNIX) || defined(_WINDOWS) |
| 3346 |
#define VERIFY_FMT "not a program: %s\n" |
| 3347 |
#define EXISTS_FMT "no file found: %s\n" |
| 3348 |
if (result != NULL) { |
| 3349 |
char *path; |
| 3350 |
|
| 3351 |
switch (VerifyProgram(result)) { |
| 3352 |
case -1: |
| 3353 |
CTRACE((tfp, VERIFY_FMT, result)); |
| 3354 |
result = NULL; |
| 3355 |
break; |
| 3356 |
case 0: |
| 3357 |
if (*result == '/') { |
| 3358 |
CTRACE((tfp, EXISTS_FMT, result)); |
| 3359 |
result = NULL; |
| 3360 |
} else if ((path = getenv("PATH")) != NULL) { |
| 3361 |
int check = 0; |
| 3362 |
char *list = NULL; |
| 3363 |
|
| 3364 |
StrAllocCopy(list, path); |
| 3365 |
if (list != NULL) { |
| 3366 |
char *item; |
| 3367 |
|
| 3368 |
path = list; |
| 3369 |
while ((item = strtok(path, PATH_SEPARATOR)) != NULL) { |
| 3370 |
path = NULL; |
| 3371 |
if (*item != '\0') { |
| 3372 |
char *full = NULL; |
| 3373 |
|
| 3374 |
HTSprintf0(&full, "%s%s%s", |
| 3375 |
item, FILE_SEPARATOR, result); |
| 3376 |
check = VerifyProgram(full); |
| 3377 |
free(full); |
| 3378 |
if (check != 0) |
| 3379 |
break; |
| 3380 |
} |
| 3381 |
} |
| 3382 |
free(list); |
| 3383 |
} |
| 3384 |
if (check <= 0) { |
| 3385 |
CTRACE((tfp, check ? VERIFY_FMT : EXISTS_FMT, result)); |
| 3386 |
result = NULL; |
| 3387 |
} |
| 3388 |
} |
| 3389 |
break; |
| 3390 |
default: |
| 3391 |
break; |
| 3392 |
} |
| 3393 |
} |
| 3394 |
#endif |
| 3395 |
} |
| 3396 |
return result; |
| 3397 |
} |
| 3398 |
|
| 3399 |
/* |
| 3400 |
* Store a program's path. The caller must allocate the string used for 'path', |
| 3401 |
* since HTInitProgramPaths() may free it. |
| 3402 |
*/ |
| 3403 |
void HTSetProgramPath(ProgramPaths code, const char *path) |
| 3404 |
{ |
| 3405 |
if (code > ppUnknown && code < pp_Last) { |
| 3406 |
program_paths[code] = isEmpty(path) ? NULL : path; |
| 3407 |
} |
| 3408 |
} |
| 3409 |
|
| 3410 |
/* |
| 3411 |
* Reset the list of known program paths to the ones that are compiled-in |
| 3412 |
*/ |
| 3413 |
void HTInitProgramPaths(BOOL init) |
| 3414 |
{ |
| 3415 |
ProgramPaths code; |
| 3416 |
int n; |
| 3417 |
const char *path; |
| 3418 |
const char *test; |
| 3419 |
|
| 3420 |
for (n = (int) ppUnknown + 1; n < (int) pp_Last; ++n) { |
| 3421 |
switch (code = (ProgramPaths) n) { |
| 3422 |
#ifdef ZSTD_PATH |
| 3423 |
case ppZSTD: |
| 3424 |
path = ZSTD_PATH; |
| 3425 |
break; |
| 3426 |
#endif |
| 3427 |
#ifdef BROTLI_PATH |
| 3428 |
case ppBROTLI: |
| 3429 |
path = BROTLI_PATH; |
| 3430 |
break; |
| 3431 |
#endif |
| 3432 |
#ifdef BZIP2_PATH |
| 3433 |
case ppBZIP2: |
| 3434 |
path = BZIP2_PATH; |
| 3435 |
break; |
| 3436 |
#endif |
| 3437 |
#ifdef CHMOD_PATH |
| 3438 |
case ppCHMOD: |
| 3439 |
path = CHMOD_PATH; |
| 3440 |
break; |
| 3441 |
#endif |
| 3442 |
#ifdef COMPRESS_PATH |
| 3443 |
case ppCOMPRESS: |
| 3444 |
path = COMPRESS_PATH; |
| 3445 |
break; |
| 3446 |
#endif |
| 3447 |
#ifdef COPY_PATH |
| 3448 |
case ppCOPY: |
| 3449 |
path = COPY_PATH; |
| 3450 |
break; |
| 3451 |
#endif |
| 3452 |
#ifdef CSWING_PATH |
| 3453 |
case ppCSWING: |
| 3454 |
path = CSWING_PATH; |
| 3455 |
break; |
| 3456 |
#endif |
| 3457 |
#ifdef GZIP_PATH |
| 3458 |
case ppGZIP: |
| 3459 |
path = GZIP_PATH; |
| 3460 |
break; |
| 3461 |
#endif |
| 3462 |
#ifdef INFLATE_PATH |
| 3463 |
case ppINFLATE: |
| 3464 |
path = INFLATE_PATH; |
| 3465 |
break; |
| 3466 |
#endif |
| 3467 |
#ifdef INSTALL_PATH |
| 3468 |
case ppINSTALL: |
| 3469 |
path = INSTALL_PATH; |
| 3470 |
break; |
| 3471 |
#endif |
| 3472 |
#ifdef MKDIR_PATH |
| 3473 |
case ppMKDIR: |
| 3474 |
path = MKDIR_PATH; |
| 3475 |
break; |
| 3476 |
#endif |
| 3477 |
#ifdef MV_PATH |
| 3478 |
case ppMV: |
| 3479 |
path = MV_PATH; |
| 3480 |
break; |
| 3481 |
#endif |
| 3482 |
#ifdef RLOGIN_PATH |
| 3483 |
case ppRLOGIN: |
| 3484 |
path = RLOGIN_PATH; |
| 3485 |
break; |
| 3486 |
#endif |
| 3487 |
#ifdef RM_PATH |
| 3488 |
case ppRM: |
| 3489 |
path = RM_PATH; |
| 3490 |
break; |
| 3491 |
#endif |
| 3492 |
#ifdef RMDIR_PATH |
| 3493 |
case ppRMDIR: |
| 3494 |
path = RMDIR_PATH; |
| 3495 |
break; |
| 3496 |
#endif |
| 3497 |
#ifdef SETFONT_PATH |
| 3498 |
case ppSETFONT: |
| 3499 |
path = SETFONT_PATH; |
| 3500 |
break; |
| 3501 |
#endif |
| 3502 |
#ifdef TAR_PATH |
| 3503 |
case ppTAR: |
| 3504 |
path = TAR_PATH; |
| 3505 |
break; |
| 3506 |
#endif |
| 3507 |
#ifdef TELNET_PATH |
| 3508 |
case ppTELNET: |
| 3509 |
path = TELNET_PATH; |
| 3510 |
break; |
| 3511 |
#endif |
| 3512 |
#ifdef TN3270_PATH |
| 3513 |
case ppTN3270: |
| 3514 |
path = TN3270_PATH; |
| 3515 |
break; |
| 3516 |
#endif |
| 3517 |
#ifdef TOUCH_PATH |
| 3518 |
case ppTOUCH: |
| 3519 |
path = TOUCH_PATH; |
| 3520 |
break; |
| 3521 |
#endif |
| 3522 |
#ifdef UNCOMPRESS_PATH |
| 3523 |
case ppUNCOMPRESS: |
| 3524 |
path = UNCOMPRESS_PATH; |
| 3525 |
break; |
| 3526 |
#endif |
| 3527 |
#ifdef UNZIP_PATH |
| 3528 |
case ppUNZIP: |
| 3529 |
path = UNZIP_PATH; |
| 3530 |
break; |
| 3531 |
#endif |
| 3532 |
#ifdef UUDECODE_PATH |
| 3533 |
case ppUUDECODE: |
| 3534 |
path = UUDECODE_PATH; |
| 3535 |
break; |
| 3536 |
#endif |
| 3537 |
#ifdef ZCAT_PATH |
| 3538 |
case ppZCAT: |
| 3539 |
path = ZCAT_PATH; |
| 3540 |
break; |
| 3541 |
#endif |
| 3542 |
#ifdef ZIP_PATH |
| 3543 |
case ppZIP: |
| 3544 |
path = ZIP_PATH; |
| 3545 |
break; |
| 3546 |
#endif |
| 3547 |
default: |
| 3548 |
path = NULL; |
| 3549 |
break; |
| 3550 |
} |
| 3551 |
test = HTGetProgramPath(code); |
| 3552 |
if (test != NULL && test != path) { |
| 3553 |
free(DeConst(test)); |
| 3554 |
} |
| 3555 |
if (init) { |
| 3556 |
HTSetProgramPath(code, path); |
| 3557 |
} |
| 3558 |
} |
| 3559 |
} |
| 3560 |
|
| 3561 |
/* |
| 3562 |
* Protocol descriptors |
| 3563 |
*/ |
| 3564 |
#ifdef GLOBALDEF_IS_MACRO |
| 3565 |
#define _HTFILE_C_1_INIT { "ftp", HTLoadFile, 0 } |
| 3566 |
GLOBALDEF(HTProtocol, HTFTP, _HTFILE_C_1_INIT); |
| 3567 |
#define _HTFILE_C_2_INIT { "file", HTLoadFile, HTFileSaveStream } |
| 3568 |
GLOBALDEF(HTProtocol, HTFile, _HTFILE_C_2_INIT); |
| 3569 |
#else |
| 3570 |
GLOBALDEF HTProtocol HTFTP = |
| 3571 |
{"ftp", HTLoadFile, NULL}; |
| 3572 |
GLOBALDEF HTProtocol HTFile = |
| 3573 |
{"file", HTLoadFile, HTFileSaveStream}; |
| 3574 |
#endif /* GLOBALDEF_IS_MACRO */ |