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

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.60      frystyk    26: #include "WWWTrans.h"
2.43      frystyk    27: #include "HTNetMan.h"
2.20      frystyk    28: #include "HTGopher.h"                                   /* Implemented here */
                     29: 
                     30: /* Macros and other defines */
                     31: #ifndef GOPHER_PORT
                     32: #define GOPHER_PORT 70                                 /* See protocol spec */
                     33: #endif
1.2       timbl      34: 
2.20      frystyk    35: /* Hypertext object building machinery */
2.17      frystyk    36: #define PUTC(c) (*target->isa->put_character)(target, c)
                     37: #define PUTS(s) (*target->isa->put_string)(target, s)
                     38: #define START(e) (*target->isa->start_element)(target, e, 0, 0)
                     39: #define END(e) (*target->isa->end_element)(target, e)
2.20      frystyk    40: 
                     41: /* Type definitions and global variables etc. local to this module */
                     42: typedef enum _HTGopherType {
2.42      frystyk    43:     GT_TEXT            = '0',
                     44:     GT_MENU            = '1',
                     45:     GT_CSO             = '2',
                     46:     GT_ERROR           = '3',
                     47:     GT_MACBINHEX       = '4',
                     48:     GT_PCBINHEX                = '5',
                     49:     GT_UUENCODED       = '6',
                     50:     GT_INDEX           = '7',
                     51:     GT_TELNET          = '8',
                     52:     GT_BINARY          = '9',
                     53:     GT_GIF             = 'g',
                     54:     GT_HTML            = 'h',                                       /* HTML */
                     55:     GT_INFO            = 'i',
                     56:     GT_SOUND           = 's',
                     57:     GT_WWW             = 'w',                                 /* W3 address */
                     58:     GT_IMAGE           = 'I',
                     59:     GT_TN3270          = 'T',
                     60:     GT_DUPLICATE       = '+',
                     61:     GT_PLUS_IMAGE      = ':',                  /* Addition from Gopher Plus */
                     62:     GT_PLUS_MOVIE      = ';',
                     63:     GT_PLUS_SOUND      = '<',
                     64:     GT_EOF             = '.'
2.20      frystyk    65: } HTGopherType;
                     66: 
2.42      frystyk    67: /* Final states have negative value */
                     68: typedef enum _GopherState {
                     69:     GOPHER_ERROR       = -3,
                     70:     GOPHER_NO_DATA     = -2,
                     71:     GOPHER_GOT_DATA    = -1,
                     72:     GOPHER_BEGIN       = 0,
                     73:     GOPHER_NEED_CONNECTION,
                     74:     GOPHER_NEED_REQUEST,
                     75:     GOPHER_NEED_RESPONSE
                     76: } GopherState;
1.2       timbl      77: 
2.42      frystyk    78: /* This is the context structure for the this module */
2.20      frystyk    79: typedef struct _gopher_info {
2.30      frystyk    80:     HTGopherType       type;                            /* Gopher item type */
2.42      frystyk    81:     GopherState                state;
                     82:     char *             cmd;
2.20      frystyk    83: } gopher_info;
1.1       timbl      84: 
2.42      frystyk    85: #define MAX_GOPHER_LINE                256
                     86: 
                     87: struct _HTStructured {
2.57      frystyk    88:     const HTStructuredClass *  isa;
2.42      frystyk    89: };
                     90: 
                     91: struct _HTStream {
2.57      frystyk    92:     const HTStreamClass *      isa;
2.42      frystyk    93:     HTStructured *             target;
                     94:     HTRequest *                        request;
2.58      frystyk    95:     HTEOLState                 state;
2.42      frystyk    96:     char *                     url;
                     97:     BOOL                       pre;                   /* Preformatted mode? */
                     98:     BOOL                       junk;                  /* For too long lines */
                     99:     BOOL                       CSO;
                    100:     char                       cso_rec[10];            /* CSO record number */
                    101:     char                       buffer[MAX_GOPHER_LINE+1];
                    102:     int                                buflen;
                    103: };
                    104: 
