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

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.48      frystyk   311:     HTChunk_clear(news->cmd);
                    312:     HTChunk_ensure(news->cmd, len);
2.39      frystyk   313:     if (pars && *pars)
2.48      frystyk   314:        sprintf(HTChunk_data(news->cmd), "%s %s%c%c", token, pars, CR, LF);
2.39      frystyk   315:     else
2.48      frystyk   316:        sprintf(HTChunk_data(news->cmd), "%s%c%c", token, CR, LF);
2.66      frystyk   317:     HTTRACE(PROT_TRACE, "News Tx..... %s" _ HTChunk_data(news->cmd));
2.59      frystyk   318:     return (*input->isa->put_block)(input, HTChunk_data(news->cmd), len);
2.39      frystyk   319: }
                    320: 
                    321: /*             Load data object from NNTP Server                    HTLoadNews
                    322: **             =================================
                    323: **
                    324: **     Given a hypertext addres, this routine loads a document
                    325: **
                    326: ** On Entry,
                    327: **     request         The request structure
                    328: **
                    329: **     returns         HT_ERROR        Error has occured or interrupted
                    330: **                     HT_WOULD_BLOCK  if operation would have blocked
                    331: **                     HT_LOADED       if 200 OK
                    332: **                     HT_NO_DATA      if No Response
                    333: **                     HT_RETRY        if Service Unavail.
                    334: */
2.61      frystyk   335: PRIVATE int NewsEvent (SOCKET soc, void * pVoid, HTEventType type);
                    336: 
                    337: PUBLIC int HTLoadNews (SOCKET soc, HTRequest * request)
                    338: {
                    339:     news_info *news;
                    340:     HTParentAnchor *anchor = HTRequest_anchor(request);
                    341:     HTNet * net = HTRequest_net(request);
                    342:     char * url = HTAnchor_physical(anchor);
                    343: 
2.66      frystyk   344:     HTTRACE(PROT_TRACE, "NNTP........ Looking for `%s\'\n" _ url);
2.61      frystyk   345:     if ((news = (news_info *) HT_CALLOC(1, sizeof(news_info))) == NULL)
                    346:        HT_OUTOFMEM("HTLoadNews");
                    347:     news->cmd = HTChunk_new(128);
                    348:     news->state = NEWS_BEGIN;
                    349:     news->net = net;
                    350:     HTNet_setContext(net, news);
                    351:     HTNet_setEventCallback(net, NewsEvent);
                    352:     HTNet_setEventParam(net, news);  /* callbacks get http* */
                    353: 
                    354:     return NewsEvent(soc, news, HTEvent_BEGIN);                /* get it started - ops is ignored */
                    355: }
                    356: 
                    357: PRIVATE int NewsEvent (SOCKET soc, void * pVoid, HTEventType type)
2.39      frystyk   358: {
2.61      frystyk   359:     news_info *news = (news_info *)pVoid;
2.39      frystyk   360:     int status = HT_ERROR;
2.61      frystyk   361:     HTNet * net = news->net;
                    362:     HTRequest * request = HTNet_request(net);
2.65      frystyk   363:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.59      frystyk   364:     char * url = HTAnchor_physical(anchor);
2.65      frystyk   365:     HTHost * host = HTNet_host(net);
2.39      frystyk   366:     
                    367:     /*
                    368:     ** Initiate a new nntp structure and bind to request structure
                    369:     ** This is actually state NNTP_BEGIN, but it can't be in the state
                    370:     ** machine as we need the structure first.
                    371:     */
2.61      frystyk   372:     if (type == HTEvent_CLOSE) {                             /* Interrupted */
2.59      frystyk   373:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_INTERRUPTED,
                    374:                           NULL, 0, "HTLoadHTTP");
                    375:        HTNewsCleanup(request, HT_INTERRUPTED);
2.36      frystyk   376:        return HT_OK;
                    377:     } else
2.62      frystyk   378:        news = (news_info *) HTNet_context(net);                /* Get existing copy */
2.59      frystyk   379: 
                    380:     /* Now jump into the machine. We know the state from the previous run */
