Annotation of libwww/Library/src/HTGopher.c, revision 2.59

2.32      frystyk     1: /*                                                                  HTGopher.c
                      2: **     GOPHER ACCESS
                      3: **
2.37      frystyk     4: **     (c) COPYRIGHT MIT 1995.
2.32      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
1.1       timbl       6: **
                      7: ** History:
                      8: **     26 Sep 90       Adapted from other accesses (News, HTTP) TBL
                      9: **     29 Nov 91       Downgraded to C, for portable implementation.
2.17      frystyk    10: **     28 Apr 94       target no more global and icons implemented
2.39      frystyk    11: **                     HF, frystyk@w3.org
2.19      luotonen   12: **      2 May 94       Fixed possible security hole when the URL contains
                     13: **                     a newline, that could cause multiple commands to be
                     14: **                     sent to a Gopher server. AL, luotonen@www.cern.ch
2.20      frystyk    15: **     12 May 94       Checked and made ready for multi-threads, Frystyk
2.27      duns       16: **      8 Jul 94  FM   Insulate free() from _free structure element.
2.42      frystyk    17: **        Sep 95  HFN  Made non-blocking and state stream oriented
1.1       timbl      18: */
                     19: 
2.20      frystyk    20: /* Library include files */
2.57      frystyk    21: #include "sysdep.h"
2.59    ! frystyk    22: #include "WWWUtil.h"
        !            23: #include "WWWCore.h"
        !            24: #include "WWWHTML.h"
        !            25: #include "WWWDir.h"
2.20      frystyk    26: #include "HTTCP.h"
2.42      frystyk    27: #include "HTReqMan.h"
2.43      frystyk    28: #include "HTNetMan.h"
2.20      frystyk    29: #include "HTGopher.h"                                   /* Implemented here */
                     30: 
                     31: /* Macros and other defines */
                     32: #ifndef GOPHER_PORT
                     33: #define GOPHER_PORT 70                                 /* See protocol spec */
                     34: #endif
1.2       timbl      35: 
2.20      frystyk    36: /* Hypertext object building machinery */
2.17      frystyk    37: #define PUTC(c) (*target->isa->put_character)(target, c)
                     38: #define PUTS(s) (*target->isa->put_string)(target, s)
                     39: #define START(e) (*target->isa->start_element)(target, e, 0, 0)
                     40: #define END(e) (*target->isa->end_element)(target, e)
2.20      frystyk    41: 
                     42: /* Type definitions and global variables etc. local to this module */
                     43: typedef enum _HTGopherType {
2.42      frystyk    44:     GT_TEXT            = '0',
                     45:     GT_MENU            = '1',
                     46:     GT_CSO             = '2',
                     47:     GT_ERROR           = '3',
                     48:     GT_MACBINHEX       = '4',
                     49:     GT_PCBINHEX                = '5',
                     50:     GT_UUENCODED       = '6',
                     51:     GT_INDEX           = '7',
                     52:     GT_TELNET          = '8',
                     53:     GT_BINARY          = '9',
                     54:     GT_GIF             = 'g',
                     55:     GT_HTML            = 'h',                                       /* HTML */
                     56:     GT_INFO            = 'i',
                     57:     GT_SOUND           = 's',
                     58:     GT_WWW             = 'w',                                 /* W3 address */
                     59:     GT_IMAGE           = 'I',
                     60:     GT_TN3270          = 'T',
                     61:     GT_DUPLICATE       = '+',
                     62:     GT_PLUS_IMAGE      = ':',                  /* Addition from Gopher Plus */
                     63:     GT_PLUS_MOVIE      = ';',
                     64:     GT_PLUS_SOUND      = '<',
                     65:     GT_EOF             = '.'
2.20      frystyk    66: } HTGopherType;
                     67: 
2.42      frystyk    68: /* Final states have negative value */
                     69: typedef enum _GopherState {
                     70:     GOPHER_ERROR       = -3,
                     71:     GOPHER_NO_DATA     = -2,
                     72:     GOPHER_GOT_DATA    = -1,
                     73:     GOPHER_BEGIN       = 0,
                     74:     GOPHER_NEED_CONNECTION,
                     75:     GOPHER_NEED_REQUEST,
                     76:     GOPHER_NEED_RESPONSE
                     77: } GopherState;
1.2       timbl      78: 
2.42      frystyk    79: /* This is the context structure for the this module */
2.20      frystyk    80: typedef struct _gopher_info {
2.30      frystyk    81:     HTGopherType       type;                            /* Gopher item type */
2.42      frystyk    82:     GopherState                state;
                     83:     char *             cmd;
2.20      frystyk    84: } gopher_info;
1.1       timbl      85: 
2.42      frystyk    86: #define MAX_GOPHER_LINE                256
                     87: 
                     88: struct _HTStructured {
2.57      frystyk    89:     const HTStructuredClass *  isa;
2.42      frystyk    90: };
                     91: 
                     92: struct _HTStream {
2.57      frystyk    93:     const HTStreamClass *      isa;
2.42      frystyk    94:     HTStructured *             target;
                     95:     HTRequest *                        request;
2.58      frystyk    96:     HTEOLState                 state;
2.42      frystyk    97:     char *                     url;
                     98:     BOOL                       pre;                   /* Preformatted mode? */
                     99:     BOOL                       junk;                  /* For too long lines */
                    100:     BOOL                       CSO;
                    101:     char                       cso_rec[10];            /* CSO record number */
                    102:     char                       buffer[MAX_GOPHER_LINE+1];
                    103:     int                                buflen;
                    104: };
                    105: 
