Annotation of libwww/Library/src/HTNews.c, revision 2.68

2.39      frystyk     1: /*                     NEWS ACCESS                             HTNews.c
                      2: **                     ===========
1.1       timbl       3: **
                      4: ** History:
                      5: **     26 Sep 90       Written TBL
                      6: **     29 Nov 91       Downgraded to C, for portable implementation.
2.19      luotonen    7: **     16 Feb 94  AL   Added Lou Montulli's Lynx & LIST NEWSGROUPS diffs.
                      8: **      2 May 94  AL   Added HTUnEscape() to HTLoadNews(), and
                      9: **                     fixed a possible security hole when the URL contains
                     10: **                     a newline, that could cause multiple commands to be
                     11: **                     sent to an NNTP server.
2.23      duns       12: **      8 Jul 94  FM   Insulate free() from _free structure element.
2.39      frystyk    13: **     30 Aug 95  FTLO Added POST functionality and updated state machine
                     14: **     30 Aug 95  HFN  Cleaned up a whole lot and made a state machine
1.1       timbl      15: */
2.27      roeber     16: 
2.39      frystyk    17: /* Library Include files */
2.64      frystyk    18: #include "wwwsys.h"
2.54      frystyk    19: #include "WWWUtil.h"
                     20: #include "WWWCore.h"
2.55      frystyk    21: #include "WWWStream.h"
2.59      frystyk    22: #include "WWWTrans.h"
2.43      frystyk    23: #include "HTReqMan.h"                          /* @@@ */
2.59      frystyk    24: #include "HTNewsRq.h"
                     25: #include "HTNewsLs.h"
2.39      frystyk    26: #include "HTNews.h"                                           /* Implements */
                     27: 
                     28: /* Macros and other defines */
                     29: #ifndef NEWS_LIST_FILE
                     30: #define NEWS_LIST_FILE         ".www_news"        /* Name of news list file */
                     31: #endif
1.1       timbl      32: 
2.45      frystyk    33: #define MAX_NEWS_ARTICLES      0       /* No default max number of articles */
2.40      frystyk    34: 
2.39      frystyk    35: #define PUTBLOCK(b, l) (*me->target->isa->put_block)        (me->target, b, l)
                     36: #define ABORT_TARGET    (*me->target->isa->abort)            (me->target, e)
2.8       timbl      37: 
2.39      frystyk    38: /* Local context structure used in the HTNet object */
                     39: typedef enum _HTNewsState {
2.59      frystyk    40:     NEWS_ERROR         = -3,
                     41:     NEWS_SUCCESS       = -2,
                     42:     NEWS_NO_DATA       = -1,
                     43:     NEWS_BEGIN         = 0,
2.39      frystyk    44:     NEWS_SEEK_CACHE,
                     45:     NEWS_NEED_CONNECTION,
                     46:     NEWS_NEED_GREETING,
                     47:     NEWS_NEED_SWITCH,
                     48:     NEWS_NEED_ARTICLE,
2.45      frystyk    49: #if HT_LISTGROUP
2.39      frystyk    50:     NEWS_NEED_LGRP,
                     51: #endif
                     52:     NEWS_NEED_LIST,
                     53:     NEWS_NEED_GROUP,
                     54:     NEWS_NEED_XOVER,
                     55:     NEWS_NEED_HEAD,
                     56:     NEWS_NEED_POST,
2.59      frystyk    57:     NEWS_NEED_BODY
2.39      frystyk    58: } HTNewsState;
                     59: 
                     60: typedef struct _news_info {
                     61:     HTChunk *          cmd;
                     62:     int                        repcode;
                     63:     char *             reply;
                     64:     HTNewsState         state;                    /* State of the connection */
                     65:     HTFormat           format;
                     66:     char *             name;                /* Name of article or newsgroup */
                     67:     BOOL               sent;                       /* Read or write command */
                     68:     int                 first;            /* First article in the group list */
                     69:     int                 last;              /* Last article in the group list */
                     70:     int                        total;               /* Estimated number of articles */
                     71:     int                 current;                 /* To count # of HEADS sent */
2.61      frystyk    72:     HTNet *            net;
2.39      frystyk    73: } news_info;
                     74: 
                     75: /* This version of a stream is used for NEWS LIST conversion to HTML */
                     76: struct _HTStream {
2.52      frystyk    77:     const HTStreamClass *      isa;
2.39      frystyk    78:     HTStream *                 target;
                     79:     HTRequest *                        request;
                     80:     news_info *                        news;
2.53      frystyk    81:     HTEOLState                 EOLstate;
2.39      frystyk    82:     BOOL                       semi_trans;
                     83:     BOOL                       junk;
                     84:     char                       buffer[MAX_NEWS_LINE+1];
                     85:     int                                buflen;
2.65      frystyk    86:     HTHost *                   host;
1.2       timbl      87: };
                     88: 
