| 1 |
/* |
| 2 |
* $LynxId: HTFormat.c,v 1.104 2026/04/20 08:08:36 tom Exp $ |
| 3 |
* |
| 4 |
* Manage different file formats HTFormat.c |
| 5 |
* ============================= |
| 6 |
* |
| 7 |
* Bugs: |
| 8 |
* Not reentrant. |
| 9 |
* |
| 10 |
* Assumes the incoming stream is ASCII, rather than a local file |
| 11 |
* format, and so ALWAYS converts from ASCII on non-ASCII machines. |
| 12 |
* Therefore, non-ASCII machines can't read local files. |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
#define HTSTREAM_INTERNAL 1 |
| 17 |
|
| 18 |
#include <HTTLS.h> |
| 19 |
|
| 20 |
/* Implements: |
| 21 |
*/ |
| 22 |
#include <HTFormat.h> |
| 23 |
|
| 24 |
static float HTMaxSecs = 1e10; /* No effective limit */ |
| 25 |
|
| 26 |
#ifdef UNIX |
| 27 |
#ifdef NeXT |
| 28 |
#define PRESENT_POSTSCRIPT "open %s; /bin/rm -f %s\n" |
| 29 |
#else |
| 30 |
#define PRESENT_POSTSCRIPT "(ghostview %s ; /bin/rm -f %s)&\n" |
| 31 |
/* Full pathname would be better! */ |
| 32 |
#endif /* NeXT */ |
| 33 |
#endif /* UNIX */ |
| 34 |
|
| 35 |
#include <HTML.h> |
| 36 |
#include <HTMLDTD.h> |
| 37 |
#include <HText.h> |
| 38 |
#include <HTAlert.h> |
| 39 |
#include <HTList.h> |
| 40 |
#include <HTInit.h> |
| 41 |
#include <HTTCP.h> |
| 42 |
#include <HTTP.h> |
| 43 |
/* Streams and structured streams which we use: |
| 44 |
*/ |
| 45 |
#include <HTFWriter.h> |
| 46 |
#include <HTPlain.h> |
| 47 |
#include <SGML.h> |
| 48 |
#include <HTMLGen.h> |
| 49 |
|
| 50 |
#include <LYexit.h> |
| 51 |
#include <LYUtils.h> |
| 52 |
#include <GridText.h> |
| 53 |
#include <LYGlobalDefs.h> |
| 54 |
#include <LYLeaks.h> |
| 55 |
|
| 56 |
#ifdef DISP_PARTIAL |
| 57 |
#include <LYMainLoop.h> |
| 58 |
#endif |
| 59 |
|
| 60 |
#ifdef USE_BROTLI |
| 61 |
#include <brotli/decode.h> |
| 62 |
#endif |
| 63 |
|
| 64 |
#ifdef USE_ZSTD |
| 65 |
# include <zstd.h> |
| 66 |
#endif |
| 67 |
|
| 68 |
BOOL HTOutputSource = NO; /* Flag: shortcut parser to stdout */ |
| 69 |
|
| 70 |
/* this version used by the NetToText stream */ |
| 71 |
struct _HTStream { |
| 72 |
const HTStreamClass *isa; |
| 73 |
BOOL had_cr; |
| 74 |
HTStream *sink; |
| 75 |
}; |
| 76 |
|
| 77 |
/* Presentation methods |
| 78 |
* -------------------- |
| 79 |
*/ |
| 80 |
HTList *HTPresentations = NULL; |
| 81 |
HTPresentation *default_presentation = NULL; |
| 82 |
|
| 83 |
/* |
| 84 |
* To free off the presentation list. |
| 85 |
*/ |
| 86 |
#ifdef LY_FIND_LEAKS |
| 87 |
static void HTFreePresentations(void); |
| 88 |
#endif |
| 89 |
|
| 90 |
/* Define a presentation system command for a content-type |
| 91 |
* ------------------------------------------------------- |
| 92 |
*/ |
| 93 |
void HTSetPresentation(const char *representation, |
| 94 |
const char *command, |
| 95 |
const char *testcommand, |
| 96 |
double quality, |
| 97 |
double secs, |
| 98 |
double secs_per_byte, |
| 99 |
long int maxbytes, |
| 100 |
AcceptMedia media) |
| 101 |
{ |
| 102 |
HTPresentation *pres = typecalloc(HTPresentation); |
| 103 |
|
| 104 |
if (pres == NULL) |
| 105 |
outofmem(__FILE__, "HTSetPresentation"); |
| 106 |
|
| 107 |
assert(representation != NULL); |
| 108 |
|
| 109 |
CTRACE2(TRACE_CFG, |
| 110 |
(tfp, |
| 111 |
"HTSetPresentation rep=%s, command=%s, test=%s, qual=%f\n", |
| 112 |
NonNull(representation), |
| 113 |
NonNull(command), |
| 114 |
NonNull(testcommand), |
| 115 |
quality)); |
| 116 |
|
| 117 |
pres->rep = HTAtom_for(representation); |
| 118 |
pres->rep_out = WWW_PRESENT; /* Fixed for now ... :-) */ |
| 119 |
pres->converter = HTSaveAndExecute; /* Fixed for now ... */ |
| 120 |
pres->quality = (float) quality; |
| 121 |
pres->secs = (float) secs; |
| 122 |
pres->secs_per_byte = (float) secs_per_byte; |
| 123 |
pres->maxbytes = maxbytes; |
| 124 |
pres->get_accept = 0; |
| 125 |
pres->accept_opt = media; |
| 126 |
|
| 127 |
pres->command = NULL; |
| 128 |
StrAllocCopy(pres->command, command); |
| 129 |
|
| 130 |
pres->testcommand = NULL; |
| 131 |
StrAllocCopy(pres->testcommand, testcommand); |
| 132 |
|
| 133 |
/* |
| 134 |
* Memory leak fixed. |
| 135 |
* 05-28-94 Lynx 2-3-1 Garrett Arch Blythe |
| 136 |
*/ |
| 137 |
if (!HTPresentations) { |
| 138 |
HTPresentations = HTList_new(); |
| 139 |
#ifdef LY_FIND_LEAKS |
| 140 |
atexit(HTFreePresentations); |
| 141 |
#endif |
| 142 |
} |
| 143 |
|
| 144 |
if (strcmp(representation, "*") == 0) { |
| 145 |
FREE(default_presentation); |
| 146 |
default_presentation = pres; |
| 147 |
} else { |
| 148 |
HTList_addObject(HTPresentations, pres); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/* Define a built-in function for a content-type |
| 153 |
* --------------------------------------------- |
| 154 |
*/ |
| 155 |
void HTSetConversion(const char *representation_in, |
| 156 |
const char *representation_out, |
| 157 |
HTConverter *converter, |
| 158 |
double quality, |
| 159 |
double secs, |
| 160 |
double secs_per_byte, |
| 161 |
long int maxbytes, |
| 162 |
AcceptMedia media) |
| 163 |
{ |
| 164 |
HTPresentation *pres = typecalloc(HTPresentation); |
| 165 |
|
| 166 |
if (pres == NULL) |
| 167 |
outofmem(__FILE__, "HTSetConversion"); |
| 168 |
|
| 169 |
CTRACE2(TRACE_CFG, |
| 170 |
(tfp, |
| 171 |
"HTSetConversion rep_in=%s, rep_out=%s, qual=%f\n", |
| 172 |
NonNull(representation_in), |
| 173 |
NonNull(representation_out), |
| 174 |
quality)); |
| 175 |
|
| 176 |
pres->rep = HTAtom_for(representation_in); |
| 177 |
pres->rep_out = HTAtom_for(representation_out); |
| 178 |
pres->converter = converter; |
| 179 |
pres->command = NULL; |
| 180 |
pres->testcommand = NULL; |
| 181 |
pres->quality = (float) quality; |
| 182 |
pres->secs = (float) secs; |
| 183 |
pres->secs_per_byte = (float) secs_per_byte; |
| 184 |
pres->maxbytes = maxbytes; |
| 185 |
pres->get_accept = TRUE; |
| 186 |
pres->accept_opt = media; |
| 187 |
|
| 188 |
/* |
| 189 |
* Memory Leak fixed. |
| 190 |
* 05-28-94 Lynx 2-3-1 Garrett Arch Blythe |
| 191 |
*/ |
| 192 |
if (!HTPresentations) { |
| 193 |
HTPresentations = HTList_new(); |
| 194 |
#ifdef LY_FIND_LEAKS |
| 195 |
atexit(HTFreePresentations); |
| 196 |
#endif |
| 197 |
} |
| 198 |
|
| 199 |
HTList_addObject(HTPresentations, pres); |
| 200 |
} |
| 201 |
|
| 202 |
#ifdef LY_FIND_LEAKS |
| 203 |
/* |
| 204 |
* Purpose: Free the presentation list. |
| 205 |
* Arguments: void |
| 206 |
* Return Value: void |
| 207 |
* Remarks/Portability/Dependencies/Restrictions: |
| 208 |
* Made to clean up Lynx's bad leakage. |
| 209 |
* Revision History: |
| 210 |
* 05-28-94 created Lynx 2-3-1 Garrett Arch Blythe |
| 211 |
*/ |
| 212 |
static void HTFreePresentations(void) |
| 213 |
{ |
| 214 |
HTPresentation *pres = NULL; |
| 215 |
|
| 216 |
/* |
| 217 |
* Loop through the list. |
| 218 |
*/ |
| 219 |
while (!HTList_isEmpty(HTPresentations)) { |
| 220 |
/* |
| 221 |
* Free off each item. May also need to free off it's items, but not |
| 222 |
* sure as of yet. |
| 223 |
*/ |
| 224 |
pres = (HTPresentation *) HTList_removeLastObject(HTPresentations); |
| 225 |
FREE(pres->command); |
| 226 |
FREE(pres->testcommand); |
| 227 |
FREE(pres); |
| 228 |
} |
| 229 |
/* |
| 230 |
* Free the list itself. |
| 231 |
*/ |
| 232 |
HTList_delete(HTPresentations); |
| 233 |
HTPresentations = NULL; |
| 234 |
} |
| 235 |
#endif /* LY_FIND_LEAKS */ |
| 236 |
|
| 237 |
/* File buffering |
| 238 |
* -------------- |
| 239 |
* |
| 240 |
* The input file is read using the macro which can read from |
| 241 |
* a socket or a file. |
| 242 |
* The input buffer size, if large will give greater efficiency and |
| 243 |
* release the server faster, and if small will save space on PCs etc. |
| 244 |
*/ |
| 245 |
#define INPUT_BUFFER_SIZE 4096 /* Tradeoff */ |
| 246 |
static char input_buffer[INPUT_BUFFER_SIZE]; |
| 247 |
static char *input_pointer; |
| 248 |
static char *input_limit; |
| 249 |
static int input_file_number; |
| 250 |
|
| 251 |
/* Set up the buffering |
| 252 |
* |
| 253 |
* These routines are public because they are in fact needed by |
| 254 |
* many parsers, and on PCs and Macs we should not duplicate |
| 255 |
* the static buffer area. |
| 256 |
*/ |
| 257 |
void HTInitInput(int file_number) |
| 258 |
{ |
| 259 |
input_file_number = file_number; |
| 260 |
input_pointer = input_limit = input_buffer; |
| 261 |
} |
| 262 |
|
| 263 |
int interrupted_in_htgetcharacter = 0; |
| 264 |
int HTGetCharacter(void) |
| 265 |
{ |
| 266 |
char ch; |
| 267 |
|
| 268 |
interrupted_in_htgetcharacter = 0; |
| 269 |
do { |
| 270 |
if (input_pointer >= input_limit) { |
| 271 |
int status = NETREAD(input_file_number, |
| 272 |
input_buffer, INPUT_BUFFER_SIZE); |
| 273 |
|
| 274 |
if (status <= 0) { |
| 275 |
if (status == 0) |
| 276 |
return EOF; |
| 277 |
if (status == HT_INTERRUPTED) { |
| 278 |
CTRACE((tfp, "HTFormat: Interrupted in HTGetCharacter\n")); |
| 279 |
interrupted_in_htgetcharacter = 1; |
| 280 |
return EOF; |
| 281 |
} |
| 282 |
CTRACE((tfp, "HTFormat: File read error %d\n", status)); |
| 283 |
return EOF; /* -1 is returned by UCX |
| 284 |
at end of HTTP link */ |
| 285 |
} |
| 286 |
input_pointer = input_buffer; |
| 287 |
input_limit = input_buffer + status; |
| 288 |
} |
| 289 |
ch = *input_pointer++; |
| 290 |
} while (ch == (char) 13); /* Ignore ASCII carriage return */ |
| 291 |
|
| 292 |
return FROMASCII(UCH(ch)); |
| 293 |
} |
| 294 |
|
| 295 |
#ifdef USE_SSL |
| 296 |
int HTGetSSLCharacter(void *handle) |
| 297 |
{ |
| 298 |
char ch; |
| 299 |
|
| 300 |
interrupted_in_htgetcharacter = 0; |
| 301 |
if (!handle) |
| 302 |
return (char) EOF; |
| 303 |
do { |
| 304 |
if (input_pointer >= input_limit) { |
| 305 |
int status = SSL_read((SSL *) handle, |
| 306 |
input_buffer, INPUT_BUFFER_SIZE); |
| 307 |
|
| 308 |
if (status <= 0) { |
| 309 |
if (status == 0) |
| 310 |
return (char) EOF; |
| 311 |
if (status == HT_INTERRUPTED) { |
| 312 |
CTRACE((tfp, |
| 313 |
"HTFormat: Interrupted in HTGetSSLCharacter\n")); |
| 314 |
interrupted_in_htgetcharacter = 1; |
| 315 |
return (char) EOF; |
| 316 |
} |
| 317 |
CTRACE((tfp, "HTFormat: SSL_read error %d\n", status)); |
| 318 |
return (char) EOF; /* -1 is returned by UCX |
| 319 |
at end of HTTP link */ |
| 320 |
} |
| 321 |
input_pointer = input_buffer; |
| 322 |
input_limit = input_buffer + status; |
| 323 |
} |
| 324 |
ch = *input_pointer++; |
| 325 |
} while (ch == (char) 13); /* Ignore ASCII carriage return */ |
| 326 |
|
| 327 |
return FROMASCII(ch); |
| 328 |
} |
| 329 |
#endif /* USE_SSL */ |
| 330 |
|
| 331 |
/* Match maintype to any MIME type starting with maintype, for example: |
| 332 |
* image/gif should match image |
| 333 |
*/ |
| 334 |
static int half_match(char *trial_type, char *target) |
| 335 |
{ |
| 336 |
char *cp = StrChr(trial_type, '/'); |
| 337 |
|
| 338 |
/* if no '/' or no '*' */ |
| 339 |
if (!cp || *(cp + 1) != '*') |
| 340 |
return 0; |
| 341 |
|
| 342 |
CTRACE((tfp, "HTFormat: comparing %s and %s for half match\n", |
| 343 |
trial_type, target)); |
| 344 |
|
| 345 |
/* main type matches */ |
| 346 |
if (!StrNCmp(trial_type, target, ((cp - trial_type) - 1))) |
| 347 |
return 1; |
| 348 |
|
| 349 |
return 0; |
| 350 |
} |
| 351 |
|
| 352 |
/* |
| 353 |
* Evaluate a deferred mailcap test command, i.e.,. one that substitutes the |
| 354 |
* document's charset or other values in %{name} format. |
| 355 |
*/ |
| 356 |
static BOOL failsMailcap(HTPresentation *pres, HTParentAnchor *anchor) |
| 357 |
{ |
| 358 |
BOOL result = FALSE; |
| 359 |
|
| 360 |
if (pres->testcommand != NULL) { |
| 361 |
if (LYTestMailcapCommand(pres->testcommand, |
| 362 |
HTAtom_name(pres->rep), |
| 363 |
(anchor |
| 364 |
? anchor->content_type_params |
| 365 |
: NULL)) != 0) { |
| 366 |
result = TRUE; |
| 367 |
} |
| 368 |
} |
| 369 |
return result; |
| 370 |
} |
| 371 |
|
| 372 |
#define WWW_WILDCARD_REP_OUT HTAtom_for("*") |
| 373 |
|
| 374 |
/* Look up a presentation |
| 375 |
* ---------------------- |
| 376 |
* |
| 377 |
* If fill_in is NULL, only look for an exact match. |
| 378 |
* If a wildcard match is made, *fill_in is used to store |
| 379 |
* a possibly modified presentation, and a pointer to it is |
| 380 |
* returned. For an exact match, a pointer to the presentation |
| 381 |
* in the HTPresentations list is returned. Returns NULL if |
| 382 |
* nothing found. - kw |
| 383 |
* |
| 384 |
*/ |
| 385 |
static HTPresentation *HTFindPresentation(HTFormat rep_in, |
| 386 |
HTFormat rep_out, |
| 387 |
HTPresentation *fill_in, |
| 388 |
HTParentAnchor *anchor) |
| 389 |
{ |
| 390 |
#undef THIS_FUNC |
| 391 |
#define THIS_FUNC "HTFindPresentation" |
| 392 |
HTAtom *wildcard = NULL; /* = HTAtom_for("*"); lookup when needed - kw */ |
| 393 |
int n; |
| 394 |
int i; |
| 395 |
HTPresentation *pres; |
| 396 |
HTPresentation *match; |
| 397 |
HTPresentation *strong_wildcard_match = NULL; |
| 398 |
HTPresentation *weak_wildcard_match = NULL; |
| 399 |
HTPresentation *last_default_match = NULL; |
| 400 |
HTPresentation *strong_subtype_wildcard_match = NULL; |
| 401 |
|
| 402 |
CTRACE((tfp, THIS_FUNC ": Looking up presentation for %s to %s\n", |
| 403 |
HTAtom_name(rep_in), HTAtom_name(rep_out))); |
| 404 |
|
| 405 |
n = HTList_count(HTPresentations); |
| 406 |
for (i = 0; i < n; i++) { |
| 407 |
pres = (HTPresentation *) HTList_objectAt(HTPresentations, i); |
| 408 |
if (pres->rep == rep_in) { |
| 409 |
if (pres->rep_out == rep_out) { |
| 410 |
if (failsMailcap(pres, anchor)) |
| 411 |
continue; |
| 412 |
CTRACE((tfp, THIS_FUNC ": found exact match: %s -> %s\n", |
| 413 |
HTAtom_name(pres->rep), |
| 414 |
HTAtom_name(pres->rep_out))); |
| 415 |
return pres; |
| 416 |
|
| 417 |
} else if (!fill_in) { |
| 418 |
continue; |
| 419 |
} else { |
| 420 |
if (!wildcard) |
| 421 |
wildcard = WWW_WILDCARD_REP_OUT; |
| 422 |
if (pres->rep_out == wildcard) { |
| 423 |
if (failsMailcap(pres, anchor)) |
| 424 |
continue; |
| 425 |
if (!strong_wildcard_match) |
| 426 |
strong_wildcard_match = pres; |
| 427 |
/* otherwise use the first one */ |
| 428 |
CTRACE((tfp, THIS_FUNC |
| 429 |
": found strong wildcard match: %s -> %s\n", |
| 430 |
HTAtom_name(pres->rep), |
| 431 |
HTAtom_name(pres->rep_out))); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
} else if (!fill_in) { |
| 436 |
continue; |
| 437 |
|
| 438 |
} else if (half_match(HTAtom_name(pres->rep), |
| 439 |
HTAtom_name(rep_in))) { |
| 440 |
if (pres->rep_out == rep_out) { |
| 441 |
if (failsMailcap(pres, anchor)) |
| 442 |
continue; |
| 443 |
if (!strong_subtype_wildcard_match) |
| 444 |
strong_subtype_wildcard_match = pres; |
| 445 |
/* otherwise use the first one */ |
| 446 |
CTRACE((tfp, THIS_FUNC |
| 447 |
": found strong subtype wildcard match: %s -> %s\n", |
| 448 |
HTAtom_name(pres->rep), |
| 449 |
HTAtom_name(pres->rep_out))); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
if (pres->rep == WWW_SOURCE) { |
| 454 |
if (pres->rep_out == rep_out) { |
| 455 |
if (failsMailcap(pres, anchor)) |
| 456 |
continue; |
| 457 |
if (!weak_wildcard_match) |
| 458 |
weak_wildcard_match = pres; |
| 459 |
/* otherwise use the first one */ |
| 460 |
CTRACE((tfp, |
| 461 |
THIS_FUNC ": found weak wildcard match: %s\n", |
| 462 |
HTAtom_name(pres->rep_out))); |
| 463 |
|
| 464 |
} else if (!last_default_match) { |
| 465 |
if (!wildcard) |
| 466 |
wildcard = WWW_WILDCARD_REP_OUT; |
| 467 |
if (pres->rep_out == wildcard) { |
| 468 |
if (failsMailcap(pres, anchor)) |
| 469 |
continue; |
| 470 |
last_default_match = pres; |
| 471 |
/* otherwise use the first one */ |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
match = (strong_subtype_wildcard_match |
| 478 |
? strong_subtype_wildcard_match |
| 479 |
: (strong_wildcard_match |
| 480 |
? strong_wildcard_match |
| 481 |
: (weak_wildcard_match |
| 482 |
? weak_wildcard_match |
| 483 |
: last_default_match))); |
| 484 |
|
| 485 |
if (match) { |
| 486 |
*fill_in = *match; /* Specific instance */ |
| 487 |
fill_in->rep = rep_in; /* yuk */ |
| 488 |
fill_in->rep_out = rep_out; /* yuk */ |
| 489 |
return fill_in; |
| 490 |
} |
| 491 |
|
| 492 |
return NULL; |
| 493 |
#undef THIS_FUNC |
| 494 |
} |
| 495 |
|
| 496 |
/* Create a filter stack |
| 497 |
* --------------------- |
| 498 |
* |
| 499 |
* If a wildcard match is made, a temporary HTPresentation |
| 500 |
* structure is made to hold the destination format while the |
| 501 |
* new stack is generated. This is just to pass the out format to |
| 502 |
* MIME so far. Storing the format of a stream in the stream might |
| 503 |
* be a lot neater. |
| 504 |
* |
| 505 |
*/ |
| 506 |
HTStream *HTStreamStack(HTFormat rep_in, |
| 507 |
HTFormat rep_out, |
| 508 |
HTStream *sink, |
| 509 |
HTParentAnchor *anchor) |
| 510 |
{ |
| 511 |
#undef THIS_FUNC |
| 512 |
#define THIS_FUNC "HTStreamStack" |
| 513 |
HTPresentation temp; |
| 514 |
HTPresentation *match; |
| 515 |
HTStream *result; |
| 516 |
|
| 517 |
CTRACE((tfp, THIS_FUNC ": Constructing stream stack for %s to %s (%s)\n", |
| 518 |
HTAtom_name(rep_in), |
| 519 |
HTAtom_name(rep_out), |
| 520 |
NONNULL(anchor->content_type_params))); |
| 521 |
|
| 522 |
if (rep_out == rep_in) { |
| 523 |
result = sink; |
| 524 |
|
| 525 |
} else if ((match = HTFindPresentation(rep_in, rep_out, &temp, anchor))) { |
| 526 |
if (match == &temp) { |
| 527 |
CTRACE((tfp, THIS_FUNC ": Using %s\n", HTAtom_name(temp.rep_out))); |
| 528 |
} else { |
| 529 |
CTRACE((tfp, THIS_FUNC ": found exact match: %s -> %s\n", |
| 530 |
HTAtom_name(match->rep), |
| 531 |
HTAtom_name(match->rep_out))); |
| 532 |
} |
| 533 |
result = (*match->converter) (match, anchor, sink); |
| 534 |
} else { |
| 535 |
result = NULL; |
| 536 |
} |
| 537 |
if (TRACE) { |
| 538 |
if (result && result->isa && result->isa->name) { |
| 539 |
CTRACE((tfp, THIS_FUNC ": Returning \"%s\"\n", result->isa->name)); |
| 540 |
} else if (result) { |
| 541 |
CTRACE((tfp, THIS_FUNC ": Returning *unknown* stream!\n")); |
| 542 |
} else { |
| 543 |
CTRACE((tfp, THIS_FUNC ": Returning NULL!\n")); |
| 544 |
CTRACE_FLUSH(tfp); /* a crash may be imminent... - kw */ |
| 545 |
} |
| 546 |
} |
| 547 |
return result; |
| 548 |
#undef THIS_FUNC |
| 549 |
} |
| 550 |
|
| 551 |
/* Put a presentation near start of list |
| 552 |
* ------------------------------------- |
| 553 |
* |
| 554 |
* Look up a presentation (exact match only) and, if found, reorder |
| 555 |
* it to the start of the HTPresentations list. - kw |
| 556 |
*/ |
| 557 |
void HTReorderPresentation(HTFormat rep_in, |
| 558 |
HTFormat rep_out) |
| 559 |
{ |
| 560 |
HTPresentation *match; |
| 561 |
|
| 562 |
if ((match = HTFindPresentation(rep_in, rep_out, NULL, NULL))) { |
| 563 |
HTList_removeObject(HTPresentations, match); |
| 564 |
HTList_addObject(HTPresentations, match); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
/* |
| 569 |
* Setup 'get_accept' flag to denote presentations that are not redundant, |
| 570 |
* and will be listed in "Accept:" header. |
| 571 |
*/ |
| 572 |
void HTFilterPresentations(void) |
| 573 |
{ |
| 574 |
int i, j; |
| 575 |
int n = HTList_count(HTPresentations); |
| 576 |
HTPresentation *p, *q; |
| 577 |
BOOL matched; |
| 578 |
char *s, *t; |
| 579 |
|
| 580 |
CTRACE((tfp, "HTFilterPresentations (AcceptMedia %#x)\n", LYAcceptMedia)); |
| 581 |
for (i = 0; i < n; i++) { |
| 582 |
p = (HTPresentation *) HTList_objectAt(HTPresentations, i); |
| 583 |
s = HTAtom_name(p->rep); |
| 584 |
|
| 585 |
p->get_accept = FALSE; |
| 586 |
if ((LYAcceptMedia & p->accept_opt) != 0 |
| 587 |
&& p->rep_out == WWW_PRESENT |
| 588 |
&& p->rep != WWW_SOURCE |
| 589 |
&& strcasecomp(s, "www/mime") |
| 590 |
&& strcasecomp(s, "www/compressed") |
| 591 |
&& p->quality <= 1.0 && p->quality >= 0.0) { |
| 592 |
matched = TRUE; |
| 593 |
for (j = 0; j < i; j++) { |
| 594 |
q = (HTPresentation *) HTList_objectAt(HTPresentations, j); |
| 595 |
t = HTAtom_name(q->rep); |
| 596 |
|
| 597 |
if (!strcasecomp(s, t)) { |
| 598 |
matched = FALSE; |
| 599 |
CTRACE((tfp, " match %s %s\n", s, t)); |
| 600 |
break; |
| 601 |
} |
| 602 |
} |
| 603 |
p->get_accept = matched; |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
/* Find the cost of a filter stack |
| 609 |
* ------------------------------- |
| 610 |
* |
| 611 |
* Must return the cost of the same stack which HTStreamStack would set up. |
| 612 |
* |
| 613 |
* On entry, |
| 614 |
* length The size of the data to be converted |
| 615 |
*/ |
| 616 |
float HTStackValue(HTFormat rep_in, |
| 617 |
HTFormat rep_out, |
| 618 |
double initial_value, |
| 619 |
long int length) |
| 620 |
{ |
| 621 |
HTAtom *wildcard = WWW_WILDCARD_REP_OUT; |
| 622 |
|
| 623 |
CTRACE((tfp, "HTFormat: Evaluating stream stack for %s worth %.3f to %s\n", |
| 624 |
HTAtom_name(rep_in), initial_value, HTAtom_name(rep_out))); |
| 625 |
|
| 626 |
if (rep_out == WWW_SOURCE || rep_out == rep_in) |
| 627 |
return 0.0; |
| 628 |
|
| 629 |
{ |
| 630 |
int n = HTList_count(HTPresentations); |
| 631 |
int i; |
| 632 |
HTPresentation *pres; |
| 633 |
|
| 634 |
for (i = 0; i < n; i++) { |
| 635 |
pres = (HTPresentation *) HTList_objectAt(HTPresentations, i); |
| 636 |
if (pres->rep == rep_in && |
| 637 |
(pres->rep_out == rep_out || pres->rep_out == wildcard)) { |
| 638 |
float value = (float) (initial_value * pres->quality); |
| 639 |
|
| 640 |
if (HTMaxSecs > 0.0) |
| 641 |
value = (value |
| 642 |
- ((float) length * pres->secs_per_byte |
| 643 |
+ pres->secs) |
| 644 |
/ HTMaxSecs); |
| 645 |
return value; |
| 646 |
} |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
return (float) -1e30; /* Really bad */ |
| 651 |
|
| 652 |
} |
| 653 |
|
| 654 |
/* Display the page while transfer in progress |
| 655 |
* ------------------------------------------- |
| 656 |
* |
| 657 |
* Repaint the page only when necessary. |
| 658 |
* This is a traverse call for HText_pageDisplay() - it works!. |
| 659 |
* |
| 660 |
*/ |
| 661 |
void HTDisplayPartial(void) |
| 662 |
{ |
| 663 |
#ifdef DISP_PARTIAL |
| 664 |
if (display_partial) { |
| 665 |
/* |
| 666 |
* HText_getNumOfLines() = "current" number of complete lines received |
| 667 |
* NumOfLines_partial = number of lines at the moment of last repaint. |
| 668 |
* (we update NumOfLines_partial only when we repaint the display.) |
| 669 |
* |
| 670 |
* display_partial could only be enabled in HText_new() so a new |
| 671 |
* HTMainText object available - all HText_ functions use it, lines |
| 672 |
* counter HText_getNumOfLines() in particular. |
| 673 |
* |
| 674 |
* Otherwise HTMainText holds info from the previous document and we |
| 675 |
* may repaint it instead of the new one: prev doc scrolled to the |
| 676 |
* first line (=Newline_partial) is not good looking :-) 23 Aug 1998 |
| 677 |
* Leonid Pauzner |
| 678 |
* |
| 679 |
* So repaint the page only when necessary: |
| 680 |
*/ |
| 681 |
int Newline_partial = LYGetNewline(); |
| 682 |
|
| 683 |
if (((Newline_partial + display_lines) - 1 > NumOfLines_partial) |
| 684 |
/* current page not complete... */ |
| 685 |
&& (partial_threshold > 0 ? |
| 686 |
((Newline_partial + partial_threshold) - 1 <= |
| 687 |
HText_getNumOfLines()) : |
| 688 |
((Newline_partial + display_lines) - 1 <= HText_getNumOfLines())) |
| 689 |
/* |
| 690 |
* Originally we rendered by increments of 2 lines, |
| 691 |
* but that got annoying on slow network connections. |
| 692 |
* Then we switched to full-pages. Now it's configurable. |
| 693 |
* If partial_threshold <= 0, then it's a full page |
| 694 |
*/ |
| 695 |
) { |
| 696 |
if (LYMainLoop_pageDisplay(Newline_partial)) |
| 697 |
NumOfLines_partial = HText_getNumOfLines(); |
| 698 |
} |
| 699 |
} |
| 700 |
#else /* nothing */ |
| 701 |
#endif /* DISP_PARTIAL */ |
| 702 |
} |
| 703 |
|
| 704 |
/* Put this as early as possible, OK just after HTDisplayPartial() */ |
| 705 |
void HTFinishDisplayPartial(void) |
| 706 |
{ |
| 707 |
#ifdef DISP_PARTIAL |
| 708 |
/* |
| 709 |
* End of incremental rendering stage here. |
| 710 |
*/ |
| 711 |
display_partial = FALSE; |
| 712 |
#endif /* DISP_PARTIAL */ |
| 713 |
} |
| 714 |
|
| 715 |
/* Push data from a socket down a stream |
| 716 |
* ------------------------------------- |
| 717 |
* |
| 718 |
* This routine is responsible for creating and PRESENTING any |
| 719 |
* graphic (or other) objects described by the file. |
| 720 |
* |
| 721 |
* The file number given is assumed to be a TELNET stream, i.e., containing |
| 722 |
* CRLF at the end of lines which need to be stripped to LF for unix |
| 723 |
* when the format is textual. |
| 724 |
* |
| 725 |
* State of socket and target stream on entry: |
| 726 |
* socket (file_number) assumed open, |
| 727 |
* target (sink) assumed valid. |
| 728 |
* |
| 729 |
* Return values: |
| 730 |
* HT_INTERRUPTED Interruption or error after some data received. |
| 731 |
* -2 Unexpected disconnect before any data received. |
| 732 |
* -1 Interruption or error before any data received, or |
| 733 |
* (UNIX) other read error before any data received, or |
| 734 |
* download cancelled. |
| 735 |
* HT_LOADED Normal close of socket (end of file indication |
| 736 |
* received), or |
| 737 |
* unexpected disconnect after some data received, or |
| 738 |
* other read error after some data received, or |
| 739 |
* (not UNIX) other read error before any data received. |
| 740 |
* |
| 741 |
* State of socket and target stream on return depends on return value: |
| 742 |
* HT_INTERRUPTED socket still open, target aborted. |
| 743 |
* -2 socket still open, target stream still valid. |
| 744 |
* -1 socket still open, target aborted. |
| 745 |
* otherwise socket closed, target stream still valid. |
| 746 |
*/ |
| 747 |
int HTCopy(HTParentAnchor *anchor, |
| 748 |
int file_number, |
| 749 |
void *handle GCC_UNUSED, |
| 750 |
HTStream *sink) |
| 751 |
{ |
| 752 |
HTStreamClass targetClass; |
| 753 |
BOOL suppress_readprogress = NO; |
| 754 |
off_t limit = anchor ? anchor->content_length : 0; |
| 755 |
off_t bytes = 0; |
| 756 |
off_t header_length = 0; |
| 757 |
int rv = 0; |
| 758 |
|
| 759 |
/* Push the data down the stream |
| 760 |
*/ |
| 761 |
targetClass = *(sink->isa); /* Copy pointers to procedures */ |
| 762 |
|
| 763 |
/* |
| 764 |
* Push binary from socket down sink |
| 765 |
* |
| 766 |
* This operation could be put into a main event loop |
| 767 |
*/ |
| 768 |
HTReadProgress(bytes, (off_t) 0); |
| 769 |
for (;;) { |
| 770 |
int status; |
| 771 |
|
| 772 |
if (LYCancelDownload) { |
| 773 |
LYCancelDownload = FALSE; |
| 774 |
(*targetClass._abort) (sink, NULL); |
| 775 |
rv = -1; |
| 776 |
goto finished; |
| 777 |
} |
| 778 |
|
| 779 |
if (HTCheckForInterrupt()) { |
| 780 |
_HTProgress(TRANSFER_INTERRUPTED); |
| 781 |
(*targetClass._abort) (sink, NULL); |
| 782 |
if (bytes) |
| 783 |
rv = HT_INTERRUPTED; |
| 784 |
else |
| 785 |
rv = -1; |
| 786 |
goto finished; |
| 787 |
} |
| 788 |
#ifdef USE_SSL |
| 789 |
if (handle) |
| 790 |
status = SSL_read((SSL *) handle, input_buffer, INPUT_BUFFER_SIZE); |
| 791 |
else |
| 792 |
status = NETREAD(file_number, input_buffer, INPUT_BUFFER_SIZE); |
| 793 |
#else |
| 794 |
status = NETREAD(file_number, input_buffer, INPUT_BUFFER_SIZE); |
| 795 |
#endif /* USE_SSL */ |
| 796 |
if (status <= 0) { |
| 797 |
if (status == 0) { |
| 798 |
break; |
| 799 |
} else if (status == HT_INTERRUPTED) { |
| 800 |
_HTProgress(TRANSFER_INTERRUPTED); |
| 801 |
(*targetClass._abort) (sink, NULL); |
| 802 |
if (bytes) |
| 803 |
rv = HT_INTERRUPTED; |
| 804 |
else |
| 805 |
rv = -1; |
| 806 |
goto finished; |
| 807 |
} else if (SOCKET_ERRNO == ENOTCONN || |
| 808 |
#ifdef _WINDOWS /* 1997/11/10 (Mon) 16:57:18 */ |
| 809 |
SOCKET_ERRNO == ETIMEDOUT || |
| 810 |
#endif |
| 811 |
SOCKET_ERRNO == ECONNRESET || |
| 812 |
SOCKET_ERRNO == EPIPE) { |
| 813 |
/* |
| 814 |
* Arrrrgh, HTTP 0/1 compatibility problem, maybe. |
| 815 |
*/ |
| 816 |
if (bytes <= 0) { |
| 817 |
/* |
| 818 |
* Don't have any data, so let the calling function decide |
| 819 |
* what to do about it. - FM |
| 820 |
*/ |
| 821 |
rv = -2; |
| 822 |
goto finished; |
| 823 |
} else { |
| 824 |
#ifdef UNIX |
| 825 |
/* |
| 826 |
* Treat what we've received already as the complete |
| 827 |
* transmission, but not without giving the user an alert. |
| 828 |
* I don't know about all the different TCP stacks for VMS |
| 829 |
* etc., so this is currently only for UNIX. - kw |
| 830 |
*/ |
| 831 |
HTInetStatus("NETREAD"); |
| 832 |
HTAlert("Unexpected server disconnect."); |
| 833 |
CTRACE((tfp, |
| 834 |
"HTCopy: Unexpected server disconnect. Treating as completed.\n")); |
| 835 |
#else /* !UNIX */ |
| 836 |
/* |
| 837 |
* Treat what we've gotten already as the complete |
| 838 |
* transmission. - FM |
| 839 |
*/ |
| 840 |
CTRACE((tfp, |
| 841 |
"HTCopy: Unexpected server disconnect. Treating as completed.\n")); |
| 842 |
status = 0; |
| 843 |
#endif /* UNIX */ |
| 844 |
} |
| 845 |
#ifdef UNIX |
| 846 |
} else { /* status < 0 and other errno */ |
| 847 |
/* |
| 848 |
* Treat what we've received already as the complete |
| 849 |
* transmission, but not without giving the user an alert. I |
| 850 |
* don't know about all the different TCP stacks for VMS etc., |
| 851 |
* so this is currently only for UNIX. - kw |
| 852 |
*/ |
| 853 |
HTInetStatus("NETREAD"); |
| 854 |
HTAlert("Unexpected read error."); |
| 855 |
if (bytes) { |
| 856 |
(void) NETCLOSE(file_number); |
| 857 |
rv = HT_LOADED; |
| 858 |
} else { |
| 859 |
(*targetClass._abort) (sink, NULL); |
| 860 |
rv = -1; |
| 861 |
} |
| 862 |
goto finished; |
| 863 |
#endif |
| 864 |
} |
| 865 |
break; |
| 866 |
} |
| 867 |
|
| 868 |
/* |
| 869 |
* Suppress ReadProgress messages when collecting a redirection |
| 870 |
* message, at least initially (unless/until anchor->content_type gets |
| 871 |
* changed, probably by the MIME message parser). That way messages |
| 872 |
* put up by the HTTP module or elsewhere can linger in the statusline |
| 873 |
* for a while. - kw |
| 874 |
*/ |
| 875 |
suppress_readprogress = (BOOL) (anchor && anchor->content_type && |
| 876 |
!strcmp(anchor->content_type, |
| 877 |
"message/x-http-redirection")); |
| 878 |
#ifdef NOT_ASCII |
| 879 |
{ |
| 880 |
char *p; |
| 881 |
|
| 882 |
for (p = input_buffer; p < input_buffer + status; p++) { |
| 883 |
*p = FROMASCII(*p); |
| 884 |
} |
| 885 |
} |
| 886 |
#endif /* NOT_ASCII */ |
| 887 |
|
| 888 |
header_length = anchor != NULL ? anchor->header_length : 0; |
| 889 |
|
| 890 |
(*targetClass.put_block) (sink, input_buffer, status); |
| 891 |
if (anchor != NULL && anchor->inHEAD) { |
| 892 |
if (!suppress_readprogress) { |
| 893 |
statusline(gettext("Reading headers...")); |
| 894 |
} |
| 895 |
CTRACE((tfp, "HTCopy read %" PRI_off_t " header bytes\n", |
| 896 |
CAST_off_t (anchor->header_length))); |
| 897 |
} else { |
| 898 |
/* |
| 899 |
* If header-length is increased at this point, that is due to |
| 900 |
* HTMIME, which detects the end of the server headers. There |
| 901 |
* may be additional (non-header) data in that block. |
| 902 |
*/ |
| 903 |
if (anchor != NULL && (anchor->header_length > header_length)) { |
| 904 |
int header = (int) (anchor->header_length - header_length); |
| 905 |
|
| 906 |
CTRACE((tfp, "HTCopy read %" PRI_off_t " header bytes " |
| 907 |
"(%d extra vs %d total)\n", |
| 908 |
CAST_off_t (anchor->header_length), |
| 909 |
header, status)); |
| 910 |
if (status > header) { |
| 911 |
bytes += (status - header); |
| 912 |
} |
| 913 |
} else { |
| 914 |
bytes += status; |
| 915 |
} |
| 916 |
if (!suppress_readprogress) { |
| 917 |
HTReadProgress(bytes, limit); |
| 918 |
} |
| 919 |
HTDisplayPartial(); |
| 920 |
} |
| 921 |
|
| 922 |
/* a few buggy implementations do not close the connection properly |
| 923 |
* and will hang if we try to read past the declared content-length. |
| 924 |
*/ |
| 925 |
if (limit > 0 && bytes >= limit) |
| 926 |
break; |
| 927 |
} /* next bufferload */ |
| 928 |
if (anchor != NULL) { |
| 929 |
CTRACE((tfp, "HTCopy copied %" |
| 930 |
PRI_off_t " actual, %" |
| 931 |
PRI_off_t " limit\n", CAST_off_t (bytes), CAST_off_t (limit))); |
| 932 |
anchor->actual_length = bytes; |
| 933 |
} |
| 934 |
|
| 935 |
_HTProgress(TRANSFER_COMPLETE); |
| 936 |
(void) NETCLOSE(file_number); |
| 937 |
rv = HT_LOADED; |
| 938 |
|
| 939 |
finished: |
| 940 |
HTFinishDisplayPartial(); |
| 941 |
return (rv); |
| 942 |
} |
| 943 |
|
| 944 |
/* Push data from a file pointer down a stream |
| 945 |
* ------------------------------------- |
| 946 |
* |
| 947 |
* This routine is responsible for creating and PRESENTING any |
| 948 |
* graphic (or other) objects described by the file. |
| 949 |
* |
| 950 |
* |
| 951 |
* State of file and target stream on entry: |
| 952 |
* FILE* (fp) assumed open, |
| 953 |
* target (sink) assumed valid. |
| 954 |
* |
| 955 |
* Return values: |
| 956 |
* HT_INTERRUPTED Interruption after some data read. |
| 957 |
* HT_PARTIAL_CONTENT Error after some data read. |
| 958 |
* -1 Error before any data read. |
| 959 |
* HT_LOADED Normal end of file indication on reading. |
| 960 |
* |
| 961 |
* State of file and target stream on return: |
| 962 |
* always fp still open, target stream still valid. |
| 963 |
*/ |
| 964 |
int HTFileCopy(FILE *fp, HTStream *sink) |
| 965 |
{ |
| 966 |
HTStreamClass targetClass; |
| 967 |
int status; |
| 968 |
off_t bytes; |
| 969 |
int rv = HT_OK; |
| 970 |
|
| 971 |
/* Push the data down the stream |
| 972 |
*/ |
| 973 |
targetClass = *(sink->isa); /* Copy pointers to procedures */ |
| 974 |
|
| 975 |
/* Push binary from socket down sink |
| 976 |
*/ |
| 977 |
HTReadProgress(bytes = 0, (off_t) 0); |
| 978 |
for (;;) { |
| 979 |
status = (int) fread(input_buffer, |
| 980 |
(size_t) 1, |
| 981 |
(size_t) INPUT_BUFFER_SIZE, fp); |
| 982 |
if (status == 0) { /* EOF or error */ |
| 983 |
if (ferror(fp) == 0) { |
| 984 |
rv = HT_LOADED; |
| 985 |
break; |
| 986 |
} |
| 987 |
CTRACE((tfp, "HTFormat: Read error, read returns %d\n", |
| 988 |
ferror(fp))); |
| 989 |
if (bytes) { |
| 990 |
rv = HT_PARTIAL_CONTENT; |
| 991 |
} else { |
| 992 |
rv = -1; |
| 993 |
} |
| 994 |
break; |
| 995 |
} |
| 996 |
|
| 997 |
(*targetClass.put_block) (sink, input_buffer, status); |
| 998 |
bytes += status; |
| 999 |
HTReadProgress(bytes, (off_t) 0); |
| 1000 |
/* Suppress last screen update in partial mode - a regular update under |
| 1001 |
* control of mainloop() should follow anyway. - kw |
| 1002 |
*/ |
| 1003 |
#ifdef DISP_PARTIAL |
| 1004 |
if (display_partial && bytes != HTMainAnchor->content_length) |
| 1005 |
HTDisplayPartial(); |
| 1006 |
#endif |
| 1007 |
|
| 1008 |
if (HTCheckForInterrupt()) { |
| 1009 |
_HTProgress(TRANSFER_INTERRUPTED); |
| 1010 |
if (bytes) { |
| 1011 |
rv = HT_INTERRUPTED; |
| 1012 |
} else { |
| 1013 |
rv = -1; |
| 1014 |
} |
| 1015 |
break; |
| 1016 |
} |
| 1017 |
} /* next bufferload */ |
| 1018 |
|
| 1019 |
HTFinishDisplayPartial(); |
| 1020 |
return rv; |
| 1021 |
} |
| 1022 |
|
| 1023 |
#ifdef USE_SOURCE_CACHE |
| 1024 |
/* Push data from an HTChunk down a stream |
| 1025 |
* --------------------------------------- |
| 1026 |
* |
| 1027 |
* This routine is responsible for creating and PRESENTING any |
| 1028 |
* graphic (or other) objects described by the file. |
| 1029 |
* |
| 1030 |
* State of memory and target stream on entry: |
| 1031 |
* HTChunk* (chunk) and target (sink) assumed valid. |
| 1032 |
* |
| 1033 |
* Return values: |
| 1034 |
* HT_LOADED All data sent. |
| 1035 |
* HT_INTERRUPTED Interruption after some data read. |
| 1036 |
* |
| 1037 |
* State of memory and target stream on return: |
| 1038 |
* always chunk unchanged, target stream still valid. |
| 1039 |
*/ |
| 1040 |
int HTMemCopy(HTChunk *chunk, HTStream *sink) |
| 1041 |
{ |
| 1042 |
HTStreamClass targetClass; |
| 1043 |
off_t bytes; |
| 1044 |
int rv = HT_OK; |
| 1045 |
|
| 1046 |
targetClass = *(sink->isa); |
| 1047 |
HTReadProgress(bytes = 0, (off_t) 0); |
| 1048 |
for (; chunk != NULL; chunk = chunk->next) { |
| 1049 |
|
| 1050 |
/* Push the data down the stream a piece at a time, in case we're |
| 1051 |
* running a large document on a slow machine. |
| 1052 |
*/ |
| 1053 |
(*targetClass.put_block) (sink, chunk->data, chunk->size); |
| 1054 |
bytes += chunk->size; |
| 1055 |
|
| 1056 |
HTReadProgress(bytes, (off_t) 0); |
| 1057 |
HTDisplayPartial(); |
| 1058 |
|
| 1059 |
if (HTCheckForInterrupt()) { |
| 1060 |
_HTProgress(TRANSFER_INTERRUPTED); |
| 1061 |
if (bytes) { |
| 1062 |
rv = HT_INTERRUPTED; |
| 1063 |
} else { |
| 1064 |
rv = -1; |
| 1065 |
} |
| 1066 |
break; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
HTFinishDisplayPartial(); |
| 1071 |
return rv; |
| 1072 |
} |
| 1073 |
#endif |
| 1074 |
|
| 1075 |
#ifdef USE_ZLIB |
| 1076 |
/* Push data from a gzip file pointer down a stream |
| 1077 |
* ------------------------------------- |
| 1078 |
* |
| 1079 |
* This routine is responsible for creating and PRESENTING any |
| 1080 |
* graphic (or other) objects described by the file. |
| 1081 |
* |
| 1082 |
* |
| 1083 |
* State of file and target stream on entry: |
| 1084 |
* gzFile (gzfp) assumed open (should have gzipped content), |
| 1085 |
* target (sink) assumed valid. |
| 1086 |
* |
| 1087 |
* Return values: |
| 1088 |
* HT_INTERRUPTED Interruption after some data read. |
| 1089 |
* HT_PARTIAL_CONTENT Error after some data read. |
| 1090 |
* -1 Error before any data read. |
| 1091 |
* HT_LOADED Normal end of file indication on reading. |
| 1092 |
* |
| 1093 |
* State of file and target stream on return: |
| 1094 |
* always gzfp still open, target stream still valid. |
| 1095 |
*/ |
| 1096 |
static int HTGzFileCopy(gzFile gzfp, HTStream *sink) |
| 1097 |
{ |
| 1098 |
HTStreamClass targetClass; |
| 1099 |
int status; |
| 1100 |
off_t bytes; |
| 1101 |
int gzerrnum; |
| 1102 |
int rv = HT_OK; |
| 1103 |
|
| 1104 |
/* Push the data down the stream |
| 1105 |
*/ |
| 1106 |
targetClass = *(sink->isa); /* Copy pointers to procedures */ |
| 1107 |
|
| 1108 |
/* read and inflate gzip'd file, and push binary down sink |
| 1109 |
*/ |
| 1110 |
HTReadProgress(bytes = 0, (off_t) 0); |
| 1111 |
for (;;) { |
| 1112 |
status = gzread(gzfp, input_buffer, INPUT_BUFFER_SIZE); |
| 1113 |
if (status <= 0) { /* EOF or error */ |
| 1114 |
if (status == 0) { |
| 1115 |
rv = HT_LOADED; |
| 1116 |
break; |
| 1117 |
} |
| 1118 |
CTRACE((tfp, "HTGzFileCopy: Read error, gzread returns %d\n", |
| 1119 |
status)); |
| 1120 |
CTRACE((tfp, "gzerror : %s\n", |
| 1121 |
gzerror(gzfp, &gzerrnum))); |
| 1122 |
if (TRACE) { |
| 1123 |
if (gzerrnum == Z_ERRNO) |
| 1124 |
perror("gzerror "); |
| 1125 |
} |
| 1126 |
if (bytes) { |
| 1127 |
rv = HT_PARTIAL_CONTENT; |
| 1128 |
} else { |
| 1129 |
rv = -1; |
| 1130 |
} |
| 1131 |
break; |
| 1132 |
} |
| 1133 |
|
| 1134 |
(*targetClass.put_block) (sink, input_buffer, status); |
| 1135 |
bytes += status; |
| 1136 |
HTReadProgress(bytes, (off_t) -1); |
| 1137 |
HTDisplayPartial(); |
| 1138 |
|
| 1139 |
if (HTCheckForInterrupt()) { |
| 1140 |
_HTProgress(TRANSFER_INTERRUPTED); |
| 1141 |
rv = HT_INTERRUPTED; |
| 1142 |
break; |
| 1143 |
} |
| 1144 |
} /* next bufferload */ |
| 1145 |
|
| 1146 |
HTFinishDisplayPartial(); |
| 1147 |
return rv; |
| 1148 |
} |
| 1149 |
|
| 1150 |
#ifndef HAVE_ZERROR |
| 1151 |
#define zError(s) LynxZError(s) |
| 1152 |
static const char *zError(int status) |
| 1153 |
{ |
| 1154 |
static char result[80]; |
| 1155 |
|
| 1156 |
sprintf(result, "zlib error %d", status); |
| 1157 |
return result; |
| 1158 |
} |
| 1159 |
#endif |
| 1160 |
|
| 1161 |
/* Push data from a deflate file pointer down a stream |
| 1162 |
* ------------------------------------- |
| 1163 |
* |
| 1164 |
* This routine is responsible for creating and PRESENTING any |
| 1165 |
* graphic (or other) objects described by the file. The code is |
| 1166 |
* loosely based on the inflate.c file from w3m. |
| 1167 |
* |
| 1168 |
* |
| 1169 |
* State of file and target stream on entry: |
| 1170 |
* FILE (zzfp) assumed open (should have deflated content), |
| 1171 |
* target (sink) assumed valid. |
| 1172 |
* |
| 1173 |
* Return values: |
| 1174 |
* HT_INTERRUPTED Interruption after some data read. |
| 1175 |
* HT_PARTIAL_CONTENT Error after some data read. |
| 1176 |
* -1 Error before any data read. |
| 1177 |
* HT_LOADED Normal end of file indication on reading. |
| 1178 |
* |
| 1179 |
* State of file and target stream on return: |
| 1180 |
* always zzfp still open, target stream still valid. |
| 1181 |
*/ |
| 1182 |
static int HTZzFileCopy(FILE *zzfp, HTStream *sink) |
| 1183 |
{ |
| 1184 |
#undef THIS_FUNC |
| 1185 |
#define THIS_FUNC "HTZzFileCopy" |
| 1186 |
static char dummy_head[1 + 1] = |
| 1187 |
{ |
| 1188 |
0x8 + 0x7 * 0x10, |
| 1189 |
(((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF, |
| 1190 |
}; |
| 1191 |
|
| 1192 |
z_stream s; |
| 1193 |
HTStreamClass targetClass; |
| 1194 |
off_t bytes; |
| 1195 |
int rv = HT_OK; |
| 1196 |
char output_buffer[INPUT_BUFFER_SIZE]; |
| 1197 |
int status; |
| 1198 |
int flush; |
| 1199 |
int retry = 0; |
| 1200 |
int len = 0; |
| 1201 |
|
| 1202 |
/* Push the data down the stream |
| 1203 |
*/ |
| 1204 |
targetClass = *(sink->isa); /* Copy pointers to procedures */ |
| 1205 |
|
| 1206 |
memset(&s, 0, sizeof(s)); |
| 1207 |
status = inflateInit(&s); |
| 1208 |
if (status != Z_OK) { |
| 1209 |
CTRACE((tfp, THIS_FUNC " inflateInit() %s\n", zError(status))); |
| 1210 |
exit_immediately(EXIT_FAILURE); |
| 1211 |
} |
| 1212 |
s.avail_in = 0; |
| 1213 |
s.next_out = (Bytef *) output_buffer; |
| 1214 |
s.avail_out = sizeof(output_buffer); |
| 1215 |
flush = Z_NO_FLUSH; |
| 1216 |
|
| 1217 |
/* read and inflate deflate'd file, and push binary down sink |
| 1218 |
*/ |
| 1219 |
HTReadProgress(bytes = 0, (off_t) 0); |
| 1220 |
for (;;) { |
| 1221 |
if (s.avail_in == 0) { |
| 1222 |
s.next_in = (Bytef *) input_buffer; |
| 1223 |
s.avail_in = (uInt) fread(input_buffer, |
| 1224 |
(size_t) 1, |
| 1225 |
(size_t) INPUT_BUFFER_SIZE, zzfp); |
| 1226 |
len = (int) s.avail_in; |
| 1227 |
} |
| 1228 |
status = inflate(&s, flush); |
| 1229 |
if (status == Z_STREAM_END || status == Z_BUF_ERROR) { |
| 1230 |
len = (int) sizeof(output_buffer) - (int) s.avail_out; |
| 1231 |
if (len > 0) { |
| 1232 |
(*targetClass.put_block) (sink, output_buffer, len); |
| 1233 |
bytes += len; |
| 1234 |
HTReadProgress(bytes, (off_t) -1); |
| 1235 |
HTDisplayPartial(); |
| 1236 |
} |
| 1237 |
rv = HT_LOADED; |
| 1238 |
break; |
| 1239 |
} else if (status == Z_DATA_ERROR && !retry++) { |
| 1240 |
status = inflateReset(&s); |
| 1241 |
if (status != Z_OK) { |
| 1242 |
CTRACE((tfp, THIS_FUNC " inflateReset() %s\n", zError(status))); |
| 1243 |
rv = -1; |
| 1244 |
break; |
| 1245 |
} |
| 1246 |
s.next_in = (Bytef *) dummy_head; |
| 1247 |
s.avail_in = sizeof(dummy_head); |
| 1248 |
(void) inflate(&s, flush); |
| 1249 |
s.next_in = (Bytef *) input_buffer; |
| 1250 |
s.avail_in = (unsigned) len; |
| 1251 |
continue; |
| 1252 |
} else if (status != Z_OK) { |
| 1253 |
CTRACE((tfp, THIS_FUNC " inflate() %s\n", zError(status))); |
| 1254 |
rv = bytes ? HT_PARTIAL_CONTENT : -1; |
| 1255 |
break; |
| 1256 |
} else if (s.avail_out == 0) { |
| 1257 |
len = sizeof(output_buffer); |
| 1258 |
s.next_out = (Bytef *) output_buffer; |
| 1259 |
s.avail_out = sizeof(output_buffer); |
| 1260 |
|
| 1261 |
(*targetClass.put_block) (sink, output_buffer, len); |
| 1262 |
bytes += len; |
| 1263 |
HTReadProgress(bytes, (off_t) -1); |
| 1264 |
HTDisplayPartial(); |
| 1265 |
|
| 1266 |
if (HTCheckForInterrupt()) { |
| 1267 |
_HTProgress(TRANSFER_INTERRUPTED); |
| 1268 |
rv = bytes ? HT_INTERRUPTED : -1; |
| 1269 |
break; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
retry = 1; |
| 1273 |
} /* next bufferload */ |
| 1274 |
|
| 1275 |
inflateEnd(&s); |
| 1276 |
HTFinishDisplayPartial(); |
| 1277 |
return rv; |
| 1278 |
#undef THIS_FUNC |
| 1279 |
} |
| 1280 |
#endif /* USE_ZLIB */ |
| 1281 |
|
| 1282 |
#ifdef USE_BZLIB |
| 1283 |
/* Push data from a bzip file pointer down a stream |
| 1284 |
* ------------------------------------- |
| 1285 |
* |
| 1286 |
* This routine is responsible for creating and PRESENTING any |
| 1287 |
* graphic (or other) objects described by the file. |
| 1288 |
* |
| 1289 |
* |
| 1290 |
* State of file and target stream on entry: |
| 1291 |
* BZFILE (bzfp) assumed open (should have bzipped content), |
| 1292 |
* target (sink) assumed valid. |
| 1293 |
* |
| 1294 |
* Return values: |
| 1295 |
* HT_INTERRUPTED Interruption after some data read. |
| 1296 |
* HT_PARTIAL_CONTENT Error after some data read. |
| 1297 |
* -1 Error before any data read. |
| 1298 |
* HT_LOADED Normal end of file indication on reading. |
| 1299 |
* |
| 1300 |
* State of file and target stream on return: |
| 1301 |
* always bzfp still open, target stream still valid. |
| 1302 |
*/ |
| 1303 |
static int HTBzFileCopy(BZFILE * bzfp, HTStream *sink) |
| 1304 |
{ |
| 1305 |
HTStreamClass targetClass; |
| 1306 |
int status; |
| 1307 |
off_t bytes; |
| 1308 |
int bzerrnum; |
| 1309 |
int rv = HT_OK; |
| 1310 |
|
| 1311 |
/* Push the data down the stream |
| 1312 |
*/ |
| 1313 |
targetClass = *(sink->isa); /* Copy pointers to procedures */ |
| 1314 |
|
| 1315 |
/* read and inflate bzip'd file, and push binary down sink |
| 1316 |
*/ |
| 1317 |
HTReadProgress(bytes = 0, (off_t) 0); |
| 1318 |
for (;;) { |
| 1319 |
status = BZ2_bzread(bzfp, input_buffer, INPUT_BUFFER_SIZE); |
| 1320 |
if (status <= 0) { /* EOF or error */ |
| 1321 |
if (status == 0) { |
| 1322 |
rv = HT_LOADED; |
| 1323 |
break; |
| 1324 |
} |
| 1325 |
CTRACE((tfp, "HTBzFileCopy: Read error, bzread returns %d\n", |
| 1326 |
status)); |
| 1327 |
CTRACE((tfp, "bzerror : %s\n", |
| 1328 |
BZ2_bzerror(bzfp, &bzerrnum))); |
| 1329 |
if (bytes) { |
| 1330 |
rv = HT_PARTIAL_CONTENT; |
| 1331 |
} else { |
| 1332 |
rv = -1; |
| 1333 |
} |
| 1334 |
break; |
| 1335 |
} |
| 1336 |
|
| 1337 |
(*targetClass.put_block) (sink, input_buffer, status); |
| 1338 |
bytes += status; |
| 1339 |
HTReadProgress(bytes, (off_t) -1); |
| 1340 |
HTDisplayPartial(); |
| 1341 |
|
| 1342 |
if (HTCheckForInterrupt()) { |
| 1343 |
_HTProgress(TRANSFER_INTERRUPTED); |
| 1344 |
rv = HT_INTERRUPTED; |
| 1345 |
break; |
| 1346 |
} |
| 1347 |
} /* next bufferload */ |
| 1348 |
|
| 1349 |
HTFinishDisplayPartial(); |
| 1350 |
return rv; |
| 1351 |
} |
| 1352 |
#endif /* USE_BZLIB */ |
| 1353 |
|
| 1354 |
#ifdef USE_BROTLI |
| 1355 |
/* Push data from a brotli file pointer down a stream |
| 1356 |
* ------------------------------------- |
| 1357 |
* |
| 1358 |
* This routine is responsible for creating and PRESENTING any |
| 1359 |
* graphic (or other) objects described by the file. |
| 1360 |
* |
| 1361 |
* |
| 1362 |
* State of file and target stream on entry: |
| 1363 |
* BZFILE (bzfp) assumed open (should have bzipped content), |
| 1364 |
* target (sink) assumed valid. |
| 1365 |
* |
| 1366 |
* Return values: |
| 1367 |
* HT_INTERRUPTED Interruption after some data read. |
| 1368 |
* HT_PARTIAL_CONTENT Error after some data read. |
| 1369 |
* -1 Error before any data read. |
| 1370 |
* HT_LOADED Normal end of file indication on reading. |
| 1371 |
* |
| 1372 |
* State of file and target stream on return: |
| 1373 |
* always bzfp still open, target stream still valid. |
| 1374 |
*/ |
| 1375 |
static int HTBrFileCopy(FILE *brfp, HTStream *sink) |
| 1376 |
{ |
| 1377 |
#undef THIS_FUNC |
| 1378 |
#define THIS_FUNC "HTBrFileCopy" |
| 1379 |
HTStreamClass targetClass; |
| 1380 |
int status; |
| 1381 |
off_t bytes; |
| 1382 |
int rv = HT_OK; |
| 1383 |
BrotliDecoderResult status2 = BROTLI_DECODER_RESULT_ERROR; |
| 1384 |
|
| 1385 |
char *brotli_buffer = NULL; |
| 1386 |
char *normal_buffer = NULL; |
| 1387 |
char *checks_buffer; |
| 1388 |
size_t brotli_size; |
| 1389 |
size_t brotli_limit = 0; |
| 1390 |
size_t brotli_offset; |
| 1391 |
size_t normal_size; |
| 1392 |
size_t normal_limit = 0; |
| 1393 |
|
| 1394 |
/* Push the data down the stream |
| 1395 |
*/ |
| 1396 |
targetClass = *(sink->isa); /* Copy pointers to procedures */ |
| 1397 |
|
| 1398 |
/* read and inflate brotli'd file, and push binary down sink |
| 1399 |
*/ |
| 1400 |
HTReadProgress(bytes = 0, (off_t) 0); |
| 1401 |
/* |
| 1402 |
* first, read all of the brotli'd file into memory, to work with the |
| 1403 |
* library's limitations. |
| 1404 |
*/ |
| 1405 |
for (;;) { |
| 1406 |
size_t input_chunk = INPUT_BUFFER_SIZE; |
| 1407 |
|
| 1408 |
brotli_offset = brotli_limit; |
| 1409 |
brotli_limit += input_chunk; |
| 1410 |
checks_buffer = realloc(brotli_buffer, brotli_limit); |
| 1411 |
if (checks_buffer == NULL) |
| 1412 |
outofmem(__FILE__, THIS_FUNC); |
| 1413 |
brotli_buffer = checks_buffer; |
| 1414 |
status = (int) fread(brotli_buffer + brotli_offset, sizeof(char), |
| 1415 |
input_chunk, brfp); |
| 1416 |
|
| 1417 |
if (status <= 0) { /* EOF or error */ |
| 1418 |
if (status == 0) { |
| 1419 |
rv = HT_LOADED; |
| 1420 |
break; |
| 1421 |
} |
| 1422 |
CTRACE((tfp, THIS_FUNC ": Read error, fread returns %d\n", status)); |
| 1423 |
if (bytes) { |
| 1424 |
if (!feof(brfp)) |
| 1425 |
rv = HT_PARTIAL_CONTENT; |
| 1426 |
} else { |
| 1427 |
rv = -1; |
| 1428 |
} |
| 1429 |
break; |
| 1430 |
} |
| 1431 |
bytes += status; |
| 1432 |
} |
| 1433 |
|
| 1434 |
/* |
| 1435 |
* next, unless we encountered an error (and have no data), try |
| 1436 |
* decompressing with increasing output buffer sizes until the brotli |
| 1437 |
* library succeeds. |
| 1438 |
*/ |
| 1439 |
if (bytes > 0) { |
| 1440 |
do { |
| 1441 |
if (normal_limit == 0) |
| 1442 |
normal_limit = (10 * brotli_limit) + INPUT_BUFFER_SIZE; |
| 1443 |
else |
| 1444 |
normal_limit *= 2; |
| 1445 |
checks_buffer = realloc(normal_buffer, normal_limit); |
| 1446 |
if (checks_buffer == NULL) |
| 1447 |
outofmem(__FILE__, THIS_FUNC); |
| 1448 |
normal_buffer = checks_buffer; |
| 1449 |
brotli_size = (size_t) bytes; |
| 1450 |
normal_size = normal_limit; |
| 1451 |
status2 = BrotliDecoderDecompress(brotli_size, |
| 1452 |
(uint8_t *) brotli_buffer, |
| 1453 |
&normal_size, |
| 1454 |
(uint8_t *) normal_buffer); |
| 1455 |
/* |
| 1456 |
* brotli library should return |
| 1457 |
* BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT, |
| 1458 |
* but actually returns |
| 1459 |
* BROTLI_DECODER_RESULT_ERROR |
| 1460 |
* |
| 1461 |
* Accommodate possible improvements... |
| 1462 |
*/ |
| 1463 |
} while (status2 != BROTLI_DECODER_RESULT_SUCCESS); |
| 1464 |
} |
| 1465 |
|
| 1466 |
/* |
| 1467 |
* finally, pump that data into the output stream. |
| 1468 |
*/ |
| 1469 |
if (status2 == BROTLI_DECODER_RESULT_SUCCESS) { |
| 1470 |
CTRACE((tfp, THIS_FUNC ": decompressed %lu -> %lu (1:%.1f)\n", |
| 1471 |
(unsigned long) brotli_size, |
| 1472 |
(unsigned long) normal_size, |
| 1473 |
(double) normal_size / (double) brotli_size)); |
| 1474 |
(*targetClass.put_block) (sink, normal_buffer, (int) normal_size); |
| 1475 |
bytes += status; |
| 1476 |
HTReadProgress(bytes, (off_t) -1); |
| 1477 |
HTDisplayPartial(); |
| 1478 |
|
| 1479 |
if (HTCheckForInterrupt()) { |
| 1480 |
_HTProgress(TRANSFER_INTERRUPTED); |
| 1481 |
rv = HT_INTERRUPTED; |
| 1482 |
} |
| 1483 |
} |
| 1484 |
free(brotli_buffer); |
| 1485 |
free(normal_buffer); |
| 1486 |
|
| 1487 |
/* next bufferload */ |
| 1488 |
HTFinishDisplayPartial(); |
| 1489 |
return rv; |
| 1490 |
#undef THIS_FUNC |
| 1491 |
} |
| 1492 |
#endif /* USE_BROTLI */ |
| 1493 |
|
| 1494 |
#ifdef USE_ZSTD |
| 1495 |
/* Push data from a zstd file pointer down a stream |
| 1496 |
* ------------------------------------- |
| 1497 |
* |
| 1498 |
* This routine is responsible for creating and PRESENTING any |
| 1499 |
* graphic (or other) objects described by the file. |
| 1500 |
* |
| 1501 |
* |
| 1502 |
* State of file and target stream on entry: |
| 1503 |
* ZFILE (zfp) assumed open (should have zstd content), |
| 1504 |
* target (sink) assumed valid. |
| 1505 |
* |
| 1506 |
* Return values: |
| 1507 |
* HT_INTERRUPTED Interruption after some data read. |
| 1508 |
* HT_PARTIAL_CONTENT Error after some data read. |
| 1509 |
* -1 Error before any data read. |
| 1510 |
* HT_LOADED Normal end of file indication on reading. |
| 1511 |
* |
| 1512 |
* State of file and target stream on return: |
| 1513 |
* always zfp still open, target stream still valid. |
| 1514 |
*/ |
| 1515 |
static int HTZstdFileCopy(FILE *zfp, HTStream *sink) |
| 1516 |
{ |
| 1517 |
off_t bytes; |
| 1518 |
HTStreamClass targetClass; |
| 1519 |
ZSTD_DStream *izsp; |
| 1520 |
void *ibp, *obp; |
| 1521 |
size_t ibl, obl; |
| 1522 |
int rv; |
| 1523 |
|
| 1524 |
rv = -1; |
| 1525 |
|
| 1526 |
ibl = ZSTD_DStreamInSize(); |
| 1527 |
ibp = malloc(ibl); |
| 1528 |
if (ibp == NULL) |
| 1529 |
goto jout0; |
| 1530 |
|
| 1531 |
obl = ZSTD_DStreamOutSize(); |
| 1532 |
obp = malloc(obl); |
| 1533 |
if (obp == NULL) |
| 1534 |
goto jout1; |
| 1535 |
|
| 1536 |
if ((izsp = ZSTD_createDStream()) == NULL) |
| 1537 |
goto jout2; |
| 1538 |
ZSTD_initDStream(izsp); |
| 1539 |
|
| 1540 |
/* Push the data down the stream */ |
| 1541 |
targetClass = *(sink->isa); /* Copy pointers to procedures */ |
| 1542 |
|
| 1543 |
/* read and inflate file, and push binary down sink */ |
| 1544 |
HTReadProgress(bytes = 0, (off_t) 0); |
| 1545 |
|
| 1546 |
for (;;) { |
| 1547 |
ZSTD_outBuffer ob; |
| 1548 |
ZSTD_inBuffer ib; |
| 1549 |
size_t r; |
| 1550 |
|
| 1551 |
r = fread(ibp, 1, ibl, zfp); |
| 1552 |
if (r == 0) { |
| 1553 |
if (feof(zfp)) |
| 1554 |
rv = HT_LOADED; |
| 1555 |
else if (bytes > 0) |
| 1556 |
rv = HT_PARTIAL_CONTENT; |
| 1557 |
break; |
| 1558 |
} |
| 1559 |
|
| 1560 |
ib.src = ibp; |
| 1561 |
ib.size = r; |
| 1562 |
ib.pos = 0; |
| 1563 |
ob.dst = obp; |
| 1564 |
ob.size = obl; |
| 1565 |
ob.pos = 0; |
| 1566 |
|
| 1567 |
r = ZSTD_decompressStream(izsp, &ob, &ib); |
| 1568 |
if (ZSTD_isError(r)) { |
| 1569 |
if (bytes > 0) |
| 1570 |
rv = HT_PARTIAL_CONTENT; |
| 1571 |
break; |
| 1572 |
} |
| 1573 |
|
| 1574 |
(*targetClass.put_block) (sink, ob.dst, (int) ob.pos); |
| 1575 |
bytes += (off_t) ob.pos; |
| 1576 |
HTReadProgress(bytes, (off_t) -1); |
| 1577 |
HTDisplayPartial(); |
| 1578 |
|
| 1579 |
if (HTCheckForInterrupt()) { |
| 1580 |
_HTProgress(TRANSFER_INTERRUPTED); |
| 1581 |
rv = HT_INTERRUPTED; |
| 1582 |
break; |
| 1583 |
} |
| 1584 |
} /* next bufferload */ |
| 1585 |
|
| 1586 |
ZSTD_freeDStream(izsp); |
| 1587 |
jout2: |
| 1588 |
free(obp); |
| 1589 |
jout1: |
| 1590 |
free(ibp); |
| 1591 |
jout0: |
| 1592 |
HTFinishDisplayPartial(); |
| 1593 |
return rv; |
| 1594 |
} |
| 1595 |
#endif /* USE_ZSTD */ |
| 1596 |
|
| 1597 |
/* Push data from a socket down a stream STRIPPING CR |
| 1598 |
* -------------------------------------------------- |
| 1599 |
* |
| 1600 |
* This routine is responsible for creating and PRESENTING any |
| 1601 |
* graphic (or other) objects described by the socket. |
| 1602 |
* |
| 1603 |
* The file number given is assumed to be a TELNET stream ie containing |
| 1604 |
* CRLF at the end of lines which need to be stripped to LF for unix |
| 1605 |
* when the format is textual. |
| 1606 |
* |
| 1607 |
*/ |
| 1608 |
void HTCopyNoCR(HTParentAnchor *anchor GCC_UNUSED, |
| 1609 |
int file_number, |
| 1610 |
HTStream *sink) |
| 1611 |
{ |
| 1612 |
HTStreamClass targetClass; |
| 1613 |
int character; |
| 1614 |
|
| 1615 |
/* Push the data, ignoring CRLF, down the stream |
| 1616 |
*/ |
| 1617 |
targetClass = *(sink->isa); /* Copy pointers to procedures */ |
| 1618 |
|
| 1619 |
/* |
| 1620 |
* Push text from telnet socket down sink |
| 1621 |
* |
| 1622 |
* @@@@@ To push strings could be faster? (especially is we cheat and |
| 1623 |
* don't ignore CR! :-} |
| 1624 |
*/ |
| 1625 |
HTInitInput(file_number); |
| 1626 |
for (;;) { |
| 1627 |
character = HTGetCharacter(); |
| 1628 |
if (character == EOF) |
| 1629 |
break; |
| 1630 |
(*targetClass.put_character) (sink, (char) character); |
| 1631 |
} |
| 1632 |
} |
| 1633 |
|
| 1634 |
/* Parse a socket given format and file number |
| 1635 |
* |
| 1636 |
* This routine is responsible for creating and PRESENTING any |
| 1637 |
* graphic (or other) objects described by the file. |
| 1638 |
* |
| 1639 |
* The file number given is assumed to be a TELNET stream ie containing |
| 1640 |
* CRLF at the end of lines which need to be stripped to LF for unix |
| 1641 |
* when the format is textual. |
| 1642 |
* |
| 1643 |
* State of socket and target stream on entry: |
| 1644 |
* socket (file_number) assumed open, |
| 1645 |
* target (sink) usually NULL (will call stream stack). |
| 1646 |
* |
| 1647 |
* Return values: |
| 1648 |
* HT_INTERRUPTED Interruption or error after some data received. |
| 1649 |
* -501 Stream stack failed (cannot present or convert). |
| 1650 |
* -2 Unexpected disconnect before any data received. |
| 1651 |
* -1 Stream stack failed (cannot present or convert), or |
| 1652 |
* Interruption or error before any data received, or |
| 1653 |
* (UNIX) other read error before any data received, or |
| 1654 |
* download cancelled. |
| 1655 |
* HT_LOADED Normal close of socket (end of file indication |
| 1656 |
* received), or |
| 1657 |
* unexpected disconnect after some data received, or |
| 1658 |
* other read error after some data received, or |
| 1659 |
* (not UNIX) other read error before any data received. |
| 1660 |
* |
| 1661 |
* State of socket and target stream on return depends on return value: |
| 1662 |
* HT_INTERRUPTED socket still open, target aborted. |
| 1663 |
* -501 socket still open, target stream NULL. |
| 1664 |
* -2 socket still open, target freed. |
| 1665 |
* -1 socket still open, target stream aborted or NULL. |
| 1666 |
* otherwise socket closed, target stream freed. |
| 1667 |
*/ |
| 1668 |
int HTParseSocket(HTFormat rep_in, |
| 1669 |
HTFormat format_out, |
| 1670 |
HTParentAnchor *anchor, |
| 1671 |
int file_number, |
| 1672 |
HTStream *sink) |
| 1673 |
{ |
| 1674 |
HTStream *stream; |
| 1675 |
HTStreamClass targetClass; |
| 1676 |
int rv; |
| 1677 |
|
| 1678 |
stream = HTStreamStack(rep_in, format_out, sink, anchor); |
| 1679 |
|
| 1680 |
if (!stream) { |
| 1681 |
char *buffer = NULL; |
| 1682 |
|
| 1683 |
if (LYCancelDownload) { |
| 1684 |
LYCancelDownload = FALSE; |
| 1685 |
return -1; |
| 1686 |
} |
| 1687 |
HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O, |
| 1688 |
HTAtom_name(rep_in), HTAtom_name(format_out)); |
| 1689 |
CTRACE((tfp, "HTFormat: %s\n", buffer)); |
| 1690 |
rv = HTLoadError(sink, 501, buffer); /* returns -501 */ |
| 1691 |
FREE(buffer); |
| 1692 |
} else { |
| 1693 |
/* |
| 1694 |
* Push the data, don't worry about CRLF we can strip them later. |
| 1695 |
*/ |
| 1696 |
targetClass = *(stream->isa); /* Copy pointers to procedures */ |
| 1697 |
rv = HTCopy(anchor, file_number, NULL, stream); |
| 1698 |
if (rv != -1 && rv != HT_INTERRUPTED) |
| 1699 |
(*targetClass._free) (stream); |
| 1700 |
} |
| 1701 |
return rv; |
| 1702 |
/* Originally: full: HT_LOADED; partial: HT_INTERRUPTED; no bytes: -1 */ |
| 1703 |
} |
| 1704 |
|
| 1705 |
/* Parse a file given format and file pointer |
| 1706 |
* |
| 1707 |
* This routine is responsible for creating and PRESENTING any |
| 1708 |
* graphic (or other) objects described by the file. |
| 1709 |
* |
| 1710 |
* The file number given is assumed to be a TELNET stream ie containing |
| 1711 |
* CRLF at the end of lines which need to be stripped to \n for unix |
| 1712 |
* when the format is textual. |
| 1713 |
* |
| 1714 |
* State of file and target stream on entry: |
| 1715 |
* FILE* (fp) assumed open, |
| 1716 |
* target (sink) usually NULL (will call stream stack). |
| 1717 |
* |
| 1718 |
* Return values: |
| 1719 |
* -501 Stream stack failed (cannot present or convert). |
| 1720 |
* -1 Download cancelled. |
| 1721 |
* HT_NO_DATA Error before any data read. |
| 1722 |
* HT_PARTIAL_CONTENT Interruption or error after some data read. |
| 1723 |
* HT_LOADED Normal end of file indication on reading. |
| 1724 |
* |
| 1725 |
* State of file and target stream on return: |
| 1726 |
* always fp still open; target freed, aborted, or NULL. |
| 1727 |
*/ |
| 1728 |
int HTParseFile(HTFormat rep_in, |
| 1729 |
HTFormat format_out, |
| 1730 |
HTParentAnchor *anchor, |
| 1731 |
FILE *fp, |
| 1732 |
HTStream *sink) |
| 1733 |
{ |
| 1734 |
HTStream *stream; |
| 1735 |
HTStreamClass targetClass; |
| 1736 |
int rv; |
| 1737 |
int result; |
| 1738 |
|
| 1739 |
if (fp == NULL) { |
| 1740 |
result = HT_LOADED; |
| 1741 |
} else { |
| 1742 |
stream = HTStreamStack(rep_in, format_out, sink, anchor); |
| 1743 |
|
| 1744 |
if (!stream || !stream->isa) { |
| 1745 |
char *buffer = NULL; |
| 1746 |
|
| 1747 |
if (LYCancelDownload) { |
| 1748 |
LYCancelDownload = FALSE; |
| 1749 |
result = -1; |
| 1750 |
} else { |
| 1751 |
HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O, |
| 1752 |
HTAtom_name(rep_in), HTAtom_name(format_out)); |
| 1753 |
CTRACE((tfp, "HTFormat(in HTParseFile): %s\n", buffer)); |
| 1754 |
rv = HTLoadError(sink, 501, buffer); |
| 1755 |
FREE(buffer); |
| 1756 |
result = rv; |
| 1757 |
} |
| 1758 |
} else { |
| 1759 |
|
| 1760 |
/* |
| 1761 |
* Push the data down the stream |
| 1762 |
* |
| 1763 |
* @@ Bug: This decision ought to be made based on "encoding" |
| 1764 |
* rather than on content-type. @@@ When we handle encoding. The |
| 1765 |
* current method smells anyway. |
| 1766 |
*/ |
| 1767 |
targetClass = *(stream->isa); /* Copy pointers to procedures */ |
| 1768 |
rv = HTFileCopy(fp, stream); |
| 1769 |
if (rv == -1 || rv == HT_INTERRUPTED) { |
| 1770 |
(*targetClass._abort) (stream, NULL); |
| 1771 |
} else { |
| 1772 |
(*targetClass._free) (stream); |
| 1773 |
} |
| 1774 |
|
| 1775 |
if (rv == -1) { |
| 1776 |
result = HT_NO_DATA; |
| 1777 |
} else if (rv == HT_INTERRUPTED || (rv > 0 && rv != HT_LOADED)) { |
| 1778 |
result = HT_PARTIAL_CONTENT; |
| 1779 |
} else { |
| 1780 |
result = HT_LOADED; |
| 1781 |
} |
| 1782 |
} |
| 1783 |
} |
| 1784 |
return result; |
| 1785 |
} |
| 1786 |
|
| 1787 |
#ifdef USE_SOURCE_CACHE |
| 1788 |
/* Parse a document in memory given format and memory block pointer |
| 1789 |
* |
| 1790 |
* This routine is responsible for creating and PRESENTING any |
| 1791 |
* graphic (or other) objects described by the file. |
| 1792 |
* |
| 1793 |
* State of memory and target stream on entry: |
| 1794 |
* HTChunk* (chunk) assumed valid, |
| 1795 |
* target (sink) usually NULL (will call stream stack). |
| 1796 |
* |
| 1797 |
* Return values: |
| 1798 |
* -501 Stream stack failed (cannot present or convert). |
| 1799 |
* HT_LOADED All data sent. |
| 1800 |
* |
| 1801 |
* State of memory and target stream on return: |
| 1802 |
* always chunk unchanged; target freed, aborted, or NULL. |
| 1803 |
*/ |
| 1804 |
int HTParseMem(HTFormat rep_in, |
| 1805 |
HTFormat format_out, |
| 1806 |
HTParentAnchor *anchor, |
| 1807 |
HTChunk *chunk, |
| 1808 |
HTStream *sink) |
| 1809 |
{ |
| 1810 |
HTStream *stream; |
| 1811 |
HTStreamClass targetClass; |
| 1812 |
int rv; |
| 1813 |
int result; |
| 1814 |
|
| 1815 |
stream = HTStreamStack(rep_in, format_out, sink, anchor); |
| 1816 |
if (!stream || !stream->isa) { |
| 1817 |
char *buffer = NULL; |
| 1818 |
|
| 1819 |
HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O, |
| 1820 |
HTAtom_name(rep_in), HTAtom_name(format_out)); |
| 1821 |
CTRACE((tfp, "HTFormat(in HTParseMem): %s\n", buffer)); |
| 1822 |
rv = HTLoadError(sink, 501, buffer); |
| 1823 |
FREE(buffer); |
| 1824 |
result = rv; |
| 1825 |
} else { |
| 1826 |
|
| 1827 |
/* Push the data down the stream |
| 1828 |
*/ |
| 1829 |
targetClass = *(stream->isa); |
| 1830 |
(void) HTMemCopy(chunk, stream); |
| 1831 |
(*targetClass._free) (stream); |
| 1832 |
result = HT_LOADED; |
| 1833 |
} |
| 1834 |
return result; |
| 1835 |
} |
| 1836 |
#endif |
| 1837 |
|
| 1838 |
#ifdef USE_ZLIB |
| 1839 |
static int HTCloseGzFile(gzFile gzfp) |
| 1840 |
{ |
| 1841 |
int gzres; |
| 1842 |
|
| 1843 |
if (gzfp == NULL) |
| 1844 |
return 0; |
| 1845 |
gzres = gzclose(gzfp); |
| 1846 |
if (TRACE) { |
| 1847 |
if (gzres == Z_ERRNO) { |
| 1848 |
perror("gzclose "); |
| 1849 |
} else if (gzres != Z_OK) { |
| 1850 |
CTRACE((tfp, "gzclose : error number %d\n", gzres)); |
| 1851 |
} |
| 1852 |
} |
| 1853 |
return (gzres); |
| 1854 |
} |
| 1855 |
|
| 1856 |
/* HTParseGzFile |
| 1857 |
* |
| 1858 |
* State of file and target stream on entry: |
| 1859 |
* gzFile (gzfp) assumed open, |
| 1860 |
* target (sink) usually NULL (will call stream stack). |
| 1861 |
* |
| 1862 |
* Return values: |
| 1863 |
* -501 Stream stack failed (cannot present or convert). |
| 1864 |
* -1 Download cancelled. |
| 1865 |
* HT_NO_DATA Error before any data read. |
| 1866 |
* HT_PARTIAL_CONTENT Interruption or error after some data read. |
| 1867 |
* HT_LOADED Normal end of file indication on reading. |
| 1868 |
* |
| 1869 |
* State of file and target stream on return: |
| 1870 |
* always gzfp closed; target freed, aborted, or NULL. |
| 1871 |
*/ |
| 1872 |
int HTParseGzFile(HTFormat rep_in, |
| 1873 |
HTFormat format_out, |
| 1874 |
HTParentAnchor *anchor, |
| 1875 |
gzFile gzfp, |
| 1876 |
HTStream *sink) |
| 1877 |
{ |
| 1878 |
HTStream *stream; |
| 1879 |
HTStreamClass targetClass; |
| 1880 |
int rv; |
| 1881 |
int result; |
| 1882 |
|
| 1883 |
stream = HTStreamStack(rep_in, format_out, sink, anchor); |
| 1884 |
|
| 1885 |
if (!stream || !stream->isa) { |
| 1886 |
char *buffer = NULL; |
| 1887 |
|
| 1888 |
HTCloseGzFile(gzfp); |
| 1889 |
if (LYCancelDownload) { |
| 1890 |
LYCancelDownload = FALSE; |
| 1891 |
result = -1; |
| 1892 |
} else { |
| 1893 |
HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O, |
| 1894 |
HTAtom_name(rep_in), HTAtom_name(format_out)); |
| 1895 |
CTRACE((tfp, "HTFormat(in HTParseGzFile): %s\n", buffer)); |
| 1896 |
rv = HTLoadError(sink, 501, buffer); |
| 1897 |
FREE(buffer); |
| 1898 |
result = rv; |
| 1899 |
} |
| 1900 |
} else { |
| 1901 |
|
| 1902 |
/* |
| 1903 |
* Push the data down the stream |
| 1904 |
* |
| 1905 |
* @@ Bug: This decision ought to be made based on "encoding" rather than |
| 1906 |
* on content-type. @@@ When we handle encoding. The current method |
| 1907 |
* smells anyway. |
| 1908 |
*/ |
| 1909 |
targetClass = *(stream->isa); /* Copy pointers to procedures */ |
| 1910 |
rv = HTGzFileCopy(gzfp, stream); |
| 1911 |
if (rv == -1 || rv == HT_INTERRUPTED) { |
| 1912 |
(*targetClass._abort) (stream, NULL); |
| 1913 |
} else { |
| 1914 |
(*targetClass._free) (stream); |
| 1915 |
} |
| 1916 |
|
| 1917 |
HTCloseGzFile(gzfp); |
| 1918 |
if (rv == -1) { |
| 1919 |
result = HT_NO_DATA; |
| 1920 |
} else if (rv == HT_INTERRUPTED || (rv > 0 && rv != HT_LOADED)) { |
| 1921 |
result = HT_PARTIAL_CONTENT; |
| 1922 |
} else { |
| 1923 |
result = HT_LOADED; |
| 1924 |
} |
| 1925 |
} |
| 1926 |
return result; |
| 1927 |
} |
| 1928 |
|
| 1929 |
/* HTParseZzFile |
| 1930 |
* |
| 1931 |
* State of file and target stream on entry: |
| 1932 |
* FILE (zzfp) assumed open, |
| 1933 |
* target (sink) usually NULL (will call stream stack). |
| 1934 |
* |
| 1935 |
* Return values: |
| 1936 |
* -501 Stream stack failed (cannot present or convert). |
| 1937 |
* -1 Download cancelled. |
| 1938 |
* HT_NO_DATA Error before any data read. |
| 1939 |
* HT_PARTIAL_CONTENT Interruption or error after some data read. |
| 1940 |
* HT_LOADED Normal end of file indication on reading. |
| 1941 |
* |
| 1942 |
* State of file and target stream on return: |
| 1943 |
* always zzfp closed; target freed, aborted, or NULL. |
| 1944 |
*/ |
| 1945 |
int HTParseZzFile(HTFormat rep_in, |
| 1946 |
HTFormat format_out, |
| 1947 |
HTParentAnchor *anchor, |
| 1948 |
FILE *zzfp, |
| 1949 |
HTStream *sink) |
| 1950 |
{ |
| 1951 |
HTStream *stream; |
| 1952 |
HTStreamClass targetClass; |
| 1953 |
int rv; |
| 1954 |
int result; |
| 1955 |
|
| 1956 |
stream = HTStreamStack(rep_in, format_out, sink, anchor); |
| 1957 |
|
| 1958 |
if (!stream || !stream->isa) { |
| 1959 |
char *buffer = NULL; |
| 1960 |
|
| 1961 |
fclose(zzfp); |
| 1962 |
if (LYCancelDownload) { |
| 1963 |
LYCancelDownload = FALSE; |
| 1964 |
result = -1; |
| 1965 |
} else { |
| 1966 |
HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O, |
| 1967 |
HTAtom_name(rep_in), HTAtom_name(format_out)); |
| 1968 |
CTRACE((tfp, "HTFormat(in HTParseGzFile): %s\n", buffer)); |
| 1969 |
rv = HTLoadError(sink, 501, buffer); |
| 1970 |
FREE(buffer); |
| 1971 |
result = rv; |
| 1972 |
} |
| 1973 |
} else { |
| 1974 |
|
| 1975 |
/* |
| 1976 |
* Push the data down the stream |
| 1977 |
* |
| 1978 |
* @@ Bug: This decision ought to be made based on "encoding" rather than |
| 1979 |
* on content-type. @@@ When we handle encoding. The current method |
| 1980 |
* smells anyway. |
| 1981 |
*/ |
| 1982 |
targetClass = *(stream->isa); /* Copy pointers to procedures */ |
| 1983 |
rv = HTZzFileCopy(zzfp, stream); |
| 1984 |
if (rv == -1 || rv == HT_INTERRUPTED) { |
| 1985 |
(*targetClass._abort) (stream, NULL); |
| 1986 |
} else { |
| 1987 |
(*targetClass._free) (stream); |
| 1988 |
} |
| 1989 |
|
| 1990 |
fclose(zzfp); |
| 1991 |
if (rv == -1) { |
| 1992 |
result = HT_NO_DATA; |
| 1993 |
} else if (rv == HT_INTERRUPTED || (rv > 0 && rv != HT_LOADED)) { |
| 1994 |
result = HT_PARTIAL_CONTENT; |
| 1995 |
} else { |
| 1996 |
result = HT_LOADED; |
| 1997 |
} |
| 1998 |
} |
| 1999 |
return result; |
| 2000 |
} |
| 2001 |
#endif /* USE_ZLIB */ |
| 2002 |
|
| 2003 |
#ifdef USE_BZLIB |
| 2004 |
static void HTCloseBzFile(BZFILE * bzfp) |
| 2005 |
{ |
| 2006 |
if (bzfp) |
| 2007 |
BZ2_bzclose(bzfp); |
| 2008 |
} |
| 2009 |
|
| 2010 |
/* HTParseBzFile |
| 2011 |
* |
| 2012 |
* State of file and target stream on entry: |
| 2013 |
* bzFile (bzfp) assumed open, |
| 2014 |
* target (sink) usually NULL (will call stream stack). |
| 2015 |
* |
| 2016 |
* Return values: |
| 2017 |
* -501 Stream stack failed (cannot present or convert). |
| 2018 |
* -1 Download cancelled. |
| 2019 |
* HT_NO_DATA Error before any data read. |
| 2020 |
* HT_PARTIAL_CONTENT Interruption or error after some data read. |
| 2021 |
* HT_LOADED Normal end of file indication on reading. |
| 2022 |
* |
| 2023 |
* State of file and target stream on return: |
| 2024 |
* always bzfp closed; target freed, aborted, or NULL. |
| 2025 |
*/ |
| 2026 |
int HTParseBzFile(HTFormat rep_in, |
| 2027 |
HTFormat format_out, |
| 2028 |
HTParentAnchor *anchor, |
| 2029 |
BZFILE * bzfp, |
| 2030 |
HTStream *sink) |
| 2031 |
{ |
| 2032 |
HTStream *stream; |
| 2033 |
HTStreamClass targetClass; |
| 2034 |
int rv; |
| 2035 |
int result; |
| 2036 |
|
| 2037 |
stream = HTStreamStack(rep_in, format_out, sink, anchor); |
| 2038 |
|
| 2039 |
if (!stream || !stream->isa) { |
| 2040 |
char *buffer = NULL; |
| 2041 |
|
| 2042 |
HTCloseBzFile(bzfp); |
| 2043 |
if (LYCancelDownload) { |
| 2044 |
LYCancelDownload = FALSE; |
| 2045 |
result = -1; |
| 2046 |
} else { |
| 2047 |
HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O, |
| 2048 |
HTAtom_name(rep_in), HTAtom_name(format_out)); |
| 2049 |
CTRACE((tfp, "HTFormat(in HTParseBzFile): %s\n", buffer)); |
| 2050 |
rv = HTLoadError(sink, 501, buffer); |
| 2051 |
FREE(buffer); |
| 2052 |
result = rv; |
| 2053 |
} |
| 2054 |
} else { |
| 2055 |
|
| 2056 |
/* |
| 2057 |
* Push the data down the stream |
| 2058 |
* |
| 2059 |
* @@ Bug: This decision ought to be made based on "encoding" rather than |
| 2060 |
* on content-type. @@@ When we handle encoding. The current method |
| 2061 |
* smells anyway. |
| 2062 |
*/ |
| 2063 |
targetClass = *(stream->isa); /* Copy pointers to procedures */ |
| 2064 |
rv = HTBzFileCopy(bzfp, stream); |
| 2065 |
if (rv == -1 || rv == HT_INTERRUPTED) { |
| 2066 |
(*targetClass._abort) (stream, NULL); |
| 2067 |
} else { |
| 2068 |
(*targetClass._free) (stream); |
| 2069 |
} |
| 2070 |
|
| 2071 |
HTCloseBzFile(bzfp); |
| 2072 |
if (rv == -1) { |
| 2073 |
result = HT_NO_DATA; |
| 2074 |
} else if (rv == HT_INTERRUPTED || (rv > 0 && rv != HT_LOADED)) { |
| 2075 |
result = HT_PARTIAL_CONTENT; |
| 2076 |
} else { |
| 2077 |
result = HT_LOADED; |
| 2078 |
} |
| 2079 |
} |
| 2080 |
return result; |
| 2081 |
} |
| 2082 |
#endif /* USE_BZLIB */ |
| 2083 |
|
| 2084 |
#ifdef USE_BROTLI |
| 2085 |
/* HTParseBrFile |
| 2086 |
* |
| 2087 |
* State of file and target stream on entry: |
| 2088 |
* FILE* (brfp) assumed open, |
| 2089 |
* target (sink) usually NULL (will call stream stack). |
| 2090 |
* |
| 2091 |
* Return values: |
| 2092 |
* -501 Stream stack failed (cannot present or convert). |
| 2093 |
* -1 Download cancelled. |
| 2094 |
* HT_NO_DATA Error before any data read. |
| 2095 |
* HT_PARTIAL_CONTENT Interruption or error after some data read. |
| 2096 |
* HT_LOADED Normal end of file indication on reading. |
| 2097 |
* |
| 2098 |
* State of file and target stream on return: |
| 2099 |
* always brfp closed; target freed, aborted, or NULL. |
| 2100 |
*/ |
| 2101 |
int HTParseBrFile(HTFormat rep_in, |
| 2102 |
HTFormat format_out, |
| 2103 |
HTParentAnchor *anchor, |
| 2104 |
FILE *brfp, |
| 2105 |
HTStream *sink) |
| 2106 |
{ |
| 2107 |
#undef THIS_FUNC |
| 2108 |
#define THIS_FUNC "HTParseBrFile" |
| 2109 |
HTStream *stream; |
| 2110 |
HTStreamClass targetClass; |
| 2111 |
int rv; |
| 2112 |
int result; |
| 2113 |
|
| 2114 |
stream = HTStreamStack(rep_in, format_out, sink, anchor); |
| 2115 |
|
| 2116 |
if (!stream || !stream->isa) { |
| 2117 |
char *buffer = NULL; |
| 2118 |
|
| 2119 |
fclose(brfp); |
| 2120 |
if (LYCancelDownload) { |
| 2121 |
LYCancelDownload = FALSE; |
| 2122 |
result = -1; |
| 2123 |
} else { |
| 2124 |
HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O, |
| 2125 |
HTAtom_name(rep_in), HTAtom_name(format_out)); |
| 2126 |
CTRACE((tfp, "HTFormat(in " THIS_FUNC "): %s\n", buffer)); |
| 2127 |
rv = HTLoadError(sink, 501, buffer); |
| 2128 |
FREE(buffer); |
| 2129 |
result = rv; |
| 2130 |
} |
| 2131 |
} else { |
| 2132 |
|
| 2133 |
/* |
| 2134 |
* Push the data down the stream |
| 2135 |
* |
| 2136 |
* @@ Bug: This decision ought to be made based on "encoding" rather than |
| 2137 |
* on content-type. @@@ When we handle encoding. The current method |
| 2138 |
* smells anyway. |
| 2139 |
*/ |
| 2140 |
targetClass = *(stream->isa); /* Copy pointers to procedures */ |
| 2141 |
rv = HTBrFileCopy(brfp, stream); |
| 2142 |
if (rv == -1 || rv == HT_INTERRUPTED) { |
| 2143 |
(*targetClass._abort) (stream, NULL); |
| 2144 |
} else { |
| 2145 |
(*targetClass._free) (stream); |
| 2146 |
} |
| 2147 |
|
| 2148 |
fclose(brfp); |
| 2149 |
if (rv == -1) { |
| 2150 |
result = HT_NO_DATA; |
| 2151 |
} else if (rv == HT_INTERRUPTED || (rv > 0 && rv != HT_LOADED)) { |
| 2152 |
result = HT_PARTIAL_CONTENT; |
| 2153 |
} else { |
| 2154 |
result = HT_LOADED; |
| 2155 |
} |
| 2156 |
} |
| 2157 |
return result; |
| 2158 |
#undef THIS_FUNC |
| 2159 |
} |
| 2160 |
#endif /* USE_BROTLI */ |
| 2161 |
|
| 2162 |
#ifdef USE_ZSTD |
| 2163 |
/* HTParseZstdFile |
| 2164 |
* |
| 2165 |
* State of file and target stream on entry: |
| 2166 |
* FILE* (zfp) assumed open, |
| 2167 |
* target (sink) usually NULL (will call stream stack). |
| 2168 |
* |
| 2169 |
* Return values: |
| 2170 |
* -501 Stream stack failed (cannot present or convert). |
| 2171 |
* -1 Download cancelled. |
| 2172 |
* HT_NO_DATA Error before any data read. |
| 2173 |
* HT_PARTIAL_CONTENT Interruption or error after some data read. |
| 2174 |
* HT_LOADED Normal end of file indication on reading. |
| 2175 |
* |
| 2176 |
* State of file and target stream on return: |
| 2177 |
* always zfp closed; target freed, aborted, or NULL. |
| 2178 |
*/ |
| 2179 |
int HTParseZstdFile(HTFormat rep_in, |
| 2180 |
HTFormat format_out, |
| 2181 |
HTParentAnchor *anchor, |
| 2182 |
FILE *zfp, |
| 2183 |
HTStream *sink) |
| 2184 |
{ |
| 2185 |
HTStream *stream; |
| 2186 |
HTStreamClass targetClass; |
| 2187 |
int rv; |
| 2188 |
int result; |
| 2189 |
|
| 2190 |
stream = HTStreamStack(rep_in, format_out, sink, anchor); |
| 2191 |
|
| 2192 |
if (!stream || !stream->isa) { |
| 2193 |
char *buffer = NULL; |
| 2194 |
|
| 2195 |
fclose(zfp); |
| 2196 |
if (LYCancelDownload) { |
| 2197 |
LYCancelDownload = FALSE; |
| 2198 |
result = -1; |
| 2199 |
} else { |
| 2200 |
HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O, |
| 2201 |
HTAtom_name(rep_in), HTAtom_name(format_out)); |
| 2202 |
CTRACE((tfp, "HTFormat(in HTParseZstdFile): %s\n", buffer)); |
| 2203 |
rv = HTLoadError(sink, 501, buffer); |
| 2204 |
FREE(buffer); |
| 2205 |
result = rv; |
| 2206 |
} |
| 2207 |
} else { |
| 2208 |
/* |
| 2209 |
* Push the data down the stream |
| 2210 |
* |
| 2211 |
* @@ Bug: This decision ought to be made based on "encoding" rather than |
| 2212 |
* on content-type. @@@ When we handle encoding. The current method |
| 2213 |
* smells anyway. |
| 2214 |
*/ |
| 2215 |
targetClass = *(stream->isa); /* Copy pointers to procedures */ |
| 2216 |
rv = HTZstdFileCopy(zfp, stream); |
| 2217 |
if (rv == -1 || rv == HT_INTERRUPTED) { |
| 2218 |
(*targetClass._abort) (stream, NULL); |
| 2219 |
} else { |
| 2220 |
(*targetClass._free) (stream); |
| 2221 |
} |
| 2222 |
|
| 2223 |
fclose(zfp); |
| 2224 |
if (rv == -1) { |
| 2225 |
result = HT_NO_DATA; |
| 2226 |
} else if (rv == HT_INTERRUPTED || (rv > 0 && rv != HT_LOADED)) { |
| 2227 |
result = HT_PARTIAL_CONTENT; |
| 2228 |
} else { |
| 2229 |
result = HT_LOADED; |
| 2230 |
} |
| 2231 |
} |
| 2232 |
return result; |
| 2233 |
} |
| 2234 |
#endif /* USE_ZSTD */ |
| 2235 |
|
| 2236 |
/* Converter stream: Network Telnet to internal character text |
| 2237 |
* ----------------------------------------------------------- |
| 2238 |
* |
| 2239 |
* The input is assumed to be in ASCII, with lines delimited |
| 2240 |
* by (13,10) pairs, These pairs are converted into (CR,LF) |
| 2241 |
* pairs in the local representation. The (CR,LF) sequence |
| 2242 |
* when found is changed to a '\n' character, the internal |
| 2243 |
* C representation of a new line. |
| 2244 |
*/ |
| 2245 |
|
| 2246 |
static void NetToText_put_character(HTStream *me, int net_char) |
| 2247 |
{ |
| 2248 |
char c = (char) FROMASCII(net_char); |
| 2249 |
|
| 2250 |
if (me->had_cr) { |
| 2251 |
if (c == LF) { |
| 2252 |
me->sink->isa->put_character(me->sink, '\n'); /* Newline */ |
| 2253 |
me->had_cr = NO; |
| 2254 |
return; |
| 2255 |
} else { |
| 2256 |
me->sink->isa->put_character(me->sink, CR); /* leftover */ |
| 2257 |
} |
| 2258 |
} |
| 2259 |
me->had_cr = (BOOL) (c == CR); |
| 2260 |
if (!me->had_cr) |
| 2261 |
me->sink->isa->put_character(me->sink, c); /* normal */ |
| 2262 |
} |
| 2263 |
|
| 2264 |
static void NetToText_put_string(HTStream *me, const char *s) |
| 2265 |
{ |
| 2266 |
const char *p; |
| 2267 |
|
| 2268 |
for (p = s; *p; p++) |
| 2269 |
NetToText_put_character(me, *p); |
| 2270 |
} |
| 2271 |
|
| 2272 |
static void NetToText_put_block(HTStream *me, const char *s, int l) |
| 2273 |
{ |
| 2274 |
const char *p; |
| 2275 |
|
| 2276 |
for (p = s; p < (s + l); p++) |
| 2277 |
NetToText_put_character(me, *p); |
| 2278 |
} |
| 2279 |
|
| 2280 |
static void NetToText_free(HTStream *me) |
| 2281 |
{ |
| 2282 |
(me->sink->isa->_free) (me->sink); /* Close rest of pipe */ |
| 2283 |
FREE(me); |
| 2284 |
} |
| 2285 |
|
| 2286 |
static void NetToText_abort(HTStream *me, HTError e) |
| 2287 |
{ |
| 2288 |
me->sink->isa->_abort(me->sink, e); /* Abort rest of pipe */ |
| 2289 |
FREE(me); |
| 2290 |
} |
| 2291 |
|
| 2292 |
/* The class structure |
| 2293 |
*/ |
| 2294 |
static HTStreamClass NetToTextClass = |
| 2295 |
{ |
| 2296 |
"NetToText", |
| 2297 |
NetToText_free, |
| 2298 |
NetToText_abort, |
| 2299 |
NetToText_put_character, |
| 2300 |
NetToText_put_string, |
| 2301 |
NetToText_put_block |
| 2302 |
}; |
| 2303 |
|
| 2304 |
/* The creation method |
| 2305 |
*/ |
| 2306 |
HTStream *HTNetToText(HTStream *sink) |
| 2307 |
{ |
| 2308 |
HTStream *me = typecalloc(HTStream); |
| 2309 |
|
| 2310 |
if (me == NULL) |
| 2311 |
outofmem(__FILE__, "NetToText"); |
| 2312 |
|
| 2313 |
me->isa = &NetToTextClass; |
| 2314 |
|
| 2315 |
me->had_cr = NO; |
| 2316 |
me->sink = sink; |
| 2317 |
return me; |
| 2318 |
} |
| 2319 |
|
| 2320 |
static HTStream HTBaseStreamInstance; /* Made static */ |
| 2321 |
|
| 2322 |
/* |
| 2323 |
* ERROR STREAM |
| 2324 |
* ------------ |
| 2325 |
* There is only one error stream shared by anyone who wants a |
| 2326 |
* generic error returned from all stream methods. |
| 2327 |
*/ |
| 2328 |
static void HTErrorStream_put_character(HTStream *me GCC_UNUSED, int c GCC_UNUSED) |
| 2329 |
{ |
| 2330 |
LYCancelDownload = TRUE; |
| 2331 |
} |
| 2332 |
|
| 2333 |
static void HTErrorStream_put_string(HTStream *me GCC_UNUSED, const char *s) |
| 2334 |
{ |
| 2335 |
if (s && *s) |
| 2336 |
LYCancelDownload = TRUE; |
| 2337 |
} |
| 2338 |
|
| 2339 |
static void HTErrorStream_write(HTStream *me GCC_UNUSED, const char *s, int l) |
| 2340 |
{ |
| 2341 |
if (l && s) |
| 2342 |
LYCancelDownload = TRUE; |
| 2343 |
} |
| 2344 |
|
| 2345 |
static void HTErrorStream_free(HTStream *me GCC_UNUSED) |
| 2346 |
{ |
| 2347 |
return; |
| 2348 |
} |
| 2349 |
|
| 2350 |
static void HTErrorStream_abort(HTStream *me GCC_UNUSED, HTError e GCC_UNUSED) |
| 2351 |
{ |
| 2352 |
return; |
| 2353 |
} |
| 2354 |
|
| 2355 |
static const HTStreamClass HTErrorStreamClass = |
| 2356 |
{ |
| 2357 |
"ErrorStream", |
| 2358 |
HTErrorStream_free, |
| 2359 |
HTErrorStream_abort, |
| 2360 |
HTErrorStream_put_character, |
| 2361 |
HTErrorStream_put_string, |
| 2362 |
HTErrorStream_write |
| 2363 |
}; |
| 2364 |
|
| 2365 |
HTStream *HTErrorStream(void) |
| 2366 |
{ |
| 2367 |
CTRACE((tfp, "ErrorStream. Created\n")); |
| 2368 |
HTBaseStreamInstance.isa = &HTErrorStreamClass; /* The rest is random */ |
| 2369 |
return &HTBaseStreamInstance; |
| 2370 |
} |