2.59    ! frystyk   106: struct _HTInputStream {
        !           107:     const HTInputStreamClass * isa;
        !           108: };
        !           109: 
2.45      frystyk   110: PRIVATE HTDirShow      dir_show = HT_DS_ICON;
                    111: 
2.20      frystyk   112: /* ------------------------------------------------------------------------- */
1.1       timbl     113: 
2.42      frystyk   114: /*     GopherIcon
                    115: **     ----------
2.20      frystyk   116: **     This function finds an appopriate icon for the item in the gopher
                    117: **     list. Actually it is only a shell build upon HTGetIcon().
2.17      frystyk   118: */
2.42      frystyk   119: PRIVATE HTIconNode *GopherIcon (HTGopherType type)
2.17      frystyk   120: {
2.36      frystyk   121:     HTFormat   content_type = NULL;
                    122:     HTEncoding content_encoding = NULL;
2.45      frystyk   123:     HTFileMode mode = HT_IS_FILE;
2.42      frystyk   124:     switch(type) {
2.45      frystyk   125:       case GT_MENU:
                    126:        mode = HT_IS_DIR;
2.42      frystyk   127:       case GT_TEXT:
2.17      frystyk   128:        content_type = HTAtom_for("text/void");
                    129:        break;
2.42      frystyk   130:       case GT_IMAGE:
                    131:       case GT_PLUS_IMAGE:
                    132:       case GT_GIF:
2.17      frystyk   133:        content_type = HTAtom_for("image/void");
                    134:        break;
2.42      frystyk   135:       case GT_WWW:
                    136:       case GT_HTML:
2.17      frystyk   137:        content_type = HTAtom_for("text/void");
                    138:        break;
2.42      frystyk   139:       case GT_SOUND:
                    140:       case GT_PLUS_SOUND:
2.17      frystyk   141:        content_type = HTAtom_for("audio/void");
                    142:        break;
2.42      frystyk   143:       case GT_PLUS_MOVIE:
2.20      frystyk   144:        content_type = HTAtom_for("video/void");
2.17      frystyk   145:        break;
2.42      frystyk   146:       case GT_INDEX:
2.17      frystyk   147:        content_type = HTAtom_for("application/x-gopher-index");
                    148:        break;
2.42      frystyk   149:       case GT_CSO:
2.17      frystyk   150:        content_type = HTAtom_for("application/x-gopher-cso");
                    151:        break;
2.42      frystyk   152:       case GT_TELNET:
2.17      frystyk   153:        content_type = HTAtom_for("application/x-gopher-telnet");
                    154:        break;
2.42      frystyk   155:       case GT_TN3270:
2.17      frystyk   156:        content_type = HTAtom_for("application/x-gopher-tn3270");
                    157:        break;
2.42      frystyk   158:       case GT_DUPLICATE:
2.17      frystyk   159:        content_type = HTAtom_for("application/x-gopher-duplicate");
                    160:        break;
2.42      frystyk   161:       case GT_ERROR:
2.17      frystyk   162:        content_type = HTAtom_for("www/unknown");
                    163:        break;
2.42      frystyk   164:       case GT_BINARY:
2.36      frystyk   165:        content_type = WWW_BINARY;
                    166:        break;
2.17      frystyk   167:       default:
                    168:        content_type = HTAtom_for("www/unknown");
                    169:        break;
                    170:     }
2.45      frystyk   171:     return HTGetIcon(mode, content_type, content_encoding);
2.17      frystyk   172: }
                    173: 
2.42      frystyk   174: /* ------------------------------------------------------------------------- */
                    175: /*                                 STREAMS                                  */
                    176: /* ------------------------------------------------------------------------- */
