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

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

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

Webmaster

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