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

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

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.
 6: **
 7: **   This module implments the HTTP protocol as a state machine
1.55 frystyk 8: **
 9: ** History:
1.59 frystyk 10: **  < May 24 94 ??  Unknown - but obviously written
1.56 frystyk 11: **   May 24 94 HF  Made reentrent and cleaned up a bit. Implemented
 12: **           Forward, redirection, error handling and referer field
1.67 duns 13: **   8 Jul 94 FM  Insulate free() from _free structure element.
1.71 frystyk 14: **   Jul 94 HFN   Written on top of HTTP.c, Henrik Frystyk
1.55 frystyk 15: **
1.1 timbl 16: */
 17: 
1.78 frystyk 18: /* Library include files */
 19: #include "tcp.h"
1.1 timbl 20: #include "HTUtils.h"
1.78 frystyk 21: #include "HTString.h"
1.71 frystyk 22: #include "HTParse.h"
1.1 timbl 23: #include "HTTCP.h"
 24: #include "HTFormat.h"
1.85 frystyk 25: #include "HTSocket.h"
1.2 timbl 26: #include "HTAlert.h"
 27: #include "HTMIME.h"
1.21 luotonen 28: #include "HTAccess.h"     /* HTRequest */
1.14 luotonen 29: #include "HTAABrow.h"     /* Access Authorization */
1.20 timbl 30: #include "HTTee.h"       /* Tee off a cache stream */
1.78 frystyk 31: #include "HTFWrite.h"     /* Write to cache file */
1.80 frystyk 32: #include "HTWriter.h"
1.54 luotonen 33: #include "HTError.h"
1.55 frystyk 34: #include "HTChunk.h"
1.71 frystyk 35: #include "HTGuess.h"
 36: #include "HTThread.h"
1.80 frystyk 37: #include "HTTPReq.h"
1.55 frystyk 38: #include "HTTP.h"                       /* Implements */
 39: 
 40: /* Macros and other defines */
1.71 frystyk 41: #define PUTC(c)        (*me->target->isa->put_character)(me->target, c)
 42: #define PUTS(s)        (*me->target->isa->put_string)(me->target, s)
 43: #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
 44: #define FREE_TARGET  (*me->target->isa->_free)(me->target)
1.74 frystyk 45: #define ABORT_TARGET  (*me->target->isa->abort)(me->target, e)
1.2 timbl 46: 
1.59 frystyk 47: /* Type definitions and global variables etc. local to this module */
 48: /* This is the local definition of HTRequest->net_info */
 49: typedef enum _HTTPState {
1.81 frystyk 50:   HTTP_RETRY     = -4,
1.71 frystyk 51:   HTTP_ERROR     = -3,
 52:   HTTP_NO_DATA    = -2,
 53:   HTTP_GOT_DATA   = -1,
 54:   HTTP_BEGIN     = 0,
 55:   HTTP_NEED_CONNECTION,
 56:   HTTP_NEED_REQUEST,
 57:   HTTP_REDIRECTION,
 58:   HTTP_AA
1.59 frystyk 59: } HTTPState;
1.55 frystyk 60: 
 61: typedef struct _http_info {
1.78 frystyk 62:   SOCKFD       sockfd;             /* Socket descripter */
1.71 frystyk 63:   SockA       sock_addr;       /* SockA is defined in tcp.h */
1.68 frystyk 64:   HTInputSocket *  isoc;                /* Input buffer */
1.80 frystyk 65:   SocAction     action;         /* Result of the select call */
 66:   HTStream *     target;               /* Target stream */
1.71 frystyk 67:   int        addressCount;    /* Attempts if multi-homed host */
 68:   time_t       connecttime;       /* Used on multihomed hosts */
 69:   struct _HTRequest *    request;      /* Link back to request structure */
1.68 frystyk 70: 
1.81 frystyk 71:   HTTPState     state;      /* Current State of the connection */
 72:   HTTPState     next;                 /* Next state */
1.55 frystyk 73: } http_info;
 74: 
1.80 frystyk 75: #define MAX_STATUS_LEN     75  /* Max nb of chars to check StatusLine */
1.55 frystyk 76: 
1.71 frystyk 77: struct _HTStream {
 78:   CONST HTStreamClass *   isa;
 79:   HTStream *         target;
 80:   HTRequest *            request;
 81:   http_info *            http;
 82:   HTSocketEOL            state;
 83:   BOOL            transparent;
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.71 frystyk 89: };
1.21 luotonen 90: 
1.71 frystyk 91: /* ------------------------------------------------------------------------- */
 92: /*                Help Functions               */
 93: /* ------------------------------------------------------------------------- */