2.17      frystyk   177: 
2.42      frystyk   178: /*     GopherTitle
                    179: **     -----------
                    180: **     Create the top part of the page
1.1       timbl     181: */
2.42      frystyk   182: PRIVATE BOOL GopherTitle (HTStream *me)
                    183: {
                    184:     HTStructured *target = me->target;
                    185:     char *str = NULL;
                    186:     StrAllocCopy(str, me->CSO ? "CSO Search " : "GopherMenu");
2.28      frystyk   187: 
2.42      frystyk   188:     START(HTML_HTML);
                    189:     START(HTML_HEAD);
                    190:     START(HTML_TITLE);
                    191:     if (me->CSO) {
                    192:        char *keyword = strchr(me->url, '?');
                    193:        if (keyword) {
                    194:            StrAllocCat(str, "for ");
                    195:            StrAllocCat(str, ++keyword);
                    196:        }
                    197:     }
                    198:     PUTS(str);
                    199:     END(HTML_TITLE);
                    200:     END(HTML_HEAD);
2.26      frystyk   201: 
2.42      frystyk   202:     START(HTML_BODY);
                    203:     START(HTML_H1);
                    204:     PUTS(str);
                    205:     END(HTML_H1);
2.55      frystyk   206:     HT_FREE(str);
2.42      frystyk   207:     return YES;
                    208: }
1.1       timbl     209: 
2.42      frystyk   210: /*     GopherBottom
                    211: **     ------------
                    212: **     Create the bottom part of the page
                    213: */
                    214: PRIVATE BOOL GopherBottom (HTStream *me)
                    215: {
                    216:     HTStructured *target = me->target;
                    217:     if (me->pre)
                    218:        END(HTML_PRE);
                    219:     END(HTML_BODY);
                    220:     END(HTML_HTML);
                    221:     return YES;
                    222: }
2.17      frystyk   223: 
2.42      frystyk   224: /*     GopherMenuLine
                    225: **     --------------
                    226: **     Parses a Gopher Menu Line
                    227: **     Return YES if more data else NO
                    228: */
                    229: PRIVATE BOOL GopherMenuLine (HTStream *me, char *line)
                    230: {
                    231:     HTStructured *target = me->target;
                    232:     HTGopherType gtype = (HTGopherType) *line++;
                    233:     if (PROT_TRACE)
2.56      eric      234:        HTTrace("HTGopher.... Menu line: `%s\'\n", line);
2.42      frystyk   235:     if (gtype == GT_INFO) {
                    236:        char *stop = strchr(line, '\t');
                    237:        if (stop) *stop = '\0';
                    238:        PUTS(line);
                    239:     } else if (gtype == GT_ERROR) {
                    240:        char *stop = strchr(line, '\t');
                    241:        if (stop) *stop = '\0';
                    242:        PUTS(line);
                    243:     } else if ((strstr(line, "error.host") || strstr(line, "errorhost"))) {
                    244:        char *stop = strchr(line, '\t');              /* Chop off error.host */
                    245:        if (stop) *stop = '\0';
                    246:        PUTS(line);
                    247:     } else if (gtype == GT_EOF) {
                    248:        return NO;
                    249:     } else {                               /* Parse normal gopher menu line */
                    250:        char *name = line;                           /* Get link information */
                    251:        char *selector = strchr(name, '\t');
                    252:        char *host = NULL;
                    253:        char *port = NULL;
                    254:        if (selector) {
                    255:            *selector++ = '\0';
                    256:            if ((host = strchr(selector, '\t'))) {
                    257:                *host++ = '\0';
                    258:                if ((port = strchr(host, '\t'))) {
                    259:                    char *junk;
                    260:                    *port = ':';                     /* delimit host a la W3 */
                    261:                    if ((junk = strchr(port, '\t')) != NULL)
                    262:                        *junk = '\0';                           /* Chop port */
                    263:                    if (*(port+1) == '0' && !*(port+2))
                    264:                        *port = '\0';
2.21      frystyk   265:                }
2.42      frystyk   266:            }
                    267:        }
                    268:        if (!me->pre) {               /* For now we use preformatted listing */
                    269:            START(HTML_PRE);
                    270:            me->pre = YES;
                    271:        }
2.45      frystyk   272:        if (dir_show & HT_DS_ICON) {                     /* Put out the icon */
2.42      frystyk   273:            HTIconNode *icon = GopherIcon(gtype);
2.52      frystyk   274:            if (icon) {
2.42      frystyk   275:                HTMLPutImg(target, icon->icon_url,
                    276:                           HTIcon_alt_string(icon->icon_alt, YES), NULL);
                    277:                PUTC(' ');
                    278:            }
                    279:        }
                    280:        if (gtype == GT_WWW) {                       /* Gopher pointer to W3 */
                    281:            char *escaped = NULL;
                    282:            escaped = HTEscape(selector, URL_PATH);
                    283:            HTStartAnchor(target, NULL, escaped);
                    284:            PUTS(name);
                    285:            END(HTML_A);
2.55      frystyk   286:            HT_FREE(escaped);
2.42      frystyk   287:        } else if (port) {                          /* Other types need port */
                    288:            char *escaped = NULL;
                    289:            char *address = NULL;
                    290:            int addr_len;
                    291:            
                    292:            /* Calculate the length of the WWW-address */
                    293:            if (selector && *selector) {
                    294:                escaped = HTEscape(selector, URL_PATH);
                    295:                addr_len = 15 + strlen(escaped) + strlen(host) + 1;
                    296:            } else {
                    297:                addr_len = 15 + strlen(host) + 1;
                    298:            }
2.55      frystyk   299:            if ((address = (char *) HT_MALLOC(addr_len)) == NULL)
                    300:                HT_OUTOFMEM("GopherMenuLine");
2.42      frystyk   301:            *address = '\0';
                    302: 
                    303:            if (gtype == GT_TELNET) {
                    304:                if (escaped)
                    305:                    sprintf(address, "telnet://%s@%s/", escaped, host);
                    306:                else
                    307:                    sprintf(address, "telnet://%s/", host);
                    308:            } else if (gtype == GT_TN3270) {
                    309:                if (escaped)
                    310:                    sprintf(address, "tn3270://%s@%s/", escaped, host);
                    311:                else
                    312:                    sprintf(address, "tn3270://%s/", host);
                    313:            } else {
                    314:                if (escaped)
                    315:                    sprintf(address, "//%s/%c%s", host, gtype, escaped);
                    316:                else
                    317:                    sprintf(address, "//%s/%c", host, gtype);
                    318:            }
                    319:            
                    320:            HTStartAnchor(target, NULL, address);
                    321:            PUTS(name);
                    322:            END(HTML_A);
2.55      frystyk   323:            HT_FREE(address);
                    324:            HT_FREE(escaped);
2.42      frystyk   325:            PUTC('\n');
                    326:        } else {                                           /* If parse error */
                    327:            if (PROT_TRACE)
2.56      eric      328:                HTTrace("HTGopher.... Bad menu item, `%s\'\n", line);
2.42      frystyk   329:        }
                    330:     }
                    331:     return YES;
                    332: }
