1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
|
/* GOPHER ACCESS HTGopher.c
** =============
**
** History:
** 26 Sep 90 Adapted from other accesses (News, HTTP) TBL
** 29 Nov 91 Downgraded to C, for portable implementation.
** 28 Apr 94 target no more global and icons implemented
** HF, frystyk@dxcern.cern.ch
** 2 May 94 Fixed possible security hole when the URL contains
** a newline, that could cause multiple commands to be
** sent to a Gopher server. AL, luotonen@www.cern.ch
** 12 May 94 Checked and made ready for multi-threads, Frystyk
** 8 Jul 94 FM Insulate free() from _free structure element.
**
** NOTE:
** When parsing a gopher menu, we can't use the default HTParseSocket
** but we will hav eto take care of the stram ourselves :-(
**
*/
/* Implementation dependent include files */
#include "tcp.h"
/* Library include files */
#include "HTParse.h"
#include "HTUtils.h"
#include "HTTCP.h"
#include "HTIcons.h"
#include "HTAccess.h"
#include "HTFormat.h"
#include "HTError.h"
#include "HTFile.h"
#include "HTML.h"
#include "HTMLPDTD.h"
#include "HTDirBrw.h"
#include "HTGopher.h" /* Implemented here */
/* Macros and other defines */
#ifndef GOPHER_PORT
#define GOPHER_PORT 70 /* See protocol spec */
#endif
/* Hypertext object building machinery */
#define PUTC(c) (*target->isa->put_character)(target, c)
#define PUTS(s) (*target->isa->put_string)(target, s)
#define START(e) (*target->isa->start_element)(target, e, 0, 0)
#define END(e) (*target->isa->end_element)(target, e)
#define FREE_TARGET (*target->isa->_free)(target)
/* Type definitions and global variables etc. local to this module */
typedef enum _HTGopherType {
GOPHER_TEXT = '0',
GOPHER_MENU = '1',
GOPHER_CSO = '2',
GOPHER_ERROR = '3',
GOPHER_MACBINHEX = '4',
GOPHER_PCBINHEX = '5',
GOPHER_UUENCODED = '6',
GOPHER_INDEX = '7',
GOPHER_TELNET = '8',
GOPHER_BINARY = '9',
GOPHER_GIF = 'g',
GOPHER_HTML = 'h', /* HTML */
GOPHER_INFO = 'i',
GOPHER_SOUND = 's',
GOPHER_WWW = 'w', /* W3 address */
GOPHER_IMAGE = 'I',
GOPHER_TN3270 = 'T',
GOPHER_DUPLICATE = '+',
GOPHER_PLUS_IMAGE = ':', /* Addition from Gopher Plus */
GOPHER_PLUS_MOVIE = ';',
GOPHER_PLUS_SOUND = '<'
} HTGopherType;
struct _HTStructured {
CONST HTStructuredClass * isa;
/* ... */
};
/* This is the local definition of HTRequest->net_info */
typedef struct _gopher_info {
int sockfd; /* Socket number for communication */
HTInputSocket * isoc; /* Input buffer */
int addressCount; /* Attempts if multi-homed */
BOOL CRLFdotCRLF; /* Transmission end like this */
HTRequest * request; /* Request structure */
HTGopherType type; /* Gopher item type */
} gopher_info;
/* ------------------------------------------------------------------------- */
/* get_gopher_icon
**
** This function finds an appopriate icon for the item in the gopher
** list. Actually it is only a shell build upon HTGetIcon().
**
*/
PRIVATE HTIconNode *get_gopher_icon ARGS2(CONST char *, filename,
int, gopher_type)
{
HTFormat content_type = NULL;
HTAtom *content_encoding = NULL;
if (gopher_type == GOPHER_MENU)
return icon_dir ? icon_dir : icon_unknown;
switch(gopher_type) {
case GOPHER_TEXT:
content_type = HTAtom_for("text/void");
break;
case GOPHER_IMAGE:
case GOPHER_PLUS_IMAGE:
case GOPHER_GIF:
content_type = HTAtom_for("image/void");
break;
case GOPHER_WWW:
case GOPHER_HTML:
content_type = HTAtom_for("text/void");
break;
case GOPHER_SOUND:
case GOPHER_PLUS_SOUND:
content_type = HTAtom_for("audio/void");
break;
case GOPHER_PLUS_MOVIE:
content_type = HTAtom_for("video/void");
break;
case GOPHER_INDEX:
content_type = HTAtom_for("application/x-gopher-index");
break;
case GOPHER_CSO:
content_type = HTAtom_for("application/x-gopher-cso");
break;
case GOPHER_TELNET:
content_type = HTAtom_for("application/x-gopher-telnet");
break;
case GOPHER_TN3270:
content_type = HTAtom_for("application/x-gopher-tn3270");
break;
case GOPHER_DUPLICATE:
content_type = HTAtom_for("application/x-gopher-duplicate");
break;
case GOPHER_ERROR:
content_type = HTAtom_for("www/unknown");
break;
case GOPHER_MACBINHEX:
case GOPHER_PCBINHEX:
case GOPHER_UUENCODED:
case GOPHER_BINARY:
{ /* Do our own filetyping -- maybe we get lucky */
HTAtom *language;
content_type = HTFileFormat(filename, &content_encoding,
&language);
}
default:
content_type = HTAtom_for("www/unknown");
break;
}
return HTGetIcon(S_IFMT & S_IFREG, content_type, content_encoding);
}
/* parse_menu
**
** This function parses a gopher menu and puts it into a iconized
** list.
**
** Returns HT_LOADED on succed, HT_INTERRUPTED if interrupted and -1
** if other error.
**
*/
PRIVATE int parse_menu ARGS3(HTRequest *, request,
gopher_info *, gopher,
CONST char *, url)
#define TAB '\t'
#define HEX_ESCAPE '%'
{
int status = -1;
unsigned int files = 0;
int ch;
HTChunk *chunk = HTChunkCreate(128);
char *message = NULL; /* For a gopher message */
char *info = NULL; /* For gopher information send as `i' type */
HTStructured *target = NULL;
gopher->isoc = HTInputSocket_new(gopher->sockfd);
/* Output the list */
while ((ch = HTInputSocket_getCharacter(gopher->isoc)) >= 0) {
if (ch == CR || ch == LF) {
if (chunk->size) { /* Got some text */
char *name = NULL; /* Gopher menu fields */
char *selector = NULL;
char *host = NULL;
char *port = NULL;
char *strptr;
char *errptr;
char gtype;
HTChunkTerminate(chunk);
strptr = chunk->data; /* Scan it to parse it */
if (PROT_TRACE)
fprintf(stderr, "HTGopher.... Menu item: `%s\'\n",
chunk->data);
gtype = *strptr++;
#if 0
if (gtype == GOPHER_ERROR) {
StrAllocCat(message, chunk->data+1);
break;
}
#endif
/* If information then add it to the info string */
if (gtype == GOPHER_INFO) {
if ((errptr = strchr(chunk->data, '\t')) != NULL)
*errptr = '\0';
if (info) {
StrAllocCat(info, "\n");
StrAllocCat(info, chunk->data+1);
} else
StrAllocCopy(info, chunk->data+1);
HTChunkClear(chunk);
continue;
}
/* If first item is an error, then don't put any header out
but wait and see if there is a next item in the list. If not
then make error message, else use as list message. */
if (!files && (strstr(chunk->data, "error.host") ||
strstr(chunk->data, "errorhost"))) {
/* If message is already initialized, then add this one. */
/* We don't want the gopher type character */
if ((errptr = strchr(chunk->data, '\t')) != NULL)
*errptr = '\0';
if (message) {
StrAllocCat(message, "\n");
StrAllocCat(message, chunk->data+1);
} else
StrAllocCopy(message, chunk->data+1);
HTChunkClear(chunk);
continue;
}
/* Stop listing if line with a dot by itself */
if (!files && message && gtype=='.' && !*strptr) {
status = -1;
break;
}
/* Output title, maybe top message and list top */
if (!files) {
CONST char *title = HTAnchor_title(request->anchor);
char *outstr = NULL;
target = HTML_new(request, NULL, WWW_HTML,
request->output_format,
request->output_stream);
if (title) {
StrAllocCopy(outstr, title);
HTUnEscape(outstr);
} else
StrAllocCopy(outstr, "Gopher Menu");
START(HTML_HTML);
START(HTML_HEAD);
START(HTML_TITLE);
PUTS(outstr);
END(HTML_TITLE);
END(HTML_HEAD);
START(HTML_BODY);
START(HTML_H1);
PUTS(outstr);
END(HTML_H1);
FREE(outstr);
/* Output any message on top of list */
if ((message || info) && HTDirInfo == HT_DIR_INFO_TOP) {
if (message) PUTS(message);
if (info) PUTS(info);
START(HTML_BR);
}
/* Make everything in list preformatted */
START(HTML_PRE);
#ifdef OLD_CODE
/* Output the header line of the list */
if (!icon_blank) icon_blank = icon_unknown;
if (HTDirShowMask & HT_DIR_SHOW_ICON && icon_blank) {
HTMLPutImg(target, icon_blank->icon_url,
HTIcon_alt_string(icon_blank->icon_alt, NO),
NULL);
}
PUTC(' ');
PUTS("Name");
PUTC('\n');
#endif /* OLD_CODE */
START(HTML_HR);
PUTC('\n');
}
/* Stop listing if line with a dot by itself */
if (gtype=='.' && !*strptr) {
status = (!files && message) ? -1 : HT_LOADED;
break;
}
/* Parse menu item */
if (*strptr) {
name = strptr;
selector = strchr(name, TAB);
if (selector) {
*selector++ = 0; /* Terminate name */
host = strchr(selector, TAB);
if (host) {
*host++ = 0; /* Terminate selector */
port = strchr(host, TAB);
if (port) {
char *junk;
*port = ':'; /* delimit host a la W3 */
if ((junk = strchr(port, TAB)) != NULL)
*junk = '\0'; /* Chop port */
if (*(port+1) == '0' && !*(port+2))
*port = '\0';
} /* port */
} /* host */
} /* selector */
} /* gtype and name */
/* Get Icon type and output the icon */
if (HTDirShowMask & HT_DIR_SHOW_ICON) {
HTIconNode *icon = get_gopher_icon(url, gtype);
if (icon && icon->icon_url) {
HTMLPutImg(target, icon->icon_url,
HTIcon_alt_string(icon->icon_alt, YES),
NULL);
PUTC(' ');
}
}
if (gtype == GOPHER_WWW) { /* Gopher pointer to W3 */
char *escaped = NULL;
escaped = HTEscape(selector, URL_PATH);
HTStartAnchor(target, NULL, escaped);
PUTS(name);
END(HTML_A);
free(escaped);
} else if (port) { /* Other types need port */
char *escaped = NULL;
char *address = NULL;
int addr_len;
/* Calculate the length of the WWW-address */
if (selector && *selector) {
escaped = HTEscape(selector, URL_PATH);
addr_len = 15 + strlen(escaped) + strlen(host) + 1;
} else {
addr_len = 15 + strlen(host) + 1;
}
if ((address = (char *) malloc(addr_len)) == NULL)
outofmem(__FILE__, "Gopher ParseMenu");
*address = '\0';
if (gtype == GOPHER_TELNET) {
if (escaped)
sprintf(address, "telnet://%s@%s/",
escaped, host);
else
sprintf(address, "telnet://%s/", host);
}
else if (gtype == GOPHER_TN3270) {
if (escaped)
sprintf(address, "tn3270://%s@%s/",
escaped, host);
else
sprintf(address, "tn3270://%s/", host);
} else {
if (escaped)
sprintf(address, "//%s/%c%s", host, gtype,
escaped);
else
sprintf(address, "//%s/%c", host, gtype);
}
/* Now output the anchor if not a Gopher error */
if (gtype != GOPHER_ERROR &&
!strstr(address, "error.host") &&
!strstr(address, "errorhost")) {
HTStartAnchor(target, NULL, address);
PUTS(name);
END(HTML_A);
} else
PUTS(name); /* Just put it out, but skip type */
FREE(address);
FREE(escaped);
} else { /* If parse error */
if (PROT_TRACE)
fprintf(stderr, "HTGopher.... Bad menu item, `%s\'\n",
chunk->data);
PUTS(chunk->data);
}
PUTC('\n');
HTChunkClear(chunk);
++files; /* Update number of files */
}
} else
HTChunkPutc(chunk, ch);
}
if (ch < 0)
status = ch;
/* If no files and message is initialized then make error message,
else output the bottom part of the list*/
if (status != HT_INTERRUPTED) {
if (!files && status < 0) {
if (message) {
HTErrorAdd(request, ERR_FATAL, NO, HTERR_GOPHER_SERVER,
(void *) message, strlen(message), "parse_menu");
} else {
HTErrorAdd(request, ERR_FATAL, NO, HTERR_GOPHER_SERVER,
chunk->data, chunk->size, "parse_menu");
}
} else if (target) {
#ifdef OLD_CODE
char *outstr;
if ((outstr = (char *) malloc(100)) == NULL)
outofmem(__FILE__, "parse_menu");
if (files == 0)
sprintf(outstr, "Empty directory");
else if (files == 1)
sprintf(outstr, "1 file");
else
sprintf(outstr, "%u files", files);
START(HTML_HR);
PUTS(outstr);
free(outstr);
#endif /* OLD_CODE */
START(HTML_HR);
if (!files) PUTS("Empty Gopher Menu");
END(HTML_PRE);
/* Put out any messages */
if ((message || info) && HTDirInfo == HT_DIR_INFO_BOTTOM) {
if (message) PUTS(message);
if (info) PUTS(info);
START(HTML_BR);
}
END(HTML_BODY);
END(HTML_HTML);
FREE_TARGET;
} else {
if (PROT_TRACE)
fprintf(stderr, "HTGopher.... Interrupted before any stream was put up.\n");
}
}
/* Cleanup */
FREE(message);
FREE(info);
HTInputSocket_free(gopher->isoc);
HTChunkFree(chunk);
return status;
}
/* Parse a Gopher CSO document
** ============================
**
** Accepts an open socket to a CSO server waiting to send us
** data and puts it on the screen in a reasonable manner.
**
** Perhaps this data can be automatically linked to some
** other source as well???
**
** Taken from hacking by Lou Montulli@ukanaix.cc.ukans.edu
** on XMosaic-1.1, and put on libwww 2.11 by Arthur Secret,
** secret@dxcern.cern.ch.
**
** Returns HT_LOADED on succed, HT_INTERRUPTED if interrupted and -1
** if other error.
*/
PRIVATE int parse_cso ARGS3(HTRequest *, request,
gopher_info *, gopher,
CONST char *, url)
{
int status = -1;
unsigned int records = 0;
int ch;
char *cur_code = NULL;
HTChunk *chunk = HTChunkCreate(128);
HTStructured *target = NULL;
gopher->isoc = HTInputSocket_new(gopher->sockfd);
/* Start grabbing chars from the network */
while ((ch = HTInputSocket_getCharacter(gopher->isoc)) >= 0) {
if (ch == CR || ch == LF) {
if (chunk->size) {
/* OK we now have a line in 'p' lets parse it and print it */
char *strptr;
HTChunkTerminate(chunk);
strptr = chunk->data;
/* If line begins with a 1, then more data is coming, so we
put out the title */
if (*strptr == '1' ||
!strncmp(strptr, "501", 3) || !strncmp(strptr, "502", 3)) {
/* Put up new stream */
target = HTML_new(request, NULL, WWW_HTML,
request->output_format,
request->output_stream);
START(HTML_H1);
PUTS("CSO Search Results");
END(HTML_H1);
/* Output the header line of the list */
START(HTML_PRE); /* To make it look as the other headers */
if (!icon_blank) icon_blank = icon_unknown;
if (HTDirShowMask & HT_DIR_SHOW_ICON && icon_blank) {
HTMLPutImg(target, icon_blank->icon_url,
HTIcon_alt_string(icon_blank->icon_alt, NO),
NULL);
}
PUTC(' ');
PUTS("Record");
PUTC('\n');
START(HTML_HR);
PUTC('\n');
END(HTML_PRE);
}
/* Break on line that begins with a 2. It's the end of data. */
if (*strptr == '2') {
status = HT_LOADED;
break;
}
/* Lines beginning with 5 are errors, generate msg and quit */
if (*strptr == '5') {
char *msgptr = strchr(chunk->data, ':');
if (!msgptr)
msgptr = chunk->data;
else
++msgptr;
if (!strncmp(strptr, "501", 3)) /* No entries */
status = HT_LOADED;
else if (!strncmp(strptr, "502", 3)) { /* Too many */
status = HT_LOADED;
PUTS(msgptr);
} else {
HTErrorAdd(request, ERR_FATAL, NO, HTERR_CSO_SERVER,
(void *) msgptr,
strlen(msgptr), "parse_cso");
}
break;
}
if(*strptr == '-') {
/* data lines look like -200:#:
* where # is the search result number and can be
* multiple digits (infinate?)
* find the second colon and check the digit to the
* left of it to see if they are diferent
* if they are then a different person is starting.
* make this line an <h2>
*/
char *code; /* format: -200:code:field:value */
char *field;
char *value;
if ((code = strchr(strptr, ':')) != NULL &&
(field = strchr(++code, ':')) != NULL) {
*field++ = '\0';
/* Let's do a strcmp instead of numbers */
if (!records) { /* Header of first record */
records++;
START(HTML_H2);
PUTS("Record 1");
END(HTML_H2);
START(HTML_DL);
} else if (cur_code && strcmp(code, cur_code)) {
char recstr[20];
records++;
END(HTML_DL);
START(HTML_H3);
PUTS("Record ");
sprintf(recstr, "%u", records);
PUTS(recstr);
END(HTML_H3);
START(HTML_DL);
} else
START(HTML_DT);
/* I'm not sure whether the name field comes in any
* special order or if its even required in a
* record, so for now the first line is the header
* no matter what it is (it's almost always the
* alias)
*/
if ((value = strchr(field, ':')) == NULL)
value = "Empty?";
else
*value++ = '\0';
{
char *strip = HTStrip(field);
PUTS(strip);
START(HTML_DD);
strip = HTStrip(value);
PUTS(strip);
}
/* save the code for comparison on the next pass */
StrAllocCopy(cur_code, code);
}
} /* end data line */
HTChunkClear(chunk);
} /* end new line */
} else
HTChunkPutc(chunk, ch);
}
if (ch < 0)
status = ch;
/* Put out the bottom line */
if (status != HT_INTERRUPTED) {
if (target) {
char *outstr;
if ((outstr = (char *) malloc(100)) == NULL)
outofmem(__FILE__, "parse_menu");
if (!records)
sprintf(outstr, "No records");
else if (records == 1)
sprintf(outstr, "1 record");
else
sprintf(outstr, "%u records", records);
START(HTML_PRE);
START(HTML_HR);
PUTS(outstr);
END(HTML_PRE);
free(outstr);
FREE_TARGET;
} else {
if (PROT_TRACE)
fprintf(stderr, "HTGopher.... Interrupted before any stream was put up.\n");
}
}
/* Clean up */
HTInputSocket_free(gopher->isoc);
HTChunkFree(chunk);
FREE(cur_code);
return status;
}
/* Display a Gopher Index document
** -------------------------------
*/
PRIVATE void display_index ARGS2(HTRequest *, request,
CONST char *, url)
{
HTStructured *target = HTML_new(request, NULL, WWW_HTML,
request->output_format,
request->output_stream);
START(HTML_H1);
PUTS("Searchable Gopher Index");
END(HTML_H1);
START(HTML_ISINDEX);
if (!HTAnchor_title(request->anchor))
HTAnchor_setTitle(request->anchor, url);
FREE_TARGET;
return;
}
/* Display a CSO index document
** -------------------------------
*/
PRIVATE void display_cso ARGS2(HTRequest *, request,
CONST char *, url)
{
HTStructured *target = HTML_new(request, NULL, WWW_HTML,
request->output_format,
request->output_stream);
START(HTML_H1);
PUTS("Searchable Index of a CSO Name Server");
END(HTML_H1);
START(HTML_ISINDEX);
if (!HTAnchor_title(request->anchor))
HTAnchor_setTitle(request->anchor, url);
FREE_TARGET;
return;
}
/* HTGopher_send_cmd
**
** This function creates a socket and writes the gopher command to it.
** The command must be terminated with <CRLF>
**
** Returns 0 on OK, else <0 but does NOT close the connection
*/
PRIVATE int HTGopher_send_cmd ARGS3(gopher_info *, gopher,
char *, url,
char *, command)
{
int status = 0;
if (!gopher || !command) {
if (PROT_TRACE)
fprintf(stderr, "Gopher Tx... Bad argument!\n");
return -1;
}
if ((status = HTDoConnect((HTNetInfo *) gopher, url, GOPHER_PORT,
NULL, NO)) < 0) {
if (PROT_TRACE)
fprintf(stderr, "HTLoadGopher Connection not established!\n");
return status;
}
if (PROT_TRACE)
fprintf(stderr, "Gopher...... Connected, socket %d\n", gopher->sockfd);
/* Write the command to the socket */
#ifdef NOT_ASCII
{
char * p;
for(p = command; *p; p++) {
*p = TOASCII(*p);
}
}
#endif
if (PROT_TRACE)
fprintf(stderr, "Gopher Tx... %s", command);
if ((status = NETWRITE(gopher->sockfd, command,
(int) strlen(command))) < 0) {
if (PROT_TRACE)
fprintf(stderr, "Gopher...... Error sending command: %s\n",
command);
HTErrorSysAdd(gopher->request, ERR_FATAL, NO, "NETWRITE");
} else
status = 0;
return status;
}
/* Load by name HTLoadGopher
** ============
**
** Given a hypertext address, this routine loads a gopher document
**
** On entry,
** request This is the request structure
** On exit,
** returns <0 Error has occured
** HT_LOADED OK
**
*/
PUBLIC int HTLoadGopher ARGS1(HTRequest *, request)
{
char *url;
int status = -1;
char *command = NULL;
gopher_info *gopher;
if (!request || !request->anchor) {
if (PROT_TRACE) fprintf(stderr, "HTLoadGopher Bad argument\n");
return -1;
}
url = HTAnchor_physical(request->anchor);
if (PROT_TRACE) fprintf(stderr, "HTGopher.... Looking for `%s\'\n", url);
/* Initiate a new gopher structure and bind to resuest structure */
if ((gopher = (gopher_info *) calloc(1, sizeof(gopher_info))) == NULL)
outofmem(__FILE__, "HTLoadGopher");
gopher->sockfd = -1;
gopher->request = request;
request->net_info = (HTNetInfo *) gopher;
gopher->type = GOPHER_MENU;
/* Get entity type, and selector string and generate command */
{
char *path = HTParse(url, "", PARSE_PATH);
char *selector = path;
char *query = NULL;
char *separator = NULL;
if (*selector)
gopher->type = *selector++; /* Pick up gtype */
if (gopher->type == GOPHER_INDEX) {
HTAnchor_setIndex(request->anchor); /* Search is allowed */
query = strchr(selector, '?'); /* Look for search string */
/* Display local "cover page" only if no search requested */
if (!query || !*(query+1)) { /* No search required */
display_index(request, url);
status = HT_LOADED; /* Local function only */
} else {
*query++ = 0; /* Skip '?' */
separator = "\t";
}
} else if (gopher->type == GOPHER_CSO) {
HTAnchor_setIndex(request->anchor); /* Search is allowed */
query = strchr(selector, '?'); /* Look for search string */
/* Display local "cover page" only if no search requested */
if (!query || !*(query+1)) { /* No search required */
display_cso(request, url);
status = HT_LOADED; /* Local function only */
} else {
*query++ = 0; /* Skip '?' */
separator = "query ";
}
}
/* Now generate the final command */
if (status != HT_LOADED) {
char crlf[3];
StrAllocCopy(command, selector);
if (query) {
char *p;
for (p=query; *p; p++) /* Remove plus signs 921006 */
if (*p == '+') *p = ' ';
StrAllocCat(command, separator);
StrAllocCat(command, query);
}
HTUnEscape(command);
HTCleanTelnetString(command); /* Prevent security holes */
*crlf = CR; /* Telnet termination */
*(crlf+1) = LF;
*(crlf+2) = '\0';
StrAllocCat(command, crlf);
}
free(path);
}
/* Now we must ask the server for real data :-( */
if (status != HT_LOADED) {
if ((status = HTGopher_send_cmd(gopher, url, command)) == 0) {
/* Now read the data from the socket: */
switch (gopher->type) {
case GOPHER_HTML:
status = HTParseSocket(WWW_HTML, gopher->sockfd, request);
break;
case GOPHER_GIF:
case GOPHER_IMAGE:
case GOPHER_PLUS_IMAGE:
status = HTParseSocket(HTAtom_for("image/gif"), gopher->sockfd,
request);
break;
case GOPHER_MENU:
case GOPHER_INDEX:
status = parse_menu(request, gopher, url);
break;
case GOPHER_CSO:
status = parse_cso(request, gopher, url);
break;
case GOPHER_MACBINHEX:
case GOPHER_PCBINHEX:
case GOPHER_UUENCODED:
case GOPHER_BINARY:
{
/* Do our own filetyping -- maybe we get lucky */
HTFormat format;
format = HTFileFormat(url, &request->content_encoding,
&request->content_language);
if (format) {
if (PROT_TRACE)
fprintf(stderr, "Gopher...... Figured out content-type myself: %s\n", HTAtom_name(format));
status = HTParseSocket(format, gopher->sockfd,
request);
}
else {
if (PROT_TRACE)
fprintf(stderr,"Gopher...... using www/unknown\n");
/* Specifying WWW_UNKNOWN forces dump to local disk */
HTParseSocket(WWW_UNKNOWN, gopher->sockfd, request);
}
}
break;
case GOPHER_SOUND:
case GOPHER_PLUS_SOUND:
status = HTParseSocket(WWW_AUDIO, gopher->sockfd, request);
break;
case GOPHER_PLUS_MOVIE:
status = HTParseSocket(WWW_VIDEO, gopher->sockfd, request);
break;
/* Try and look at the suffix - maybe it is a PostScript file
so that we should start an externam viewer. */
case GOPHER_TEXT:
default:
{
HTFormat format;
format = HTFileFormat(url, &request->content_encoding,
&request->content_language);
if (format) {
if (PROT_TRACE)
fprintf(stderr, "Gopher...... Figured out content-type myself: %s\n", HTAtom_name(format));
status = HTParseSocket(format, gopher->sockfd,
request);
}
else {
status = HTParseSocket(WWW_PLAINTEXT, gopher->sockfd,
request);
}
}
break;
}
}
/* Close the connection */
if (gopher->sockfd >= 0) {
if (PROT_TRACE) fprintf(stderr, "Gopher...... Closing socket %d\n",
gopher->sockfd);
if (NETCLOSE(gopher->sockfd) < 0)
status = HTErrorSysAdd(request, ERR_FATAL, NO, "NETCLOSE");
}
}
if (status == HT_INTERRUPTED) {
HTErrorAdd(request, ERR_WARNING, NO, HTERR_INTERRUPTED, NULL, 0,
"HTLoadGopher");
}
FREE(command);
free(gopher);
if (status < 0 && status != HT_INTERRUPTED) {
char *unescaped = NULL;
StrAllocCopy(unescaped, url);
HTUnEscape(unescaped);
HTErrorAdd(request, ERR_FATAL, NO, HTERR_INTERNAL, (void *) unescaped,
(int) strlen(unescaped), "HTLoadGopher");
HTAnchor_clearIndex(request->anchor);
free(unescaped);
}
return status;
}
GLOBALDEF PUBLIC HTProtocol HTGopher = { "gopher", HTLoadGopher, NULL, NULL };
|