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

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

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

Webmaster

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