2.21      frystyk   333: 
2.42      frystyk   334: /*     GopherCSOLine
                    335: **     --------------
                    336: **     Parses a Gopher Menu Line
                    337: **     Return YES if more data else NO
                    338: */
                    339: PRIVATE BOOL GopherCSOLine (HTStream *me, char *line)
                    340: {
                    341:     HTStructured *target = me->target;
                    342:     if (*line == '1') {                                         /* Information line */
                    343:        char *start = strchr(line, ':');
                    344:        start = start ? ++start : line;
                    345:        PUTS(start);
                    346:     } else if (*line == '2') {                         /* Transfer complete */
                    347:        return NO;
                    348:     } else if (*line == '5') {                                     /* Error */
                    349:        char *start = strchr(line, ':');
                    350:        start = start ? ++start : line;
                    351:        PUTS(start);
                    352:     } else if (*line == '-') {                                      /* data */
                    353:        /*  data lines look like '-200:code:field:value'
                    354:         *  where code is the search result number and can be  
                    355:         *  multiple digits (infinte?)
                    356:         *  find the second colon and check the digit to the
                    357:         *  left of it to see if they are diferent
                    358:         *  if they are then a different person is starting. 
                    359:         */
                    360:        char *code;
                    361:        char *field;
                    362:        if ((code = strchr(line, ':')) && (field = strchr(++code, ':'))) {
                    363:            BOOL newrec = YES;
                    364:            *field++ = '\0';
                    365:            if (!*me->cso_rec) {                   /* Header of first record */
                    366:                START(HTML_DL);
                    367:            } else if (strcmp(me->cso_rec, code)) {    /* Another new record */
                    368:                START(HTML_B);
                    369:            } else
                    370:                newrec = NO;
                    371:            START(HTML_DT);
                    372:            
                    373:            /* I'm not sure whether the name field comes in any
                    374:             *  special order or if its even required in a 
                    375:             *  record, so for now the first line is the header
                    376:             *  no matter what it is (it's almost always the
                    377:             *  alias)
                    378:             */
                    379:            {
                    380:                char *value = strchr(field, ':');
                    381:                if (!value)
                    382:                    value = "Empty value";
                    383:                else
                    384:                    *value++ = '\0';
                    385:                {
                    386:                    char *strip = HTStrip(field);
                    387:                    PUTS(strip);
                    388:                    START(HTML_DD);
                    389:                    strip = HTStrip(value);
                    390:                    if (newrec) {
                    391:                        PUTS(strip);
                    392:                        END(HTML_B);
2.20      frystyk   393:                    } else
2.42      frystyk   394:                        PUTS(strip);
                    395:            }
2.20      frystyk   396: 
2.42      frystyk   397:            /* Save the code for comparison on the next pass */
                    398:            strcpy(me->cso_rec, code);
2.20      frystyk   399:            }
2.42      frystyk   400:        }
                    401:     } else {                                                /* Unknown line */
                    402:        char *start = strchr(line, ':');
                    403:        start = start ? ++start : line;
                    404:        PUTS(start);
2.20      frystyk   405:     }
2.42      frystyk   406:     return YES;
                    407: }
1.2       timbl     408: 
2.42      frystyk   409: /*
                    410: **  Searches for Gopher line until buffer fills up or a CRLF or LF is found
                    411: */