2.59      frystyk   105: struct _HTInputStream {
                    106:     const HTInputStreamClass * isa;
                    107: };
                    108: 
2.45      frystyk   109: PRIVATE HTDirShow      dir_show = HT_DS_ICON;
                    110: 
2.20      frystyk   111: /* ------------------------------------------------------------------------- */
1.1       timbl     112: 
2.42      frystyk   113: /*     GopherIcon
                    114: **     ----------
2.20      frystyk   115: **     This function finds an appopriate icon for the item in the gopher
2.61    ! frystyk   116: **     list. Actually it is only a shell build upon HTIcon_find().
2.17      frystyk   117: */
2.42      frystyk   118: PRIVATE HTIconNode *GopherIcon (HTGopherType type)
2.17      frystyk   119: {
2.36      frystyk   120:     HTFormat   content_type = NULL;
                    121:     HTEncoding content_encoding = NULL;
2.45      frystyk   122:     HTFileMode mode = HT_IS_FILE;
2.42      frystyk   123:     switch(type) {
2.45      frystyk   124:       case GT_MENU:
                    125:        mode = HT_IS_DIR;
2.42      frystyk   126:       case GT_TEXT:
2.17      frystyk   127:        content_type = HTAtom_for("text/void");
                    128:        break;
2.42      frystyk   129:       case GT_IMAGE:
                    130:       case GT_PLUS_IMAGE:
                    131:       case GT_GIF:
2.17      frystyk   132:        content_type = HTAtom_for("image/void");
                    133:        break;
2.42      frystyk   134:       case GT_WWW:
                    135:       case GT_HTML:
2.17      frystyk   136:        content_type = HTAtom_for("text/void");
                    137:        break;
2.42      frystyk   138:       case GT_SOUND:
                    139:       case GT_PLUS_SOUND:
2.17      frystyk   140:        content_type = HTAtom_for("audio/void");
                    141:        break;
2.42      frystyk   142:       case GT_PLUS_MOVIE:
2.20      frystyk   143:        content_type = HTAtom_for("video/void");
2.17      frystyk   144:        break;
2.42      frystyk   145:       case GT_INDEX:
2.17      frystyk   146:        content_type = HTAtom_for("application/x-gopher-index");
                    147:        break;
2.42      frystyk   148:       case GT_CSO:
2.17      frystyk   149:        content_type = HTAtom_for("application/x-gopher-cso");
                    150:        break;
2.42      frystyk   151:       case GT_TELNET:
2.17      frystyk   152:        content_type = HTAtom_for("application/x-gopher-telnet");
                    153:        break;
2.42      frystyk   154:       case GT_TN3270:
2.17      frystyk   155:        content_type = HTAtom_for("application/x-gopher-tn3270");
                    156:        break;
2.42      frystyk   157:       case GT_DUPLICATE:
2.17      frystyk   158:        content_type = HTAtom_for("application/x-gopher-duplicate");
                    159:        break;
2.42      frystyk   160:       case GT_ERROR:
2.17      frystyk   161:        content_type = HTAtom_for("www/unknown");
                    162:        break;
2.42      frystyk   163:       case GT_BINARY:
2.36      frystyk   164:        content_type = WWW_BINARY;
                    165:        break;
2.17      frystyk   166:       default:
                    167:        content_type = HTAtom_for("www/unknown");
                    168:        break;
                    169:     }
2.61    ! frystyk   170:     return HTIcon_find(mode, content_type, content_encoding);
2.17      frystyk   171: }
                    172: 
2.42      frystyk   173: /* ------------------------------------------------------------------------- */
                    174: /*                                 STREAMS                                  */
                    175: /* ------------------------------------------------------------------------- */