1.21 luotonen 94: 
1.71 frystyk 95: /*                                 HTTPCleanup
1.1 timbl 96: **
1.55 frystyk 97: **   This function closes the connection and frees memory.
1.1 timbl 98: **
1.55 frystyk 99: **   Returns 0 on OK, else -1
1.1 timbl 100: */
1.80 frystyk 101: PRIVATE int HTTPCleanup ARGS2(HTRequest *, req, BOOL, abort)
1.1 timbl 102: {
1.71 frystyk 103:   http_info *http;
1.55 frystyk 104:   int status = 0;
1.80 frystyk 105:   if (!req || !req->net_info) {
1.78 frystyk 106:    if (PROT_TRACE) fprintf(TDEST, "HTTPCleanup. Bad argument!\n");
1.55 frystyk 107:    status = -1;
 108:   } else {
1.80 frystyk 109:    http = (http_info *) req->net_info;
1.78 frystyk 110:    if (http->sockfd != INVSOC) {
1.80 frystyk 111: 
 112:      /* Free stream with data TO network */
1.85 frystyk 113:      if (http->target) {
 114:        if (!req->PostCallBack) {
 115:          if (abort) {
 116:            (*req->input_stream->isa->abort)(req->input_stream,
 117:                             NULL);
 118:          } else {
 119:            (*req->input_stream->isa->_free)(req->input_stream);
 120:          }
 121:        }
 122:        
 123:        /* Free stream with data FROM network */
1.80 frystyk 124:        if (abort)
1.85 frystyk 125:          (*http->target->isa->abort)(http->target, NULL);
1.80 frystyk 126:        else
1.85 frystyk 127:          (*http->target->isa->_free)(http->target);
1.80 frystyk 128:      }
1.78 frystyk 129:      if (PROT_TRACE)
 130:        fprintf(TDEST,"HTTP........ Closing socket %d\n",http->sockfd);
1.59 frystyk 131:      if ((status = NETCLOSE(http->sockfd)) < 0)
1.78 frystyk 132:        HTErrorSysAdd(http->request, ERR_FATAL, socerrno, NO,
 133:               "NETCLOSE");
1.71 frystyk 134:      HTThreadState(http->sockfd, THD_CLOSE);
1.80 frystyk 135:      http->sockfd = INVSOC;
1.71 frystyk 136:      HTThread_clear((HTNetInfo *) http);
 137:    }
 138:    if (http->isoc)
 139:      HTInputSocket_free(http->isoc);
1.80 frystyk 140:    free(http);
 141:    req->net_info = NULL;
1.55 frystyk 142:   } 
 143:   return status;
 144: }
 145: 
 146: 