2.54      frystyk    89: struct _HTInputStream {
                     90:     const HTInputStreamClass * isa;
                     91: };
                     92: 
2.45      frystyk    93: PRIVATE int MaxArt = MAX_NEWS_ARTICLES;
1.1       timbl      94: 
2.39      frystyk    95: /* ------------------------------------------------------------------------- */
                     96: /*                            NEWS INPUT STREAM                             */
                     97: /* ------------------------------------------------------------------------- */
                     98: 
                     99: /*     ScanResponse
                    100: **     ------------
                    101: **     Analyzes the response from the NNTP server.
                    102: **     We only expect one line response codes.
                    103: **     Returns HT_LOADED if OK, HT_ERROR if error
                    104: */
                    105: PRIVATE int ScanResponse (HTStream * me)
                    106: {
                    107:     news_info *news = me->news;
                    108:     *(me->buffer+me->buflen) = '\0';
2.63      frystyk   109:     if (isdigit((int) *(me->buffer))) sscanf(me->buffer, "%d", &news->repcode);
2.39      frystyk   110:     me->buflen = 0;
                    111:     news->reply = me->buffer+4;
2.66      frystyk   112:     HTTRACE(PROT_TRACE, "News Rx..... `%s\'\n" _ news->reply);
2.39      frystyk   113: 
                    114:     /* If 2xx code and we expect data then go into semi-transparent mode */
                    115:     if (me->news->format && news->repcode/100 == 2) {
                    116:        HTRequest *req = me->request;
                    117:        me->target = HTStreamStack(me->news->format, req->output_format,
                    118:                                   req->output_stream, req, NO);
                    119:        me->semi_trans = YES;
                    120:        if (!me->target) return HT_ERROR;
2.65      frystyk   121:     } else if (news->repcode/100 == 4) {
                    122:        HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_NOT_FOUND,
                    123:                           news->reply, strlen(news->reply), "ScanResponse");
2.39      frystyk   124:     }
                    125:     return HT_LOADED;
1.2       timbl     126: }
1.1       timbl     127: 
2.39      frystyk   128: /*
                    129: **     Searches for NNTP header line until buffer fills up or a CRLF or LF
                    130: **     is found
1.1       timbl     131: */
2.52      frystyk   132: PRIVATE int HTNewsStatus_put_block (HTStream * me, const char * b, int l)
1.1       timbl     133: {
2.65      frystyk   134:     int status;
                    135:     HTHost_setConsumed(me->host, l);
2.39      frystyk   136:     while (!me->semi_trans && l-- > 0) {
                    137:        if (me->EOLstate == EOL_FCR) {
                    138:            if (*b == LF) {
                    139:                if (me->junk) me->junk = NO;
                    140:                me->EOLstate = EOL_BEGIN;
                    141:                if ((status = ScanResponse(me)) != HT_LOADED) return status;
                    142:            }
                    143:        } else if (*b == CR) {
                    144:            me->EOLstate = EOL_FCR;
                    145:        } else if (*b == LF) {
                    146:            if (me->junk) me->junk = NO;
                    147:            me->EOLstate = EOL_BEGIN;
                    148:            if ((status = ScanResponse(me)) != HT_LOADED) return status;
                    149:        } else {
                    150:            *(me->buffer+me->buflen++) = *b;
                    151:            if (me->buflen >= MAX_NEWS_LINE) {
2.66      frystyk   152:                HTTRACE(PROT_TRACE, "News Status. Line too long - chopped\n");
2.39      frystyk   153:                me->junk = YES;
                    154:                if ((status = ScanResponse(me)) != HT_LOADED) return status;
1.1       timbl     155:            }
                    156:        }
2.39      frystyk   157:        b++;
                    158:     }  
1.1       timbl     159: 
2.39      frystyk   160:     /*
                    161:     ** Now see if we have parts of the body to put down the stream pipe.
                    162:     ** At this point we are looking for CRLF.CRLF. We are guaranteed a stream
                    163:     */
                    164:     if (l > 0) {
                    165:        int rest = l;
2.52      frystyk   166:        const char *ptr = b;
2.39      frystyk   167:        while (rest-- > 0) {
                    168:            if (*ptr == CR) {
                    169:                me->EOLstate = me->EOLstate==EOL_DOT ? EOL_SCR : EOL_FCR;
                    170:            } else if (*ptr == '.') {
                    171:                me->EOLstate = me->EOLstate==EOL_FLF ? EOL_DOT : EOL_BEGIN;
                    172:            } else if (*ptr == LF) {
                    173:                me->EOLstate = me->EOLstate>EOL_DOT ? EOL_SLF : EOL_FLF;
                    174:            } else
                    175:                me->EOLstate = EOL_BEGIN;
                    176:            ptr++;
                    177:        }
                    178:        if (me->EOLstate == EOL_SLF) {
                    179:            int status = PUTBLOCK(b, l-5);
                    180:            return status != HT_OK ? status : HT_LOADED;
                    181:        } else {
                    182:            int status = PUTBLOCK(b, l);
                    183:            return status;
1.1       timbl     184:        }
                    185:     }
2.39      frystyk   186:     return HT_LOADED;
1.1       timbl     187: }
                    188: 
