HTFormat.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  1. /* Manage different file formats HTFormat.c
  2. * =============================
  3. *
  4. * Bugs:
  5. * Not reentrant.
  6. *
  7. * Assumes the incoming stream is ASCII, rather than a local file
  8. * format, and so ALWAYS converts from ASCII on non-ASCII machines.
  9. * Therefore, non-ASCII machines can't read local files.
  10. *
  11. */
  12. #include <HTUtils.h>
  13. /* Implements:
  14. */
  15. #include <HTFormat.h>
  16. static float HTMaxSecs = 1e10; /* No effective limit */
  17. #ifdef UNIX
  18. #ifdef NeXT
  19. #define PRESENT_POSTSCRIPT "open %s; /bin/rm -f %s\n"
  20. #else
  21. #define PRESENT_POSTSCRIPT "(ghostview %s ; /bin/rm -f %s)&\n"
  22. /* Full pathname would be better! */
  23. #endif /* NeXT */
  24. #endif /* UNIX */
  25. #include <HTML.h>
  26. #include <HTMLDTD.h>
  27. #include <HText.h>
  28. #include <HTAlert.h>
  29. #include <HTList.h>
  30. #include <HTInit.h>
  31. #include <HTTCP.h>
  32. #include <HTTP.h>
  33. /* Streams and structured streams which we use:
  34. */
  35. #include <HTFWriter.h>
  36. #include <HTPlain.h>
  37. #include <SGML.h>
  38. #include <HTMLGen.h>
  39. #include <LYexit.h>
  40. #include <LYUtils.h>
  41. #include <GridText.h>
  42. #include <LYGlobalDefs.h>
  43. #include <LYLeaks.h>
  44. #ifdef DISP_PARTIAL
  45. #include <LYMainLoop.h>
  46. #endif
  47. BOOL HTOutputSource = NO; /* Flag: shortcut parser to stdout */
  48. #ifdef ORIGINAL
  49. struct _HTStream {
  50. const HTStreamClass *isa;
  51. /* ... */
  52. };
  53. #endif /* ORIGINAL */
  54. /* this version used by the NetToText stream */
  55. struct _HTStream {
  56. const HTStreamClass *isa;
  57. BOOL had_cr;
  58. HTStream *sink;
  59. };
  60. /* Presentation methods
  61. * --------------------
  62. */
  63. HTList *HTPresentations = NULL;
  64. HTPresentation *default_presentation = NULL;
  65. /*
  66. * To free off the presentation list.
  67. */
  68. #ifdef LY_FIND_LEAKS
  69. static void HTFreePresentations(void);
  70. #endif
  71. /* Define a presentation system command for a content-type
  72. * -------------------------------------------------------
  73. */
  74. void HTSetPresentation(const char *representation,
  75. const char *command,
  76. const char *testcommand,
  77. double quality,
  78. double secs,
  79. double secs_per_byte,
  80. long int maxbytes,
  81. AcceptMedia media)
  82. {
  83. HTPresentation *pres = typecalloc(HTPresentation);
  84. if (pres == NULL)
  85. outofmem(__FILE__, "HTSetPresentation");
  86. pres->rep = HTAtom_for(representation);
  87. pres->rep_out = WWW_PRESENT; /* Fixed for now ... :-) */
  88. pres->converter = HTSaveAndExecute; /* Fixed for now ... */
  89. pres->quality = (float) quality;
  90. pres->secs = (float) secs;
  91. pres->secs_per_byte = (float) secs_per_byte;
  92. pres->maxbytes = maxbytes;
  93. pres->get_accept = 0;
  94. pres->accept_opt = media;
  95. pres->command = NULL;
  96. StrAllocCopy(pres->command, command);
  97. pres->testcommand = NULL;
  98. StrAllocCopy(pres->testcommand, testcommand);
  99. /*
  100. * Memory leak fixed.
  101. * 05-28-94 Lynx 2-3-1 Garrett Arch Blythe
  102. */
  103. if (!HTPresentations) {
  104. HTPresentations = HTList_new();
  105. #ifdef LY_FIND_LEAKS
  106. atexit(HTFreePresentations);
  107. #endif
  108. }
  109. if (strcmp(representation, "*") == 0) {
  110. FREE(default_presentation);
  111. default_presentation = pres;
  112. } else {
  113. HTList_addObject(HTPresentations, pres);
  114. }
  115. }
  116. /* Define a built-in function for a content-type
  117. * ---------------------------------------------
  118. */
  119. void HTSetConversion(const char *representation_in,
  120. const char *representation_out,
  121. HTConverter *converter,
  122. float quality,
  123. float secs,
  124. float secs_per_byte,
  125. long int maxbytes,
  126. AcceptMedia media)
  127. {
  128. HTPresentation *pres = typecalloc(HTPresentation);
  129. if (pres == NULL)
  130. outofmem(__FILE__, "HTSetConversion");
  131. pres->rep = HTAtom_for(representation_in);
  132. pres->rep_out = HTAtom_for(representation_out);
  133. pres->converter = converter;
  134. pres->command = NULL;
  135. pres->testcommand = NULL;
  136. pres->quality = quality;
  137. pres->secs = secs;
  138. pres->secs_per_byte = secs_per_byte;
  139. pres->maxbytes = maxbytes;
  140. pres->get_accept = TRUE;
  141. pres->accept_opt = media;
  142. /*
  143. * Memory Leak fixed.
  144. * 05-28-94 Lynx 2-3-1 Garrett Arch Blythe
  145. */
  146. if (!HTPresentations) {
  147. HTPresentations = HTList_new();
  148. #ifdef LY_FIND_LEAKS
  149. atexit(HTFreePresentations);
  150. #endif
  151. }
  152. HTList_addObject(HTPresentations, pres);
  153. }
  154. #ifdef LY_FIND_LEAKS
  155. /*
  156. * Purpose: Free the presentation list.
  157. * Arguments: void
  158. * Return Value: void
  159. * Remarks/Portability/Dependencies/Restrictions:
  160. * Made to clean up Lynx's bad leakage.
  161. * Revision History:
  162. * 05-28-94 created Lynx 2-3-1 Garrett Arch Blythe
  163. */
  164. static void HTFreePresentations(void)
  165. {
  166. HTPresentation *pres = NULL;
  167. /*
  168. * Loop through the list.
  169. */
  170. while (!HTList_isEmpty(HTPresentations)) {
  171. /*
  172. * Free off each item. May also need to free off it's items, but not
  173. * sure as of yet.
  174. */
  175. pres = (HTPresentation *) HTList_removeLastObject(HTPresentations);
  176. FREE(pres->command);
  177. FREE(pres->testcommand);
  178. FREE(pres);
  179. }
  180. /*
  181. * Free the list itself.
  182. */
  183. HTList_delete(HTPresentations);
  184. HTPresentations = NULL;
  185. }
  186. #endif /* LY_FIND_LEAKS */
  187. /* File buffering
  188. * --------------
  189. *
  190. * The input file is read using the macro which can read from
  191. * a socket or a file.
  192. * The input buffer size, if large will give greater efficiency and
  193. * release the server faster, and if small will save space on PCs etc.
  194. */
  195. #define INPUT_BUFFER_SIZE 4096 /* Tradeoff */
  196. static char input_buffer[INPUT_BUFFER_SIZE];
  197. static char *input_pointer;
  198. static char *input_limit;
  199. static int input_file_number;
  200. /* Set up the buffering
  201. *
  202. * These routines are public because they are in fact needed by
  203. * many parsers, and on PCs and Macs we should not duplicate
  204. * the static buffer area.
  205. */
  206. void HTInitInput(int file_number)
  207. {
  208. input_file_number = file_number;
  209. input_pointer = input_limit = input_buffer;
  210. }
  211. int interrupted_in_htgetcharacter = 0;
  212. int HTGetCharacter(void)
  213. {
  214. char ch;
  215. interrupted_in_htgetcharacter = 0;
  216. do {
  217. if (input_pointer >= input_limit) {
  218. int status = NETREAD(input_file_number,
  219. input_buffer, INPUT_BUFFER_SIZE);
  220. if (status <= 0) {
  221. if (status == 0)
  222. return EOF;
  223. if (status == HT_INTERRUPTED) {
  224. CTRACE((tfp, "HTFormat: Interrupted in HTGetCharacter\n"));
  225. interrupted_in_htgetcharacter = 1;
  226. return EOF;
  227. }
  228. CTRACE((tfp, "HTFormat: File read error %d\n", status));
  229. return EOF; /* -1 is returned by UCX
  230. at end of HTTP link */
  231. }
  232. input_pointer = input_buffer;
  233. input_limit = input_buffer + status;
  234. }
  235. ch = *input_pointer++;
  236. } while (ch == (char) 13); /* Ignore ASCII carriage return */
  237. return FROMASCII(UCH(ch));
  238. }
  239. #ifdef USE_SSL
  240. char HTGetSSLCharacter(void *handle)
  241. {
  242. char ch;
  243. interrupted_in_htgetcharacter = 0;
  244. if (!handle)
  245. return (char) EOF;
  246. do {
  247. if (input_pointer >= input_limit) {
  248. int status = SSL_read((SSL *) handle,
  249. input_buffer, INPUT_BUFFER_SIZE);
  250. if (status <= 0) {
  251. if (status == 0)
  252. return (char) EOF;
  253. if (status == HT_INTERRUPTED) {
  254. CTRACE((tfp,
  255. "HTFormat: Interrupted in HTGetSSLCharacter\n"));
  256. interrupted_in_htgetcharacter = 1;
  257. return (char) EOF;
  258. }
  259. CTRACE((tfp, "HTFormat: SSL_read error %d\n", status));
  260. return (char) EOF; /* -1 is returned by UCX
  261. at end of HTTP link */
  262. }
  263. input_pointer = input_buffer;
  264. input_limit = input_buffer + status;
  265. }
  266. ch = *input_pointer++;
  267. } while (ch == (char) 13); /* Ignore ASCII carriage return */
  268. return FROMASCII(ch);
  269. }
  270. #endif /* USE_SSL */
  271. /* Match maintype to any MIME type starting with maintype, for example:
  272. * image/gif should match image
  273. */
  274. static int half_match(char *trial_type, char *target)
  275. {
  276. char *cp = strchr(trial_type, '/');
  277. /* if no '/' or no '*' */
  278. if (!cp || *(cp + 1) != '*')
  279. return 0;
  280. CTRACE((tfp, "HTFormat: comparing %s and %s for half match\n",
  281. trial_type, target));
  282. /* main type matches */
  283. if (!strncmp(trial_type, target, (cp - trial_type) - 1))
  284. return 1;
  285. return 0;
  286. }
  287. /*
  288. * Evaluate a deferred mailcap test command, i.e.,. one that substitutes the
  289. * document's charset or other values in %{name} format.
  290. */
  291. static BOOL failsMailcap(HTPresentation *pres, HTParentAnchor *anchor)
  292. {
  293. if (pres->testcommand != 0) {
  294. if (LYTestMailcapCommand(pres->testcommand,
  295. anchor->content_type_params) != 0)
  296. return TRUE;
  297. }
  298. return FALSE;
  299. }
  300. #define WWW_WILDCARD_REP_OUT HTAtom_for("*")
  301. /* Look up a presentation
  302. * ----------------------
  303. *
  304. * If fill_in is NULL, only look for an exact match.
  305. * If a wildcard match is made, *fill_in is used to store
  306. * a possibly modified presentation, and a pointer to it is
  307. * returned. For an exact match, a pointer to the presentation
  308. * in the HTPresentations list is returned. Returns NULL if
  309. * nothing found. - kw
  310. *
  311. */
  312. static HTPresentation *HTFindPresentation(HTFormat rep_in,
  313. HTFormat rep_out,
  314. HTPresentation *fill_in,
  315. HTParentAnchor *anchor)
  316. {
  317. HTAtom *wildcard = NULL; /* = HTAtom_for("*"); lookup when needed - kw */
  318. int n;
  319. int i;
  320. HTPresentation *pres;
  321. HTPresentation *match;
  322. HTPresentation *strong_wildcard_match = 0;
  323. HTPresentation *weak_wildcard_match = 0;
  324. HTPresentation *last_default_match = 0;
  325. HTPresentation *strong_subtype_wildcard_match = 0;
  326. CTRACE((tfp, "HTFormat: Looking up presentation for %s to %s\n",
  327. HTAtom_name(rep_in), HTAtom_name(rep_out)));
  328. n = HTList_count(HTPresentations);
  329. for (i = 0; i < n; i++) {
  330. pres = (HTPresentation *) HTList_objectAt(HTPresentations, i);
  331. if (pres->rep == rep_in) {
  332. if (pres->rep_out == rep_out) {
  333. if (failsMailcap(pres, anchor))
  334. continue;
  335. CTRACE((tfp, "FindPresentation: found exact match: %s\n",
  336. HTAtom_name(pres->rep)));
  337. return pres;
  338. } else if (!fill_in) {
  339. continue;
  340. } else {
  341. if (!wildcard)
  342. wildcard = WWW_WILDCARD_REP_OUT;
  343. if (pres->rep_out == wildcard) {
  344. if (failsMailcap(pres, anchor))
  345. continue;
  346. if (!strong_wildcard_match)
  347. strong_wildcard_match = pres;
  348. /* otherwise use the first one */
  349. CTRACE((tfp,
  350. "StreamStack: found strong wildcard match: %s\n",
  351. HTAtom_name(pres->rep)));
  352. }
  353. }
  354. } else if (!fill_in) {
  355. continue;
  356. } else if (half_match(HTAtom_name(pres->rep),
  357. HTAtom_name(rep_in))) {
  358. if (pres->rep_out == rep_out) {
  359. if (failsMailcap(pres, anchor))
  360. continue;
  361. if (!strong_subtype_wildcard_match)
  362. strong_subtype_wildcard_match = pres;
  363. /* otherwise use the first one */
  364. CTRACE((tfp,
  365. "StreamStack: found strong subtype wildcard match: %s\n",
  366. HTAtom_name(pres->rep)));
  367. }
  368. }
  369. if (pres->rep == WWW_SOURCE) {
  370. if (pres->rep_out == rep_out) {
  371. if (failsMailcap(pres, anchor))
  372. continue;
  373. if (!weak_wildcard_match)
  374. weak_wildcard_match = pres;
  375. /* otherwise use the first one */
  376. CTRACE((tfp,
  377. "StreamStack: found weak wildcard match: %s\n",
  378. HTAtom_name(pres->rep_out)));
  379. } else if (!last_default_match) {
  380. if (!wildcard)
  381. wildcard = WWW_WILDCARD_REP_OUT;
  382. if (pres->rep_out == wildcard) {
  383. if (failsMailcap(pres, anchor))
  384. continue;
  385. last_default_match = pres;
  386. /* otherwise use the first one */
  387. }
  388. }
  389. }
  390. }
  391. match = (strong_subtype_wildcard_match
  392. ? strong_subtype_wildcard_match
  393. : (strong_wildcard_match
  394. ? strong_wildcard_match
  395. : (weak_wildcard_match
  396. ? weak_wildcard_match
  397. : last_default_match)));
  398. if (match) {
  399. *fill_in = *match; /* Specific instance */
  400. fill_in->rep = rep_in; /* yuk */
  401. fill_in->rep_out = rep_out; /* yuk */
  402. return fill_in;
  403. }
  404. return NULL;
  405. }
  406. /* Create a filter stack
  407. * ---------------------
  408. *
  409. * If a wildcard match is made, a temporary HTPresentation
  410. * structure is made to hold the destination format while the
  411. * new stack is generated. This is just to pass the out format to
  412. * MIME so far. Storing the format of a stream in the stream might
  413. * be a lot neater.
  414. *
  415. */
  416. HTStream *HTStreamStack(HTFormat rep_in,
  417. HTFormat rep_out,
  418. HTStream *sink,
  419. HTParentAnchor *anchor)
  420. {
  421. HTPresentation temp;
  422. HTPresentation *match;
  423. HTStream *result;
  424. CTRACE((tfp, "HTFormat: Constructing stream stack for %s to %s (%s)\n",
  425. HTAtom_name(rep_in),
  426. HTAtom_name(rep_out),
  427. NONNULL(anchor->content_type_params)));
  428. /* don't return on WWW_SOURCE some people might like
  429. * to make use of the source!!!! LJM
  430. */
  431. #if 0
  432. if (rep_out == WWW_SOURCE || rep_out == rep_in)
  433. return sink; /* LJM */
  434. #endif
  435. if (rep_out == rep_in) {
  436. result = sink;
  437. } else if ((match = HTFindPresentation(rep_in, rep_out, &temp, anchor))) {
  438. if (match == &temp) {
  439. CTRACE((tfp, "StreamStack: Using %s\n", HTAtom_name(temp.rep_out)));
  440. } else {
  441. CTRACE((tfp, "StreamStack: found exact match: %s\n",
  442. HTAtom_name(match->rep)));
  443. }
  444. result = (*match->converter) (match, anchor, sink);
  445. } else {
  446. result = NULL;
  447. }
  448. if (TRACE) {
  449. if (result && result->isa && result->isa->name) {
  450. CTRACE((tfp, "StreamStack: Returning \"%s\"\n", result->isa->name));
  451. } else if (result) {
  452. CTRACE((tfp, "StreamStack: Returning *unknown* stream!\n"));
  453. } else {
  454. CTRACE((tfp, "StreamStack: Returning NULL!\n"));
  455. CTRACE_FLUSH(tfp); /* a crash may be imminent... - kw */
  456. }
  457. }
  458. return result;
  459. }
  460. /* Put a presentation near start of list
  461. * -------------------------------------
  462. *
  463. * Look up a presentation (exact match only) and, if found, reorder
  464. * it to the start of the HTPresentations list. - kw
  465. */
  466. void HTReorderPresentation(HTFormat rep_in,
  467. HTFormat rep_out)
  468. {
  469. HTPresentation *match;
  470. if ((match = HTFindPresentation(rep_in, rep_out, NULL, NULL))) {
  471. HTList_removeObject(HTPresentations, match);
  472. HTList_addObject(HTPresentations, match);
  473. }
  474. }
  475. /*
  476. * Setup 'get_accept' flag to denote presentations that are not redundant,
  477. * and will be listed in "Accept:" header.
  478. */
  479. void HTFilterPresentations(void)
  480. {
  481. int i, j;
  482. int n = HTList_count(HTPresentations);
  483. HTPresentation *p, *q;
  484. BOOL matched;
  485. char *s, *t;
  486. CTRACE((tfp, "HTFilterPresentations (AcceptMedia %#x)\n", LYAcceptMedia));
  487. for (i = 0; i < n; i++) {
  488. p = (HTPresentation *) HTList_objectAt(HTPresentations, i);
  489. s = HTAtom_name(p->rep);
  490. p->get_accept = FALSE;
  491. if ((LYAcceptMedia & p->accept_opt) != 0
  492. && p->rep_out == WWW_PRESENT
  493. && p->rep != WWW_SOURCE
  494. && strcasecomp(s, "www/mime")
  495. && strcasecomp(s, "www/compressed")
  496. && p->quality <= 1.0 && p->quality >= 0.0) {
  497. matched = TRUE;
  498. for (j = 0; j < i; j++) {
  499. q = (HTPresentation *) HTList_objectAt(HTPresentations, j);
  500. t = HTAtom_name(q->rep);
  501. if (!strcasecomp(s, t)) {
  502. matched = FALSE;
  503. CTRACE((tfp, " match %s %s\n", s, t));
  504. break;
  505. }
  506. }
  507. p->get_accept = matched;
  508. }
  509. }
  510. }
  511. /* Find the cost of a filter stack
  512. * -------------------------------
  513. *
  514. * Must return the cost of the same stack which StreamStack would set up.
  515. *
  516. * On entry,
  517. * length The size of the data to be converted
  518. */
  519. float HTStackValue(HTFormat rep_in,
  520. HTFormat rep_out,
  521. float initial_value,
  522. long int length)
  523. {
  524. HTAtom *wildcard = WWW_WILDCARD_REP_OUT;
  525. CTRACE((tfp, "HTFormat: Evaluating stream stack for %s worth %.3f to %s\n",
  526. HTAtom_name(rep_in), initial_value, HTAtom_name(rep_out)));
  527. if (rep_out == WWW_SOURCE || rep_out == rep_in)
  528. return 0.0;
  529. {
  530. int n = HTList_count(HTPresentations);
  531. int i;
  532. HTPresentation *pres;
  533. for (i = 0; i < n; i++) {
  534. pres = (HTPresentation *) HTList_objectAt(HTPresentations, i);
  535. if (pres->rep == rep_in &&
  536. (pres->rep_out == rep_out || pres->rep_out == wildcard)) {
  537. float value = initial_value * pres->quality;
  538. if (HTMaxSecs != 0.0)
  539. value = value - (length * pres->secs_per_byte + pres->secs)
  540. / HTMaxSecs;
  541. return value;
  542. }
  543. }
  544. }
  545. return (float) -1e30; /* Really bad */
  546. }
  547. /* Display the page while transfer in progress
  548. * -------------------------------------------
  549. *
  550. * Repaint the page only when necessary.
  551. * This is a traverse call for HText_pageDisplay() - it works!.
  552. *
  553. */
  554. void HTDisplayPartial(void)
  555. {
  556. #ifdef DISP_PARTIAL
  557. if (display_partial) {
  558. /*
  559. * HText_getNumOfLines() = "current" number of complete lines received
  560. * NumOfLines_partial = number of lines at the moment of last repaint.
  561. * (we update NumOfLines_partial only when we repaint the display.)
  562. *
  563. * display_partial could only be enabled in HText_new() so a new
  564. * HTMainText object available - all HText_ functions use it, lines
  565. * counter HText_getNumOfLines() in particular.
  566. *
  567. * Otherwise HTMainText holds info from the previous document and we
  568. * may repaint it instead of the new one: prev doc scrolled to the
  569. * first line (=Newline_partial) is not good looking :-) 23 Aug 1998
  570. * Leonid Pauzner
  571. *
  572. * So repaint the page only when necessary:
  573. */
  574. int Newline_partial = LYGetNewline();
  575. if (((Newline_partial + display_lines) - 1 > NumOfLines_partial)
  576. /* current page not complete... */
  577. && (partial_threshold > 0 ?
  578. ((Newline_partial + partial_threshold) - 1 <=
  579. HText_getNumOfLines()) :
  580. ((Newline_partial + display_lines) - 1 <= HText_getNumOfLines()))
  581. /*
  582. * Originally we rendered by increments of 2 lines,
  583. * but that got annoying on slow network connections.
  584. * Then we switched to full-pages. Now it's configurable.
  585. * If partial_threshold <= 0, then it's a full page
  586. */
  587. ) {
  588. if (LYMainLoop_pageDisplay(Newline_partial))
  589. NumOfLines_partial = HText_getNumOfLines();
  590. }
  591. }
  592. #else /* nothing */
  593. #endif /* DISP_PARTIAL */
  594. }
  595. /* Put this as early as possible, OK just after HTDisplayPartial() */
  596. void HTFinishDisplayPartial(void)
  597. {
  598. #ifdef DISP_PARTIAL
  599. /*
  600. * End of incremental rendering stage here.
  601. */
  602. display_partial = FALSE;
  603. #endif /* DISP_PARTIAL */
  604. }
  605. /* Push data from a socket down a stream
  606. * -------------------------------------
  607. *
  608. * This routine is responsible for creating and PRESENTING any
  609. * graphic (or other) objects described by the file.
  610. *
  611. * The file number given is assumed to be a TELNET stream, i.e., containing
  612. * CRLF at the end of lines which need to be stripped to LF for unix
  613. * when the format is textual.
  614. *
  615. * State of socket and target stream on entry:
  616. * socket (file_number) assumed open,
  617. * target (sink) assumed valid.
  618. *
  619. * Return values:
  620. * HT_INTERRUPTED Interruption or error after some data received.
  621. * -2 Unexpected disconnect before any data received.
  622. * -1 Interruption or error before any data received, or
  623. * (UNIX) other read error before any data received, or
  624. * download cancelled.
  625. * HT_LOADED Normal close of socket (end of file indication
  626. * received), or
  627. * unexpected disconnect after some data received, or
  628. * other read error after some data received, or
  629. * (not UNIX) other read error before any data received.
  630. *
  631. * State of socket and target stream on return depends on return value:
  632. * HT_INTERRUPTED socket still open, target aborted.
  633. * -2 socket still open, target stream still valid.
  634. * -1 socket still open, target aborted.
  635. * otherwise socket closed, target stream still valid.
  636. */
  637. int HTCopy(HTParentAnchor *anchor,
  638. int file_number,
  639. void *handle GCC_UNUSED,
  640. HTStream *sink)
  641. {
  642. HTStreamClass targetClass;
  643. BOOL suppress_readprogress = NO;
  644. int bytes;
  645. int rv = 0;
  646. /* Push the data down the stream
  647. */
  648. targetClass = *(sink->isa); /* Copy pointers to procedures */
  649. /*
  650. * Push binary from socket down sink
  651. *
  652. * This operation could be put into a main event loop
  653. */
  654. HTReadProgress(bytes = 0, 0);
  655. for (;;) {
  656. int status;
  657. if (LYCancelDownload) {
  658. LYCancelDownload = FALSE;
  659. (*targetClass._abort) (sink, NULL);
  660. rv = -1;
  661. goto finished;
  662. }
  663. if (HTCheckForInterrupt()) {
  664. _HTProgress(TRANSFER_INTERRUPTED);
  665. (*targetClass._abort) (sink, NULL);
  666. if (bytes)
  667. rv = HT_INTERRUPTED;
  668. else
  669. rv = -1;
  670. goto finished;
  671. }
  672. #ifdef USE_SSL
  673. if (handle)
  674. status = SSL_read((SSL *) handle, input_buffer, INPUT_BUFFER_SIZE);
  675. else
  676. status = NETREAD(file_number, input_buffer, INPUT_BUFFER_SIZE);
  677. #else
  678. status = NETREAD(file_number, input_buffer, INPUT_BUFFER_SIZE);
  679. #endif /* USE_SSL */
  680. if (status <= 0) {
  681. if (status == 0) {
  682. break;
  683. } else if (status == HT_INTERRUPTED) {
  684. _HTProgress(TRANSFER_INTERRUPTED);
  685. (*targetClass._abort) (sink, NULL);
  686. if (bytes)
  687. rv = HT_INTERRUPTED;
  688. else
  689. rv = -1;
  690. goto finished;
  691. } else if (SOCKET_ERRNO == ENOTCONN ||
  692. #ifdef _WINDOWS /* 1997/11/10 (Mon) 16:57:18 */
  693. SOCKET_ERRNO == ETIMEDOUT ||
  694. #endif
  695. SOCKET_ERRNO == ECONNRESET ||
  696. SOCKET_ERRNO == EPIPE) {
  697. /*
  698. * Arrrrgh, HTTP 0/1 compatibility problem, maybe.
  699. */
  700. if (bytes <= 0) {
  701. /*
  702. * Don't have any data, so let the calling function decide
  703. * what to do about it. - FM
  704. */
  705. rv = -2;
  706. goto finished;
  707. } else {
  708. #ifdef UNIX
  709. /*
  710. * Treat what we've received already as the complete
  711. * transmission, but not without giving the user an alert.
  712. * I don't know about all the different TCP stacks for VMS
  713. * etc., so this is currently only for UNIX. - kw
  714. */
  715. HTInetStatus("NETREAD");
  716. HTAlert("Unexpected server disconnect.");
  717. CTRACE((tfp,
  718. "HTCopy: Unexpected server disconnect. Treating as completed.\n"));
  719. status = 0;
  720. break;
  721. #else /* !UNIX */
  722. /*
  723. * Treat what we've gotten already as the complete
  724. * transmission. - FM
  725. */
  726. CTRACE((tfp,
  727. "HTCopy: Unexpected server disconnect. Treating as completed.\n"));
  728. status = 0;
  729. break;
  730. #endif /* UNIX */
  731. }
  732. #ifdef UNIX
  733. } else { /* status < 0 and other errno */
  734. /*
  735. * Treat what we've received already as the complete
  736. * transmission, but not without giving the user an alert. I
  737. * don't know about all the different TCP stacks for VMS etc.,
  738. * so this is currently only for UNIX. - kw
  739. */
  740. HTInetStatus("NETREAD");
  741. HTAlert("Unexpected read error.");
  742. if (bytes) {
  743. (void) NETCLOSE(file_number);
  744. rv = HT_LOADED;
  745. } else {
  746. (*targetClass._abort) (sink, NULL);
  747. rv = -1;
  748. }
  749. goto finished;
  750. #endif
  751. }
  752. break;
  753. }
  754. /*
  755. * Suppress ReadProgress messages when collecting a redirection
  756. * message, at least initially (unless/until anchor->content_type gets
  757. * changed, probably by the MIME message parser). That way messages
  758. * put up by the HTTP module or elsewhere can linger in the statusline
  759. * for a while. - kw
  760. */
  761. suppress_readprogress = (anchor && anchor->content_type &&
  762. !strcmp(anchor->content_type,
  763. "message/x-http-redirection"));
  764. #ifdef NOT_ASCII
  765. {
  766. char *p;
  767. for (p = input_buffer; p < input_buffer + status; p++) {
  768. *p = FROMASCII(*p);
  769. }
  770. }
  771. #endif /* NOT_ASCII */
  772. (*targetClass.put_block) (sink, input_buffer, status);
  773. bytes += status;
  774. if (!suppress_readprogress)
  775. HTReadProgress(bytes, anchor ? anchor->content_length : 0);
  776. HTDisplayPartial();
  777. } /* next bufferload */
  778. _HTProgress(TRANSFER_COMPLETE);
  779. (void) NETCLOSE(file_number);
  780. rv = HT_LOADED;
  781. finished:
  782. HTFinishDisplayPartial();
  783. return (rv);
  784. }
  785. /* Push data from a file pointer down a stream
  786. * -------------------------------------
  787. *
  788. * This routine is responsible for creating and PRESENTING any
  789. * graphic (or other) objects described by the file.
  790. *
  791. *
  792. * State of file and target stream on entry:
  793. * FILE* (fp) assumed open,
  794. * target (sink) assumed valid.
  795. *
  796. * Return values:
  797. * HT_INTERRUPTED Interruption after some data read.
  798. * HT_PARTIAL_CONTENT Error after some data read.
  799. * -1 Error before any data read.
  800. * HT_LOADED Normal end of file indication on reading.
  801. *
  802. * State of file and target stream on return:
  803. * always fp still open, target stream still valid.
  804. */
  805. int HTFileCopy(FILE *fp, HTStream *sink)
  806. {
  807. HTStreamClass targetClass;
  808. int status, bytes;
  809. int rv = HT_OK;
  810. /* Push the data down the stream
  811. */
  812. targetClass = *(sink->isa); /* Copy pointers to procedures */
  813. /* Push binary from socket down sink
  814. */
  815. HTReadProgress(bytes = 0, 0);
  816. for (;;) {
  817. status = fread(input_buffer, 1, INPUT_BUFFER_SIZE, fp);
  818. if (status == 0) { /* EOF or error */
  819. if (ferror(fp) == 0) {
  820. rv = HT_LOADED;
  821. break;
  822. }
  823. CTRACE((tfp, "HTFormat: Read error, read returns %d\n",
  824. ferror(fp)));
  825. if (bytes) {
  826. rv = HT_PARTIAL_CONTENT;
  827. } else {
  828. rv = -1;
  829. }
  830. break;
  831. }
  832. (*targetClass.put_block) (sink, input_buffer, status);
  833. bytes += status;
  834. HTReadProgress(bytes, 0);
  835. /* Suppress last screen update in partial mode - a regular update under
  836. * control of mainloop() should follow anyway. - kw
  837. */
  838. #ifdef DISP_PARTIAL
  839. if (display_partial && bytes != HTMainAnchor->content_length)
  840. HTDisplayPartial();
  841. #endif
  842. if (HTCheckForInterrupt()) {
  843. _HTProgress(TRANSFER_INTERRUPTED);
  844. if (bytes) {
  845. rv = HT_INTERRUPTED;
  846. } else {
  847. rv = -1;
  848. }
  849. break;
  850. }
  851. } /* next bufferload */
  852. HTFinishDisplayPartial();
  853. return rv;
  854. }
  855. #ifdef USE_SOURCE_CACHE
  856. /* Push data from an HTChunk down a stream
  857. * ---------------------------------------
  858. *
  859. * This routine is responsible for creating and PRESENTING any
  860. * graphic (or other) objects described by the file.
  861. *
  862. * State of memory and target stream on entry:
  863. * HTChunk* (chunk) and target (sink) assumed valid.
  864. *
  865. * Return values:
  866. * HT_LOADED All data sent.
  867. * HT_INTERRUPTED Interruption after some data read.
  868. *
  869. * State of memory and target stream on return:
  870. * always chunk unchanged, target stream still valid.
  871. */
  872. int HTMemCopy(HTChunk *chunk, HTStream *sink)
  873. {
  874. HTStreamClass targetClass;
  875. int bytes = 0;
  876. int rv = HT_OK;
  877. targetClass = *(sink->isa);
  878. HTReadProgress(0, 0);
  879. for (; chunk != NULL; chunk = chunk->next) {
  880. /* Push the data down the stream a piece at a time, in case we're
  881. * running a large document on a slow machine.
  882. */
  883. (*targetClass.put_block) (sink, chunk->data, chunk->size);
  884. bytes += chunk->size;
  885. HTReadProgress(bytes, 0);
  886. HTDisplayPartial();
  887. if (HTCheckForInterrupt()) {
  888. _HTProgress(TRANSFER_INTERRUPTED);
  889. if (bytes) {
  890. rv = HT_INTERRUPTED;
  891. } else {
  892. rv = -1;
  893. }
  894. break;
  895. }
  896. }
  897. HTFinishDisplayPartial();
  898. return rv;
  899. }
  900. #endif
  901. #ifdef USE_ZLIB
  902. /* Push data from a gzip file pointer down a stream
  903. * -------------------------------------
  904. *
  905. * This routine is responsible for creating and PRESENTING any
  906. * graphic (or other) objects described by the file.
  907. *
  908. *
  909. * State of file and target stream on entry:
  910. * gzFile (gzfp) assumed open (should have gzipped content),
  911. * target (sink) assumed valid.
  912. *
  913. * Return values:
  914. * HT_INTERRUPTED Interruption after some data read.
  915. * HT_PARTIAL_CONTENT Error after some data read.
  916. * -1 Error before any data read.
  917. * HT_LOADED Normal end of file indication on reading.
  918. *
  919. * State of file and target stream on return:
  920. * always gzfp still open, target stream still valid.
  921. */
  922. static int HTGzFileCopy(gzFile gzfp, HTStream *sink)
  923. {
  924. HTStreamClass targetClass;
  925. int status, bytes;
  926. int gzerrnum;
  927. int rv = HT_OK;
  928. /* Push the data down the stream
  929. */
  930. targetClass = *(sink->isa); /* Copy pointers to procedures */
  931. /* read and inflate gzip'd file, and push binary down sink
  932. */
  933. HTReadProgress(bytes = 0, 0);
  934. for (;;) {
  935. status = gzread(gzfp, input_buffer, INPUT_BUFFER_SIZE);
  936. if (status <= 0) { /* EOF or error */
  937. if (status == 0) {
  938. rv = HT_LOADED;
  939. break;
  940. }
  941. CTRACE((tfp, "HTGzFileCopy: Read error, gzread returns %d\n",
  942. status));
  943. CTRACE((tfp, "gzerror : %s\n",
  944. gzerror(gzfp, &gzerrnum)));
  945. if (TRACE) {
  946. if (gzerrnum == Z_ERRNO)
  947. perror("gzerror ");
  948. }
  949. if (bytes) {
  950. rv = HT_PARTIAL_CONTENT;
  951. } else {
  952. rv = -1;
  953. }
  954. break;
  955. }
  956. (*targetClass.put_block) (sink, input_buffer, status);
  957. bytes += status;
  958. HTReadProgress(bytes, -1);
  959. HTDisplayPartial();
  960. if (HTCheckForInterrupt()) {
  961. _HTProgress(TRANSFER_INTERRUPTED);
  962. if (bytes) {
  963. rv = HT_INTERRUPTED;
  964. } else {
  965. rv = -1;
  966. }
  967. break;
  968. }
  969. } /* next bufferload */
  970. HTFinishDisplayPartial();
  971. return rv;
  972. }
  973. #ifndef HAVE_ZERROR
  974. #define zError(s) LynxZError(s)
  975. static const char *zError(int status)
  976. {
  977. static char result[80];
  978. sprintf(result, "zlib error %d", status);
  979. return result;
  980. }
  981. #endif
  982. /* Push data from a deflate file pointer down a stream
  983. * -------------------------------------
  984. *
  985. * This routine is responsible for creating and PRESENTING any
  986. * graphic (or other) objects described by the file. The code is
  987. * loosely based on the inflate.c file from w3m.
  988. *
  989. *
  990. * State of file and target stream on entry:
  991. * FILE (zzfp) assumed open (should have deflated content),
  992. * target (sink) assumed valid.
  993. *
  994. * Return values:
  995. * HT_INTERRUPTED Interruption after some data read.
  996. * HT_PARTIAL_CONTENT Error after some data read.
  997. * -1 Error before any data read.
  998. * HT_LOADED Normal end of file indication on reading.
  999. *
  1000. * State of file and target stream on return:
  1001. * always zzfp still open, target stream still valid.
  1002. */
  1003. static int HTZzFileCopy(FILE *zzfp, HTStream *sink)
  1004. {
  1005. static char dummy_head[1 + 1] =
  1006. {
  1007. 0x8 + 0x7 * 0x10,
  1008. (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
  1009. };
  1010. z_stream s;
  1011. HTStreamClass targetClass;
  1012. int bytes;
  1013. int rv = HT_OK;
  1014. char output_buffer[INPUT_BUFFER_SIZE];
  1015. int status;
  1016. int flush;
  1017. int retry = 0;
  1018. int len = 0;
  1019. /* Push the data down the stream
  1020. */
  1021. targetClass = *(sink->isa); /* Copy pointers to procedures */
  1022. s.zalloc = Z_NULL;
  1023. s.zfree = Z_NULL;
  1024. s.opaque = Z_NULL;
  1025. status = inflateInit(&s);
  1026. if (status != Z_OK) {
  1027. CTRACE((tfp, "HTZzFileCopy inflateInit() %s\n", zError(status)));
  1028. exit_immediately(1);
  1029. }
  1030. s.avail_in = 0;
  1031. s.next_out = (Bytef *) output_buffer;
  1032. s.avail_out = sizeof(output_buffer);
  1033. flush = Z_NO_FLUSH;
  1034. /* read and inflate deflate'd file, and push binary down sink
  1035. */
  1036. HTReadProgress(bytes = 0, 0);
  1037. for (;;) {
  1038. if (s.avail_in == 0) {
  1039. s.next_in = (Bytef *) input_buffer;
  1040. len = s.avail_in = fread(input_buffer, 1, INPUT_BUFFER_SIZE, zzfp);
  1041. }
  1042. status = inflate(&s, flush);
  1043. if (status == Z_STREAM_END || status == Z_BUF_ERROR) {
  1044. len = sizeof(output_buffer) - s.avail_out;
  1045. if (len > 0) {
  1046. (*targetClass.put_block) (sink, output_buffer, len);
  1047. bytes += len;
  1048. HTReadProgress(bytes, -1);
  1049. HTDisplayPartial();
  1050. }
  1051. rv = HT_LOADED;
  1052. break;
  1053. } else if (status == Z_DATA_ERROR && !retry++) {
  1054. status = inflateReset(&s);
  1055. if (status != Z_OK) {
  1056. CTRACE((tfp, "HTZzFileCopy inflateReset() %s\n", zError(status)));
  1057. rv = bytes ? HT_PARTIAL_CONTENT : -1;
  1058. break;
  1059. }
  1060. s.next_in = (Bytef *) dummy_head;
  1061. s.avail_in = sizeof(dummy_head);
  1062. status = inflate(&s, flush);
  1063. s.next_in = (Bytef *) input_buffer;
  1064. s.avail_in = len;
  1065. continue;
  1066. } else if (status != Z_OK) {
  1067. CTRACE((tfp, "HTZzFileCopy inflate() %s\n", zError(status)));
  1068. rv = bytes ? HT_PARTIAL_CONTENT : -1;
  1069. break;
  1070. } else if (s.avail_out == 0) {
  1071. len = sizeof(output_buffer);
  1072. s.next_out = (Bytef *) output_buffer;
  1073. s.avail_out = sizeof(output_buffer);
  1074. (*targetClass.put_block) (sink, output_buffer, len);
  1075. bytes += len;
  1076. HTReadProgress(bytes, -1);
  1077. HTDisplayPartial();
  1078. if (HTCheckForInterrupt()) {
  1079. _HTProgress(TRANSFER_INTERRUPTED);
  1080. rv = bytes ? HT_INTERRUPTED : -1;
  1081. break;
  1082. }
  1083. }
  1084. retry = 1;
  1085. } /* next bufferload */
  1086. inflateEnd(&s);
  1087. HTFinishDisplayPartial();
  1088. return rv;
  1089. }
  1090. #endif /* USE_ZLIB */
  1091. #ifdef USE_BZLIB
  1092. /* Push data from a bzip file pointer down a stream
  1093. * -------------------------------------
  1094. *
  1095. * This routine is responsible for creating and PRESENTING any
  1096. * graphic (or other) objects described by the file.
  1097. *
  1098. *
  1099. * State of file and target stream on entry:
  1100. * BZFILE (bzfp) assumed open (should have bzipped content),
  1101. * target (sink) assumed valid.
  1102. *
  1103. * Return values:
  1104. * HT_INTERRUPTED Interruption after some data read.
  1105. * HT_PARTIAL_CONTENT Error after some data read.
  1106. * -1 Error before any data read.
  1107. * HT_LOADED Normal end of file indication on reading.
  1108. *
  1109. * State of file and target stream on return:
  1110. * always bzfp still open, target stream still valid.
  1111. */
  1112. static int HTBzFileCopy(BZFILE * bzfp, HTStream *sink)
  1113. {
  1114. HTStreamClass targetClass;
  1115. int status, bytes;
  1116. int bzerrnum;
  1117. int rv = HT_OK;
  1118. /* Push the data down the stream
  1119. */
  1120. targetClass = *(sink->isa); /* Copy pointers to procedures */
  1121. /* read and inflate bzip'd file, and push binary down sink
  1122. */
  1123. HTReadProgress(bytes = 0, 0);
  1124. for (;;) {
  1125. status = BZ2_bzread(bzfp, input_buffer, INPUT_BUFFER_SIZE);
  1126. if (status <= 0) { /* EOF or error */
  1127. if (status == 0) {
  1128. rv = HT_LOADED;
  1129. break;
  1130. }
  1131. CTRACE((tfp, "HTBzFileCopy: Read error, bzread returns %d\n",
  1132. status));
  1133. CTRACE((tfp, "bzerror : %s\n",
  1134. BZ2_bzerror(bzfp, &bzerrnum)));
  1135. if (bytes) {
  1136. rv = HT_PARTIAL_CONTENT;
  1137. } else {
  1138. rv = -1;
  1139. }
  1140. break;
  1141. }
  1142. (*targetClass.put_block) (sink, input_buffer, status);
  1143. bytes += status;
  1144. HTReadProgress(bytes, -1);
  1145. HTDisplayPartial();
  1146. if (HTCheckForInterrupt()) {
  1147. _HTProgress(TRANSFER_INTERRUPTED);
  1148. if (bytes) {
  1149. rv = HT_INTERRUPTED;
  1150. } else {
  1151. rv = -1;
  1152. }
  1153. break;
  1154. }
  1155. } /* next bufferload */
  1156. HTFinishDisplayPartial();
  1157. return rv;
  1158. }
  1159. #endif /* USE_BZLIB */
  1160. /* Push data from a socket down a stream STRIPPING CR
  1161. * --------------------------------------------------
  1162. *
  1163. * This routine is responsible for creating and PRESENTING any
  1164. * graphic (or other) objects described by the socket.
  1165. *
  1166. * The file number given is assumed to be a TELNET stream ie containing
  1167. * CRLF at the end of lines which need to be stripped to LF for unix
  1168. * when the format is textual.
  1169. *
  1170. */
  1171. void HTCopyNoCR(HTParentAnchor *anchor GCC_UNUSED,
  1172. int file_number,
  1173. HTStream *sink)
  1174. {
  1175. HTStreamClass targetClass;
  1176. int character;
  1177. /* Push the data, ignoring CRLF, down the stream
  1178. */
  1179. targetClass = *(sink->isa); /* Copy pointers to procedures */
  1180. /*
  1181. * Push text from telnet socket down sink
  1182. *
  1183. * @@@@@ To push strings could be faster? (especially is we cheat and
  1184. * don't ignore CR! :-}
  1185. */
  1186. HTInitInput(file_number);
  1187. for (;;) {
  1188. character = HTGetCharacter();
  1189. if (character == EOF)
  1190. break;
  1191. (*targetClass.put_character) (sink, UCH(character));
  1192. }
  1193. }
  1194. /* Parse a socket given format and file number
  1195. *
  1196. * This routine is responsible for creating and PRESENTING any
  1197. * graphic (or other) objects described by the file.
  1198. *
  1199. * The file number given is assumed to be a TELNET stream ie containing
  1200. * CRLF at the end of lines which need to be stripped to LF for unix
  1201. * when the format is textual.
  1202. *
  1203. * State of socket and target stream on entry:
  1204. * socket (file_number) assumed open,
  1205. * target (sink) usually NULL (will call stream stack).
  1206. *
  1207. * Return values:
  1208. * HT_INTERRUPTED Interruption or error after some data received.
  1209. * -501 Stream stack failed (cannot present or convert).
  1210. * -2 Unexpected disconnect before any data received.
  1211. * -1 Stream stack failed (cannot present or convert), or
  1212. * Interruption or error before any data received, or
  1213. * (UNIX) other read error before any data received, or
  1214. * download cancelled.
  1215. * HT_LOADED Normal close of socket (end of file indication
  1216. * received), or
  1217. * unexpected disconnect after some data received, or
  1218. * other read error after some data received, or
  1219. * (not UNIX) other read error before any data received.
  1220. *
  1221. * State of socket and target stream on return depends on return value:
  1222. * HT_INTERRUPTED socket still open, target aborted.
  1223. * -501 socket still open, target stream NULL.
  1224. * -2 socket still open, target freed.
  1225. * -1 socket still open, target stream aborted or NULL.
  1226. * otherwise socket closed, target stream freed.
  1227. */
  1228. int HTParseSocket(HTFormat rep_in,
  1229. HTFormat format_out,
  1230. HTParentAnchor *anchor,
  1231. int file_number,
  1232. HTStream *sink)
  1233. {
  1234. HTStream *stream;
  1235. HTStreamClass targetClass;
  1236. int rv;
  1237. stream = HTStreamStack(rep_in, format_out, sink, anchor);
  1238. if (!stream) {
  1239. char *buffer = 0;
  1240. if (LYCancelDownload) {
  1241. LYCancelDownload = FALSE;
  1242. return -1;
  1243. }
  1244. HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O,
  1245. HTAtom_name(rep_in), HTAtom_name(format_out));
  1246. CTRACE((tfp, "HTFormat: %s\n", buffer));
  1247. rv = HTLoadError(sink, 501, buffer); /* returns -501 */
  1248. FREE(buffer);
  1249. } else {
  1250. /*
  1251. * Push the data, don't worry about CRLF we can strip them later.
  1252. */
  1253. targetClass = *(stream->isa); /* Copy pointers to procedures */
  1254. rv = HTCopy(anchor, file_number, NULL, stream);
  1255. if (rv != -1 && rv != HT_INTERRUPTED)
  1256. (*targetClass._free) (stream);
  1257. }
  1258. return rv;
  1259. /* Originally: full: HT_LOADED; partial: HT_INTERRUPTED; no bytes: -1 */
  1260. }
  1261. /* Parse a file given format and file pointer
  1262. *
  1263. * This routine is responsible for creating and PRESENTING any
  1264. * graphic (or other) objects described by the file.
  1265. *
  1266. * The file number given is assumed to be a TELNET stream ie containing
  1267. * CRLF at the end of lines which need to be stripped to \n for unix
  1268. * when the format is textual.
  1269. *
  1270. * State of file and target stream on entry:
  1271. * FILE* (fp) assumed open,
  1272. * target (sink) usually NULL (will call stream stack).
  1273. *
  1274. * Return values:
  1275. * -501 Stream stack failed (cannot present or convert).
  1276. * -1 Download cancelled.
  1277. * HT_NO_DATA Error before any data read.
  1278. * HT_PARTIAL_CONTENT Interruption or error after some data read.
  1279. * HT_LOADED Normal end of file indication on reading.
  1280. *
  1281. * State of file and target stream on return:
  1282. * always fp still open; target freed, aborted, or NULL.
  1283. */
  1284. int HTParseFile(HTFormat rep_in,
  1285. HTFormat format_out,
  1286. HTParentAnchor *anchor,
  1287. FILE *fp,
  1288. HTStream *sink)
  1289. {
  1290. HTStream *stream;
  1291. HTStreamClass targetClass;
  1292. int rv;
  1293. #ifdef SH_EX /* 1998/01/04 (Sun) 16:04:09 */
  1294. if (fp == NULL)
  1295. return HT_LOADED;
  1296. #endif
  1297. stream = HTStreamStack(rep_in, format_out, sink, anchor);
  1298. if (!stream) {
  1299. char *buffer = 0;
  1300. if (LYCancelDownload) {
  1301. LYCancelDownload = FALSE;
  1302. return -1;
  1303. }
  1304. HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O,
  1305. HTAtom_name(rep_in), HTAtom_name(format_out));
  1306. CTRACE((tfp, "HTFormat(in HTParseFile): %s\n", buffer));
  1307. rv = HTLoadError(sink, 501, buffer);
  1308. FREE(buffer);
  1309. return rv;
  1310. }
  1311. /*
  1312. * Push the data down the stream
  1313. *
  1314. * @@ Bug: This decision ought to be made based on "encoding" rather than
  1315. * on content-type. @@@ When we handle encoding. The current method
  1316. * smells anyway.
  1317. */
  1318. targetClass = *(stream->isa); /* Copy pointers to procedures */
  1319. rv = HTFileCopy(fp, stream);
  1320. if (rv == -1 || rv == HT_INTERRUPTED) {
  1321. (*targetClass._abort) (stream, NULL);
  1322. } else {
  1323. (*targetClass._free) (stream);
  1324. }
  1325. if (rv == -1)
  1326. return HT_NO_DATA;
  1327. else if (rv == HT_INTERRUPTED || (rv > 0 && rv != HT_LOADED))
  1328. return HT_PARTIAL_CONTENT;
  1329. else
  1330. return HT_LOADED;
  1331. }
  1332. #ifdef USE_SOURCE_CACHE
  1333. /* Parse a document in memory given format and memory block pointer
  1334. *
  1335. * This routine is responsible for creating and PRESENTING any
  1336. * graphic (or other) objects described by the file.
  1337. *
  1338. * State of memory and target stream on entry:
  1339. * HTChunk* (chunk) assumed valid,
  1340. * target (sink) usually NULL (will call stream stack).
  1341. *
  1342. * Return values:
  1343. * -501 Stream stack failed (cannot present or convert).
  1344. * HT_LOADED All data sent.
  1345. *
  1346. * State of memory and target stream on return:
  1347. * always chunk unchanged; target freed, aborted, or NULL.
  1348. */
  1349. int HTParseMem(HTFormat rep_in,
  1350. HTFormat format_out,
  1351. HTParentAnchor *anchor,
  1352. HTChunk *chunk,
  1353. HTStream *sink)
  1354. {
  1355. HTStream *stream;
  1356. HTStreamClass targetClass;
  1357. int rv;
  1358. stream = HTStreamStack(rep_in, format_out, sink, anchor);
  1359. if (!stream) {
  1360. char *buffer = 0;
  1361. HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O,
  1362. HTAtom_name(rep_in), HTAtom_name(format_out));
  1363. CTRACE((tfp, "HTFormat(in HTParseMem): %s\n", buffer));
  1364. rv = HTLoadError(sink, 501, buffer);
  1365. FREE(buffer);
  1366. return rv;
  1367. }
  1368. /* Push the data down the stream
  1369. */
  1370. targetClass = *(stream->isa);
  1371. rv = HTMemCopy(chunk, stream);
  1372. (*targetClass._free) (stream);
  1373. return HT_LOADED;
  1374. }
  1375. #endif
  1376. #ifdef USE_ZLIB
  1377. static int HTCloseGzFile(gzFile gzfp)
  1378. {
  1379. int gzres;
  1380. if (gzfp == NULL)
  1381. return 0;
  1382. gzres = gzclose(gzfp);
  1383. if (TRACE) {
  1384. if (gzres == Z_ERRNO) {
  1385. perror("gzclose ");
  1386. } else if (gzres != Z_OK) {
  1387. CTRACE((tfp, "gzclose : error number %d\n", gzres));
  1388. }
  1389. }
  1390. return (gzres);
  1391. }
  1392. /* HTParseGzFile
  1393. *
  1394. * State of file and target stream on entry:
  1395. * gzFile (gzfp) assumed open,
  1396. * target (sink) usually NULL (will call stream stack).
  1397. *
  1398. * Return values:
  1399. * -501 Stream stack failed (cannot present or convert).
  1400. * -1 Download cancelled.
  1401. * HT_NO_DATA Error before any data read.
  1402. * HT_PARTIAL_CONTENT Interruption or error after some data read.
  1403. * HT_LOADED Normal end of file indication on reading.
  1404. *
  1405. * State of file and target stream on return:
  1406. * always gzfp closed; target freed, aborted, or NULL.
  1407. */
  1408. int HTParseGzFile(HTFormat rep_in,
  1409. HTFormat format_out,
  1410. HTParentAnchor *anchor,
  1411. gzFile gzfp,
  1412. HTStream *sink)
  1413. {
  1414. HTStream *stream;
  1415. HTStreamClass targetClass;
  1416. int rv;
  1417. stream = HTStreamStack(rep_in, format_out, sink, anchor);
  1418. if (!stream) {
  1419. char *buffer = 0;
  1420. HTCloseGzFile(gzfp);
  1421. if (LYCancelDownload) {
  1422. LYCancelDownload = FALSE;
  1423. return -1;
  1424. }
  1425. HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O,
  1426. HTAtom_name(rep_in), HTAtom_name(format_out));
  1427. CTRACE((tfp, "HTFormat(in HTParseGzFile): %s\n", buffer));
  1428. rv = HTLoadError(sink, 501, buffer);
  1429. FREE(buffer);
  1430. return rv;
  1431. }
  1432. /*
  1433. * Push the data down the stream
  1434. *
  1435. * @@ Bug: This decision ought to be made based on "encoding" rather than
  1436. * on content-type. @@@ When we handle encoding. The current method
  1437. * smells anyway.
  1438. */
  1439. targetClass = *(stream->isa); /* Copy pointers to procedures */
  1440. rv = HTGzFileCopy(gzfp, stream);
  1441. if (rv == -1 || rv == HT_INTERRUPTED) {
  1442. (*targetClass._abort) (stream, NULL);
  1443. } else {
  1444. (*targetClass._free) (stream);
  1445. }
  1446. HTCloseGzFile(gzfp);
  1447. if (rv == -1)
  1448. return HT_NO_DATA;
  1449. else if (rv == HT_INTERRUPTED || (rv > 0 && rv != HT_LOADED))
  1450. return HT_PARTIAL_CONTENT;
  1451. else
  1452. return HT_LOADED;
  1453. }
  1454. /* HTParseZzFile
  1455. *
  1456. * State of file and target stream on entry:
  1457. * FILE (zzfp) assumed open,
  1458. * target (sink) usually NULL (will call stream stack).
  1459. *
  1460. * Return values:
  1461. * -501 Stream stack failed (cannot present or convert).
  1462. * -1 Download cancelled.
  1463. * HT_NO_DATA Error before any data read.
  1464. * HT_PARTIAL_CONTENT Interruption or error after some data read.
  1465. * HT_LOADED Normal end of file indication on reading.
  1466. *
  1467. * State of file and target stream on return:
  1468. * always zzfp closed; target freed, aborted, or NULL.
  1469. */
  1470. int HTParseZzFile(HTFormat rep_in,
  1471. HTFormat format_out,
  1472. HTParentAnchor *anchor,
  1473. FILE *zzfp,
  1474. HTStream *sink)
  1475. {
  1476. HTStream *stream;
  1477. HTStreamClass targetClass;
  1478. int rv;
  1479. stream = HTStreamStack(rep_in, format_out, sink, anchor);
  1480. if (!stream) {
  1481. char *buffer = 0;
  1482. fclose(zzfp);
  1483. if (LYCancelDownload) {
  1484. LYCancelDownload = FALSE;
  1485. return -1;
  1486. }
  1487. HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O,
  1488. HTAtom_name(rep_in), HTAtom_name(format_out));
  1489. CTRACE((tfp, "HTFormat(in HTParseGzFile): %s\n", buffer));
  1490. rv = HTLoadError(sink, 501, buffer);
  1491. FREE(buffer);
  1492. return rv;
  1493. }
  1494. /*
  1495. * Push the data down the stream
  1496. *
  1497. * @@ Bug: This decision ought to be made based on "encoding" rather than
  1498. * on content-type. @@@ When we handle encoding. The current method
  1499. * smells anyway.
  1500. */
  1501. targetClass = *(stream->isa); /* Copy pointers to procedures */
  1502. rv = HTZzFileCopy(zzfp, stream);
  1503. if (rv == -1 || rv == HT_INTERRUPTED) {
  1504. (*targetClass._abort) (stream, NULL);
  1505. } else {
  1506. (*targetClass._free) (stream);
  1507. }
  1508. fclose(zzfp);
  1509. if (rv == -1)
  1510. return HT_NO_DATA;
  1511. else if (rv == HT_INTERRUPTED || (rv > 0 && rv != HT_LOADED))
  1512. return HT_PARTIAL_CONTENT;
  1513. else
  1514. return HT_LOADED;
  1515. }
  1516. #endif /* USE_ZLIB */
  1517. #ifdef USE_BZLIB
  1518. static void HTCloseBzFile(BZFILE * bzfp)
  1519. {
  1520. if (bzfp)
  1521. BZ2_bzclose(bzfp);
  1522. }
  1523. /* HTParseBzFile
  1524. *
  1525. * State of file and target stream on entry:
  1526. * bzFile (bzfp) assumed open,
  1527. * target (sink) usually NULL (will call stream stack).
  1528. *
  1529. * Return values:
  1530. * -501 Stream stack failed (cannot present or convert).
  1531. * -1 Download cancelled.
  1532. * HT_NO_DATA Error before any data read.
  1533. * HT_PARTIAL_CONTENT Interruption or error after some data read.
  1534. * HT_LOADED Normal end of file indication on reading.
  1535. *
  1536. * State of file and target stream on return:
  1537. * always bzfp closed; target freed, aborted, or NULL.
  1538. */
  1539. int HTParseBzFile(HTFormat rep_in,
  1540. HTFormat format_out,
  1541. HTParentAnchor *anchor,
  1542. BZFILE * bzfp,
  1543. HTStream *sink)
  1544. {
  1545. HTStream *stream;
  1546. HTStreamClass targetClass;
  1547. int rv;
  1548. stream = HTStreamStack(rep_in, format_out, sink, anchor);
  1549. if (!stream) {
  1550. char *buffer = 0;
  1551. HTCloseBzFile(bzfp);
  1552. if (LYCancelDownload) {
  1553. LYCancelDownload = FALSE;
  1554. return -1;
  1555. }
  1556. HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O,
  1557. HTAtom_name(rep_in), HTAtom_name(format_out));
  1558. CTRACE((tfp, "HTFormat(in HTParseBzFile): %s\n", buffer));
  1559. rv = HTLoadError(sink, 501, buffer);
  1560. FREE(buffer);
  1561. return rv;
  1562. }
  1563. /*
  1564. * Push the data down the stream
  1565. *
  1566. * @@ Bug: This decision ought to be made based on "encoding" rather than
  1567. * on content-type. @@@ When we handle encoding. The current method
  1568. * smells anyway.
  1569. */
  1570. targetClass = *(stream->isa); /* Copy pointers to procedures */
  1571. rv = HTBzFileCopy(bzfp, stream);
  1572. if (rv == -1 || rv == HT_INTERRUPTED) {
  1573. (*targetClass._abort) (stream, NULL);
  1574. } else {
  1575. (*targetClass._free) (stream);
  1576. }
  1577. HTCloseBzFile(bzfp);
  1578. if (rv == -1)
  1579. return HT_NO_DATA;
  1580. else if (rv == HT_INTERRUPTED || (rv > 0 && rv != HT_LOADED))
  1581. return HT_PARTIAL_CONTENT;
  1582. else
  1583. return HT_LOADED;
  1584. }
  1585. #endif /* USE_BZLIB */
  1586. /* Converter stream: Network Telnet to internal character text
  1587. * -----------------------------------------------------------
  1588. *
  1589. * The input is assumed to be in ASCII, with lines delimited
  1590. * by (13,10) pairs, These pairs are converted into (CR,LF)
  1591. * pairs in the local representation. The (CR,LF) sequence
  1592. * when found is changed to a '\n' character, the internal
  1593. * C representation of a new line.
  1594. */
  1595. static void NetToText_put_character(HTStream *me, char net_char)
  1596. {
  1597. char c = FROMASCII(net_char);
  1598. if (me->had_cr) {
  1599. if (c == LF) {
  1600. me->sink->isa->put_character(me->sink, '\n'); /* Newline */
  1601. me->had_cr = NO;
  1602. return;
  1603. } else {
  1604. me->sink->isa->put_character(me->sink, CR); /* leftover */
  1605. }
  1606. }
  1607. me->had_cr = (BOOL) (c == CR);
  1608. if (!me->had_cr)
  1609. me->sink->isa->put_character(me->sink, c); /* normal */
  1610. }
  1611. static void NetToText_put_string(HTStream *me, const char *s)
  1612. {
  1613. const char *p;
  1614. for (p = s; *p; p++)
  1615. NetToText_put_character(me, *p);
  1616. }
  1617. static void NetToText_put_block(HTStream *me, const char *s, int l)
  1618. {
  1619. const char *p;
  1620. for (p = s; p < (s + l); p++)
  1621. NetToText_put_character(me, *p);
  1622. }
  1623. static void NetToText_free(HTStream *me)
  1624. {
  1625. (me->sink->isa->_free) (me->sink); /* Close rest of pipe */
  1626. FREE(me);
  1627. }
  1628. static void NetToText_abort(HTStream *me, HTError e)
  1629. {
  1630. me->sink->isa->_abort(me->sink, e); /* Abort rest of pipe */
  1631. FREE(me);
  1632. }
  1633. /* The class structure
  1634. */
  1635. static HTStreamClass NetToTextClass =
  1636. {
  1637. "NetToText",
  1638. NetToText_free,
  1639. NetToText_abort,
  1640. NetToText_put_character,
  1641. NetToText_put_string,
  1642. NetToText_put_block
  1643. };
  1644. /* The creation method
  1645. */
  1646. HTStream *HTNetToText(HTStream *sink)
  1647. {
  1648. HTStream *me = typecalloc(HTStream);
  1649. if (me == NULL)
  1650. outofmem(__FILE__, "NetToText");
  1651. me->isa = &NetToTextClass;
  1652. me->had_cr = NO;
  1653. me->sink = sink;
  1654. return me;
  1655. }
  1656. static HTStream HTBaseStreamInstance; /* Made static */
  1657. /*
  1658. * ERROR STREAM
  1659. * ------------
  1660. * There is only one error stream shared by anyone who wants a
  1661. * generic error returned from all stream methods.
  1662. */
  1663. static void HTErrorStream_put_character(HTStream *me GCC_UNUSED, char c GCC_UNUSED)
  1664. {
  1665. LYCancelDownload = TRUE;
  1666. }
  1667. static void HTErrorStream_put_string(HTStream *me GCC_UNUSED, const char *s)
  1668. {
  1669. if (s && *s)
  1670. LYCancelDownload = TRUE;
  1671. }
  1672. static void HTErrorStream_write(HTStream *me GCC_UNUSED, const char *s, int l)
  1673. {
  1674. if (l && s)
  1675. LYCancelDownload = TRUE;
  1676. }
  1677. static void HTErrorStream_free(HTStream *me GCC_UNUSED)
  1678. {
  1679. return;
  1680. }
  1681. static void HTErrorStream_abort(HTStream *me GCC_UNUSED, HTError e GCC_UNUSED)
  1682. {
  1683. return;
  1684. }
  1685. static const HTStreamClass HTErrorStreamClass =
  1686. {
  1687. "ErrorStream",
  1688. HTErrorStream_free,
  1689. HTErrorStream_abort,
  1690. HTErrorStream_put_character,
  1691. HTErrorStream_put_string,
  1692. HTErrorStream_write
  1693. };
  1694. HTStream *HTErrorStream(void)
  1695. {
  1696. CTRACE((tfp, "ErrorStream. Created\n"));
  1697. HTBaseStreamInstance.isa = &HTErrorStreamClass; /* The rest is random */
  1698. return &HTBaseStreamInstance;
  1699. }