2.17      frystyk   176: 
2.42      frystyk   177: /*     GopherTitle
                    178: **     -----------
                    179: **     Create the top part of the page
1.1       timbl     180: */
2.42      frystyk   181: PRIVATE BOOL GopherTitle (HTStream *me)
                    182: {
                    183:     HTStructured *target = me->target;
                    184:     char *str = NULL;
                    185:     StrAllocCopy(str, me->CSO ? "CSO Search " : "GopherMenu");
2.28      frystyk   186: 
2.42      frystyk   187:     START(HTML_HTML);
                    188:     START(HTML_HEAD);
                    189:     START(HTML_TITLE);
                    190:     if (me->CSO) {
                    191:        char *keyword = strchr(me->url, '?');
                    192:        if (keyword) {
                    193:            StrAllocCat(str, "for ");
                    194:            StrAllocCat(str, ++keyword);
                    195:        }
                    196:     }
                    197:     PUTS(str);
                    198:     END(HTML_TITLE);
                    199:     END(HTML_HEAD);
2.26      frystyk   200: 
2.42      frystyk   201:     START(HTML_BODY);
                    202:     START(HTML_H1);
                    203:     PUTS(str);
                    204:     END(HTML_H1);
2.55      frystyk   205:     HT_FREE(str);
2.42      frystyk   206:     return YES;
                    207: }
1.1       timbl     208: 
2.42      frystyk   209: /*     GopherBottom
                    210: **     ------------
                    211: **     Create the bottom part of the page
                    212: */
                    213: PRIVATE BOOL GopherBottom (HTStream *me)
                    214: {
                    215:     HTStructured *target = me->target;
                    216:     if (me->pre)
                    217:        END(HTML_PRE);
                    218:     END(HTML_BODY);
                    219:     END(HTML_HTML);
                    220:     return YES;
                    221: }
