[BACK] Return to HTTP.c CVS log [TXT] [DIR] Up to [Public] / libwww / Library / src

Annotation of libwww/Library/src/HTTP.c, revision 1.144

1.74 frystyk 1: /*                                   HTTP.c
 2: **   MULTITHREADED IMPLEMENTATION OF HTTP CLIENT
1.2 timbl 3: **
1.84 frystyk 4: **   (c) COPYRIGHT MIT 1995.
1.74 frystyk 5: **   Please first read the full copyright statement in the file COPYRIGH.
1.144 ! frystyk 6: **   @(#) $Id: HTTP.c,v 1.143 1996年12月05日 01:16:25 frystyk Exp $
1.74 frystyk 7: **
 8: **   This module implments the HTTP protocol as a state machine
1.55 frystyk 9: **
 10: ** History:
1.59 frystyk 11: **  < May 24 94 ??  Unknown - but obviously written
1.56 frystyk 12: **   May 24 94 HF  Made reentrent and cleaned up a bit. Implemented
 13: **           Forward, redirection, error handling and referer field
1.67 duns 14: **   8 Jul 94 FM  Insulate free() from _free structure element.
1.71 frystyk 15: **   Jul 94 HFN   Written on top of HTTP.c, Henrik Frystyk
1.55 frystyk 16: **
1.1 timbl 17: */
 18: 
1.78 frystyk 19: /* Library include files */
1.119 frystyk 20: #include "sysdep.h"
1.123 frystyk 21: #include "WWWUtil.h"
 22: #include "WWWCore.h"
 23: #include "WWWMIME.h"
 24: #include "WWWStream.h"
1.125 frystyk 25: #include "WWWTrans.h"
1.94 frystyk 26: #include "HTReqMan.h"
1.95 frystyk 27: #include "HTNetMan.h"
1.109 frystyk 28: #include "HTTPUtil.h"
1.80 frystyk 29: #include "HTTPReq.h"
1.55 frystyk 30: #include "HTTP.h"                       /* Implements */
 31: 
 32: /* Macros and other defines */
1.94 frystyk 33: #ifndef HTTP_PORT
 34: #define HTTP_PORT 80  /* Allocated to http by Jon Postel/ISI 24-Jan-92 */
 35: #endif
 36: 
1.71 frystyk 37: #define PUTC(c)        (*me->target->isa->put_character)(me->target, c)
 38: #define PUTS(s)        (*me->target->isa->put_string)(me->target, s)
 39: #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
 40: #define FREE_TARGET  (*me->target->isa->_free)(me->target)
1.74 frystyk 41: #define ABORT_TARGET  (*me->target->isa->abort)(me->target, e)
1.2 timbl 42: 
1.140 frystyk 43: #define HTTP_DUMP
 44: 
 45: #ifdef HTTP_DUMP
1.139 frystyk 46: #define HTTP_OUTPUT  "w3chttp.out"
1.140 frystyk 47: PRIVATE FILE * htfp = NULL;
 48: #endif
1.139 frystyk 49: 
1.59 frystyk 50: /* Type definitions and global variables etc. local to this module */
1.94 frystyk 51: 
 52: /* Final states have negative value */
1.59 frystyk 53: typedef enum _HTTPState {
1.141 frystyk 54:   HTTP_RECOVER_PIPE = -3,
1.134 frystyk 55:   HTTP_ERROR     = -2,
 56:   HTTP_OK      = -1,
1.71 frystyk 57:   HTTP_BEGIN     = 0,
1.144 ! frystyk 58:   HTTP_NEED_STREAM,
1.141 frystyk 59:   HTTP_CONNECTED
1.59 frystyk 60: } HTTPState;
1.55 frystyk 61: 
1.94 frystyk 62: /* This is the context structure for the this module */
1.55 frystyk 63: typedef struct _http_info {
1.81 frystyk 64:   HTTPState     state;      /* Current State of the connection */
 65:   HTTPState     next;                 /* Next state */
1.134 frystyk 66:   int            result;   /* Result to report to the after filter */
1.139 frystyk 67:   BOOL        lock;              /* Block for writing */
1.141 frystyk 68:   HTNet *      net;
1.55 frystyk 69: } http_info;
 70: 
1.88 frystyk 71: #define MAX_STATUS_LEN     100  /* Max nb of chars to check StatusLine */
1.55 frystyk 72: 
1.71 frystyk 73: struct _HTStream {
1.119 frystyk 74:   const HTStreamClass *   isa;
1.71 frystyk 75:   HTStream *         target;
 76:   HTRequest *            request;
 77:   http_info *            http;
1.121 frystyk 78:   HTEOLState         state;
1.71 frystyk 79:   BOOL            transparent;
1.81 frystyk 80:   char *           version;       /* Should we save this? */
1.71 frystyk 81:   int                status;
1.81 frystyk 82:   char *           reason;
1.71 frystyk 83:   char            buffer[MAX_STATUS_LEN+1];
1.80 frystyk 84:   int                buflen;
1.71 frystyk 85: };
1.21 luotonen 86: 
1.123 frystyk 87: struct _HTInputStream {
 88:   const HTInputStreamClass * isa;
 89: };
 90: 
1.71 frystyk 91: /* ------------------------------------------------------------------------- */
 92: /*                Help Functions               */
 93: /* ------------------------------------------------------------------------- */
1.21 luotonen 94: 
1.94 frystyk 95: /*   HTTPCleanup
 96: **   -----------
1.55 frystyk 97: **   This function closes the connection and frees memory.
1.94 frystyk 98: **   Returns YES on OK, else NO
1.1 timbl 99: */
1.94 frystyk 100: PRIVATE int HTTPCleanup (HTRequest *req, int status)
1.1 timbl 101: {
1.126 frystyk 102:   HTNet * net = HTRequest_net(req);
 103:   http_info * http = (http_info *) HTNet_context(net);
 104:   HTStream * input = HTRequest_inputStream(req);
1.80 frystyk 105: 
1.144 ! frystyk 106:   if (PROT_TRACE)
 ! 107:    HTTrace("HTTP Clean.. Called with status %d, net %p\n", status, net);
 ! 108: 
1.94 frystyk 109:   /* Free stream with data TO network */
1.112 frystyk 110:   if (HTRequest_isDestination(req))
 111:    HTRequest_removeDestination(req);
1.126 frystyk 112:   else if (input) {
1.94 frystyk 113:    if (status == HT_INTERRUPTED)
1.126 frystyk 114:      (*input->isa->abort)(input, NULL);
1.94 frystyk 115:    else
1.126 frystyk 116:      (*input->isa->_free)(input);
 117:    HTRequest_setInputStream(req, NULL);
1.94 frystyk 118:   }
1.88 frystyk 119: 
1.140 frystyk 120: #ifdef HTTP_DUMP
 121:   if (htfp) {
 122:    fclose(htfp);
 123:    htfp = NULL;
 124:   }
 125: #endif
 126: 
1.144 ! frystyk 127:   /*
 ! 128:   ** Remove the request object and our own context structure for http.
 ! 129:   */
 ! 130:   if (status != HT_RECOVER_PIPE) {
 ! 131:    HTNet_delete(net, status);
1.141 frystyk 132:    HT_FREE(http);
 133:   }
1.94 frystyk 134:   return YES;
1.55 frystyk 135: }
 136: 