1.71 frystyk 147: PRIVATE BOOL HTTPAuthentication ARGS1(HTRequest *, request)
 148: {
 149:   HTAAScheme scheme;
 150:   HTList *valid_schemes = HTList_new();
 151:   HTAssocList **scheme_specifics = NULL;
1.76 frystyk 152:   char *tmplate = NULL;
1.71 frystyk 153: 
 154:   if (request->WWWAAScheme) {
 155:    if ((scheme = HTAAScheme_enum(request->WWWAAScheme)) != HTAA_UNKNOWN) {
 156:      HTList_addObject(valid_schemes, (void *) scheme);
 157:      if (!scheme_specifics) {
 158:        int i;
 159:        scheme_specifics = (HTAssocList**)
 160:          malloc(HTAA_MAX_SCHEMES * sizeof(HTAssocList*));
 161:        if (!scheme_specifics)
 162:          outofmem(__FILE__, "HTTPAuthentication");
 163:        for (i=0; i < HTAA_MAX_SCHEMES; i++)
 164:          scheme_specifics[i] = NULL;
 165:      }
 166:      scheme_specifics[scheme] = HTAA_parseArgList(request->WWWAARealm);
 167:    } else if (PROT_TRACE) {
 168:      HTErrorAdd(request, ERR_INFO, NO, HTERR_UNKNOWN_AA,
 169:            (void *) request->WWWAAScheme, 0, "HTTPAuthentication");
 170:      return NO;
 171:    }
 172:   }
 173:   if (request->WWWprotection) {
 174:    if (PROT_TRACE)
1.78 frystyk 175:      fprintf(TDEST, "Protection template set to `%s'\n",
1.71 frystyk 176:          request->WWWprotection);
1.76 frystyk 177:    StrAllocCopy(tmplate, request->WWWprotection);
1.71 frystyk 178:   }
 179:   request->valid_schemes = valid_schemes;
 180:   request->scheme_specifics = scheme_specifics;
1.76 frystyk 181:   request->prot_template = tmplate;
1.71 frystyk 182:   return YES;
 183: }
 184: 
 185: 
 186: /*
 187: **   This is a big switch handling all HTTP return codes. It puts in any
 188: **   appropiate error message and decides whether we should expect data
1.78 frystyk 189: **   or not.
1.55 frystyk 190: */
1.81 frystyk 191: PRIVATE void HTTPNextState ARGS1(HTStream *, me)
1.55 frystyk 192: {
1.71 frystyk 193:   switch (me->status) {
 194: 
1.78 frystyk 195:    case 0:                        /* 0.9 response */
 196:    case 200:
 197:    case 201:
 198:    case 202:
 199:    case 203:
1.81 frystyk 200:    me->http->next = HTTP_GOT_DATA;
1.71 frystyk 201:    break;
1.78 frystyk 202: 
 203:    case 204:                           /* No Response */
1.81 frystyk 204:    me->http->next = HTTP_NO_DATA;
1.71 frystyk 205:    break;
1.78 frystyk 206: 
1.71 frystyk 207:    case 301:                              /* Moved */
 208:    case 302:                              /* Found */
1.81 frystyk 209:    me->http->next = HTTP_REDIRECTION;
1.71 frystyk 210:    break;
1.55 frystyk 211:    
1.71 frystyk 212:    case 303:                              /* Method */
 213:    HTAlert("This client doesn't support automatic redirection of type `Method'");
1.81 frystyk 214:    me->http->next = HTTP_ERROR;
1.71 frystyk 215:    break;
1.55 frystyk 216:    
1.78 frystyk 217:    case 400:                           /* Bad Request */
 218:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_BAD_REQUEST,
1.81 frystyk 219:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 220:    me->http->next = HTTP_ERROR;
1.71 frystyk 221:    break;
1.70 howcome 222: 
1.71 frystyk 223:    case 401:
1.78 frystyk 224:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
1.81 frystyk 225:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 226:    me->http->next = HTTP_AA;
1.71 frystyk 227:    break;
 228:    
 229:    case 402:                         /* Payment required */
1.78 frystyk 230:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_PAYMENT_REQUIRED,
1.81 frystyk 231:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 232:    me->http->next = HTTP_ERROR;
1.71 frystyk 233:    break;
1.55 frystyk 234:    
1.71 frystyk 235:    case 403:                            /* Forbidden */
1.78 frystyk 236:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_FORBIDDEN,
1.81 frystyk 237:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 238:    me->http->next = HTTP_ERROR;
1.71 frystyk 239:    break;
1.55 frystyk 240:    
1.71 frystyk 241:    case 404:                            /* Not Found */
1.78 frystyk 242:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NOT_FOUND,
1.81 frystyk 243:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 244:    me->http->next = HTTP_ERROR;
1.71 frystyk 245:    break;
 246:    
1.78 frystyk 247:    case 405:                           /* Not Allowed */
 248:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NOT_ALLOWED,
1.81 frystyk 249:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 250:    me->http->next = HTTP_ERROR;
1.78 frystyk 251:    break;
 252: 
 253:    case 406:                         /* None Acceptable */
 254:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NONE_ACCEPTABLE,
1.81 frystyk 255:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 256:    me->http->next = HTTP_ERROR;
1.78 frystyk 257:    break;
 258: 
 259:    case 407:                  /* Proxy Authentication Required */
 260:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_PROXY,
1.81 frystyk 261:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 262:    me->http->next = HTTP_ERROR;
1.78 frystyk 263:    break;
 264: 
 265:    case 408:                         /* Request Timeout */
 266:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_TIMEOUT,
1.81 frystyk 267:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 268:    me->http->next = HTTP_ERROR;
1.78 frystyk 269:    break;
 270: 
1.71 frystyk 271:    case 500:
1.78 frystyk 272:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_INTERNAL,
1.81 frystyk 273:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 274:    me->http->next = HTTP_ERROR;
1.78 frystyk 275:    break;
 276:    
1.71 frystyk 277:    case 501:
1.78 frystyk 278:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NOT_IMPLEMENTED,
1.81 frystyk 279:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 280:    me->http->next = HTTP_ERROR;
1.78 frystyk 281:    break;
 282: 
 283:    case 502:
 284:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_BAD_GATE,
1.81 frystyk 285:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 286:    me->http->next = HTTP_ERROR;
1.78 frystyk 287:    break;
 288: 
 289:    case 503:
 290:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_DOWN,
1.81 frystyk 291:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 292: 
 293:    /* If Retry-After header is found then return HT_RETRY else HT_ERROR */
 294:    if (me->request->retry_after)
 295:      me->http->next = HTTP_RETRY;
 296:    else
 297:      me->http->next = HTTP_ERROR;
1.78 frystyk 298:    break;
 299: 
 300:    case 504:
 301:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_GATE_TIMEOUT,
1.81 frystyk 302:          me->reason, (int) strlen(me->reason), "HTLoadHTTP");
 303:     me->http->next = HTTP_ERROR;
1.71 frystyk 304:    break;
1.78 frystyk 305: 
1.71 frystyk 306:    default:                        /* bad number */
 307:    HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_BAD_REPLY,
1.80 frystyk 308:          (void *) me->buffer, me->buflen, "HTLoadHTTP");
1.81 frystyk 309:    me->http->next = HTTP_ERROR;
1.71 frystyk 310:    break;
1.55 frystyk 311:   }
 312: }
 313: 