2.17      frystyk   222: 
2.42      frystyk   223: /*     GopherMenuLine
                    224: **     --------------
                    225: **     Parses a Gopher Menu Line
                    226: **     Return YES if more data else NO
                    227: */
                    228: PRIVATE BOOL GopherMenuLine (HTStream *me, char *line)
                    229: {
                    230:     HTStructured *target = me->target;
                    231:     HTGopherType gtype = (HTGopherType) *line++;
                    232:     if (PROT_TRACE)
2.56      eric      233:        HTTrace("HTGopher.... Menu line: `%s\'\n", line);
2.42      frystyk   234:     if (gtype == GT_INFO) {
                    235:        char *stop = strchr(line, '\t');
                    236:        if (stop) *stop = '\0';
                    237:        PUTS(line);
                    238:     } else if (gtype == GT_ERROR) {
                    239:        char *stop = strchr(line, '\t');
                    240:        if (stop) *stop = '\0';
                    241:        PUTS(line);
                    242:     } else if ((strstr(line, "error.host") || strstr(line, "errorhost"))) {
                    243:        char *stop = strchr(line, '\t');              /* Chop off error.host */
                    244:        if (stop) *stop = '\0';
                    245:        PUTS(line);
                    246:     } else if (gtype == GT_EOF) {
                    247:        return NO;
                    248:     } else {                               /* Parse normal gopher menu line */
                    249:        char *name = line;                           /* Get link information */
                    250:        char *selector = strchr(name, '\t');
                    251:        char *host = NULL;
                    252:        char *port = NULL;
                    253:        if (selector) {
                    254:            *selector++ = '\0';
                    255:            if ((host = strchr(selector, '\t'))) {
                    256:                *host++ = '\0';
                    257:                if ((port = strchr(host, '\t'))) {
                    258:                    char *junk;
                    259:                    *port = ':';                     /* delimit host a la W3 */
                    260:                    if ((junk = strchr(port, '\t')) != NULL)
                    261:                        *junk = '\0';                           /* Chop port */
                    262:                    if (*(port+1) == '0' && !*(port+2))
                    263:                        *port = '\0';
2.21      frystyk   264:                }
2.42      frystyk   265:            }
                    266:        }
                    267:        if (!me->pre) {               /* For now we use preformatted listing */
                    268:            START(HTML_PRE);
                    269:            me->pre = YES;
                    270:        }
2.45      frystyk   271:        if (dir_show & HT_DS_ICON) {                     /* Put out the icon */
2.42      frystyk   272:            HTIconNode *icon = GopherIcon(gtype);
2.52      frystyk   273:            if (icon) {
2.61    ! frystyk   274:                char * alt = HTIcon_alternative(icon, YES);
        !           275:                HTMLPutImg(target, HTIcon_url(icon), alt, NULL);
        !           276:                HT_FREE(alt);
2.42      frystyk   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,
2.60      frystyk   513:                               HTRequest_outputFormat(request),
                    514:                               HTRequest_outputStream(request));
                    515:     HTAnchor_setFormat(HTRequest_anchor(request), WWW_HTML);
2.42      frystyk   516:     me->request = request;    
                    517:     me->state = EOL_BEGIN;
                    518:     me->url = url;
                    519:     me->CSO = CSO;
                    520:     GopherTitle(me);
                    521:     return me;
                    522: }
2.20      frystyk   523: 
2.42      frystyk   524: /* ------------------------------------------------------------------------- */
                    525: /*                             GOPHER STATE MACHINE                         */
                    526: /* ------------------------------------------------------------------------- */
2.7       secret    527: 
2.42      frystyk   528: /*     GopherCleanup
                    529: **     -------------
                    530: **      This function closes the connection and frees memory.
                    531: **      Returns YES if OK, else NO
                    532: */
2.60      frystyk   533: PRIVATE BOOL GopherCleanup (HTRequest * request, int status)
2.42      frystyk   534: {
2.60      frystyk   535:     HTNet * net = HTRequest_net(request);
                    536:     gopher_info * gopher = (gopher_info *) HTNet_context(net);
                    537:     HTStream * input = HTRequest_inputStream(request);
2.20      frystyk   538: 
2.42      frystyk   539:     /* Free stream with data TO network */
2.60      frystyk   540:     if (input) {
2.42      frystyk   541:        if (status == HT_INTERRUPTED)
2.60      frystyk   542:            (*input->isa->abort)(input, NULL);
2.42      frystyk   543:        else
2.60      frystyk   544:            (*input->isa->_free)(input);
2.20      frystyk   545:     }
                    546: 
2.42      frystyk   547:     /* Remove the request object and our own context structure for gopher */
                    548:     HTNet_delete(net, status);
                    549:     if (gopher) {
2.55      frystyk   550:        HT_FREE(gopher->cmd);
                    551:        HT_FREE(gopher);
2.34      frystyk   552:     }
2.42      frystyk   553:     return YES;
2.20      frystyk   554: }
2.7       secret    555: 
1.1       timbl     556: /*     Display a Gopher Index document
2.20      frystyk   557: **     -------------------------------
2.42      frystyk   558: **     We create a small HTML object as we have no network activity
2.20      frystyk   559: */
2.42      frystyk   560: PRIVATE void display_index (HTRequest * request)
1.1       timbl     561: {
2.60      frystyk   562:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.40      frystyk   563:     HTStructured *target = HTMLGenerator(request, NULL, WWW_HTML,
2.60      frystyk   564:                                         HTRequest_outputFormat(request),
                    565:                                         HTRequest_outputStream(request));
                    566: 
2.42      frystyk   567:     /* Update anchor information */
2.60      frystyk   568:     HTAnchor_setFormat(anchor, WWW_HTML);
                    569:     HTAnchor_setTitle(anchor, "Searchable Gopher Index");
2.42      frystyk   570:     /* @@@ We don't set Content-Length */
                    571: 
2.34      frystyk   572:     START(HTML_HTML);
                    573:     START(HTML_HEAD);
                    574:     START(HTML_TITLE);
                    575:     PUTS("Searchable Gopher Index");
                    576:     END(HTML_TITLE);
                    577:     END(HTML_HEAD);
                    578:     START(HTML_BODY);
                    579: 
1.2       timbl     580:     START(HTML_H1);
2.20      frystyk   581:     PUTS("Searchable Gopher Index");
1.2       timbl     582:     END(HTML_H1);
2.7       secret    583:     START(HTML_ISINDEX);
2.34      frystyk   584:     END(HTML_BODY);
                    585:     END(HTML_HTML);
2.42      frystyk   586:     (*target->isa->_free)(target);
2.7       secret    587: }
                    588: 
                    589: 
                    590: /*      Display a CSO index document
                    591: **      -------------------------------
2.42      frystyk   592: **     We create a small HTML object as we have no network activity
2.7       secret    593: */
2.42      frystyk   594: PRIVATE void display_cso (HTRequest * request)
2.7       secret    595: {
2.60      frystyk   596:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.40      frystyk   597:     HTStructured *target = HTMLGenerator(request, NULL, WWW_HTML,
2.60      frystyk   598:                                         HTRequest_outputFormat(request),
                    599:                                         HTRequest_outputStream(request));
                    600: 
2.42      frystyk   601:     /* Update anchor information */
2.60      frystyk   602:     HTAnchor_setFormat(anchor, WWW_HTML);
                    603:     HTAnchor_setTitle(anchor, "Searchable SCO Index");
2.42      frystyk   604:     /* @@@ We don't set Content-Length */
                    605: 
2.34      frystyk   606:     START(HTML_HTML);
                    607:     START(HTML_HEAD);
                    608:     START(HTML_TITLE);
                    609:     PUTS("Searchable Index of a CSO Name Server");
                    610:     END(HTML_TITLE);
                    611:     END(HTML_HEAD);
                    612:     START(HTML_BODY);
                    613: 
2.7       secret    614:     START(HTML_H1);
2.20      frystyk   615:     PUTS("Searchable Index of a CSO Name Server");
2.7       secret    616:     END(HTML_H1);
2.34      frystyk   617:     PUTS("A CSO Name Server usually provides directory information about people.");
2.7       secret    618:     START(HTML_ISINDEX);
2.34      frystyk   619:     END(HTML_BODY);
                    620:     END(HTML_HTML);
2.42      frystyk   621:     (*target->isa->_free)(target);
1.1       timbl     622: }
                    623: 
2.42      frystyk   624: /*     HTLoadGopher
                    625: **     ------------
2.24      frystyk   626: **     Given a hypertext address, this routine loads a gopher document
                    627: **
                    628: ** On entry,
2.42      frystyk   629: **      request                This is the request structure
                    630: ** On Exit
                    631: **     returns         HT_ERROR        Error has occured in call back
                    632: **                     HT_OK           Call back was OK
1.1       timbl     633: */
2.42      frystyk   634: PUBLIC int HTLoadGopher (SOCKET soc, HTRequest * request, SockOps ops)
1.1       timbl     635: {
2.42      frystyk   636:     int status = HT_ERROR;
2.60      frystyk   637:     HTNet * net = HTRequest_net(request);
                    638:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    639:     char * url = HTAnchor_physical(anchor);
                    640:     gopher_info * gopher;
2.20      frystyk   641:     
2.42      frystyk   642:     /*
                    643:     ** Initiate a new gopher structure and bind to request structure
                    644:     ** This is actually state GOPHER_BEGIN, but it can't be in the state
                    645:     ** machine as we need the structure first.
                    646:     */
                    647:     if (ops == FD_NONE) {
2.56      eric      648:        if (PROT_TRACE) HTTrace("Gopher...... Looking for `%s\'\n",url);
2.55      frystyk   649:        if ((gopher = (gopher_info *) HT_CALLOC(1, sizeof(gopher_info))) == NULL)
                    650:            HT_OUTOFMEM("HTLoadGopher");
2.42      frystyk   651:        gopher->type = GT_MENU;
                    652:        gopher->state = GOPHER_BEGIN;
2.60      frystyk   653:        HTNet_setContext(net, gopher);
2.44      frystyk   654:     } else if (ops == FD_CLOSE) {                            /* Interrupted */
2.42      frystyk   655:        GopherCleanup(request, HT_INTERRUPTED);
                    656:        return HT_OK;
                    657:     } else
2.60      frystyk   658:        gopher = (gopher_info *) HTNet_context(net);    /* Get existing copy */
2.42      frystyk   659: 
                    660:     /* Now jump into the machine. We know the state from the previous run */
                    661:     while (1) {
                    662:        switch (gopher->state) {
                    663: 
                    664:          case GOPHER_BEGIN:         /* Get entity type, and selector string */
                    665:            {
                    666:                char *path = HTParse(url, "", PARSE_PATH);
                    667:                char *selector = path;
                    668:                char *query = NULL;
                    669:                char *separator = NULL;
                    670:                if (*selector) gopher->type = (HTGopherType) *selector++;
                    671:                if (gopher->type == GT_INDEX) {
2.60      frystyk   672:                    HTAnchor_setIndex(anchor);                   /* Is index */
2.42      frystyk   673:                    query = strchr(selector, '?');
                    674: 
                    675:                    /* Display local cover page only if no search requested */
                    676:                    if (!query || !*(query+1)) {       /* No search required */
                    677:                        display_index(request);
                    678:                        gopher->state = GOPHER_GOT_DATA;
2.55      frystyk   679:                        HT_FREE(path);
2.42      frystyk   680:                        break;
                    681:                    } else {
                    682:                        *query++ = 0;                            /* Skip '?' */
                    683:                        separator = "\t";
                    684:                    }
                    685:                } else if (gopher->type == GT_CSO) {
2.60      frystyk   686:                    HTAnchor_setIndex(anchor);          /* Search is allowed */
2.42      frystyk   687:                    query = strchr(selector, '?'); /* Look for search string */
                    688:                    
                    689:                    /* Display local cover page only if no search requested */
                    690:                    if (!query || !*(query+1)) {       /* No search required */
                    691:                        display_cso(request);
                    692:                        gopher->state = GOPHER_GOT_DATA;
2.55      frystyk   693:                        HT_FREE(path);
2.42      frystyk   694:                        break;
                    695:                    } else {
                    696:                        *query++ = 0;                            /* Skip '?' */
                    697:                        *selector = 0;
                    698:                        separator = "query ";
                    699:                    }
                    700:                }
2.20      frystyk   701: 
2.42      frystyk   702:                /* Now generate the final command */
                    703:                {
                    704:                    char crlf[3];
                    705:                    StrAllocCopy(gopher->cmd, selector);
                    706:                    if (query) {
                    707:                        char *p;
                    708:                        for (p=query; *p; p++)   /* Remove plus signs 921006 */
                    709:                            if (*p == '+') *p = ' ';
                    710:                        StrAllocCat(gopher->cmd, separator);
                    711:                        StrAllocCat(gopher->cmd, query);
                    712:                    }
                    713:                    HTUnEscape(gopher->cmd);
                    714:                    HTCleanTelnetString(gopher->cmd);   /* Prevent sec holes */
                    715:                    *crlf = CR;                        /* Telnet termination */
                    716:                    *(crlf+1) = LF;
                    717:                    *(crlf+2) = '\0';
                    718:                    StrAllocCat(gopher->cmd, crlf);
                    719:                }
2.55      frystyk   720:                HT_FREE(path);
2.42      frystyk   721:                gopher->state = GOPHER_NEED_CONNECTION;
1.1       timbl     722:            }
2.42      frystyk   723:            break;
                    724: 
                    725:          case GOPHER_NEED_CONNECTION:
2.43      frystyk   726:            status = HTDoConnect(net, url, GOPHER_PORT);
2.42      frystyk   727:            if (status == HT_OK) {
2.59      frystyk   728:                /*
                    729:                ** Check the protocol class to see if we have connected to a
                    730:                ** the right class of server, in this case HTTP.
                    731:                */
                    732:                {
                    733:                    HTHost * host = HTNet_host(net);
                    734:                    char * s_class = HTHost_class(host);
                    735:                    if (s_class && strcasecomp(s_class, "gopher")) {
                    736:                        HTRequest_addError(request, ERR_FATAL, NO, HTERR_CLASS,
                    737:                                           NULL, 0, "HTLoadGopher");
                    738:                        gopher->state = GOPHER_ERROR;
                    739:                        break;
                    740:                    }
                    741:                    HTHost_setClass(host, "gopher");
                    742:                }
2.42      frystyk   743: 
2.59      frystyk   744:                /* 
                    745:                ** Create the stream pipe FROM the channel to the application.
                    746:                ** The target for the input stream pipe is set up using the
                    747:                ** stream stack.
                    748:                */
                    749:                {
                    750:                    HTStream * target;
                    751:                    if (gopher->type == GT_MENU || gopher->type == GT_INDEX)
                    752:                        target = GopherMenu_new(request, url, NO);
                    753:                    else if (gopher->type == GT_CSO)
                    754:                        target = GopherMenu_new(request, url, YES);
                    755:                    else
                    756:                        target = HTStreamStack(WWW_UNKNOWN,
2.60      frystyk   757:                                               HTRequest_outputFormat(request),
                    758:                                               HTRequest_outputStream(request),
2.59      frystyk   759:                                               request, NO);
                    760:                    HTNet_getInput(net, target, NULL, 0);
                    761:                }
2.42      frystyk   762: 
2.59      frystyk   763:                /*
                    764:                ** Create the stream pipe TO the channel from the application
                    765:                ** and hook it up to the request object
                    766:                */
                    767:                {
                    768:                    HTOutputStream * output = HTNet_getOutput(net, NULL, 0);
                    769:                    HTRequest_setInputStream(request, (HTStream *) output);
                    770:                }
2.42      frystyk   771:                gopher->state = GOPHER_NEED_REQUEST;
2.59      frystyk   772: 
2.42      frystyk   773:            } else if (status == HT_WOULD_BLOCK)
                    774:                return HT_OK;
                    775:            else
                    776:                gopher->state = GOPHER_ERROR;
                    777:            break;
                    778: 
                    779:          case GOPHER_NEED_REQUEST:
2.56      eric      780:            if (PROT_TRACE) HTTrace("Gopher Tx... `%s\'", gopher->cmd);
2.60      frystyk   781:            {   
                    782:                HTStream * input = HTRequest_inputStream(request);
                    783:                status = (*input->isa->put_block)
                    784:                    (input, gopher->cmd, strlen(gopher->cmd));
                    785:                if (status == HT_WOULD_BLOCK)
                    786:                    return HT_OK;
                    787:                else if (status == HT_ERROR)
                    788:                    gopher->state = GOPHER_ERROR;
                    789:                else
                    790:                    gopher->state = GOPHER_NEED_RESPONSE;
                    791:            }
2.42      frystyk   792:            break;
1.1       timbl     793: 
2.42      frystyk   794:          case GOPHER_NEED_RESPONSE:
2.59      frystyk   795:            status = (*net->input->isa->read)(net->input);
2.42      frystyk   796:            if (status == HT_WOULD_BLOCK)
                    797:                return HT_OK;
2.53      frystyk   798:            else if (status == HT_LOADED || status == HT_CLOSED)
2.42      frystyk   799:                gopher->state = GOPHER_GOT_DATA;
                    800:            else
                    801:                gopher->state = GOPHER_ERROR;
                    802:            break;
1.2       timbl     803: 
2.42      frystyk   804:          case GOPHER_GOT_DATA:
                    805:            GopherCleanup(request, HT_LOADED);
                    806:            return HT_OK;
                    807:            break;
                    808: 
                    809:          case GOPHER_NO_DATA:
                    810:            GopherCleanup(request, HT_NO_DATA);
                    811:            return HT_OK;
                    812:            break;
                    813: 
                    814:          case GOPHER_ERROR:
                    815:            GopherCleanup(request, HT_ERROR);
                    816:            return HT_OK;
                    817:            break;
2.25      frystyk   818:        }
2.42      frystyk   819:     }  /* while(1) */
1.1       timbl     820: }

Webmaster