2.39      frystyk   381:     while (1) {
                    382:         switch (news->state) {
                    383:           case NEWS_BEGIN:
                    384:            news->state = (!strchr(url, '@') && strchr(url, '*')) ?
                    385:                NEWS_SEEK_CACHE : NEWS_NEED_CONNECTION;
                    386:            break;
                    387: 
2.59      frystyk   388:        case NEWS_SEEK_CACHE:
                    389:            if (HTNewsCache_before(request, NULL, 0) == HT_LOADED)
                    390:                news->state = NEWS_SUCCESS;
                    391:            else
                    392:                news->state = NEWS_NEED_CONNECTION;
2.39      frystyk   393:            break;
                    394: 
                    395:          case NEWS_NEED_CONNECTION:            /* Let's set up a connection */
                    396:            if (!strncasecomp(url, "news:", 5)) {
2.57      frystyk   397:                HTUserProfile * up = HTRequest_userProfile(request);
                    398:                char * newshost = HTUserProfile_news(up);
2.39      frystyk   399:                StrAllocCopy(news->name, url+5);
                    400:                if (newshost) {
                    401:                    char *newshack = NULL;    /* Then we can use HTParse :-) */
                    402:                    StrAllocCopy(newshack, "news://");
                    403:                    StrAllocCat(newshack, newshost);
2.67    ! frystyk   404:                    status = HTHost_connect(host, net, (char *) newshack);
2.65      frystyk   405:                    host = HTNet_host(net);
2.50      frystyk   406:                    HT_FREE(newshack);
2.39      frystyk   407:                } else
                    408:                    news->state = NEWS_ERROR;
                    409:            } else if (!strncasecomp(url, "nntp:", 5)) {
                    410:                news->name = HTParse(url, "", PARSE_PATH);
2.67    ! frystyk   411:                status = HTHost_connect(host, net, url);
2.65      frystyk   412:                host = HTNet_host(net);
2.39      frystyk   413:            } else {
2.66      frystyk   414:                HTTRACE(PROT_TRACE, "News........ Huh?");
2.39      frystyk   415:                news->state = NEWS_ERROR;
                    416:             }
                    417:             if (status == HT_OK) {
                    418:                BOOL greeting = NO;
2.54      frystyk   419: 
                    420:                /* Set up the persistent connection */
2.53      frystyk   421:                if (!HTNet_persistent(net)) {
2.60      frystyk   422:                    HTNet_setPersistent(net, YES, HT_TP_SINGLE);
2.39      frystyk   423:                    greeting = YES;
                    424:                }
2.36      frystyk   425: 
2.39      frystyk   426:                /*
2.54      frystyk   427:                ** Check the protocol class to see if we have connected to a
                    428:                ** the right class of server, in this case HTTP.
                    429:                */
                    430:                {
                    431:                    HTHost * host = HTNet_host(net);
                    432:                    char * s_class = HTHost_class(host);
                    433:                    if (s_class && strcasecomp(s_class, "nntp")) {
                    434:                        HTRequest_addError(request, ERR_FATAL, NO, HTERR_CLASS,
                    435:                                           NULL, 0, "HTLoadNews");
                    436:                        news->state = NEWS_ERROR;
                    437:                        break;
                    438:                    }
                    439:                    HTHost_setClass(host, "nntp");
                    440:                }
                    441: 
                    442:                /* 
                    443:                ** Create the stream pipe FROM the channel to the application.
                    444:                ** The target for the input stream pipe is set up using the
                    445:                ** stream stack.
                    446:                */
2.62      frystyk   447:                {
2.65      frystyk   448:                    HTStream * rstream = HTNewsStatus_new(request, news, host);
2.62      frystyk   449:                    HTNet_setReadStream(net, rstream);
                    450:                    HTRequest_setOutputConnected(request, YES);
                    451:                }
2.54      frystyk   452: 
                    453:                /*
                    454:                ** Create the stream pipe TO the channel from the application
                    455:                ** and hook it up to the request object
                    456:                */
                    457:                {
                    458:                    HTOutputStream * output = HTNet_getOutput(net, NULL, 0);
                    459:                    HTRequest_setInputStream(request, (HTStream *) output);
                    460:                }
                    461: 
                    462:                news->state = greeting ? NEWS_NEED_GREETING : NEWS_NEED_SWITCH;
2.39      frystyk   463: 
2.60      frystyk   464:            } else if (status == HT_WOULD_BLOCK || status == HT_PENDING)
2.39      frystyk   465:                return HT_OK;
                    466:            else
                    467:                news->state = NEWS_ERROR;
                    468:            break;
                    469: 
                    470:          case NEWS_NEED_GREETING:
2.62      frystyk   471:            status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   472:            if (status == HT_WOULD_BLOCK)
                    473:                return HT_OK;
                    474:            else if (status == HT_LOADED) {
                    475:                if (news->repcode/100 == 2)
                    476:                    news->state = NEWS_NEED_SWITCH;
                    477:                else
                    478:                    news->state = NEWS_ERROR;
                    479:            } else
                    480:                news->state = NEWS_ERROR;
                    481:            break;
                    482: 
                    483:          case NEWS_NEED_SWITCH:
2.59      frystyk   484:          {
                    485:              HTMethod method = HTRequest_method(request);
                    486:              /*
                    487:              ** Find out what to ask the news server. Syntax of address is
                    488:              **        xxx@yyy         Article
                    489:              **        <xxx@yyy>       Same article
                    490:              **        xxxxx           News group (no "@")
                    491:              */
                    492:              if (method == METHOD_GET) {
                    493:                  if (strchr(url, '@')) {                                 /* ARTICLE */
                    494:                      if (*(news->name) != '<') {                 /* Add '<' and '>' */
                    495:                          char *newart;
                    496:                          if ((newart = (char  *) HT_MALLOC(strlen(news->name)+3)) == NULL)
                    497:                              HT_OUTOFMEM("HTLoadNews");
                    498:                          sprintf(newart, "<%s>", news->name);
                    499:                          HT_FREE(news->name);
                    500:                          news->name = newart;
                    501:                      }
                    502:                      news->state = NEWS_NEED_ARTICLE;
                    503:                  } else if (strchr(url, '*'))
                    504:                      news->state = NEWS_NEED_LIST;
                    505:                  else
                    506:                      news->state = NEWS_NEED_GROUP;
                    507:              } else if (method == METHOD_POST)
                    508:                  news->state = NEWS_NEED_POST;
                    509:              else {
                    510:                  HTRequest_addError(request, ERR_FATAL, NO,
                    511:                                     HTERR_NOT_IMPLEMENTED,NULL, 0,"HTLoadNews");
                    512:                  news->state = NEWS_ERROR;
                    513:              }
                    514:              HTUnEscape(news->name);
                    515:              HTCleanTelnetString(news->name);
                    516:          }
                    517:          break;
2.39      frystyk   518: 
                    519:          case NEWS_NEED_ARTICLE:
                    520:            if (!news->sent) {
                    521:                status = SendCommand(request, news, "ARTICLE", news->name);
                    522:                if (status == HT_WOULD_BLOCK)
                    523:                    return HT_OK;
                    524:                else if (status == HT_ERROR)
                    525:                    news->state = NEWS_ERROR;
                    526:                news->format = WWW_MIME;
2.60      frystyk   527: 
                    528:                /*
                    529:                ** Set the default content type to plain text as news servers
                    530:                ** almost never send any useful information about the length
                    531:                ** of the body or the type - the success of MIME!
                    532:                */
                    533:                HTAnchor_setFormat(anchor, WWW_PLAINTEXT);
2.39      frystyk   534:                news->sent = YES;
                    535:            } else {
2.62      frystyk   536:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   537:                if (status == HT_WOULD_BLOCK)
                    538:                    return HT_OK;
                    539:                else if (status == HT_OK)
                    540:                    news->state = NEWS_NEED_BODY;
                    541:                else if (status == HT_LOADED) {
                    542:                    news->state = (news->repcode/100 == 2) ?
                    543:                        NEWS_SUCCESS : NEWS_ERROR;
                    544:                } else
                    545:                    news->state = NEWS_ERROR;
                    546:                news->sent = NO;
                    547:            }
                    548:            break;
                    549: 
2.45      frystyk   550: #if HT_LISTGROUP
2.39      frystyk   551:          case NEWS_NEED_LGRP:
                    552:            if (!news->sent) {
                    553:                status = SendCommand(request, news, "LIST", "NEWSGROUPS");
                    554:                if (status == HT_WOULD_BLOCK)
                    555:                    return HT_OK;
                    556:                else if (status == HT_ERROR)
                    557:                    news->state = NEWS_ERROR;
                    558:                news->format = WWW_NNTP_LIST;
                    559:                news->sent = YES;
                    560:            } else {
2.62      frystyk   561:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   562:                if (status == HT_WOULD_BLOCK)
                    563:                    return HT_OK;
                    564:                else if (status == HT_OK)
                    565:                    news->state = NEWS_NEED_BODY;
                    566:                else if (status == HT_LOADED) {
                    567:                    news->state = (news->repcode/100 == 2) ?
                    568:                        NEWS_SUCCESS : NEWS_NEED_LIST;
                    569:                } else
                    570:                    news->state = NEWS_ERROR;
                    571:                news->sent = NO;
                    572:            }
                    573:            break;
2.45      frystyk   574: #endif /* HT_LISTGROUP */
2.39      frystyk   575: 
                    576:          case NEWS_NEED_LIST:
                    577:            if (!news->sent) {
                    578:                status = SendCommand(request, news, "LIST", NULL);
                    579:                if (status == HT_WOULD_BLOCK)
                    580:                    return HT_OK;
                    581:                else if (status == HT_ERROR)
                    582:                    news->state = NEWS_ERROR;
                    583:                news->format = WWW_NNTP_LIST;
                    584:                news->sent = YES;
                    585:            } else {
2.62      frystyk   586:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   587:                if (status == HT_WOULD_BLOCK)
                    588:                    return HT_OK;
                    589:                else if (status == HT_OK)
                    590:                    news->state = NEWS_NEED_BODY;
                    591:                else if (status == HT_LOADED) {
                    592:                    news->state = (news->repcode/100 == 2) ?
                    593:                        NEWS_SUCCESS : NEWS_ERROR;
                    594:                } else
                    595:                    news->state = NEWS_ERROR;
                    596:                news->sent = NO;
                    597:            }
                    598:            break;
1.1       timbl     599: 
2.39      frystyk   600:          case NEWS_NEED_GROUP:
                    601:            if (!news->sent) {
                    602:                status = SendCommand(request, news, "GROUP", news->name);
                    603:                if (status == HT_WOULD_BLOCK)
                    604:                    return HT_OK;
                    605:                else if (status == HT_ERROR)
                    606:                    news->state = NEWS_ERROR;
                    607:                news->sent = YES;
1.1       timbl     608:            } else {
2.62      frystyk   609:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   610:                if (status == HT_WOULD_BLOCK)
                    611:                    return HT_OK;
                    612:                else if (status == HT_LOADED) {
                    613:                    if (news->repcode/100 == 2) {
                    614:                        if (sscanf(news->reply, "%d%d%d", &news->total,
                    615:                                   &news->first, &news->last) == 3) {
2.45      frystyk   616:                            if (MaxArt && news->total>MaxArt)
                    617:                                news->last = news->first-MaxArt;
2.39      frystyk   618:                            news->current = news->first;
2.59      frystyk   619: 
                    620:                            /* If no content in this group */
                    621:                            if (news->first == news->last) {
                    622:                                HTRequest_addError(request, ERR_FATAL, NO,
                    623:                                                   HTERR_NO_CONTENT,
                    624:                                                   NULL, 0, "HTLoadNews");
                    625:                                news->state = NEWS_NO_DATA;
                    626:                                break;
                    627:                            }
2.39      frystyk   628:                            news->state = NEWS_NEED_XOVER;
                    629:                        } else
                    630:                            news->state = NEWS_ERROR;
                    631:                    } else
                    632:                        news->state = NEWS_ERROR;
                    633:                } else
                    634:                    news->state = NEWS_ERROR;
                    635:                news->sent = NO;
1.1       timbl     636:            }
2.39      frystyk   637:            break;
1.1       timbl     638: 
2.39      frystyk   639:          case NEWS_NEED_XOVER:
                    640:            if (!news->sent) {
                    641:                char buf[20];
                    642:                sprintf(buf, "%d-%d", news->first, news->last);
                    643:                status = SendCommand(request, news, "XOVER", buf);
                    644:                if (status == HT_WOULD_BLOCK)
                    645:                    return HT_OK;
                    646:                else if (status == HT_ERROR)
                    647:                    news->state = NEWS_ERROR;
                    648:                news->format = WWW_NNTP_OVER;
                    649:                news->sent = YES;
                    650:            } else {
2.62      frystyk   651:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   652:                if (status == HT_WOULD_BLOCK)
                    653:                    return HT_OK;
                    654:                else if (status == HT_OK)
                    655:                    news->state = NEWS_NEED_BODY;
                    656:                else if (status == HT_LOADED) {
                    657:                    if (news->repcode/100 == 2)
                    658:                        news->state = NEWS_SUCCESS;
                    659:                    else {
                    660:                        news->format = WWW_NNTP_HEAD;
                    661:                        news->state = NEWS_NEED_HEAD;
                    662:                    }
                    663:                } else
                    664:                    news->state = NEWS_ERROR;
                    665:                news->sent = NO;
                    666:            }
                    667:            break;
1.1       timbl     668: 
2.39      frystyk   669:          case NEWS_NEED_HEAD:
                    670:            if (!news->sent) {
                    671:                char buf[10];
                    672:                sprintf(buf, "%d", news->current++);
                    673:                status = SendCommand(request, news, "HEAD", buf);
                    674:                if (status == HT_WOULD_BLOCK)
2.36      frystyk   675:                    return HT_OK;
2.39      frystyk   676:                else if (status == HT_ERROR)
                    677:                    news->state = NEWS_ERROR;
                    678:                news->sent = YES;
1.1       timbl     679:            } else {
2.62      frystyk   680:                status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   681:                if (status == HT_WOULD_BLOCK)
                    682:                    return HT_OK;
                    683:                else if (status == HT_LOADED) {
                    684:                    if (news->repcode/100 == 2) {
                    685:                        if (news->current > news->last)
                    686:                            news->state = NEWS_SUCCESS;
                    687:                    } else
                    688:                        news->state = NEWS_ERROR;
                    689:                } else
                    690:                    news->state = NEWS_ERROR;
                    691:                news->sent = NO;
                    692:            }
                    693:            break;
                    694: 
                    695:          case NEWS_NEED_POST:
2.54      frystyk   696:          {
                    697:              HTStream * oldinput = HTRequest_inputStream(request);
                    698:              HTStream * newinput =
                    699:                  HTNewsPost_new(request, HTBuffer_new(oldinput, request,512));
                    700:              HTRequest_setInputStream(request, newinput);
                    701:              
                    702:              /* Remember to convert to CRLF */
2.39      frystyk   703: 
2.54      frystyk   704:          }
2.59      frystyk   705:          news->state = NEWS_NEED_BODY;
                    706:          break;
2.39      frystyk   707: 
                    708:           case NEWS_NEED_BODY:
2.61      frystyk   709:             if (type == HTEvent_WRITE || type == HTEvent_BEGIN) {
2.39      frystyk   710:                if (HTRequest_isDestination(request)) {
2.59      frystyk   711:                    HTRequest * source = HTRequest_source(request);
                    712:                    HTNet * srcnet = HTRequest_net(source);
                    713:                    if (srcnet) {
2.62      frystyk   714:                        HTHost_register(HTNet_host(srcnet), srcnet, HTEvent_READ);
                    715:                        HTHost_unregister(HTNet_host(srcnet), srcnet, HTEvent_WRITE);
2.59      frystyk   716:                    }
2.36      frystyk   717:                    return HT_OK;
2.21      frystyk   718:                }
2.59      frystyk   719: 
                    720:                /*
                    721:                **  Should we use the input stream directly or call the post
                    722:                **  callback function to send data down to the network?
                    723:                */
                    724:                {
                    725:                    HTStream * input = HTRequest_inputStream(request);
                    726:                    HTPostCallback * pcbf = HTRequest_postCallback(request);
                    727:                    if (pcbf) {
                    728:                        status = pcbf(request, input);
                    729:                        if (status == HT_PAUSE || status == HT_LOADED)
2.61      frystyk   730:                            type = HTEvent_READ;
2.59      frystyk   731:                    } else {
                    732:                        status = (*input->isa->flush)(input);
2.61      frystyk   733:                        type = HTEvent_READ;
2.59      frystyk   734:                    }
                    735:                    if (status == HT_WOULD_BLOCK) return HT_OK;
                    736:                }
2.41      frystyk   737:                status = request->PostCallback ?
                    738:                     request->PostCallback(request, request->input_stream) :
2.39      frystyk   739:                        (*request->input_stream->isa->flush)(request->input_stream);
                    740:                if (status == HT_WOULD_BLOCK)
                    741:                     return HT_OK;
                    742:                 else   
2.61      frystyk   743:                     type = HTEvent_READ;         /* Trick to ensure that we do READ */
                    744:            } else if (type == HTEvent_READ) {
2.62      frystyk   745:                 status = HTHost_read(HTNet_host(net), net);
2.39      frystyk   746:                if (status == HT_WOULD_BLOCK)
                    747:                    return HT_OK;
                    748:                 else if (status == HT_LOADED)
                    749:                    news->state = NEWS_SUCCESS;
                    750:                else
                    751:                    news->state = NEWS_ERROR;
                    752:            } else {
                    753:                news->state = NEWS_ERROR;
1.1       timbl     754:            }
2.39      frystyk   755:            break;
                    756:                
                    757:          case NEWS_SUCCESS:
2.59      frystyk   758:            HTNewsCleanup(request, HT_LOADED);
                    759:            return HT_OK;
                    760:            break;
                    761: 
                    762:        case NEWS_NO_DATA:
                    763:            HTNewsCleanup(request, HT_NO_DATA);
2.39      frystyk   764:            return HT_OK;
                    765:            break;
2.59      frystyk   766: 
2.39      frystyk   767:          case NEWS_ERROR:
2.65      frystyk   768:            HTNewsCleanup(request, HT_NOT_FOUND);
2.39      frystyk   769:            return HT_OK;
                    770:            break;
1.1       timbl     771:        }
2.39      frystyk   772:     } /* End of while(1) */
1.1       timbl     773: }

Webmaster