2.39      frystyk   189: PRIVATE int HTNewsStatus_put_character (HTStream * me, char ch)
1.1       timbl     190: {
2.39      frystyk   191:     return HTNewsStatus_put_block(me, &ch, 1);
1.1       timbl     192: }
                    193: 
2.52      frystyk   194: PRIVATE int HTNewsStatus_put_string (HTStream * me, const char * str)
1.1       timbl     195: {
2.39      frystyk   196:     return HTNewsStatus_put_block(me, str, (int) strlen(str));
1.1       timbl     197: }
                    198: 
2.39      frystyk   199: PRIVATE int HTNewsStatus_flush (HTStream * me)
1.1       timbl     200: {
2.39      frystyk   201:     return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
1.1       timbl     202: }
                    203: 
2.39      frystyk   204: PRIVATE int HTNewsStatus_free (HTStream * me)
1.2       timbl     205: {
2.39      frystyk   206:     int status = HT_OK;
                    207:     if (me->target) {
                    208:        if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
                    209:            return HT_WOULD_BLOCK;
1.2       timbl     210:     }
2.50      frystyk   211:     HT_FREE(me);
2.39      frystyk   212:     return status;
1.2       timbl     213: }
1.1       timbl     214: 
2.44      frystyk   215: PRIVATE int HTNewsStatus_abort (HTStream * me, HTList * e)
2.16      luotonen  216: {
2.39      frystyk   217:     if (me->target)
                    218:         ABORT_TARGET;
2.50      frystyk   219:     HT_FREE(me);
2.66      frystyk   220:     HTTRACE(PROT_TRACE, "NewsStatus.. ABORTING...\n");
2.39      frystyk   221:     return HT_ERROR;
2.16      luotonen  222: }
                    223: 
2.52      frystyk   224: PRIVATE const HTStreamClass HTNewsStatusClass =
2.39      frystyk   225: {              
                    226:     "NewsStatus",
                    227:     HTNewsStatus_flush,
                    228:     HTNewsStatus_free,
                    229:     HTNewsStatus_abort,
                    230:     HTNewsStatus_put_character,
                    231:     HTNewsStatus_put_string,
                    232:     HTNewsStatus_put_block
                    233: };
2.16      luotonen  234: 
2.65      frystyk   235: PRIVATE HTStream * HTNewsStatus_new (HTRequest * request, news_info * news,
                    236:                                     HTHost * host)
