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