2.57      frystyk   412: PRIVATE int GopherMenu_put_block (HTStream * me, const char * b, int l)
2.42      frystyk   413: {
                    414:     while (l-- > 0) {
                    415:        if (me->state == EOL_FCR) {
                    416:            if (*b == LF && me->buflen) {
                    417:                if (!me->junk) {
                    418:                    BOOL cont;
                    419:                    *(me->buffer+me->buflen) = '\0';
                    420:                    cont = me->CSO ? GopherCSOLine(me, me->buffer) :
                    421:                        GopherMenuLine(me, me->buffer);
                    422:                    if (cont == NO) return HT_LOADED;
                    423:                } else
                    424:                    me->junk = NO;                         /* back to normal */
2.20      frystyk   425:            }
2.45      frystyk   426:            me->buflen = 0;
                    427:            me->state = EOL_BEGIN;
2.42      frystyk   428:        } else if (*b == CR) {
                    429:            me->state = EOL_FCR;
                    430:        } else if (*b == LF && me->buflen) {
                    431:            if (!me->junk) {
                    432:                BOOL cont;
                    433:                *(me->buffer+me->buflen) = '\0';
                    434:                cont = me->CSO ? GopherCSOLine(me, me->buffer) :
                    435:                    GopherMenuLine(me, me->buffer);
                    436:                if (cont == NO) return HT_LOADED;
                    437:            } else
                    438:                me->junk = NO;                             /* back to normal */
                    439:            me->buflen = 0;
                    440:            me->state = EOL_BEGIN;
                    441:        } else {
                    442:            *(me->buffer+me->buflen++) = *b;
                    443:            if (me->buflen >= MAX_GOPHER_LINE) {
                    444:                if (PROT_TRACE)
2.56      eric      445:                    HTTrace("Gopher...... Line too long - ignored\n");
2.42      frystyk   446:                me->buflen = 0;
                    447:                me->junk = YES;
2.20      frystyk   448:            }
                    449:        }
2.42      frystyk   450:        b++;
2.17      frystyk   451:     }
2.42      frystyk   452:     return HT_OK;
                    453: }
2.17      frystyk   454: 
2.57      frystyk   455: PRIVATE int GopherMenu_put_string (HTStream * me, const char* s)
2.42      frystyk   456: {
                    457:     return GopherMenu_put_block(me, s, (int) strlen(s));
1.1       timbl     458: }
2.11      timbl     459: 
2.50      frystyk   460: PRIVATE int GopherMenu_put_character (HTStream * me, char c)
2.42      frystyk   461: {
                    462:     return GopherMenu_put_block(me, &c, 1);
                    463: }
2.11      timbl     464: 
2.50      frystyk   465: PRIVATE int GopherMenu_flush (HTStream * me)
2.42      frystyk   466: {    
                    467:     return (*me->target->isa->flush)(me->target);
                    468: }
                    469: 
2.50      frystyk   470: PRIVATE int GopherMenu_free (HTStream * me)
2.42      frystyk   471: {
                    472:     int status = HT_OK;
                    473:     GopherBottom(me);
                    474:     if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
                    475:        return HT_WOULD_BLOCK;
2.55      frystyk   476:     HT_FREE(me);
2.42      frystyk   477:     return HT_OK;
                    478: }
                    479: 
2.50      frystyk   480: PRIVATE int GopherMenu_abort (HTStream * me, HTList * e)
2.42      frystyk   481: {
                    482:     (*me->target->isa->abort)(me->target, e);
2.55      frystyk   483:     HT_FREE(me);
2.42      frystyk   484:     if (PROT_TRACE)
2.56      eric      485:        HTTrace("Gopher...... ABORTING...\n");
2.42      frystyk   486:     return HT_ERROR;
                    487: }
                    488: 
                    489: /*     Goper Menu Stream
                    490: **     -----------------
2.20      frystyk   491: */
2.57      frystyk   492: PRIVATE const HTStreamClass GopherMenuClass =
2.42      frystyk   493: {              
                    494:     "GopherMenu",
                    495:     GopherMenu_flush,
                    496:     GopherMenu_free,
                    497:     GopherMenu_abort,
                    498:     GopherMenu_put_character,
                    499:     GopherMenu_put_string,
                    500:     GopherMenu_put_block
                    501: };