1.71 frystyk 314: /* ------------------------------------------------------------------------- */
 315: /*             HTTP Status Line Stream             */
 316: /* ------------------------------------------------------------------------- */
1.55 frystyk 317: 
1.71 frystyk 318: /*
1.80 frystyk 319: **   Analyse the stream we have read. If it is a HTTP 1.0 or higher
1.71 frystyk 320: **   then create a MIME-stream, else create a Guess stream to find out
 321: **   what the 0.9 server is sending. We need to copy the buffer as we don't
 322: **   know if we can modify the contents or not.
1.78 frystyk 323: **
 324: **   Stream handling is a function of the status code returned from the 
 325: **   server:
 326: **       200:   Use `output_stream' in HTRequest structure
1.80 frystyk 327: **       else:  Use `error_stream' in HTRequest structure
 328: **
 329: **   Return: YES if buffer should be written out. NO otherwise
1.56 frystyk 330: */
1.80 frystyk 331: PRIVATE int stream_pipe ARGS1(HTStream *, me)
1.56 frystyk 332: {
1.71 frystyk 333:   HTRequest *req = me->request;
1.80 frystyk 334:   if (me->target) {
 335:    int status = PUTBLOCK(me->buffer, me->buflen);
 336:    if (status == HT_OK)
 337:      me->transparent = YES;
 338:    return status;
 339:   }
1.81 frystyk 340:   if (strncasecomp(me->buffer, "http/", 5)) {
1.80 frystyk 341:    int status;
 342:    HTErrorAdd(req, ERR_INFO, NO, HTERR_HTTP09,
 343:          (void *) me->buffer, me->buflen, "HTTPStatusStream");
 344:    me->target = HTGuess_new(req, NULL, WWW_UNKNOWN,
 345:                 req->output_format, req->output_stream);
 346:    if ((status = PUTBLOCK(me->buffer, me->buflen)) == HT_OK)
 347:      me->transparent = YES;
 348:    return status;
 349:   } else {
1.81 frystyk 350:    char *ptr = me->buffer+5;           /* Skip the HTTP part */
 351:    me->version = HTNextField(&ptr);
 352:    me->status = atoi(HTNextField(&ptr));
 353:    me->reason = ptr;
 354:    if ((ptr = strchr(me->reason, '\r')) != NULL)   /* Strip \r and \n */
 355:      *ptr = '0円';
 356:    else if ((ptr = strchr(me->reason, '\n')) != NULL)
 357:      *ptr = '0円';
 358: 
 359:    /* Set up the streams */
 360:    if (me->status==200) {
1.80 frystyk 361:      HTStream *s;
1.85 frystyk 362:      if (req->output_format == WWW_SOURCE) {
 363:        me->target = HTMIMEConvert(req, NULL, WWW_MIME,
 364:                      req->output_format,
 365:                      req->output_stream);
 366:      } else {
 367:        me->target = HTStreamStack(WWW_MIME, req->output_format,
 368:                      req->output_stream, req, NO);
1.80 frystyk 369:      
1.85 frystyk 370:        /* howcome: test for return value from HTCacheWriter 12/1/95 */
 371:        if (req->method==METHOD_GET && HTCache_isEnabled() &&
 372:          (s = HTCacheWriter(req, NULL, WWW_MIME, req->output_format,
 373:                    req->output_stream))) {
 374:          me->target = HTTee(me->target, s);
 375:        }
1.80 frystyk 376:      }
1.71 frystyk 377:    } else {
1.81 frystyk 378:      me->target = HTMIMEConvert(req, NULL, WWW_MIME, req->error_format,
 379:                    req->error_stream);
1.56 frystyk 380:    }
1.80 frystyk 381:    if (!me->target)
 382:      me->target = HTBlackHole();             /* What else */
1.56 frystyk 383:   }
1.81 frystyk 384:   HTTPNextState(me);                  /* Get next state */
1.80 frystyk 385:   me->transparent = YES;
 386:   return HT_OK;
1.71 frystyk 387: }
1.56 frystyk 388: 
1.80 frystyk 389: /*
 390: **   Searches for HTTP header line until buffer fills up or a CRLF or LF
 391: **   is found
 392: */
 393: PRIVATE int HTTPStatus_put_block ARGS3(HTStream *, me, CONST char*, b, int, l)