1.1       timbl     237: {
2.50      frystyk   238:     HTStream *me;
                    239:     if ((me = (HTStream  *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
                    240:         HT_OUTOFMEM("HTNewsStatus_new");
2.39      frystyk   241:     me->isa = &HTNewsStatusClass;
                    242:     me->request = request;
                    243:     me->news = news;
                    244:     me->EOLstate = EOL_BEGIN;
2.65      frystyk   245:     me->host = host;
2.39      frystyk   246:     return me;
1.1       timbl     247: }
                    248: 
2.39      frystyk   249: /* ------------------------------------------------------------------------- */
                    250: /*                             PROTOCOL FUNCTIONS                           */
                    251: /* ------------------------------------------------------------------------- */
1.1       timbl     252: 
2.39      frystyk   253: /*
                    254: **     Set the max number of articles at the same time.
                    255: **     Default is MAX_NEWS_ARTICLES
1.1       timbl     256: */
2.39      frystyk   257: PUBLIC BOOL HTNews_setMaxArticles (int new_max)
                    258: { 
2.45      frystyk   259:     if (new_max >= 0) {
                    260:        MaxArt = new_max;
2.39      frystyk   261:        return YES;
1.1       timbl     262:     }
2.39      frystyk   263:     return NO;
1.1       timbl     264: }
                    265: 
2.39      frystyk   266: /*
                    267: **     Get current max number of articles at the same time.
1.1       timbl     268: */
2.39      frystyk   269: PUBLIC int HTNews_maxArticles (void)
                    270: { 
2.45      frystyk   271:     return MaxArt;
1.1       timbl     272: }
                    273: 
2.39      frystyk   274: /*     HTNewsCleanup
                    275: **     -------------
                    276: **      This function closes the connection and frees memory.
                    277: **      Returns YES on OK, else NO
1.1       timbl     278: */
2.39      frystyk   279: PRIVATE int HTNewsCleanup (HTRequest * req, int status)
                    280: {
2.59      frystyk   281:     HTNet * net = HTRequest_net(req);
2.62      frystyk   282:     news_info *news = (news_info *) HTNet_context(net);
2.59      frystyk   283:     HTStream * input = HTRequest_inputStream(req);
1.1       timbl     284: 
2.39      frystyk   285:     /* Free stream with data TO network */
2.59      frystyk   286:     if (!HTRequest_isDestination(req))
                    287:        HTRequest_removeDestination(req);
                    288:     else if (input) {
2.39      frystyk   289:        if (status == HT_INTERRUPTED)
2.59      frystyk   290:            (*input->isa->abort)(input, NULL);
2.39      frystyk   291:        else
2.59      frystyk   292:            (*input->isa->_free)(input);
                    293:        HTRequest_setInputStream(req, NULL);
1.1       timbl     294:     }
2.16      luotonen  295: 
2.39      frystyk   296:     /* Remove the request object and our own context structure for nntp */
                    297:     HTNet_delete(net, status);
2.49      frystyk   298:     if (news) {
2.50      frystyk   299:        HT_FREE(news->name);
2.49      frystyk   300:        HTChunk_delete(news->cmd);
2.50      frystyk   301:        HT_FREE(news);
2.49      frystyk   302:     }
2.39      frystyk   303:     return YES;
1.1       timbl     304: }
                    305: 
2.39      frystyk   306: PRIVATE int SendCommand (HTRequest *request, news_info *news,
                    307:                         char *token, char *pars)
1.1       timbl     308: {
2.59      frystyk   309:     HTStream * input = HTRequest_inputStream(request);
2.39      frystyk   310:     int len = strlen(token) + (pars ? strlen(pars)+1:0) + 2;
2.68    ! kahan     311:     HTChunk_setSize(news->cmd, len);
2.39      frystyk   312:     if (pars && *pars)
2.48      frystyk   313:        sprintf(HTChunk_data(news->cmd), "%s %s%c%c", token, pars, CR, LF);
2.39      frystyk   314:     else
2.48      frystyk   315:        sprintf(HTChunk_data(news->cmd), "%s%c%c", token, CR, LF);
2.66      frystyk   316:     HTTRACE(PROT_TRACE, "News Tx..... %s" _ HTChunk_data(news->cmd));
2.59      frystyk   317:     return (*input->isa->put_block)(input, HTChunk_data(news->cmd), len);
2.39      frystyk   318: }
                    319: 
                    320: /*             Load data object from NNTP Server                    HTLoadNews
                    321: **             =================================
                    322: **
                    323: **     Given a hypertext addres, this routine loads a document
                    324: **
                    325: ** On Entry,
                    326: **     request         The request structure
                    327: **
                    328: **     returns         HT_ERROR        Error has occured or interrupted
                    329: **                     HT_WOULD_BLOCK  if operation would have blocked
                    330: **                     HT_LOADED       if 200 OK
                    331: **                     HT_NO_DATA      if No Response
                    332: **                     HT_RETRY        if Service Unavail.
                    333: */
2.61      frystyk   334: PRIVATE int NewsEvent (SOCKET soc, void * pVoid, HTEventType type);
                    335: 
                    336: PUBLIC int HTLoadNews (SOCKET soc, HTRequest * request)
                    337: {
                    338:     news_info *news;
                    339:     HTParentAnchor *anchor = HTRequest_anchor(request);
                    340:     HTNet * net = HTRequest_net(request);
                    341:     char * url = HTAnchor_physical(anchor);
                    342: 
2.66      frystyk   343:     HTTRACE(PROT_TRACE, "NNTP........ Looking for `%s\'\n" _ url);
2.61      frystyk   344:     if ((news = (news_info *) HT_CALLOC(1, sizeof(news_info))) == NULL)
                    345:        HT_OUTOFMEM("HTLoadNews");
                    346:     news->cmd = HTChunk_new(128);
                    347:     news->state = NEWS_BEGIN;
                    348:     news->net = net;
                    349:     HTNet_setContext(net, news);
                    350:     HTNet_setEventCallback(net, NewsEvent);
                    351:     HTNet_setEventParam(net, news);  /* callbacks get http* */
                    352: 
                    353:     return NewsEvent(soc, news, HTEvent_BEGIN);                /* get it started - ops is ignored */
                    354: }
                    355: 
                    356: PRIVATE int NewsEvent (SOCKET soc, void * pVoid, HTEventType type)
2.39      frystyk   357: {
2.61      frystyk   358:     news_info *news = (news_info *)pVoid;
2.39      frystyk   359:     int status = HT_ERROR;
2.61      frystyk   360:     HTNet * net = news->net;
                    361:     HTRequest * request = HTNet_request(net);
2.65      frystyk   362:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.59      frystyk   363:     char * url = HTAnchor_physical(anchor);
2.65      frystyk   364:     HTHost * host = HTNet_host(net);
2.39      frystyk   365:     
                    366:     /*
                    367:     ** Initiate a new nntp structure and bind to request structure
                    368:     ** This is actually state NNTP_BEGIN, but it can't be in the state
                    369:     ** machine as we need the structure first.
                    370:     */
2.61      frystyk   371:     if (type == HTEvent_CLOSE) {                             /* Interrupted */
2.59      frystyk   372:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_INTERRUPTED,
                    373:                           NULL, 0, "HTLoadHTTP");
                    374:        HTNewsCleanup(request, HT_INTERRUPTED);
2.36      frystyk   375:        return HT_OK;
                    376:     } else
2.62      frystyk   377:        news = (news_info *) HTNet_context(net);                /* Get existing copy */
2.59      frystyk   378: 
                    379:     /* Now jump into the machine. We know the state from the previous run */
2.39      frystyk   380:     while (1) {
                    381:         switch (news->state) {
                    382:           case NEWS_BEGIN:
                    383:            news->state = (!strchr(url, '@') && strchr(url, '*')) ?
                    384:                NEWS_SEEK_CACHE : NEWS_NEED_CONNECTION;
                    385:            break;
                    386: 
2.59      frystyk   387:        case NEWS_SEEK_CACHE:
                    388:            if (HTNewsCache_before(request, NULL, 0) == HT_LOADED)
                    389:                news->state = NEWS_SUCCESS;
                    390:            else
                    391:                news->state = NEWS_NEED_CONNECTION;
2.39      frystyk   392:            break;
                    393: 
                    394:          case NEWS_NEED_CONNECTION:            /* Let's set up a connection */
                    395:            if (!strncasecomp(url, "news:", 5)) {
2.57      frystyk   396:                HTUserProfile * up = HTRequest_userProfile(request);
                    397:                char * newshost = HTUserProfile_news(up);
2.39      frystyk   398:                StrAllocCopy(news->name, url+5);
                    399:                if (newshost) {
                    400:                    char *newshack = NULL;    /* Then we can use HTParse :-) */
                    401:                    StrAllocCopy(newshack, "news://");
                    402:                    StrAllocCat(newshack, newshost);
2.67      frystyk   403:                    status = HTHost_connect(host, net, (char *) newshack);
2.65      frystyk   404:                    host = HTNet_host(net);
2.50      frystyk   405:                    HT_FREE(newshack);
2.39      frystyk   406:                } else
                    407:                    news->state = NEWS_ERROR;
                    408:            } else if (!strncasecomp(url, "nntp:", 5)) {
                    409:                news->name = HTParse(url, "", PARSE_PATH);
2.67      frystyk   410:                status = HTHost_connect(host, net, url);
2.65      frystyk   411:                host = HTNet_host(net);
2.39      frystyk   412:            } else {
2.66      frystyk   413:                HTTRACE(PROT_TRACE, "News........ Huh?");
2.39      frystyk   414:                news->state = NEWS_ERROR;
                    415:             }
                    416:             if (status == HT_OK) {
                    417:                BOOL greeting = NO;
2.54      frystyk   418: 
                    419:                /* Set up the persistent connection */
2.53      frystyk   420:                if (!HTNet_persistent(net)) {
2.60      frystyk   421:                    HTNet_setPersistent(net, YES, HT_TP_SINGLE);
2.39      frystyk   422:                    greeting = YES;
                    423:                }
2.36      frystyk   424: 
2.39      frystyk   425:                /*
2.54      frystyk   426:                ** Check the protocol class to see if we have connected to a
                    427:                ** the right class of server, in this case HTTP.
                    428:                */
                    429:                {
                    430:                    HTHost * host = HTNet_host(net);
                    431:                    char * s_class = HTHost_class(host);
                    432:                    if (s_class && strcasecomp(s_class, "nntp")) {
                    433:                        HTRequest_addError(request, ERR_FATAL, NO, HTERR_CLASS,
                    434:                                           NULL, 0, "HTLoadNews");
                    435:                        news->state = NEWS_ERROR;
                    436:                        break;
                    437:                    }
                    438:                    HTHost_setClass(host, "nntp");
                    439:                }
                    440: 
                    441:                /* 
                    442:                ** Create the stream pipe FROM the channel to the application.
                    443:                ** The target for the input stream pipe is set up using the
                    444:                ** stream stack.
                    445:                */
2.62      frystyk   446:                {
2.65      frystyk   447:                    HTStream * rstream = HTNewsStatus_new(request, news, host);
2.62      frystyk   448:                    HTNet_setReadStream(net, rstream);
                    449:                    HTRequest_setOutputConnected(request, YES);
                    450:                }
2.54      frystyk   451: 
                    452:                /*
                    453:                ** Create the stream pipe TO the channel from the application
                    454:                ** and hook it up to the request object
                    455:                */
                    456:                {
                    457:                    HTOutputStream * output = HTNet_getOutput(net, NULL, 0);
                    458:                    HTRequest_setInputStream(request, (HTStream *) output);
                    459:                }
                    460: 
                    461:                news->state = greeting ? NEWS_NEED_GREETING : NEWS_NEED_SWITCH;
2.39      frystyk   462: 
2.60      frystyk   463:            } else if (status == HT_WOULD_BLOCK || status == HT_PENDING)
2.39      frystyk   464:                return HT_OK;
                    465:            else
                    466:                news->state = NEWS_ERROR;
                    467:            break;
                    468: 
                    469:          case NEWS_NEED_GREETING:
2.62      frystyk   470:            status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   471:            if (status == HT_WOULD_BLOCK)
                    472:                return HT_OK;
                    473:            else if (status == HT_LOADED) {
                    474:                if (news->repcode/100 == 2)
                    475:                    news->state = NEWS_NEED_SWITCH;
                    476:                else
                    477:                    news->state = NEWS_ERROR;
                    478:            } else
                    479:                news->state = NEWS_ERROR;
                    480:            break;
                    481: 
                    482:          case NEWS_NEED_SWITCH:
2.59      frystyk   483:          {
                    484:              HTMethod method = HTRequest_method(request);
                    485:              /*
                    486:              ** Find out what to ask the news server. Syntax of address is
                    487:              **        xxx@yyy         Article
                    488:              **        <xxx@yyy>       Same article
                    489:              **        xxxxx           News group (no "@")
                    490:              */
                    491:              if (method == METHOD_GET) {
                    492:                  if (strchr(url, '@')) {                                 /* ARTICLE */
                    493:                      if (*(news->name) != '<') {                 /* Add '<' and '>' */
                    494:                          char *newart;
                    495:                          if ((newart = (char  *) HT_MALLOC(strlen(news->name)+3)) == NULL)
                    496:                              HT_OUTOFMEM("HTLoadNews");
                    497:                          sprintf(newart, "<%s>", news->name);
                    498:                          HT_FREE(news->name);
                    499:                          news->name = newart;
                    500:                      }
                    501:                      news->state = NEWS_NEED_ARTICLE;
                    502:                  } else if (strchr(url, '*'))
                    503:                      news->state = NEWS_NEED_LIST;
                    504:                  else
                    505:                      news->state = NEWS_NEED_GROUP;
                    506:              } else if (method == METHOD_POST)
                    507:                  news->state = NEWS_NEED_POST;
                    508:              else {
                    509:                  HTRequest_addError(request, ERR_FATAL, NO,
                    510:                                     HTERR_NOT_IMPLEMENTED,NULL, 0,"HTLoadNews");
                    511:                  news->state = NEWS_ERROR;
                    512:              }
                    513:              HTUnEscape(news->name);
                    514:              HTCleanTelnetString(news->name);
                    515:          }
                    516:          break;
2.39      frystyk   517: 
                    518:          case NEWS_NEED_ARTICLE:
                    519:            if (!news->sent) {
                    520:                status = SendCommand(request, news, "ARTICLE", news->name);
                    521:                if (status == HT_WOULD_BLOCK)
                    522:                    return HT_OK;
                    523:                else if (status == HT_ERROR)
                    524:                    news->state = NEWS_ERROR;
                    525:                news->format = WWW_MIME;
2.60      frystyk   526: 
                    527:                /*
                    528:                ** Set the default content type to plain text as news servers
                    529:                ** almost never send any useful information about the length
                    530:                ** of the body or the type - the success of MIME!
                    531:                */
                    532:                HTAnchor_setFormat(anchor, WWW_PLAINTEXT);
2.39      frystyk   533:                news->sent = YES;
                    534:            } else {
2.62      frystyk   535:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   536:                if (status == HT_WOULD_BLOCK)
                    537:                    return HT_OK;
                    538:                else if (status == HT_OK)
                    539:                    news->state = NEWS_NEED_BODY;
                    540:                else if (status == HT_LOADED) {
                    541:                    news->state = (news->repcode/100 == 2) ?
                    542:                        NEWS_SUCCESS : NEWS_ERROR;
                    543:                } else
                    544:                    news->state = NEWS_ERROR;
                    545:                news->sent = NO;
                    546:            }
                    547:            break;
                    548: 
2.45      frystyk   549: #if HT_LISTGROUP
2.39      frystyk   550:          case NEWS_NEED_LGRP:
                    551:            if (!news->sent) {
                    552:                status = SendCommand(request, news, "LIST", "NEWSGROUPS");
                    553:                if (status == HT_WOULD_BLOCK)
                    554:                    return HT_OK;
                    555:                else if (status == HT_ERROR)
                    556:                    news->state = NEWS_ERROR;
                    557:                news->format = WWW_NNTP_LIST;
                    558:                news->sent = YES;
                    559:            } else {
2.62      frystyk   560:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   561:                if (status == HT_WOULD_BLOCK)
                    562:                    return HT_OK;
                    563:                else if (status == HT_OK)
                    564:                    news->state = NEWS_NEED_BODY;
                    565:                else if (status == HT_LOADED) {
                    566:                    news->state = (news->repcode/100 == 2) ?
                    567:                        NEWS_SUCCESS : NEWS_NEED_LIST;
                    568:                } else
                    569:                    news->state = NEWS_ERROR;
                    570:                news->sent = NO;
                    571:            }
                    572:            break;
2.45      frystyk   573: #endif /* HT_LISTGROUP */
2.39      frystyk   574: 
                    575:          case NEWS_NEED_LIST:
                    576:            if (!news->sent) {
                    577:                status = SendCommand(request, news, "LIST", NULL);
                    578:                if (status == HT_WOULD_BLOCK)
                    579:                    return HT_OK;
                    580:                else if (status == HT_ERROR)
                    581:                    news->state = NEWS_ERROR;
                    582:                news->format = WWW_NNTP_LIST;
                    583:                news->sent = YES;
                    584:            } else {
2.62      frystyk   585:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   586:                if (status == HT_WOULD_BLOCK)
                    587:                    return HT_OK;
                    588:                else if (status == HT_OK)
                    589:                    news->state = NEWS_NEED_BODY;
                    590:                else if (status == HT_LOADED) {
                    591:                    news->state = (news->repcode/100 == 2) ?
                    592:                        NEWS_SUCCESS : NEWS_ERROR;
                    593:                } else
                    594:                    news->state = NEWS_ERROR;
                    595:                news->sent = NO;
                    596:            }
                    597:            break;
1.1       timbl     598: 
2.39      frystyk   599:          case NEWS_NEED_GROUP:
                    600:            if (!news->sent) {
                    601:                status = SendCommand(request, news, "GROUP", news->name);
                    602:                if (status == HT_WOULD_BLOCK)
                    603:                    return HT_OK;
                    604:                else if (status == HT_ERROR)
                    605:                    news->state = NEWS_ERROR;
                    606:                news->sent = YES;
1.1       timbl     607:            } else {
2.62      frystyk   608:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   609:                if (status == HT_WOULD_BLOCK)
                    610:                    return HT_OK;
                    611:                else if (status == HT_LOADED) {
                    612:                    if (news->repcode/100 == 2) {
                    613:                        if (sscanf(news->reply, "%d%d%d", &news->total,
                    614:                                   &news->first, &news->last) == 3) {
2.45      frystyk   615:                            if (MaxArt && news->total>MaxArt)
                    616:                                news->last = news->first-MaxArt;
2.39      frystyk   617:                            news->current = news->first;
2.59      frystyk   618: 
                    619:                            /* If no content in this group */
                    620:                            if (news->first == news->last) {
                    621:                                HTRequest_addError(request, ERR_FATAL, NO,
                    622:                                                   HTERR_NO_CONTENT,
                    623:                                                   NULL, 0, "HTLoadNews");
                    624:                                news->state = NEWS_NO_DATA;
                    625:                                break;
                    626:                            }
2.39      frystyk   627:                            news->state = NEWS_NEED_XOVER;
                    628:                        } else
                    629:                            news->state = NEWS_ERROR;
                    630:                    } else
                    631:                        news->state = NEWS_ERROR;
                    632:                } else
                    633:                    news->state = NEWS_ERROR;
                    634:                news->sent = NO;
1.1       timbl     635:            }
2.39      frystyk   636:            break;
1.1       timbl     637: 
2.39      frystyk   638:          case NEWS_NEED_XOVER:
                    639:            if (!news->sent) {
                    640:                char buf[20];
                    641:                sprintf(buf, "%d-%d", news->first, news->last);
                    642:                status = SendCommand(request, news, "XOVER", buf);
                    643:                if (status == HT_WOULD_BLOCK)
                    644:                    return HT_OK;
                    645:                else if (status == HT_ERROR)
                    646:                    news->state = NEWS_ERROR;
                    647:                news->format = WWW_NNTP_OVER;
                    648:                news->sent = YES;
                    649:            } else {
2.62      frystyk   650:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   651:                if (status == HT_WOULD_BLOCK)
                    652:                    return HT_OK;
                    653:                else if (status == HT_OK)
                    654:                    news->state = NEWS_NEED_BODY;
                    655:                else if (status == HT_LOADED) {
                    656:                    if (news->repcode/100 == 2)
                    657:                        news->state = NEWS_SUCCESS;
                    658:                    else {
                    659:                        news->format = WWW_NNTP_HEAD;
                    660:                        news->state = NEWS_NEED_HEAD;
                    661:                    }
                    662:                } else
                    663:                    news->state = NEWS_ERROR;
                    664:                news->sent = NO;
                    665:            }
                    666:            break;
1.1       timbl     667: 
2.39      frystyk   668:          case NEWS_NEED_HEAD:
                    669:            if (!news->sent) {
                    670:                char buf[10];
                    671:                sprintf(buf, "%d", news->current++);
                    672:                status = SendCommand(request, news, "HEAD", buf);
                    673:                if (status == HT_WOULD_BLOCK)
2.36      frystyk   674:                    return HT_OK;
2.39      frystyk   675:                else if (status == HT_ERROR)
                    676:                    news->state = NEWS_ERROR;
                    677:                news->sent = YES;
1.1       timbl     678:            } else {
2.62      frystyk   679:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   680:                if (status == HT_WOULD_BLOCK)
                    681:                    return HT_OK;
                    682:                else if (status == HT_LOADED) {
                    683:                    if (news->repcode/100 == 2) {
                    684:                        if (news->current > news->last)
                    685:                            news->state = NEWS_SUCCESS;
                    686:                    } else
                    687:                        news->state = NEWS_ERROR;
                    688:                } else
                    689:                    news->state = NEWS_ERROR;
                    690:                news->sent = NO;
                    691:            }
                    692:            break;
                    693: 
                    694:          case NEWS_NEED_POST:
2.54      frystyk   695:          {
                    696:              HTStream * oldinput = HTRequest_inputStream(request);
                    697:              HTStream * newinput =
                    698:                  HTNewsPost_new(request, HTBuffer_new(oldinput, request,512));
                    699:              HTRequest_setInputStream(request, newinput);
                    700:              
                    701:              /* Remember to convert to CRLF */
2.39      frystyk   702: 
2.54      frystyk   703:          }
2.59      frystyk   704:          news->state = NEWS_NEED_BODY;
                    705:          break;
2.39      frystyk   706: 
                    707:           case NEWS_NEED_BODY:
2.61      frystyk   708:             if (type == HTEvent_WRITE || type == HTEvent_BEGIN) {
2.39      frystyk   709:                if (HTRequest_isDestination(request)) {
2.59      frystyk   710:                    HTRequest * source = HTRequest_source(request);
                    711:                    HTNet * srcnet = HTRequest_net(source);
                    712:                    if (srcnet) {
2.62      frystyk   713:                        HTHost_register(HTNet_host(srcnet), srcnet, HTEvent_READ);
                    714:                        HTHost_unregister(HTNet_host(srcnet), srcnet, HTEvent_WRITE);
2.59      frystyk   715:                    }
2.36      frystyk   716:                    return HT_OK;
2.21      frystyk   717:                }
2.59      frystyk   718: 
                    719:                /*
                    720:                **  Should we use the input stream directly or call the post
                    721:                **  callback function to send data down to the network?
                    722:                */
                    723:                {
                    724:                    HTStream * input = HTRequest_inputStream(request);
                    725:                    HTPostCallback * pcbf = HTRequest_postCallback(request);
                    726:                    if (pcbf) {
                    727:                        status = pcbf(request, input);
                    728:                        if (status == HT_PAUSE || status == HT_LOADED)
2.61      frystyk   729:                            type = HTEvent_READ;
2.59      frystyk   730:                    } else {
                    731:                        status = (*input->isa->flush)(input);
2.61      frystyk   732:                        type = HTEvent_READ;
2.59      frystyk   733:                    }
                    734:                    if (status == HT_WOULD_BLOCK) return HT_OK;
                    735:                }
2.41      frystyk   736:                status = request->PostCallback ?
                    737:                     request->PostCallback(request, request->input_stream) :
2.39      frystyk   738:                        (*request->input_stream->isa->flush)(request->input_stream);
                    739:                if (status == HT_WOULD_BLOCK)
                    740:                     return HT_OK;
                    741:                 else   
2.61      frystyk   742:                     type = HTEvent_READ;         /* Trick to ensure that we do READ */
                    743:            } else if (type == HTEvent_READ) {
2.62      frystyk   744:                 status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   745:                if (status == HT_WOULD_BLOCK)
                    746:                    return HT_OK;
                    747:                 else if (status == HT_LOADED)
                    748:                    news->state = NEWS_SUCCESS;
                    749:                else
                    750:                    news->state = NEWS_ERROR;
                    751:            } else {
                    752:                news->state = NEWS_ERROR;
1.1       timbl     753:            }
2.39      frystyk   754:            break;
                    755:                
                    756:          case NEWS_SUCCESS:
2.59      frystyk   757:            HTNewsCleanup(request, HT_LOADED);
                    758:            return HT_OK;
                    759:            break;
                    760: 
                    761:        case NEWS_NO_DATA:
                    762:            HTNewsCleanup(request, HT_NO_DATA);
2.39      frystyk   763:            return HT_OK;
                    764:            break;
2.59      frystyk   765: 
2.39      frystyk   766:          case NEWS_ERROR:
2.65      frystyk   767:            HTNewsCleanup(request, HT_NOT_FOUND);
2.39      frystyk   768:            return HT_OK;
                    769:            break;
1.1       timbl     770:        }
2.39      frystyk   771:     } /* End of while(1) */
1.1       timbl     772: }

Webmaster