2.26      frystyk   502: 
2.42      frystyk   503: /*
                    504: **  Stream for creating a HTML object out of a Gopher Menu object
                    505: */
                    506: PRIVATE HTStream * GopherMenu_new (HTRequest * request, char *url, BOOL CSO)
                    507: {
2.55      frystyk   508:     HTStream * me;
                    509:     if ((me = (HTStream  *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
                    510:         HT_OUTOFMEM("GopherMenu_new");
2.42      frystyk   511:     me->isa = &GopherMenuClass;
                    512:     me->target = HTMLGenerator(request, NULL, WWW_HTML,
                    513:                               request->output_format, request->output_stream);
                    514:     HTAnchor_setFormat(request->anchor, WWW_HTML);
                    515:     me->request = request;    
                    516:     me->state = EOL_BEGIN;
                    517:     me->url = url;
                    518:     me->CSO = CSO;
                    519:     GopherTitle(me);
                    520:     return me;
                    521: }
2.20      frystyk   522: 
2.42      frystyk   523: /* ------------------------------------------------------------------------- */
                    524: /*                             GOPHER STATE MACHINE                         */
                    525: /* ------------------------------------------------------------------------- */
2.7       secret    526: 
2.42      frystyk   527: /*     GopherCleanup
                    528: **     -------------
                    529: **      This function closes the connection and frees memory.
                    530: **      Returns YES if OK, else NO
                    531: */
                    532: PRIVATE BOOL GopherCleanup (HTRequest *req, int status)
                    533: {
                    534:     HTNet *net = req->net;
                    535:     gopher_info *gopher = (gopher_info *) net->context;
2.20      frystyk   536: 
2.42      frystyk   537:     /* Free stream with data TO network */
                    538:     if (req->input_stream) {
                    539:        if (status == HT_INTERRUPTED)
                    540:            (*req->input_stream->isa->abort)(req->input_stream, NULL);
                    541:        else
                    542:            (*req->input_stream->isa->_free)(req->input_stream);
2.20      frystyk   543:     }
                    544: 
2.42      frystyk   545:     /* Remove the request object and our own context structure for gopher */
                    546:     HTNet_delete(net, status);
                    547:     if (gopher) {
2.55      frystyk   548:        HT_FREE(gopher->cmd);
                    549:        HT_FREE(gopher);
2.34      frystyk   550:     }
2.42      frystyk   551:     return YES;
2.20      frystyk   552: }
2.7       secret    553: 
1.1       timbl     554: /*     Display a Gopher Index document
2.20      frystyk   555: **     -------------------------------
2.42      frystyk   556: **     We create a small HTML object as we have no network activity
2.20      frystyk   557: */
2.42      frystyk   558: PRIVATE void display_index (HTRequest * request)
1.1       timbl     559: {
2.40      frystyk   560:     HTStructured *target = HTMLGenerator(request, NULL, WWW_HTML,
2.42      frystyk   561:                                         request->output_format,
                    562:                                         request->output_stream);
                    563:     /* Update anchor information */
2.36      frystyk   564:     HTAnchor_setFormat(request->anchor, WWW_HTML);
2.42      frystyk   565:     HTAnchor_setTitle(request->anchor, "Searchable Gopher Index");
                    566:     /* @@@ We don't set Content-Length */
                    567: 
2.34      frystyk   568:     START(HTML_HTML);
                    569:     START(HTML_HEAD);
                    570:     START(HTML_TITLE);
                    571:     PUTS("Searchable Gopher Index");
                    572:     END(HTML_TITLE);
                    573:     END(HTML_HEAD);
                    574:     START(HTML_BODY);
                    575: 
1.2       timbl     576:     START(HTML_H1);
2.20      frystyk   577:     PUTS("Searchable Gopher Index");
1.2       timbl     578:     END(HTML_H1);
2.7       secret    579:     START(HTML_ISINDEX);
2.34      frystyk   580:     END(HTML_BODY);
                    581:     END(HTML_HTML);
2.42      frystyk   582:     (*target->isa->_free)(target);
2.7       secret    583: }
                    584: 
                    585: 
                    586: /*      Display a CSO index document
                    587: **      -------------------------------
2.42      frystyk   588: **     We create a small HTML object as we have no network activity
2.7       secret    589: */
2.42      frystyk   590: PRIVATE void display_cso (HTRequest * request)
2.7       secret    591: {
2.40      frystyk   592:     HTStructured *target = HTMLGenerator(request, NULL, WWW_HTML,
2.42      frystyk   593:                                         request->output_format,
                    594:                                         request->output_stream);
                    595:     /* Update anchor information */
2.36      frystyk   596:     HTAnchor_setFormat(request->anchor, WWW_HTML);
2.42      frystyk   597:     HTAnchor_setTitle(request->anchor, "Searchable SCO Index");
                    598:     /* @@@ We don't set Content-Length */
                    599: 
2.34      frystyk   600:     START(HTML_HTML);
                    601:     START(HTML_HEAD);
                    602:     START(HTML_TITLE);
                    603:     PUTS("Searchable Index of a CSO Name Server");
                    604:     END(HTML_TITLE);
                    605:     END(HTML_HEAD);
                    606:     START(HTML_BODY);
                    607: 
2.7       secret    608:     START(HTML_H1);
2.20      frystyk   609:     PUTS("Searchable Index of a CSO Name Server");
2.7       secret    610:     END(HTML_H1);
2.34      frystyk   611:     PUTS("A CSO Name Server usually provides directory information about people.");
2.7       secret    612:     START(HTML_ISINDEX);
2.34      frystyk   613:     END(HTML_BODY);
                    614:     END(HTML_HTML);
2.42      frystyk   615:     (*target->isa->_free)(target);
1.1       timbl     616: }
                    617: 
2.42      frystyk   618: /*     HTLoadGopher
                    619: **     ------------
2.24      frystyk   620: **     Given a hypertext address, this routine loads a gopher document
                    621: **
                    622: ** On entry,
2.42      frystyk   623: **      request                This is the request structure
                    624: ** On Exit
                    625: **     returns         HT_ERROR        Error has occured in call back
                    626: **                     HT_OK           Call back was OK
1.1       timbl     627: */
2.42      frystyk   628: PUBLIC int HTLoadGopher (SOCKET soc, HTRequest * request, SockOps ops)
1.1       timbl     629: {
2.42      frystyk   630:     int status = HT_ERROR;
                    631:     HTNet *net = request->net;
2.20      frystyk   632:     gopher_info *gopher;
2.42      frystyk   633:     char *url = HTAnchor_physical(request->anchor);
2.20      frystyk   634:     
2.42      frystyk   635:     /*
                    636:     ** Initiate a new gopher structure and bind to request structure
                    637:     ** This is actually state GOPHER_BEGIN, but it can't be in the state
                    638:     ** machine as we need the structure first.
                    639:     */
                    640:     if (ops == FD_NONE) {
2.56      eric      641:        if (PROT_TRACE) HTTrace("Gopher...... Looking for `%s\'\n",url);
2.55      frystyk   642:        if ((gopher = (gopher_info *) HT_CALLOC(1, sizeof(gopher_info))) == NULL)
                    643:            HT_OUTOFMEM("HTLoadGopher");
2.42      frystyk   644:        gopher->type = GT_MENU;
                    645:        gopher->state = GOPHER_BEGIN;
                    646:        net->context = gopher;
2.44      frystyk   647:     } else if (ops == FD_CLOSE) {                            /* Interrupted */
2.42      frystyk   648:        GopherCleanup(request, HT_INTERRUPTED);
                    649:        return HT_OK;
                    650:     } else
                    651:        gopher = (gopher_info *) net->context;          /* Get existing copy */
                    652: 
                    653:     /* Now jump into the machine. We know the state from the previous run */
                    654:     while (1) {
                    655:        switch (gopher->state) {
                    656: 
                    657:          case GOPHER_BEGIN:         /* Get entity type, and selector string */
                    658:            {
                    659:                char *path = HTParse(url, "", PARSE_PATH);
                    660:                char *selector = path;
                    661:                char *query = NULL;
                    662:                char *separator = NULL;
                    663:                if (*selector) gopher->type = (HTGopherType) *selector++;
                    664:                if (gopher->type == GT_INDEX) {
                    665:                    HTAnchor_setIndex(request->anchor);          /* Is index */
                    666:                    query = strchr(selector, '?');
                    667: 
                    668:                    /* Display local cover page only if no search requested */
                    669:                    if (!query || !*(query+1)) {       /* No search required */
                    670:                        display_index(request);
                    671:                        gopher->state = GOPHER_GOT_DATA;
2.55      frystyk   672:                        HT_FREE(path);
2.42      frystyk   673:                        break;
                    674:                    } else {
                    675:                        *query++ = 0;                            /* Skip '?' */
                    676:                        separator = "\t";
                    677:                    }
                    678:                } else if (gopher->type == GT_CSO) {
                    679:                    HTAnchor_setIndex(request->anchor); /* Search is allowed */
                    680:                    query = strchr(selector, '?'); /* Look for search string */
                    681:                    
                    682:                    /* Display local cover page only if no search requested */
                    683:                    if (!query || !*(query+1)) {       /* No search required */
                    684:                        display_cso(request);
                    685:                        gopher->state = GOPHER_GOT_DATA;
2.55      frystyk   686:                        HT_FREE(path);
2.42      frystyk   687:                        break;
                    688:                    } else {
                    689:                        *query++ = 0;                            /* Skip '?' */
                    690:                        *selector = 0;
                    691:                        separator = "query ";
                    692:                    }
                    693:                }
2.20      frystyk   694: 
2.42      frystyk   695:                /* Now generate the final command */
                    696:                {
                    697:                    char crlf[3];
                    698:                    StrAllocCopy(gopher->cmd, selector);
                    699:                    if (query) {
                    700:                        char *p;
                    701:                        for (p=query; *p; p++)   /* Remove plus signs 921006 */
                    702:                            if (*p == '+') *p = ' ';
                    703:                        StrAllocCat(gopher->cmd, separator);
                    704:                        StrAllocCat(gopher->cmd, query);
                    705:                    }
                    706:                    HTUnEscape(gopher->cmd);
                    707:                    HTCleanTelnetString(gopher->cmd);   /* Prevent sec holes */
                    708:                    *crlf = CR;                        /* Telnet termination */
                    709:                    *(crlf+1) = LF;
                    710:                    *(crlf+2) = '\0';
                    711:                    StrAllocCat(gopher->cmd, crlf);
                    712:                }
2.55      frystyk   713:                HT_FREE(path);
2.42      frystyk   714:                gopher->state = GOPHER_NEED_CONNECTION;
1.1       timbl     715:            }
2.42      frystyk   716:            break;
                    717: 
                    718:          case GOPHER_NEED_CONNECTION:
2.43      frystyk   719:            status = HTDoConnect(net, url, GOPHER_PORT);
2.42      frystyk   720:            if (status == HT_OK) {
2.59    ! frystyk   721:                /*
        !           722:                ** Check the protocol class to see if we have connected to a
        !           723:                ** the right class of server, in this case HTTP.
        !           724:                */
        !           725:                {
        !           726:                    HTHost * host = HTNet_host(net);
        !           727:                    char * s_class = HTHost_class(host);
        !           728:                    if (s_class && strcasecomp(s_class, "gopher")) {
        !           729:                        HTRequest_addError(request, ERR_FATAL, NO, HTERR_CLASS,
        !           730:                                           NULL, 0, "HTLoadGopher");
        !           731:                        gopher->state = GOPHER_ERROR;
        !           732:                        break;
        !           733:                    }
        !           734:                    HTHost_setClass(host, "gopher");
        !           735:                }
2.42      frystyk   736: 
2.59    ! frystyk   737:                /* 
        !           738:                ** Create the stream pipe FROM the channel to the application.
        !           739:                ** The target for the input stream pipe is set up using the
        !           740:                ** stream stack.
        !           741:                */
        !           742:                {
        !           743:                    HTStream * target;
        !           744:                    if (gopher->type == GT_MENU || gopher->type == GT_INDEX)
        !           745:                        target = GopherMenu_new(request, url, NO);
        !           746:                    else if (gopher->type == GT_CSO)
        !           747:                        target = GopherMenu_new(request, url, YES);
        !           748:                    else
        !           749:                        target = HTStreamStack(WWW_UNKNOWN,
        !           750:                                               request->output_format,
        !           751:                                               request->output_stream,
        !           752:                                               request, NO);
        !           753:                    HTNet_getInput(net, target, NULL, 0);
        !           754:                }
2.42      frystyk   755: 
2.59    ! frystyk   756:                /*
        !           757:                ** Create the stream pipe TO the channel from the application
        !           758:                ** and hook it up to the request object
        !           759:                */
        !           760:                {
        !           761:                    HTOutputStream * output = HTNet_getOutput(net, NULL, 0);
        !           762:                    HTRequest_setInputStream(request, (HTStream *) output);
        !           763:                }
2.42      frystyk   764:                gopher->state = GOPHER_NEED_REQUEST;
2.59    ! frystyk   765: 
2.42      frystyk   766:            } else if (status == HT_WOULD_BLOCK)
                    767:                return HT_OK;
                    768:            else
                    769:                gopher->state = GOPHER_ERROR;
                    770:            break;
                    771: 
                    772:          case GOPHER_NEED_REQUEST:
2.56      eric      773:            if (PROT_TRACE) HTTrace("Gopher Tx... `%s\'", gopher->cmd);
2.42      frystyk   774:            status = (*request->input_stream->isa->put_block)
                    775:                (request->input_stream, gopher->cmd, strlen(gopher->cmd));
                    776:            if (status == HT_WOULD_BLOCK)
                    777:                return HT_OK;
                    778:            else if (status == HT_ERROR)
                    779:                gopher->state = GOPHER_ERROR;
                    780:            else
                    781:                gopher->state = GOPHER_NEED_RESPONSE;
                    782:            break;
1.1       timbl     783: 
2.42      frystyk   784:          case GOPHER_NEED_RESPONSE:
2.59    ! frystyk   785:            status = (*net->input->isa->read)(net->input);
2.42      frystyk   786:            if (status == HT_WOULD_BLOCK)
                    787:                return HT_OK;
2.53      frystyk   788:            else if (status == HT_LOADED || status == HT_CLOSED)
2.42      frystyk   789:                gopher->state = GOPHER_GOT_DATA;
                    790:            else
                    791:                gopher->state = GOPHER_ERROR;
                    792:            break;
1.2       timbl     793: 
2.42      frystyk   794:          case GOPHER_GOT_DATA:
                    795:            GopherCleanup(request, HT_LOADED);
                    796:            return HT_OK;
                    797:            break;
                    798: 
                    799:          case GOPHER_NO_DATA:
                    800:            GopherCleanup(request, HT_NO_DATA);
                    801:            return HT_OK;
                    802:            break;
                    803: 
                    804:          case GOPHER_ERROR:
                    805:            GopherCleanup(request, HT_ERROR);
                    806:            return HT_OK;
                    807:            break;
2.25      frystyk   808:        }
2.42      frystyk   809:     }  /* while(1) */
1.1       timbl     810: }

Webmaster