1.71 frystyk 394: {
1.80 frystyk 395:   while (!me->transparent && l-- > 0) {
 396:    int status;
 397:    if (me->target) {
 398:      if ((status = stream_pipe(me)) != HT_OK)
 399:        return status;
 400:    } else {
 401:      *(me->buffer+me->buflen++) = *b;
 402:      if (me->state == EOL_FCR) {
 403:        if (*b == LF) { /* Line found */
 404:          if ((status = stream_pipe(me)) != HT_OK)
 405:            return status;
 406:        } else {
 407:          me->state = EOL_BEGIN;
 408:        }
 409:      } else if (*b == CR) {
 410:        me->state = EOL_FCR;
 411:      } else if (*b == LF) {
 412:        if ((status = stream_pipe(me)) != HT_OK)
 413:          return status;
1.71 frystyk 414:      } else {
1.80 frystyk 415:        if (me->buflen >= MAX_STATUS_LEN) {
 416:          if ((status = stream_pipe(me)) != HT_OK)
 417:            return status;
 418:        }
1.71 frystyk 419:      }
1.80 frystyk 420:      b++;
1.71 frystyk 421:    }
1.56 frystyk 422:   }
1.80 frystyk 423:   if (l > 0)
 424:    return PUTBLOCK(b, l);
 425:   return HT_OK;
1.56 frystyk 426: }
 427: 