1.71 frystyk 137: /*
1.130 frystyk 138: **   Informational 1xx codes are handled separately
1.136 frystyk 139: **   Returns YES if we should continue, NO if we should stop
1.55 frystyk 140: */
1.130 frystyk 141: PRIVATE BOOL HTTPInformation (HTStream * me)
1.55 frystyk 142: {
1.136 frystyk 143:   http_info * http = me->http;
1.71 frystyk 144:   switch (me->status) {
 145: 
1.136 frystyk 146:   case 100:
 147:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_CONTINUE,
 148:              me->reason, (int) strlen(me->reason),
 149:              "HTTPInformation");
 150:    return YES;
 151:    break;
 152: 
1.130 frystyk 153:   case 101:
1.136 frystyk 154:    /*
 155:    ** We consider 101 Switching as a final state and exit this request
 156:    */
1.130 frystyk 157:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_SWITCHING,
1.127 frystyk 158:              me->reason, (int) strlen(me->reason),
1.130 frystyk 159:              "HTTPInformation");
1.136 frystyk 160:    http->next = HTTP_OK;
 161:    http->result = HT_UPGRADE;
1.127 frystyk 162:    break;
 163: 
1.130 frystyk 164:   default:
1.136 frystyk 165:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_BAD_REPLY,
 166:          (void *) me->buffer, me->buflen, "HTTPNextState");
 167:    http->next = HTTP_ERROR;
 168:    http->result = HT_ERROR;
1.127 frystyk 169:    break;
1.130 frystyk 170:   }
1.136 frystyk 171:   return NO;
1.130 frystyk 172: }
 173: 
 174: /*
 175: **   This is a big switch handling all HTTP return codes. It puts in any
 176: **   appropiate error message and decides whether we should expect data
 177: **   or not.
 178: */
 179: PRIVATE void HTTPNextState (HTStream * me)
 180: {
1.134 frystyk 181:   http_info * http = me->http;
1.130 frystyk 182:   switch (me->status) {
1.127 frystyk 183: 
1.78 frystyk 184:    case 0:                        /* 0.9 response */
1.127 frystyk 185:    case 200:                                /* OK */
 186:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_OK,
 187:              me->reason, (int) strlen(me->reason),
 188:              "HTTPNextState");
1.134 frystyk 189:    http->next = HTTP_OK;
 190:    http->result = HT_LOADED;
1.112 frystyk 191:    break;
 192: 
1.127 frystyk 193:    case 201:                             /* Created */
1.112 frystyk 194:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_CREATED,
 195:              me->reason, (int) strlen(me->reason),
 196:              "HTTPNextState");
1.134 frystyk 197:    http->next = HTTP_OK;
 198:    http->result = HT_CREATED;
1.112 frystyk 199:    break;
 200: 
1.127 frystyk 201:    case 202:                             /* Accepted */
1.112 frystyk 202:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_ACCEPTED,
 203:              me->reason, (int) strlen(me->reason),
 204:              "HTTPNextState");
1.134 frystyk 205:    http->next = HTTP_OK;
 206:    http->result = HT_ACCEPTED;
1.112 frystyk 207:    break;
 208: 
1.127 frystyk 209:    case 203:                  /* Non-authoritative information */
 210:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_NON_AUTHORITATIVE,
1.112 frystyk 211:              me->reason, (int) strlen(me->reason),
 212:              "HTTPNextState");
1.134 frystyk 213:    http->next = HTTP_OK;
 214:    http->result = HT_LOADED;
1.71 frystyk 215:    break;
1.78 frystyk 216: 
 217:    case 204:                           /* No Response */
1.112 frystyk 218:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_NO_CONTENT,
 219:              me->reason, (int) strlen(me->reason),
 220:              "HTTPNextState");
1.134 frystyk 221:    http->next = HTTP_OK;
 222:    http->result = HT_NO_DATA;
 223:    break;
 224: 
 225:    case 205:                          /* Reset Content */
 226:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_RESET,
 227:              me->reason, (int) strlen(me->reason),
 228:              "HTTPNextState");
 229:    http->next = HTTP_OK;
 230:    http->result = HT_RESET_CONTENT;
 231:    break;
 232: 
 233:    case 206:                         /* Partial Content */
 234:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_PARTIAL,
 235:              me->reason, (int) strlen(me->reason),
 236:              "HTTPNextState");
 237:    http->next = HTTP_OK;
 238:    http->result = HT_PARTIAL_CONTENT;
 239:    break;
 240: 
 241:    case 300:                         /* Multiple Choices */
 242:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_MULTIPLE,
 243:              me->reason, (int) strlen(me->reason),
 244:              "HTTPNextState");
 245:    http->next = HTTP_OK;
 246:    http->result = HT_LOADED;
1.71 frystyk 247:    break;
1.78 frystyk 248: 
1.71 frystyk 249:    case 301:                              /* Moved */
1.112 frystyk 250:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_MOVED,
 251:              me->reason, (int) strlen(me->reason),
 252:              "HTTPNextState");
1.134 frystyk 253:    http->next = HTTP_ERROR;
 254:    http->result = HT_PERM_REDIRECT;
1.112 frystyk 255:    break;
 256: 
1.71 frystyk 257:    case 302:                              /* Found */
1.112 frystyk 258:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_FOUND,
 259:              me->reason, (int) strlen(me->reason),
 260:              "HTTPNextState");
1.134 frystyk 261:    http->next = HTTP_ERROR;
 262:    http->result = HT_TEMP_REDIRECT;
