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

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

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

Webmaster

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