1.80 frystyk 428: PRIVATE int HTTPStatus_put_string ARGS2(HTStream *, me, CONST char*, s)
1.71 frystyk 429: {
1.80 frystyk 430:   return HTTPStatus_put_block(me, s, (int) strlen(s));
1.71 frystyk 431: }
1.56 frystyk 432: 
1.80 frystyk 433: PRIVATE int HTTPStatus_put_character ARGS2(HTStream *, me, char, c)
1.71 frystyk 434: {
1.80 frystyk 435:   return HTTPStatus_put_block(me, &c, 1);
 436: }
 437: 
 438: PRIVATE int HTTPStatus_flush ARGS1(HTStream *, me)
 439: {
 440:   return (*me->target->isa->flush)(me->target);
1.71 frystyk 441: }
 442: 
 443: PRIVATE int HTTPStatus_free ARGS1(HTStream *, me)
 444: {
1.87 ! frystyk 445:   int status = HT_OK;
 ! 446:   if (me->target) {
 ! 447:    if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
 ! 448:      return HT_WOULD_BLOCK;
 ! 449:   }
1.71 frystyk 450:   free(me);
1.80 frystyk 451:   return HT_OK;
1.71 frystyk 452: }
 453: 
 454: PRIVATE int HTTPStatus_abort ARGS2(HTStream *, me, HTError, e)
 455: {
 456:   if (me->target)
1.74 frystyk 457:    ABORT_TARGET;
1.71 frystyk 458:   free(me);
1.74 frystyk 459:   if (PROT_TRACE)
1.80 frystyk 460:    fprintf(TDEST, "HTTPStatus.. ABORTING...\n");
 461:   return HT_ERROR;
1.71 frystyk 462: }
 463: 
 464: /*   HTTPStatus Stream
 465: **   -----------------
 466: */
 467: PRIVATE CONST HTStreamClass HTTPStatusClass =
 468: {       
 469:   "HTTPStatus",
1.80 frystyk 470:   HTTPStatus_flush,
1.71 frystyk 471:   HTTPStatus_free,
 472:   HTTPStatus_abort,
 473:   HTTPStatus_put_character,
 474:   HTTPStatus_put_string,
 475:   HTTPStatus_put_block
 476: };
 477: 
 478: PUBLIC HTStream * HTTPStatus_new ARGS2(HTRequest *, request,
 479:                    http_info *, http)
 480: {
 481:   HTStream * me = (HTStream *) calloc(1, sizeof(HTStream));
 482:   if (!me) outofmem(__FILE__, "HTTPStatus_new");
 483:   me->isa = &HTTPStatusClass;
 484:   me->request = request;
 485:   me->http = http;
 486:   me->state = EOL_BEGIN;
 487:   return me;
 488: }
 489: 
 490: /* ------------------------------------------------------------------------- */
 491: 
 492: /*       Load Document from HTTP Server            HTLoadHTTP
1.55 frystyk 493: **       ==============================
 494: **
 495: **   Given a hypertext address, this routine loads a document.
 496: **
 497: ** On entry,
 498: **   request        This is the request structure
 499: ** On exit,
1.80 frystyk 500: **   returns     HT_ERROR    Error has occured or interrupted
1.71 frystyk 501: **           HT_WOULD_BLOCK if operation would have blocked
1.58 frystyk 502: **           HT_LOADED    if return status 200 OK
 503: **           HT_NO_DATA   if return status 204 No Response
1.81 frystyk 504: **           HT_RETRY    if return status 503 Service Unavail.
1.55 frystyk 505: */
 506: PUBLIC int HTLoadHTTP ARGS1 (HTRequest *, request)
 507: {
1.71 frystyk 508:   int status = HT_ERROR;
 509:   char *url;              /* Gets initialized on every entry */
1.55 frystyk 510:   http_info *http;            /* Specific protocol information */
 511: 
 512:   if (!request || !request->anchor) {
1.78 frystyk 513:     if (PROT_TRACE) fprintf(TDEST, "HTLoadHTTP.. Bad argument\n");
1.71 frystyk 514:     return HT_ERROR;
1.55 frystyk 515:   }
 516:   url = HTAnchor_physical(request->anchor);
1.17 timbl 517:   
1.71 frystyk 518:   /* Only do the setup first time through. This is actually state HTTP_BEGIN
 519:    but it can't be in the state machine as we need the structure first */
 520:   if (!request->net_info) {
1.22 luotonen 521:    /*
1.71 frystyk 522:    ** Initiate a new http structure and bind to request structure
 523:    ** This is actually state HTTP_BEGIN, but it can't be in the state
 524:    ** machine as we need the structure first.
1.22 luotonen 525:    */
1.80 frystyk 526:    if (PROT_TRACE) fprintf(TDEST, "HTTP........ Looking for `%s\'\n",url);
1.71 frystyk 527:    if ((http = (http_info *) calloc(1, sizeof(http_info))) == NULL)
 528:      outofmem(__FILE__, "HTLoadHTTP");
1.78 frystyk 529:    http->sockfd = INVSOC;           /* Invalid socket number */
1.71 frystyk 530:    http->request = request;
 531:    http->state = HTTP_BEGIN;
1.85 frystyk 532:    http->next = HTTP_ERROR;
1.71 frystyk 533:    request->net_info = (HTNetInfo *) http;
 534:    HTThread_new((HTNetInfo *) http);
1.80 frystyk 535:    request->input_stream = HTTPRequest_new(request,request->input_stream);
1.85 frystyk 536:   } else {
1.71 frystyk 537:    http = (http_info *) request->net_info;     /* Get existing copy */
1.85 frystyk 538:    if (http->sockfd != INVSOC && HTThreadIntr(http->sockfd))
 539:      http->state = HTTP_ERROR;
 540:   }
1.71 frystyk 541: 
 542:   /* Now jump into the machine. We know the state from the previous run */
 543:   while (1) {
 544:    switch (http->state) {
 545:     case HTTP_BEGIN:
 546:      /*
 547:       ** Compose authorization information (this was moved here
 548:       ** from after the making of the connection so that the connection
 549:       ** wouldn't have to wait while prompting username and password
 550:       ** from the user).             -- AL 13.10.93
 551:       */
 552:      HTAA_composeAuth(request);
 553:      if (PROT_TRACE) {
 554:        if (request->authorization)
1.78 frystyk 555:          fprintf(TDEST, "HTTP........ Sending Authorization: %s\n",
1.71 frystyk 556:              request->authorization);
 557:        else
1.78 frystyk 558:          fprintf(TDEST,
1.71 frystyk 559:              "HTTP........ Not sending authorization (yet)\n");
 560:      }
 561:      http->state = HTTP_NEED_CONNECTION;
 562:      break;
 563:      
 564:     case HTTP_NEED_CONNECTION:    /* Now let's set up a connection */
 565:      status = HTDoConnect((HTNetInfo *) http, url, TCP_PORT,
 566:                 NULL, NO);
1.80 frystyk 567:      if (status == HT_OK) {
1.71 frystyk 568:        if (PROT_TRACE)
1.78 frystyk 569:          fprintf(TDEST, "HTTP........ Connected, socket %d\n",
1.71 frystyk 570:              http->sockfd);
1.80 frystyk 571: 
 572:        /* Set up read buffer, streams and concurrent read/write */
1.71 frystyk 573:        http->isoc = HTInputSocket_new(http->sockfd);
1.80 frystyk 574:        request->input_stream->target=HTWriter_new(http->sockfd, YES);
 575:        http->target = HTImProxy ?
 576:          request->output_stream : HTTPStatus_new(request, http);
1.85 frystyk 577:        HTThreadState(http->sockfd, THD_SET_READ);
1.71 frystyk 578:        http->state = HTTP_NEED_REQUEST;
 579:      } else if (status == HT_WOULD_BLOCK)
 580:        return status;
 581:      else
 582:        http->state = HTTP_ERROR;       /* Error or interrupt */
 583:      break;
 584: 
1.87 ! frystyk 585:      /* As we can do simultanous read and write this is now one state */
1.80 frystyk 586:     case HTTP_NEED_REQUEST:
 587:      if (http->action == SOC_WRITE) {
 588: 
 589:        /* Find the right way to call back */
 590:        if (request->CopyRequest) {
 591:          if (!HTAnchor_headerParsed(request->CopyRequest->anchor))
 592:            return HT_WOULD_BLOCK;
 593:          status = request->PostCallBack(request->CopyRequest,
 594:                          request->input_stream);
 595:        } else if (request->PostCallBack) {
 596:          status = request->PostCallBack(request,
 597:                          request->input_stream);
 598:        } else {
 599:          status = (*request->input_stream->isa->flush)
 600:            (request->input_stream);
 601:        }
1.71 frystyk 602:        if (status == HT_WOULD_BLOCK)
1.80 frystyk 603:          return HT_WOULD_BLOCK;
 604:        else if (status == HT_INTERRUPTED)
 605:          http->state = HTTP_ERROR;
1.71 frystyk 606:        else
1.80 frystyk 607:          http->action = SOC_READ;
 608:      } else if (http->action == SOC_READ) {
 609:        status = HTSocketRead(request, http->target);
 610:        if (status == HT_WOULD_BLOCK)
 611:          return HT_WOULD_BLOCK;
 612:        else if (status == HT_INTERRUPTED)
1.71 frystyk 613:          http->state = HTTP_ERROR;
1.85 frystyk 614: #if 0
1.80 frystyk 615:        else if (status == HT_LOADED) {
1.81 frystyk 616:          http->state = http->next;     /* Jump to next state */
1.80 frystyk 617:        } else
 618:          http->state = HTTP_ERROR;
1.85 frystyk 619: #else
 620:        else {
 621:          if (PROT_TRACE)
1.87 ! frystyk 622:            fprintf(TDEST, "TESTTESTTEST Jumping to next state\n");
1.85 frystyk 623:          http->state = http->next;     /* Jump to next state */
 624:        }
 625: #endif
1.80 frystyk 626:      } else
1.78 frystyk 627:        http->state = HTTP_ERROR;
1.71 frystyk 628:      break;
1.80 frystyk 629:      
 630:     case HTTP_REDIRECTION:
1.71 frystyk 631:      if (request->redirect) {
 632:        if (status == 301) {
 633:          HTErrorAdd(request, ERR_INFO, NO, HTERR_MOVED,
 634:                (void *) request->redirect,
 635:                (int) strlen(request->redirect), "HTLoadHTTP");
 636:        } else if (status == 302) {
 637:          HTErrorAdd(request, ERR_INFO, NO, HTERR_FOUND,
 638:                (void *) request->redirect,
 639:                (int) strlen(request->redirect), "HTLoadHTTP");
1.55 frystyk 640:        }
1.71 frystyk 641:        if (++request->redirections < HTMaxRedirections) {
1.87 ! frystyk 642:          HTAnchor *anchor = HTAnchor_findAddress(request->redirect);
1.80 frystyk 643:          HTTPCleanup(request, NO);
1.87 ! frystyk 644:          if (request->CopyRequest) {
 ! 645:            HTThread_kill(request->CopyRequest->net_info);
 ! 646:            request->input_stream = NULL;
 ! 647:          }
 ! 648: 
 ! 649:          /* We have to figure out where we were called from */
 ! 650:          if (request->CopyRequest) {
 ! 651:            char *msg = malloc(strlen(request->redirect)+100);
 ! 652:            if (!msg) outofmem(__FILE__, "HTLoadHTTP");
 ! 653:            sprintf(msg, "Location for destination location has changed to %s, continue operation?", request->redirect);
 ! 654:            if (!HTConfirm(msg)) {
 ! 655:              http->state = HTTP_ERROR;
 ! 656:              free(msg);
 ! 657:              break;
 ! 658:            }
 ! 659:            free(msg);
 ! 660:            return HTCopyAnchor((HTAnchor *)
 ! 661:                      request->CopyRequest->anchor,
 ! 662:                      (HTParentAnchor *) anchor,
 ! 663:                      request);
 ! 664:          } else if (request->PostCallBack) {
 ! 665: #if 0
 ! 666:            /* Ask if OK */
 ! 667:            return HTUploadAnchor((HTAnchor *) anchor, request);
 ! 668: #endif
 ! 669:          } else {
 ! 670:            return HTLoadAnchor((HTAnchor *) anchor, request);
 ! 671:          }
1.71 frystyk 672:        } else {
 673:          HTErrorAdd(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
1.58 frystyk 674:                NULL, 0, "HTLoadHTTP");
1.71 frystyk 675:          http->state = HTTP_ERROR;
1.58 frystyk 676:        }
1.71 frystyk 677:      } else {
 678:        HTErrorAdd(request, ERR_FATAL, NO, HTERR_BAD_REPLY,
 679:              NULL, 0, "HTLoadHTTP");
 680:        http->state = HTTP_ERROR;
 681:      }
 682:      break;
1.80 frystyk 683:      
1.71 frystyk 684:     case HTTP_AA:
1.80 frystyk 685:      HTTPCleanup(request, NO);          /* Close connection */
1.85 frystyk 686:      /*
1.87 ! frystyk 687:      ** We must also kill the source request. If this request has 
 ! 688:      ** already got all the date this will result in that we close
 ! 689:      ** the connection but keeps the stream chain and return 
 ! 690:      ** HT_WOULD_BLOCK so that we can get the data using the
 ! 691:      ** call back function
1.85 frystyk 692:      */
 693:      if (request->CopyRequest) {
 694:        HTThread_kill(request->CopyRequest->net_info);
 695:        request->input_stream = NULL;
 696:      }
1.71 frystyk 697:      if (HTTPAuthentication(request) == YES &&
 698:        HTAA_retryWithAuth(request) == YES) {
1.85 frystyk 699: 
 700:        /* We have to figure out where we were called from */
 701:        if (request->CopyRequest) {
 702:          return HTCopyAnchor((HTAnchor *)
 703:                    request->CopyRequest->anchor,
 704:                    request->anchor,
 705:                    request);
 706:        } else if (request->PostCallBack) {
 707: #if 0
 708:          return HTUploadAnchor((HTAnchor *)request->anchor,request);
 709: #endif
 710:        } else {
 711:          return HTLoadAnchor((HTAnchor *) request->anchor, request);
 712:        }
1.71 frystyk 713:      } else {
 714:        char *unescaped = NULL;
 715:        StrAllocCopy(unescaped, url);
 716:        HTUnEscape(unescaped);
 717:        HTErrorAdd(request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
 718:              (void *) unescaped,
 719:              (int) strlen(unescaped), "HTLoadHTTP");
 720:        free(unescaped);
1.80 frystyk 721:        return HT_ERROR;
1.71 frystyk 722:      }
 723:      break;
1.80 frystyk 724:      
1.71 frystyk 725:     case HTTP_GOT_DATA:
1.80 frystyk 726:      HTTPCleanup(request, NO);
1.87 ! frystyk 727: 
 ! 728:      /* HT_OK means evreything is OK, but not finished! */
 ! 729:      return request->Source ? HT_OK : HT_LOADED;
1.71 frystyk 730:      break;
 731:      
 732:     case HTTP_NO_DATA:
1.80 frystyk 733:      HTTPCleanup(request, NO);
1.71 frystyk 734:      return HT_NO_DATA;
 735:      break;
1.80 frystyk 736:      
1.81 frystyk 737:     case HTTP_RETRY:
 738:      HTTPCleanup(request, YES);
 739:      return HT_RETRY;
 740:      break;
 741: 
1.71 frystyk 742:     case HTTP_ERROR:
1.80 frystyk 743:      HTTPCleanup(request, YES);
1.71 frystyk 744:      return HT_ERROR;
 745:      break;
 746:    }
 747:   } /* End of while(1) */
 748: }  
 749: 
 750: /* Protocol descriptor */
 751: 
 752: GLOBALDEF PUBLIC HTProtocol HTTP = {
 753:   "http", SOC_NON_BLOCK, HTLoadHTTP, NULL, NULL
 754: };
1.21 luotonen 755: 

Webmaster

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