1.71 frystyk 263:    break;
1.55 frystyk 264:    
1.71 frystyk 265:    case 303:                              /* Method */
1.107 frystyk 266:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_NOT_IMPLEMENTED,
 267:              me->reason, (int) strlen(me->reason),
 268:              "HTTPNextState");
1.134 frystyk 269:    http->next = HTTP_ERROR;
 270:    http->result = HT_SEE_OTHER;
1.71 frystyk 271:    break;
1.91 frystyk 272: 
 273:    case 304:                           /* Not Modified */
1.131 frystyk 274:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_NOT_MODIFIED,
1.112 frystyk 275:              me->reason, (int) strlen(me->reason),
 276:              "HTTPNextState");
1.135 frystyk 277:    http->next = HTTP_OK;
1.134 frystyk 278:    http->result = HT_NOT_MODIFIED;
 279:    break;
 280:    
 281:    case 305:                            /* Use proxy */
 282:    HTRequest_addError(me->request, ERR_INFO, NO, HTERR_USE_PROXY,
 283:              me->reason, (int) strlen(me->reason),
 284:              "HTTPNextState");
 285:    http->next = HTTP_ERROR;
 286:    http->result = HT_USE_PROXY;
1.91 frystyk 287:    break;
1.55 frystyk 288:    
1.78 frystyk 289:    case 400:                           /* Bad Request */
1.104 frystyk 290:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_BAD_REQUEST,
1.100 frystyk 291:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 292:    http->next = HTTP_ERROR;
 293:    http->result = HT_ERROR;
1.71 frystyk 294:    break;
1.70 howcome 295: 
1.71 frystyk 296:    case 401:
1.104 frystyk 297:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
1.100 frystyk 298:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 299:    http->next = HTTP_ERROR;
 300:    http->result = HT_NO_ACCESS;
1.71 frystyk 301:    break;
 302:    
 303:    case 402:                         /* Payment required */
1.104 frystyk 304:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_PAYMENT_REQUIRED,
1.100 frystyk 305:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 306:    http->next = HTTP_ERROR;
 307:    http->result = HT_ERROR;
1.71 frystyk 308:    break;
1.55 frystyk 309:    
1.71 frystyk 310:    case 403:                            /* Forbidden */
1.104 frystyk 311:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_FORBIDDEN,
1.100 frystyk 312:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 313:    http->next = HTTP_ERROR;
 314:    http->result = HT_FORBIDDEN;
1.71 frystyk 315:    break;
1.55 frystyk 316:    
1.71 frystyk 317:    case 404:                            /* Not Found */
1.104 frystyk 318:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_NOT_FOUND,
1.100 frystyk 319:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 320:    http->next = HTTP_ERROR;
 321:    http->result = HT_ERROR;
1.71 frystyk 322:    break;
 323:    
1.78 frystyk 324:    case 405:                           /* Not Allowed */
1.104 frystyk 325:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_NOT_ALLOWED,
1.100 frystyk 326:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 327:    http->next = HTTP_ERROR;
 328:    http->result = HT_ERROR;
1.78 frystyk 329:    break;
 330: 
 331:    case 406:                         /* None Acceptable */
1.104 frystyk 332:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_NONE_ACCEPTABLE,
1.100 frystyk 333:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 334:    http->next = HTTP_ERROR;
 335:    http->result = HT_NOT_ACCEPTABLE;
1.78 frystyk 336:    break;
 337: 
 338:    case 407:                  /* Proxy Authentication Required */
1.127 frystyk 339:    HTRequest_addError(me->request, ERR_FATAL, NO,HTERR_PROXY_UNAUTHORIZED,
1.100 frystyk 340:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 341:    http->next = HTTP_ERROR;
 342:    http->result = HT_NO_PROXY_ACCESS;
1.78 frystyk 343:    break;
 344: 
 345:    case 408:                         /* Request Timeout */
1.104 frystyk 346:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_TIMEOUT,
1.100 frystyk 347:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 348:    http->next = HTTP_ERROR;
 349:    http->result = HT_ERROR;
 350:    break;
 351: 
 352:    case 409:                             /* Conflict */
 353:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_CONFLICT,
 354:          me->reason, (int) strlen(me->reason), "HTTPNextState");
 355:    http->next = HTTP_ERROR;
 356:    http->result = HT_CONFLICT;
 357:    break;
 358: 
 359:    case 410:                               /* Gone */
 360:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_GONE,
 361:          me->reason, (int) strlen(me->reason), "HTTPNextState");
 362:    http->next = HTTP_ERROR;
 363:    http->result = HT_ERROR;
 364:    break;
 365: 
 366:    case 411:                         /* Length Required */
 367:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_LENGTH_REQUIRED,
 368:          me->reason, (int) strlen(me->reason), "HTTPNextState");
 369:    http->next = HTTP_ERROR;
 370:    http->result = HT_LENGTH_REQUIRED;
 371:    break;
 372: 
 373:    case 412:                       /* Precondition failed */
 374:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_PRECON_FAILED,
 375:          me->reason, (int) strlen(me->reason), "HTTPNextState");
 376:    http->next = HTTP_ERROR;
 377:    http->result = HT_ERROR;
 378:    break;
 379: 
 380:    case 413:                     /* Request entity too large */
 381:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_TOO_BIG,
 382:          me->reason, (int) strlen(me->reason), "HTTPNextState");
 383:    http->next = HTTP_ERROR;
 384:    http->result = HT_ERROR;
 385:    break;
 386: 
 387:    case 414:                       /* Request-URI too long */
 388:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_URI_TOO_BIG,
 389:          me->reason, (int) strlen(me->reason), "HTTPNextState");
 390:    http->next = HTTP_ERROR;
 391:    http->result = HT_ERROR;
 392:    break;
 393: 
 394:    case 415:                           /* Unsupported */
 395:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_UNSUPPORTED,
 396:          me->reason, (int) strlen(me->reason), "HTTPNextState");
 397:    http->next = HTTP_ERROR;
 398:    http->result = HT_ERROR;
1.78 frystyk 399:    break;
 400: 
1.71 frystyk 401:    case 500:
1.104 frystyk 402:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_INTERNAL,
1.100 frystyk 403:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 404:    http->next = HTTP_ERROR;
 405:    http->result = HT_ERROR;
1.78 frystyk 406:    break;
 407:    
1.71 frystyk 408:    case 501:
1.104 frystyk 409:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_NOT_IMPLEMENTED,
1.100 frystyk 410:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 411:    http->next = HTTP_ERROR;
 412:    http->result = HT_ERROR;
1.78 frystyk 413:    break;
 414: 
 415:    case 502:
1.104 frystyk 416:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_BAD_GATE,
1.100 frystyk 417:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 418:    http->next = HTTP_ERROR;
 419:    http->result = HT_ERROR;
1.78 frystyk 420:    break;
 421: 
 422:    case 503:
1.104 frystyk 423:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_DOWN,
1.100 frystyk 424:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 425:    http->next = HTTP_ERROR;
1.81 frystyk 426: 
1.140 frystyk 427:    /*
 428:    ** If Retry-After header is found then return HT_RETRY else HT_ERROR.
 429:    ** The caller may want to reissue the request at a later point in time.
 430:    */
 431:    {
 432:      HTResponse * response = HTRequest_response(me->request);
 433:      if (HTResponse_retryTime(response))
 434:        http->result = HT_RETRY;
 435:      else
 436:        http->result = HT_ERROR;
 437:    }
1.78 frystyk 438:    break;
 439: 
 440:    case 504:
1.104 frystyk 441:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_GATE_TIMEOUT,
1.100 frystyk 442:          me->reason, (int) strlen(me->reason), "HTTPNextState");
1.134 frystyk 443:     http->next = HTTP_ERROR;
 444:    http->result = HT_ERROR;
 445:    break;
 446: 
 447:    case 505:                   /* Unsupported protocol version */
 448:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_BAD_VERSION,
 449:          me->reason, (int) strlen(me->reason), "HTTPNextState");
 450:     http->next = HTTP_ERROR;
 451:    http->result = HT_BAD_VERSION;
1.71 frystyk 452:    break;
1.78 frystyk 453: 
1.71 frystyk 454:    default:                        /* bad number */
1.104 frystyk 455:    HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_BAD_REPLY,
1.100 frystyk 456:          (void *) me->buffer, me->buflen, "HTTPNextState");
1.134 frystyk 457:    http->next = HTTP_ERROR;
 458:    http->result = HT_ERROR;
1.71 frystyk 459:    break;
1.55 frystyk 460:   }
 461: }
 462: 
1.71 frystyk 463: /* ------------------------------------------------------------------------- */
 464: /*             HTTP Status Line Stream             */
 465: /* ------------------------------------------------------------------------- */
1.55 frystyk 466: 
1.71 frystyk 467: /*
1.141 frystyk 468: **   Analyze the stream we have read. If it is a HTTP 1.0 or higher
1.71 frystyk 469: **   then create a MIME-stream, else create a Guess stream to find out
 470: **   what the 0.9 server is sending. We need to copy the buffer as we don't
 471: **   know if we can modify the contents or not.
1.78 frystyk 472: **
 473: **   Stream handling is a function of the status code returned from the 
 474: **   server:
 475: **       200:   Use `output_stream' in HTRequest structure
1.94 frystyk 476: **       else:  Use `debug_stream' in HTRequest structure
1.80 frystyk 477: **
 478: **   Return: YES if buffer should be written out. NO otherwise
1.56 frystyk 479: */
1.100 frystyk 480: PRIVATE int stream_pipe (HTStream * me)
1.56 frystyk 481: {
1.136 frystyk 482:   HTRequest * request = me->request;
 483:   HTNet * net = HTRequest_net(request);
1.123 frystyk 484:   HTHost * host = HTNet_host(net);
1.141 frystyk 485: 
1.80 frystyk 486:   if (me->target) {
 487:    int status = PUTBLOCK(me->buffer, me->buflen);
 488:    if (status == HT_OK)
 489:      me->transparent = YES;
 490:    return status;
1.136 frystyk 491:   } else if (HTRequest_isSource(request)&&!HTRequest_outputStream(request)) {
1.88 frystyk 492:    /*
 493:    ** We need to wait for the destinations to get ready
 494:    */
 495:    return HT_WOULD_BLOCK;
1.80 frystyk 496:   }
1.88 frystyk 497:    
1.132 frystyk 498:   /*
 499:   ** Just check for HTTP and not HTTP/ as NCSA server chokes on 1.1 replies
 500:   ** Thanks to Markku Savela <msa@msa.tte.vtt.fi>
1.141 frystyk 501: 
1.132 frystyk 502:   */
 503: #if 0
1.81 frystyk 504:   if (strncasecomp(me->buffer, "http/", 5)) {
1.132 frystyk 505: #else
 506:   if (strncasecomp(me->buffer, "http", 4)) {
 507: #endif
1.80 frystyk 508:    int status;
1.136 frystyk 509:    HTRequest_addError(request, ERR_INFO, NO, HTERR_HTTP09,
1.80 frystyk 510:          (void *) me->buffer, me->buflen, "HTTPStatusStream");
1.136 frystyk 511:    me->target = HTStreamStack(WWW_UNKNOWN,
 512:                  HTRequest_outputFormat(request),
 513:                  HTRequest_outputStream(request),
 514:                  request, NO);
1.134 frystyk 515:    me->http->next = HTTP_OK;
1.80 frystyk 516:    if ((status = PUTBLOCK(me->buffer, me->buflen)) == HT_OK)
 517:      me->transparent = YES;
1.123 frystyk 518:    HTHost_setVersion(host, HTTP_09);
1.80 frystyk 519:    return status;
 520:   } else {
1.140 frystyk 521:    HTResponse * response = HTRequest_response(request);
1.133 frystyk 522:    char *ptr = me->buffer+4;           /* Skip the HTTP part */
1.81 frystyk 523:    me->version = HTNextField(&ptr);
1.141 frystyk 524:    HTHost_setConsumed(net->host, me->buflen);
1.95 frystyk 525: 
1.130 frystyk 526:    /* Here we want to find out when to use persistent connection */
1.133 frystyk 527:    if (!strncasecomp(me->version, "/1.0", 4)) {
1.128 frystyk 528:      if (PROT_TRACE)HTTrace("HTTP Status. This is a HTTP/1.0 server\n");
 529:      HTHost_setVersion(host, HTTP_10);
1.133 frystyk 530:    } else if (!strncasecomp(me->version, "/1.", 3)) {   /* 1.x family */
1.128 frystyk 531:      HTHost_setVersion(host, HTTP_11);
1.141 frystyk 532: #ifdef HT_MUX
 533:      HTNet_setPersistent(net, YES, HT_TP_INTERLEAVE);
 534: #else
 535:      HTNet_setPersistent(net, YES, HT_TP_PIPELINE);
 536: #endif
1.133 frystyk 537:    } else { 
 538:      if (PROT_TRACE)HTTrace("HTTP Status. No 1.x version number - treat it as a HTTP/1.0 server\n");
 539:      HTHost_setVersion(host, HTTP_10);
1.128 frystyk 540:    }
1.95 frystyk 541: 
1.81 frystyk 542:    me->status = atoi(HTNextField(&ptr));
1.133 frystyk 543:    /* Kludge to fix the NCSA server version bug */
 544:    if (me->status == 0) me->status = atoi(me->version);
 545: 
1.81 frystyk 546:    me->reason = ptr;
 547:    if ((ptr = strchr(me->reason, '\r')) != NULL)   /* Strip \r and \n */
 548:      *ptr = '0円';
 549:    else if ((ptr = strchr(me->reason, '\n')) != NULL)
 550:      *ptr = '0円';
 551: 
1.130 frystyk 552:    /* 
 553:    ** If it is a 1xx code then find out what to do and return until we
 554:    ** get the next code. In the case of Upgrade we may not get back here
 555:    ** at all. If we are uploading an entity then continue doing that
 556:    */
 557:    if (me->status/100 == 1) {
1.136 frystyk 558:      if (HTTPInformation(me) == YES) {
 559:        me->buflen = 0;
 560:        me->state = EOL_BEGIN;
 561:        return HTMethod_hasEntity(HTRequest_method(request)) ?
 562:          HT_CONTINUE : HT_OK;
1.103 frystyk 563:      }
1.56 frystyk 564:    }
1.133 frystyk 565: 
 566:    /*
 567:    ** As we are getting fresh metainformation in the HTTP response,
 568:    ** we clear the old metainfomation in order not to mix it with the new
 569:    ** one. This is particularly important for the content-length and the
1.139 frystyk 570:    ** like. The TRACE and OPTIONS method just adds to the current 
 571:    ** metainformation so in that case we don't clear the anchor.
1.133 frystyk 572:    */
1.136 frystyk 573:    if (me->status==200 || me->status==203 || me->status==300) {
1.140 frystyk 574:      /*
 575:      ** 200, 203 and 300 are all fully cacheable responses. No byte 
 576:      ** ranges or anything else make life hard in this case.
 577:      */
 578:      HTResponse_setCachable(response, YES);
1.136 frystyk 579:      me->target = HTStreamStack(WWW_MIME,
 580:                    HTRequest_outputFormat(request),
 581:                    HTRequest_outputStream(request),
 582:                    request, NO);
1.140 frystyk 583:    } else if (me->status==206) {
 584:      /*
 585:      ** We got a partial response and now we must check whether
 586:      ** we issued a cache If-Range request or it was a new 
 587:      ** partial response which we don't have in cache. In the latter
 588:      ** case, we don't cache the object and in the former we append
 589:      ** the result to the already existing cache entry.
 590:      */
 591:      HTReload reload = HTRequest_reloadMode(request);
 592:      if (reload == HT_CACHE_RANGE_VALIDATE) {
 593:        HTResponse_setCachable(response, YES);
 594:        me->target = HTStreamStack(WWW_MIME_PART,
 595:                      HTRequest_outputFormat(request),
 596:                      HTRequest_outputStream(request),
 597:                      request, NO);
 598:      } else {
 599:        me->target = HTStreamStack(WWW_MIME,
 600:                      HTRequest_outputFormat(request),
 601:                      HTRequest_outputStream(request),
 602:                      request, NO);
 603:      }
1.139 frystyk 604:    } else if (me->status==204 || me->status==304) {
1.140 frystyk 605:      HTResponse_setCachable(response, YES);
1.136 frystyk 606:      me->target = HTStreamStack(WWW_MIME_HEAD,
 607:                    HTRequest_debugFormat(request),
 608:                    HTRequest_debugStream(request),
 609:                    request, NO);
 610:    } else if (HTRequest_debugStream(request)) {
 611:      me->target = HTStreamStack(WWW_MIME,
 612:                    HTRequest_debugFormat(request),
 613:                    HTRequest_debugStream(request),
 614:                    request, NO);
 615:    } else {
1.140 frystyk 616:      /*
 617:      ** We still need to parse the MIME part in order to find any
 618:      ** valuable meta information which is needed from the response.
 619:      */
1.136 frystyk 620:      me->target = HTStreamStack(WWW_MIME,
 621:                    HTRequest_debugFormat(request),
 622:                    HTRequest_debugStream(request),
 623:                    request, NO);
1.133 frystyk 624:    }
1.56 frystyk 625:   }
1.113 frystyk 626:   if (!me->target) me->target = HTErrorStream();
1.81 frystyk 627:   HTTPNextState(me);                  /* Get next state */
1.80 frystyk 628:   me->transparent = YES;
 629:   return HT_OK;
1.71 frystyk 630: }
1.56 frystyk 631: 
1.80 frystyk 632: /*
 633: **   Searches for HTTP header line until buffer fills up or a CRLF or LF
 634: **   is found
 635: */
1.119 frystyk 636: PRIVATE int HTTPStatus_put_block (HTStream * me, const char * b, int l)
1.71 frystyk 637: {
1.80 frystyk 638:   while (!me->transparent && l-- > 0) {
 639:    int status;
 640:    if (me->target) {
 641:      if ((status = stream_pipe(me)) != HT_OK)
 642:        return status;
 643:    } else {
 644:      *(me->buffer+me->buflen++) = *b;
 645:      if (me->state == EOL_FCR) {
 646:        if (*b == LF) { /* Line found */
 647:          if ((status = stream_pipe(me)) != HT_OK)
 648:            return status;
 649:        } else {
 650:          me->state = EOL_BEGIN;
 651:        }
 652:      } else if (*b == CR) {
 653:        me->state = EOL_FCR;
 654:      } else if (*b == LF) {
 655:        if ((status = stream_pipe(me)) != HT_OK)
 656:          return status;
1.71 frystyk 657:      } else {
1.80 frystyk 658:        if (me->buflen >= MAX_STATUS_LEN) {
 659:          if ((status = stream_pipe(me)) != HT_OK)
 660:            return status;
 661:        }
1.71 frystyk 662:      }
1.80 frystyk 663:      b++;
1.71 frystyk 664:    }
1.56 frystyk 665:   }
1.99 frystyk 666:   if (l > 0) return PUTBLOCK(b, l);
 667:   return HT_OK;
1.56 frystyk 668: }
 669: 
1.119 frystyk 670: PRIVATE int HTTPStatus_put_string (HTStream * me, const char * s)
1.71 frystyk 671: {
1.80 frystyk 672:   return HTTPStatus_put_block(me, s, (int) strlen(s));
1.71 frystyk 673: }
1.56 frystyk 674: 
1.100 frystyk 675: PRIVATE int HTTPStatus_put_character (HTStream * me, char c)
1.71 frystyk 676: {
1.80 frystyk 677:   return HTTPStatus_put_block(me, &c, 1);
 678: }
 679: 
1.100 frystyk 680: PRIVATE int HTTPStatus_flush (HTStream * me)
1.80 frystyk 681: {
 682:   return (*me->target->isa->flush)(me->target);
1.71 frystyk 683: }
 684: 
1.100 frystyk 685: PRIVATE int HTTPStatus_free (HTStream * me)
1.71 frystyk 686: {
1.87 frystyk 687:   int status = HT_OK;
 688:   if (me->target) {
 689:    if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
 690:      return HT_WOULD_BLOCK;
 691:   }
1.115 frystyk 692:   HT_FREE(me);
1.100 frystyk 693:   return status;
1.71 frystyk 694: }
 695: 
1.104 frystyk 696: PRIVATE int HTTPStatus_abort (HTStream * me, HTList * e)
1.71 frystyk 697: {
 698:   if (me->target)
1.74 frystyk 699:    ABORT_TARGET;
1.115 frystyk 700:   HT_FREE(me);
1.74 frystyk 701:   if (PROT_TRACE)
1.116 eric 702:    HTTrace("HTTPStatus.. ABORTING...\n");
1.80 frystyk 703:   return HT_ERROR;
1.71 frystyk 704: }
 705: 
 706: /*   HTTPStatus Stream
 707: **   -----------------
 708: */
1.119 frystyk 709: PRIVATE const HTStreamClass HTTPStatusClass =
1.71 frystyk 710: {       
 711:   "HTTPStatus",
1.80 frystyk 712:   HTTPStatus_flush,
1.71 frystyk 713:   HTTPStatus_free,
 714:   HTTPStatus_abort,
 715:   HTTPStatus_put_character,
 716:   HTTPStatus_put_string,
 717:   HTTPStatus_put_block
 718: };
 719: 
1.113 frystyk 720: PUBLIC HTStream * HTTPStatus_new (HTRequest * request,
 721:                 void *    param,
 722:                 HTFormat   input_format,
 723:                 HTFormat   output_format,
 724:                 HTStream *  output_stream)
1.71 frystyk 725: {
1.115 frystyk 726:   HTStream * me;
 727:   if ((me = (HTStream *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
 728:     HT_OUTOFMEM("HTTPStatus_new");
1.71 frystyk 729:   me->isa = &HTTPStatusClass;
1.113 frystyk 730:   if (request) {
1.126 frystyk 731:    HTNet * net = HTRequest_net(request);
1.125 frystyk 732:     /* Get existing copy */
 733:    http_info * http = (http_info *) HTNet_context(net);
1.113 frystyk 734:    me->request = request;
 735:    me->http = http;
 736:    http->next = HTTP_ERROR;
 737:    me->state = EOL_BEGIN;
 738:    return me;
 739:   } else
 740:    return HTErrorStream();
1.71 frystyk 741: }
 742: 
 743: /* ------------------------------------------------------------------------- */
 744: 
 745: /*       Load Document from HTTP Server            HTLoadHTTP
1.55 frystyk 746: **       ==============================
 747: **
 748: **   Given a hypertext address, this routine loads a document.
 749: **
 750: ** On entry,
 751: **   request        This is the request structure
1.94 frystyk 752: **   returns     HT_ERROR    Error has occured in call back
 753: **           HT_OK      Call back was OK
1.55 frystyk 754: */
1.141 frystyk 755: PRIVATE int HTTPEvent (SOCKET soc, void * pVoid, HTEventType type);
 756: 
 757: PUBLIC int HTLoadHTTP (SOCKET soc, HTRequest * request)
1.55 frystyk 758: {
 759:   http_info *http;            /* Specific protocol information */
1.112 frystyk 760:   HTParentAnchor *anchor = HTRequest_anchor(request);
1.141 frystyk 761:   HTNet * net = HTRequest_net(request);
1.112 frystyk 762: 
1.94 frystyk 763:   /*
 764:   ** Initiate a new http structure and bind to request structure
 765:   ** This is actually state HTTP_BEGIN, but it can't be in the state
 766:   ** machine as we need the structure first.
 767:   */
1.141 frystyk 768:   if (PROT_TRACE) HTTrace("HTTP........ Looking for `%s\'\n",
 769:              HTAnchor_physical(anchor));
 770:   if ((http = (http_info *) HT_CALLOC(1, sizeof(http_info))) == NULL)
 771:    HT_OUTOFMEM("HTLoadHTTP");
 772:   http->net = net;
 773:   HTNet_setContext(net, http);
 774:   HTNet_setEventCallback(net, HTTPEvent);
 775:   HTNet_setEventParam(net, http); /* callbacks get http* */
 776: 
 777:   return HTTPEvent(soc, http, HTEvent_BEGIN);        /* get it started - ops is ignored */
 778: }
 779: 
 780: PRIVATE int HTTPEvent (SOCKET soc, void * pVoid, HTEventType type)
 781: {
 782:   http_info * http = (http_info *)pVoid;
 783:   int status = HT_ERROR;
 784:   HTNet * net = http->net;
 785:   HTRequest * request = HTNet_request(net);
1.144 ! frystyk 786:   HTParentAnchor * anchor = HTRequest_anchor(request);
 ! 787:   HTHost * host = HTNet_host(net);
1.141 frystyk 788: 
 789:   /*
 790:   ** Check whether we have been interrupted
 791:   */
 792:   if (type == HTEvent_BEGIN) {
1.134 frystyk 793:    http->next = HTTP_OK;
 794:    http->result = HT_ERROR;
1.141 frystyk 795:   } else if (type == HTEvent_CLOSE) {
1.112 frystyk 796:    HTRequest_addError(request, ERR_FATAL, NO, HTERR_INTERRUPTED,
 797:              NULL, 0, "HTLoadHTTP");
 798:    HTTPCleanup(request, HT_INTERRUPTED);
1.94 frystyk 799:    return HT_OK;
1.141 frystyk 800:   } else if (type == HTEvent_END) {
 801:    HTTPCleanup(request, http->result);
 802:    return HT_OK;
 803:   } else if (type == HTEvent_RESET) {
 804:    if (HTRequest_isPostWeb(request)) {
 805:      if (HTRequest_isDestination(request)) {
 806:        HTRequest * source = HTRequest_source(request);
 807:        HTLink *link =
 808:          HTLink_find((HTAnchor *)HTRequest_anchor(source),
 809:                (HTAnchor *) anchor);
 810:        HTLink_setResult(link, HT_LINK_ERROR);
 811:      }
 812:      HTRequest_killPostWeb(request);
 813:    }
 814:    HTTPCleanup(request, HT_RECOVER_PIPE);
1.144 ! frystyk 815:    http->state = HTTP_NEED_STREAM;
1.141 frystyk 816:    return HT_OK;
 817:   }
 818: 
1.71 frystyk 819:   /* Now jump into the machine. We know the state from the previous run */
 820:   while (1) {
 821:    switch (http->state) {
1.134 frystyk 822:    case HTTP_BEGIN:
1.144 ! frystyk 823:      status = HTHost_connect(host, net, HTAnchor_physical(anchor), HTTP_PORT);
 ! 824:      host = HTNet_host(net);
1.80 frystyk 825:      if (status == HT_OK) {
1.140 frystyk 826: 
1.123 frystyk 827:        /*
1.140 frystyk 828:        ** Check the protocol class to see if we have connected to a
 829:        ** the right class of server, in this case HTTP. If we don't 
 830:        ** know the server then assume a HTTP/1.0
1.123 frystyk 831:        */
 832:        {
 833:          char * s_class = HTHost_class(host);
1.140 frystyk 834:          if (!s_class) {
 835:            if (HTRequest_proxy(request) == NULL)
 836:              HTRequest_addConnection(request, "Keep-Alive", "");
 837:            HTHost_setClass(host, "http");
 838:          } else if (strcasecomp(s_class, "http")) {
1.123 frystyk 839:            HTRequest_addError(request, ERR_FATAL, NO, HTERR_CLASS,
 840:                      NULL, 0, "HTLoadHTTP");
 841:            http->state = HTTP_ERROR;
 842:            break;
 843:          }
1.95 frystyk 844:        }
1.144 ! frystyk 845:        http->state = HTTP_NEED_STREAM;
 ! 846:      } else if (status == HT_WOULD_BLOCK || status == HT_PENDING)
 ! 847:        return HT_OK;
 ! 848:      else    
 ! 849:        http->state = HTTP_ERROR;       /* Error or interrupt */
 ! 850:      break;
 ! 851:      
 ! 852:    case HTTP_NEED_STREAM:
1.138 frystyk 853: 
1.144 ! frystyk 854: #if 0
 ! 855:      HTChannel_upSemaphore(host->channel);
 ! 856: #endif
 ! 857: 
 ! 858:      /* 
 ! 859:      ** Create the stream pipe FROM the channel to the application.
 ! 860:      ** The target for the input stream pipe is set up using the
 ! 861:      ** stream stack.
 ! 862:      */
 ! 863:      {
 ! 864:        HTStream *me=HTStreamStack(WWW_HTTP,
 ! 865:                      HTRequest_outputFormat(request),
 ! 866:                      HTRequest_outputStream(request),
 ! 867:                      request, YES);
 ! 868:        HTNet_setReadStream(net, me);
 ! 869:        HTRequest_setOutputConnected(request, YES);
 ! 870:      }
1.127 frystyk 871: 
1.144 ! frystyk 872:      /*
 ! 873:      ** Create the stream pipe TO the channel from the application
 ! 874:      ** and hook it up to the request object
 ! 875:      */
 ! 876:      {
 ! 877:        HTChannel * channel = HTHost_channel(host);
 ! 878:        HTOutputStream * output = HTChannel_getChannelOStream(channel);
 ! 879:        int version = HTHost_version(host);
 ! 880:        HTStream * app = NULL;
 ! 881:        
1.123 frystyk 882:        /*
1.144 ! frystyk 883:        ** Instead of calling this directly we could have a 
 ! 884:        ** stream stack builder on the output stream as well
1.123 frystyk 885:        */
1.141 frystyk 886: #ifdef HT_MUX
1.144 ! frystyk 887:        output = (HTOutputStream *)
 ! 888:          HTBuffer_new(HTMuxWriter_new(host, net, output), request, 512);
1.141 frystyk 889: #endif
 890:          
1.139 frystyk 891: #ifdef HTTP_DUMP
1.144 ! frystyk 892:        if (PROT_TRACE) {
 ! 893:          if ((htfp = fopen(HTTP_OUTPUT, "ab"))) {
 ! 894:            output = (HTOutputStream *)
 ! 895:              HTTee((HTStream *) output,
 ! 896:                 HTFWriter_new(request, htfp, YES), NULL);
 ! 897:            HTTrace("HTTP........ Dumping request to `%s\'\n",
 ! 898:                HTTP_OUTPUT);
 ! 899:          }
 ! 900:        }    
1.139 frystyk 901: #endif /* HTTP_DUMP */
1.144 ! frystyk 902:        app = HTMethod_hasEntity(HTRequest_method(request)) ?
 ! 903:          HTMIMERequest_new(request,
 ! 904:                   HTTPRequest_new(request, (HTStream *) output, NO,
 ! 905:                           version),
 ! 906:                   YES) :
 ! 907:          HTTPRequest_new(request, (HTStream *) output, YES, version);
 ! 908:        HTRequest_setInputStream(request, app);
 ! 909:      }
1.88 frystyk 910: 
1.144 ! frystyk 911:      /*
 ! 912:      ** Set up concurrent read/write if this request isn't the
 ! 913:      ** source for a PUT or POST. As source we don't start reading
 ! 914:      ** before all destinations are ready. If destination then
 ! 915:      ** register the input stream and get ready for read
 ! 916:      */
 ! 917:      if (HTRequest_isDestination(request)) {
 ! 918:        HTHost_register(host, net, HTEvent_READ);
 ! 919:        HTRequest_linkDestination(request);
 ! 920:      }
 ! 921:      http->state = HTTP_CONNECTED;
 ! 922:      type = HTEvent_WRITE;              /* fresh, so try a write */
1.71 frystyk 923:      break;
 924: 
1.87 frystyk 925:      /* As we can do simultanous read and write this is now one state */
1.141 frystyk 926:     case HTTP_CONNECTED:
 927:       if (type == HTEvent_WRITE) {
 928:         if (HTRequest_isDestination(request)) {
 929:           HTRequest * source = HTRequest_source(request);
 930:           HTNet * srcnet = HTRequest_net(source);
 931:           if (srcnet) {
 932:             HTHost_register(srcnet->host, srcnet, HTEvent_READ);
 933:             HTHost_unregister(srcnet->host, srcnet, HTEvent_WRITE);
 934:           }
 935:           return HT_OK;
 936:         }
1.126 frystyk 937: 
 938:        /*
 939:        ** Should we use the input stream directly or call the post
 940:        ** callback function to send data down to the network?
 941:        */
 942:        {
 943:          HTStream * input = HTRequest_inputStream(request);
 944:          HTPostCallback * pcbf = HTRequest_postCallback(request);
1.131 frystyk 945:          if (pcbf) {
1.139 frystyk 946:            if (http->lock)
 947:              return HT_OK;
 948:            else {
1.141 frystyk 949:              status = (*pcbf)(request, input);
1.140 frystyk 950:              if (status == HT_PAUSE || status == HT_LOADED) {
1.141 frystyk 951:                type = HTEvent_READ;
1.139 frystyk 952:                http->lock = YES;
1.141 frystyk 953:              } else if (status==HT_CLOSED)
 954:                http->state = HTTP_RECOVER_PIPE;
 955:              else if (status == HT_ERROR) {
1.139 frystyk 956:                http->result = HT_INTERRUPTED;
 957:                http->state = HTTP_ERROR;
 958:                break;
 959:              }
 960:            }
1.131 frystyk 961:          } else {
1.142 frystyk 962:            /*
 963:            ** Check to see if we can start a new request
 964:            ** pending in the host object.
 965:            */
1.144 ! frystyk 966:            HTHost_launchPending(host);
1.142 frystyk 967:            status = HTRequest_flush(request) ?
1.144 ! frystyk 968:              HTHost_forceFlush(host) : (*input->isa->flush)(input);
1.141 frystyk 969:            type = HTEvent_READ;
1.131 frystyk 970:          }
 971:          if (status == HT_WOULD_BLOCK) return HT_OK;
1.126 frystyk 972:        }
1.141 frystyk 973:      } else if (type == HTEvent_FLUSH) {
 974:        HTStream * input = HTRequest_inputStream(request);
 975:        if (input == NULL)
 976:          return HT_ERROR;
 977:        return (*input->isa->flush)(input);
 978:      } else if (type == HTEvent_READ) {
1.144 ! frystyk 979:        status = HTHost_read(host, net);
1.131 frystyk 980:        if (status == HT_WOULD_BLOCK)
1.94 frystyk 981:          return HT_OK;
1.131 frystyk 982:        else if (status == HT_CONTINUE) {
1.130 frystyk 983:          if (PROT_TRACE) HTTrace("HTTP........ Continuing\n");
1.139 frystyk 984:          http->lock = NO;
1.141 frystyk 985:          type = HTEvent_WRITE;
1.130 frystyk 986:          continue;
1.133 frystyk 987:        } else if (status==HT_LOADED)
1.141 frystyk 988:          http->state = http->next;  /* Jump to next state (OK or ERROR) */
 989:        else if (status==HT_CLOSED)
 990:          http->state = HTTP_RECOVER_PIPE;
 991:        else
1.94 frystyk 992:          http->state = HTTP_ERROR;
1.90 frystyk 993:      } else {
1.141 frystyk 994:        http->state = HTTP_ERROR;    /* don't know how to handle OOB */
1.90 frystyk 995:      }
1.71 frystyk 996:      break;
1.141 frystyk 997: 
1.134 frystyk 998:     case HTTP_OK:
1.88 frystyk 999:      if (HTRequest_isPostWeb(request)) {
1.91 frystyk 1000:        if (HTRequest_isDestination(request)) {
1.126 frystyk 1001:          HTRequest * source = HTRequest_source(request);
1.91 frystyk 1002:          HTLink *link =
1.127 frystyk 1003:            HTLink_find((HTAnchor *)HTRequest_anchor(source),
1.112 frystyk 1004:                     (HTAnchor *) anchor);
1.109 frystyk 1005:          HTLink_setResult(link, HT_LINK_OK);
1.91 frystyk 1006:        }
1.112 frystyk 1007:      }
1.134 frystyk 1008:      HTTPCleanup(request, http->result);
1.94 frystyk 1009:      return HT_OK;
1.71 frystyk 1010:      break;
1.141 frystyk 1011: 
 1012:      case HTTP_RECOVER_PIPE:
 1013:     {
 1014:       /*
 1015:       ** If this is a persistent connection and we get a close
 1016:       ** then it is an error and we should recover from it by
 1017:       ** restarting the pipe line of requests if any
 1018:       */
 1019:       if (HTHost_isPersistent(host)) {
1.143 frystyk 1020:         if (host == NULL) return HT_ERROR;
1.144 ! frystyk 1021:         HTRequest_setFlush(request, YES);
 ! 1022:         HTHost_recoverPipe(host);
 ! 1023:         http->state = HTTP_BEGIN;
 ! 1024:         HTHost_launchPending(host);
 ! 1025:         return HT_OK;
1.141 frystyk 1026:       } else
 1027:         http->state = HTTP_OK;
 1028:     }
1.143 frystyk 1029:     break;
1.141 frystyk 1030: 
1.71 frystyk 1031:     case HTTP_ERROR:
1.143 frystyk 1032:       if (HTRequest_isPostWeb(request)) {
 1033:         if (HTRequest_isDestination(request)) {
 1034:           HTRequest * source = HTRequest_source(request);
 1035:           HTLink *link =
 1036:             HTLink_find((HTAnchor *)HTRequest_anchor(source),
 1037:                   (HTAnchor *) anchor);
 1038:           HTLink_setResult(link, HT_LINK_ERROR);
 1039:         }
 1040:         HTRequest_killPostWeb(request);
 1041:       }
 1042:       HTTPCleanup(request, http->result);
 1043:       return HT_OK;
 1044:       break;
 1045: 
1.141 frystyk 1046:    default:
 1047:      HTTrace("bad http state %d.\n", http->state);
 1048:      HTDebugBreak();
1.71 frystyk 1049:    }
 1050:   } /* End of while(1) */
 1051: }  
1.88 frystyk 1052: 
1.21 luotonen 1053: 

Webmaster

AltStyle によって変換されたページ (->オリジナル) /