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

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

1.1 timbl 1: /*   HyperText Tranfer Protocol   - Client implementation     HTTP.c
 2: **   ==========================
1.2 timbl 3: **
 4: ** Bugs:
 5: **   Not implemented:
 6: **       Forward
 7: **       Redirection
 8: **       Error handling
1.1 timbl 9: */
 10: 
 11: /*   Module parameters:
 12: **   -----------------
 13: **
 14: ** These may be undefined and redefined by syspec.h
 15: */
1.2 timbl 16: 
1.12 timbl 17: /*   MOSAIC_HACK2 is a kludge to guess the file type of trabsferred
 18: **   file from the URL. It is STRICTLY illegal to do this!
 19: */
 20: 
1.2 timbl 21: /* Implements:
 22: */
 23: #include "HTTP.h"
 24: 
 25: #define HTTP_VERSION  "HTTP/1.0"
 26: #define HTTP2             /* Version is greater than 0.9 */
 27: 
 28: #define INIT_LINE_SIZE     1024  /* Start with line buffer this big */
 29: #define LINE_EXTEND_THRESH   256   /* Minimum read size */
 30: #define VERSION_LENGTH         20   /* for returned protocol version */
 31: 
 32: /* Uses:
 33: */
1.1 timbl 34: #include "HTParse.h"
 35: #include "HTUtils.h"
 36: #include "tcp.h"
 37: #include "HTTCP.h"
 38: #include "HTFormat.h"
1.2 timbl 39: #include <ctype.h>
 40: #include "HTAlert.h"
 41: #include "HTMIME.h"
1.5 timbl 42: #include "HTML.h"       /* SCW */
 43: #include "HTInit.h"      /* SCW */
1.21 luotonen 44: #include "HTAccess.h"     /* HTRequest */
1.14 luotonen 45: #include "HTAABrow.h"     /* Access Authorization */
1.20 timbl 46: #include "HTTee.h"       /* Tee off a cache stream */
 47: #include "HTFWriter.h"     /* Write to cache file */
1.1 timbl 48: 
1.2 timbl 49: struct _HTStream {
 50:    HTStreamClass * isa;      /* all we need to know */
 51: };
 52: 
 53: 
1.6 timbl 54: extern char * HTAppName;    /* Application name: please supply */
 55: extern char * HTAppVersion;  /* Application version: please supply */
 56: 
1.19 timbl 57: PUBLIC BOOL HTCacheHTTP = YES; /* Enable caching of HTTP-retrieved files */
 58: 
1.21 luotonen 59: 
1.23 ! luotonen 60: PRIVATE char * HTCacheRoot = NULL;
 ! 61: PRIVATE char * gateway_cache_filename ARGS1(CONST char *, name)
 ! 62: {
 ! 63:   char * access;
 ! 64:   char * host;
 ! 65:   char * path;
 ! 66:   char * cache_filename;
 ! 67: 
 ! 68:   if (HTCacheRoot && name && !strchr(name, '?') &&
 ! 69:    (access = HTParse(name, "", PARSE_ACCESS))) {
 ! 70: 
 ! 71:    if (!strcmp(access, "file")) {
 ! 72:      free(access);
 ! 73:      return NULL;
 ! 74:    }
 ! 75: 
 ! 76:    host  = HTParse(name, "", PARSE_HOST);
 ! 77:    path  = HTParse(name, "", PARSE_PATH | PARSE_PUNCTUATION);
 ! 78:    cache_filename = (char*)malloc(strlen(HTCacheRoot) + strlen(access) +
 ! 79:                    (host ? strlen(host) : 0) +
 ! 80:                    (path ? strlen(path) : 0) + 3);
 ! 81:    if (!cache_filename) outofmem(__FILE__, "cache_filename");
 ! 82:    sprintf(cache_filename, "%s/%s/%s%s", HTCacheRoot, access, host, path);
 ! 83: 
 ! 84:    FREE(access); FREE(host); FREE(path);
 ! 85:    return cache_filename;
 ! 86:   }
 ! 87:   return NULL;
 ! 88: }
 ! 89: 
 ! 90: 
 ! 91: PRIVATE FILE * gateway_cache_lookup ARGS1(CONST char *, name)
 ! 92: {
 ! 93:   char * cache_filename = gateway_cache_filename(name);
 ! 94: 
 ! 95:   if (cache_filename) {
 ! 96:    FILE * cache_file = fopen(cache_filename, "r");
 ! 97:    if (cache_file && TRACE)
 ! 98:      fprintf(stderr, "Gateway cache: HIT \"%s\"\n", cache_filename);
 ! 99:    free(cache_filename);
 ! 100:    return cache_file;
 ! 101:   }
 ! 102:   return NULL;
 ! 103: }
 ! 104: 
 ! 105: 
 ! 106: PRIVATE FILE * gateway_cache_open ARGS1(CONST char *, name)
 ! 107: {
 ! 108:   char * cache_filename = gateway_cache_filename(name);
 ! 109: 
 ! 110:   if (HTCacheRoot && cache_filename) {
 ! 111:    char * cur = cache_filename + strlen(HTCacheRoot) + 1;
 ! 112:    FILE * cache_file;
 ! 113:    struct stat stat_info;
 ! 114: 
 ! 115:    while ((cur = strchr(cur, '/'))) {
 ! 116:      *cur = 0;
 ! 117:      if (-1 == stat(cache_filename, &stat_info)) {
 ! 118:        CTRACE(stderr, "Gateway cache: creating cache dir \"%s\"\n",
 ! 119:            cache_filename);
 ! 120:        if (-1 == mkdir(cache_filename, 0775)) {
 ! 121:          CTRACE(stderr, "Gateway cache: can't create dir \"%s\"\n",
 ! 122:              cache_filename);
 ! 123:          free(cache_filename);
 ! 124:          return NULL;
 ! 125:        }
 ! 126:        CTRACE(stderr, "Gateway cache: created directory \"%s\"\n",
 ! 127:            cache_filename);
 ! 128:      }
 ! 129:      else CTRACE(stderr, "Gateway cache: dir \"%s\" already exists\n",
 ! 130:            cache_filename);
 ! 131:      *cur = '/';
 ! 132:      cur++;
 ! 133:    }
 ! 134:    cache_file = fopen(cache_filename, "w");
 ! 135:    if (!cache_file) {
 ! 136:      CTRACE(stderr, "Gateway cache: can't create cache file \"%s\"\n",
 ! 137:          cache_filename);
 ! 138:    }
 ! 139:    free(cache_filename);
 ! 140:    return cache_file;
 ! 141:   }
 ! 142:   return NULL;
 ! 143: }
 ! 144: 
 ! 145: 
 ! 146: 
 ! 147: 
1.21 luotonen 148: PRIVATE void parse_401_headers ARGS2(HTRequest *,   req,
 149:                   HTInputSocket *,  isoc)
 150: {
 151:   HTAAScheme scheme;
 152:   char *line;
 153:   int num_schemes = 0;
 154:   HTList *valid_schemes = HTList_new();
 155:   HTAssocList **scheme_specifics = NULL;
 156:   char *template = NULL;
 157: 
 158:   /* Read server reply header lines */
 159: 
 160:   if (TRACE)
 161:    fprintf(stderr, "Server 401 reply header lines:\n");
 162: 
 163:   while (NULL != (line = HTInputSocket_getUnfoldedLine(isoc)) &&
 164:      *line != 0) {
 165: 
 166:    if (TRACE) fprintf(stderr, "%s\n", line);
 167: 
 168:    if (strchr(line, ':')) {    /* Valid header line */
 169: 
 170:      char *p = line;
 171:      char *fieldname = HTNextField(&p);
 172:      char *arg1 = HTNextField(&p);
 173:      char *args = p;
 174:      
 175:      if (0==strcasecomp(fieldname, "WWW-Authenticate:")) {
 176:        if (HTAA_UNKNOWN != (scheme = HTAAScheme_enum(arg1))) {
 177:          HTList_addObject(valid_schemes, (void*)scheme);
 178:          if (!scheme_specifics) {
 179:            int i;
 180:            scheme_specifics = (HTAssocList**)
 181:              malloc(HTAA_MAX_SCHEMES * sizeof(HTAssocList*));
 182:            if (!scheme_specifics)
 183:              outofmem(__FILE__, "parse_401_headers");
 184:            for (i=0; i < HTAA_MAX_SCHEMES; i++)
 185:              scheme_specifics[i] = NULL;
 186:          }
 187:          scheme_specifics[scheme] = HTAA_parseArgList(args);
 188:          num_schemes++;
 189:        }
 190:        else if (TRACE) {
 191:          fprintf(stderr, "Unknown scheme `%s' %s\n",
 192:              (arg1 ? arg1 : "(null)"),
 193:              "in WWW-Authenticate: field");
 194:        }
 195:      }
 196: 
 197:      else if (0==strcasecomp(fieldname, "WWW-Protection-Template:")) {
 198:        if (TRACE)
 199:          fprintf(stderr, "Protection template set to `%s'\n", arg1);
 200:        StrAllocCopy(template, arg1);
 201:      }
 202: 
 203:    } /* if a valid header line */
 204:    else if (TRACE) {
 205:      fprintf(stderr, "Invalid header line `%s' ignored\n", line);
 206:    } /* else invalid header line */
 207:   } /* while header lines remain */
 208: 
 209:   req->valid_schemes = valid_schemes;
 210:   req->scheme_specifics = scheme_specifics;
 211:   req->prot_template = template;
 212: }
 213: 
 214: 
 215: 
1.1 timbl 216: /*       Load Document from HTTP Server         HTLoadHTTP()
 217: **       ==============================
 218: **
 219: **   Given a hypertext address, this routine loads a document.
 220: **
 221: **
 222: ** On entry,
 223: **   arg   is the hypertext reference of the article to be loaded.
 224: **
 225: ** On exit,
 226: **   returns >=0   If no error, a good socket number
 227: **       <0   Error.
 228: **
 229: **   The socket must be closed by the caller after the document has been
 230: **   read.
 231: **
 232: */
1.19 timbl 233: PUBLIC int HTLoadHTTP ARGS1 (HTRequest *, request)
1.1 timbl 234: {
1.22 luotonen 235:   CONST char * arg = NULL;
1.1 timbl 236:   int s;               /* Socket number for returned data */
 237:   int status;                /* tcp return */
1.10 timbl 238:   char crlf[3];           /* A CR LF equivalent string */
1.3 timbl 239:   HTStream * target = NULL;     /* Unconverted data */
 240:   
1.2 timbl 241:   CONST char* gate = 0;       /* disable this feature */
1.1 timbl 242:   SockA soc_address;         /* Binary network address */
 243:   SockA * sin = &soc_address;
1.2 timbl 244:   BOOL extensions = YES;       /* Assume good HTTP server */
1.17 timbl 245: 
1.22 luotonen 246:   if (request->reason == HTAA_OK_GATEWAY) {
 247:    arg = request->translated;
1.23 ! luotonen 248: 
 ! 249:    /*
 ! 250:    ** Cache lookup
 ! 251:    */
 ! 252:    if (HTCacheRoot && request->method == METHOD_GET &&
 ! 253:      !request->authorization && !request->arg_keywords) {
 ! 254: 
 ! 255:      FILE * cache_file = gateway_cache_lookup(arg);
 ! 256: 
 ! 257:      if (cache_file) {
 ! 258:        HTFileCopy(cache_file, request->output_stream);
 ! 259:        return HT_LOADED;
 ! 260:      }
 ! 261: 
 ! 262:    } /* Cache lookup */
 ! 263: 
1.22 luotonen 264:   }
 265:   else {
 266:    arg = HTAnchor_physical(request->anchor);
 267:    StrAllocCopy(request->argument, arg);
 268:   }
 269: 
1.1 timbl 270:   if (!arg) return -3;        /* Bad if no name sepcified   */
 271:   if (!*arg) return -2;       /* Bad if name had zero length */
 272: 
 273: /* Set up defaults:
 274: */
 275: #ifdef DECNET
1.2 timbl 276:   sin->sdn_family = AF_DECnet;      /* Family = DECnet, host order */
 277:   sin->sdn_objnum = DNP_OBJ;     /* Default: http object number */
1.1 timbl 278: #else /* Internet */
1.2 timbl 279:   sin->sin_family = AF_INET;   /* Family = internet, host order */
 280:   sin->sin_port = htons(TCP_PORT);  /* Default: http port  */
1.1 timbl 281: #endif
 282: 
1.10 timbl 283:   sprintf(crlf, "%c%c", CR, LF);   /* To be corect on Mac, VM, etc */
 284:   
1.1 timbl 285:   if (TRACE) {
 286:     if (gate) fprintf(stderr,
 287:        "HTTPAccess: Using gateway %s for %s\n", gate, arg);
 288:     else fprintf(stderr, "HTTPAccess: Direct access for %s\n", arg);
 289:   }
 290:   
 291: /* Get node name and optional port number:
 292: */
 293:   {
 294:    char *p1 = HTParse(gate ? gate : arg, "", PARSE_HOST);
 295:    int status = HTParseInet(sin, p1); /* TBL 920622 */
 296:     free(p1);
 297:    if (status) return status;  /* No such host for example */
 298:   }
 299:   
1.15 luotonen 300: /*
 301: ** Compose authorization information (this was moved here
 302: ** from after the making of the connection so that the connection
 303: ** wouldn't have to wait while prompting username and password
 304: ** from the user).               -- AL 13.10.93
 305: */
 306: #ifdef ACCESS_AUTH
1.21 luotonen 307:   HTAA_composeAuth(request);
 308:   if (TRACE) {
 309:    if (request->authorization)
 310:      fprintf(stderr, "HTTP: Sending Authorization: %s\n",
 311:          request->authorization);
 312:    else
 313:      fprintf(stderr, "HTTP: Not sending authorization (yet)\n");
1.15 luotonen 314:   }
 315: #endif /* ACCESS_AUTH */
1.1 timbl 316:  
1.10 timbl 317: /*   Now, let's get a socket set up from the server for the data:
1.1 timbl 318: */   
 319: #ifdef DECNET
 320:   s = socket(AF_DECnet, SOCK_STREAM, 0);
 321: #else
 322:   s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 323: #endif
 324:   status = connect(s, (struct sockaddr*)&soc_address, sizeof(soc_address));
 325:   if (status < 0) {
 326:      if (TRACE) fprintf(stderr, 
 327:       "HTTP: Unable to connect to remote host for `%s' (errno = %d).\n", arg, errno);
1.17 timbl 328: 
1.1 timbl 329:      return HTInetStatus("connect");
 330:    }
 331:   
 332:   if (TRACE) fprintf(stderr, "HTTP connected, socket %d\n", s);
 333: 
1.17 timbl 334: 
 335: /*   Compose and send command
 336: **   ------------------------
 337: */
 338:   {
 339:     char *command;         /* The whole command */
 340:    
1.1 timbl 341: /*   Ask that node for the document,
 342: **   omitting the host name & anchor if not gatewayed.
 343: */    
1.17 timbl 344:    if (gate) {
 345:      command = malloc(4 + strlen(arg)+ 2 + 31);
 346:      if (command == NULL) outofmem(__FILE__, "HTLoadHTTP");
 347:      strcpy(command, "GET ");
 348:      strcat(command, arg);
 349:    } else { /* not gatewayed */
 350:      char * p1 = HTParse(arg, "", PARSE_PATH|PARSE_PUNCTUATION);
 351:      command = malloc(4 + strlen(p1)+ 2 + 31);
 352:      if (command == NULL) outofmem(__FILE__, "HTLoadHTTP");
1.23 ! luotonen 353:      if (request->method != METHOD_INVALID) {
 ! 354:        strcpy(command, HTMethod_name(request->method));
1.22 luotonen 355:        strcat(command, " ");
 356:      }
 357:      else {
 358:        strcpy(command, "GET ");
 359:      }
1.17 timbl 360:      strcat(command, p1);
 361:      free(p1);
 362:    }
1.2 timbl 363: #ifdef HTTP2
1.17 timbl 364:    if (extensions) {
 365:      strcat(command, " ");
 366:      strcat(command, HTTP_VERSION);
 367:    }
1.2 timbl 368: #endif
1.17 timbl 369:   
 370:    strcat(command, crlf); /* CR LF, as in rfc 977 */
 371:   
 372:    if (extensions) {
1.21 luotonen 373: 
1.17 timbl 374:      int i;
 375:      HTAtom * present = WWW_PRESENT;
 376:      char line[256];  /*@@@@ */
1.21 luotonen 377:      HTList *conversions[2];
 378: 
1.22 luotonen 379:      if (!HTConversions) {
 380:        HTConversions = HTList_new();
 381:        HTFormatInit(HTConversions);
 382:      }
1.21 luotonen 383:      conversions[0] = HTConversions;
 384:      conversions[1] = request->conversions;
 385: 
 386:      for (i=0; i<2; i++) {
 387:        HTList *cur = conversions[i];
 388:        HTPresentation *pres;
 389: 
 390:        while ((pres = (HTPresentation*)HTList_nextObject(cur))) {
 391:          if (pres->rep_out == present) {
 392:            if (pres->quality != 1.0) {
 393:              sprintf(line, "Accept: %s q=%.3f%c%c",
 394:                  HTAtom_name(pres->rep),
 395:                  pres->quality, CR, LF);
 396:            } else {
 397:              sprintf(line, "Accept: %s%c%c",
 398:                  HTAtom_name(pres->rep), CR, LF);
 399:            }
 400:            StrAllocCat(command, line);
1.17 timbl 401:          }
 402:        }
1.2 timbl 403:      }
1.22 luotonen 404: 
 405:      sprintf(line, "User-Agent: %s%s %s/%s libwww/%s%c%c",
 406:          request->user_agent ? request->user_agent : "",
 407:          request->user_agent ? " VIA Gateway" : "",
1.17 timbl 408:          HTAppName ? HTAppName : "unknown",
 409:          HTAppVersion ? HTAppVersion : "0.0",
 410:          HTLibraryVersion, CR, LF);
 411:          StrAllocCat(command, line);
 412:   
1.22 luotonen 413:      if (request->from) {
 414:        sprintf(line, "From: %s%c%c", request->from, CR, LF);
 415:        StrAllocCat(command, line);
 416:      }
 417: 
1.14 luotonen 418: #ifdef ACCESS_AUTH
1.21 luotonen 419:      if (request->authorization != NULL) {
 420:        sprintf(line, "Authorization: %s%c%c",
 421:            request->authorization, CR, LF);
1.17 timbl 422:        StrAllocCat(command, line);
 423:      }
 424: #endif /* ACCESS_AUTH */
1.22 luotonen 425: 
 426:      if (request->content_type) {
 427:        sprintf(line, "Content-Type: %s%c%c",
 428:            HTAtom_name(request->content_type), CR, LF);
 429:        StrAllocCat(command, line);
 430:      }
 431: 
 432:      if (request->content_length > 0) {
 433:        sprintf(line, "Content-Length: %d%c%c",
 434:            request->content_length, CR, LF);
 435:        StrAllocCat(command, line);
 436:      }
 437: 
 438: 
1.14 luotonen 439:    }
1.17 timbl 440:   
 441:    StrAllocCat(command, crlf);   /* Blank line means "end" */
 442:   
 443:    if (TRACE) fprintf(stderr, "HTTP Tx: %s\n", command);
 444:   
 445:   /* Translate into ASCII if necessary
 446:   */
1.4 timbl 447: #ifdef NOT_ASCII
1.17 timbl 448:    {
 449:      char * p;
 450:      for(p = command; *p; p++) {
 451:        *p = TOASCII(*p);
 452:      }
1.1 timbl 453:    }
1.3 timbl 454: #endif
1.17 timbl 455:   
 456:    status = NETWRITE(s, command, (int)strlen(command));
 457:    free(command);
 458:    if (status<0) {
 459:      if (TRACE) fprintf(stderr,
 460:        "HTTPAccess: Unable to send command.\n");
1.1 timbl 461:      return HTInetStatus("send");
1.17 timbl 462:    }
 463:   } /* compose and send command */
 464:   
1.2 timbl 465: 
1.17 timbl 466: /*   Read the response
 467: **   -----------------
1.11 timbl 468: **
 469: **   HTTP0 servers must return ASCII style text, though it can in
 470: **   principle be just text without any markup at all.
 471: **   Full HTTP servers must return a response
 472: **   line and RFC822 style header. The response must therefore in
 473: **   either case have a CRLF somewhere soon.
 474: **
 475: **   This is the theory. In practice, there are (1993) unfortunately
 476: **   many binary documents just served up with HTTP0.9. This
 477: **   means we have to preserve the binary buffer (on the assumption that
 478: **   conversion from ASCII may lose information) in case it turns
 479: **   out that we want the binary original.
1.2 timbl 480: */
1.3 timbl 481: 
1.22 luotonen 482:   if (request->reason == HTAA_OK_GATEWAY) {
 483:    /*
 484:    ** Server as a gateway -- send body of the message
 485:    ** received from client (if any).
 486:    */
 487:    if (request->isoc && request->content_length > 0) {
 488:      int remain = request->content_length;
 489:      int i = remain;
 490:      char * buf;
 491: 
 492:      while (remain > 0 &&
 493:          (buf = HTInputSocket_getBlock(request->isoc, &i))) {
 494:        int status = NETWRITE(s, buf, i);
 495:        if (status < 0) {
 496:          CTRACE(stderr, "HTTPAccess: Unable to forward body\n");
 497:          return HTInetStatus("send");
 498:        }
 499:        remain -= i;
 500:        i = remain;
 501:      }
 502:    }
1.23 ! luotonen 503: 
 ! 504:    /*
 ! 505:    ** Cache the document if it a GET request,
 ! 506:    ** not protected and not a search request.
 ! 507:    */
 ! 508:    if (HTCacheRoot && request->method == METHOD_GET &&
 ! 509:      !request->authorization && !request->arg_keywords) {
 ! 510: 
 ! 511:      FILE * cache_file = gateway_cache_open(request->translated);
 ! 512: 
 ! 513:      if (cache_file) {
 ! 514:        request->output_stream = HTTee(request->output_stream,
 ! 515:                        HTFWriter_new(cache_file));
 ! 516:        CTRACE(stderr, "Gateway cache: writing to cache file\n");
 ! 517:      }
 ! 518:      else {
 ! 519:        CTRACE(stderr, "Gateway cache: couldn't create cache file\n");
 ! 520:      }
 ! 521:    }
 ! 522:    else CTRACE(stderr, "Gateway cache: not caching\n");
 ! 523: 
1.22 luotonen 524:    /*
 525:    ** Load results directly to client
 526:    */
 527:    HTCopy(s, request->output_stream);
 528:    return HT_LOADED;
 529:   }
 530:   else {   /* read response */
1.21 luotonen 531: 
1.17 timbl 532:    HTFormat format_in;       /* Format arriving in the message */
1.21 luotonen 533:    HTInputSocket *isoc = HTInputSocket_new(s);
 534:    char * status_line = HTInputSocket_getStatusLine(isoc);
1.2 timbl 535: 
1.11 timbl 536: /* Kludge to trap binary responses from illegal HTTP0.9 servers.
 537: ** First time we have enough, look at the stub in ASCII
 538: ** and get out of here if it doesn't look right.
 539: **
 540: ** We also check for characters above 128 in the first few bytes, and
 541: ** if we find them we forget the html default.
 542: **
 543: ** Bugs: A HTTP0.9 server returning a document starting "HTTP/"
 544: **   will be taken as a HTTP 1.0 server. Failure.
 545: **   An HTTP 0.9 server returning a binary document with
 546: **   characters < 128 will be read as ASCII.
 547: */
1.21 luotonen 548:    if (!status_line) {   /* HTTP0 response */
 549:      if (HTInputSocket_seemsBinary(isoc)) {
 550:        format_in = HTAtom_for("www/unknown");
 551:      }
 552:      else {
 553:        format_in = WWW_HTML;
 554:      }
 555:      goto copy;
 556:    } /* end kludge */
 557: 
 558:    if (status_line) {   /* Decode full HTTP response */
 559:      /*
 560:      ** We now have a terminated server status line, and we have
 561:      ** checked that it is most probably a legal one. Parse it.
 562:      */
 563:      char server_version[VERSION_LENGTH+1];
 564:      int server_status;
 565: 
 566:      if (TRACE)
 567:        fprintf(stderr, "HTTP Status Line: Rx: %.70s\n", status_line);
1.17 timbl 568:   
1.21 luotonen 569:      sscanf(status_line, "%20s%d", server_version, &server_status);
1.2 timbl 570: 
1.21 luotonen 571:      format_in = HTAtom_for("www/mime");
1.7 timbl 572:   
1.21 luotonen 573:      switch (server_status / 100) {
1.2 timbl 574: 
1.21 luotonen 575:       default:     /* bad number */
 576:        HTAlert("Unknown status reply from server!");
 577:        break;
1.17 timbl 578:          
1.21 luotonen 579:       case 3:      /* Various forms of redirection */
 580:        HTAlert(
1.17 timbl 581:      "Redirection response from server is not handled by this client");
1.21 luotonen 582:        break;
1.17 timbl 583:          
1.21 luotonen 584:       case 4:      /* Access Authorization problem */
1.14 luotonen 585: #ifdef ACCESS_AUTH
1.21 luotonen 586:        switch (server_status) {
 587:         case 401:
 588:          parse_401_headers(request, isoc);
 589: 
 590:          if (TRACE) fprintf(stderr, "%s %d %s\n",
 591:                    "HTTP: close socket", s,
 592:                    "to retry with Access Authorization");
 593:          HTInputSocket_free(isoc);
 594:          (void)NETCLOSE(s);
 595:          if (HTAA_retryWithAuth(request, &HTLoadHTTP)) {
 596:            status = HT_LOADED;/* @@ THIS ONLY WORKS ON LINEMODE */
 597:            goto clean_up;
 598:          }
 599:          /* else falltrough */
 600:         default:
1.14 luotonen 601:          {
1.21 luotonen 602:            char *p1 = HTParse(gate ? gate : arg, "",
 603:                      PARSE_HOST);
 604:            char * message;
 605: 
 606:            if (!(message = (char*)malloc(strlen(status_line) +
 607:                           strlen(p1) + 100)))
 608:              outofmem(__FILE__, "HTTP 4xx status");
1.14 luotonen 609:            sprintf(message,
1.21 luotonen 610:                "HTTP server at %s replies:\n%s\n\n%s\n",
 611:                p1, status_line,
 612:                ((server_status == 401) 
 613:                 ? "Access Authorization package giving up.\n"
 614:                 : ""));
1.22 luotonen 615:            status = HTLoadError(request, server_status, message);
1.14 luotonen 616:            free(message);
 617:            free(p1);
 618:            goto clean_up;
 619:          }
1.21 luotonen 620:        } /* switch */
 621:        goto clean_up;
 622:        break;
 623: #else
 624:        /* case 4 without Access Authorization falls through */
 625:        /* to case 5 (previously "I think I goofed"). -- AL */
 626: #endif /* ACCESS_AUTH */
 627: 
 628:       case 5:      /* I think you goofed */
 629:        {
 630:          char *p1 = HTParse(gate ? gate : arg, "", PARSE_HOST);
 631:          char * message = (char*)malloc(strlen(status_line) + 
 632:                          strlen(p1) + 100);
 633:          if (!message) outofmem(__FILE__, "HTTP 5xx status");
 634:          sprintf(message,
 635:              "HTTP server at %s replies:\n%s", p1, status_line);
1.22 luotonen 636:          status = HTLoadError(request, server_status, message);
1.21 luotonen 637:          free(message);
 638:          free(p1);
 639:          goto clean_up;
 640:        }
 641:        break;
1.17 timbl 642:          
1.21 luotonen 643:       case 2:      /* Good: Got MIME object */
 644:        break;
1.17 timbl 645:   
1.21 luotonen 646:      } /* switch on response code */
1.17 timbl 647:      
1.21 luotonen 648:    } /* Full HTTP reply */
1.17 timbl 649:      
 650:   
1.3 timbl 651: /*   Set up the stream stack to handle the body of the message
 652: */
1.21 luotonen 653: 
1.13 duns 654: copy:
1.21 luotonen 655: 
1.18 timbl 656:    target = HTStreamStack(format_in, request);
1.21 luotonen 657: 
1.17 timbl 658:    if (!target) {
 659:      char buffer[1024]; /* @@@@@@@@ */
 660:      sprintf(buffer, "Sorry, no known way of converting %s to %s.",
 661:          HTAtom_name(format_in), HTAtom_name(request->output_format));
 662:      fprintf(stderr, "HTTP: %s", buffer);
1.22 luotonen 663:      status = HTLoadError(request, 501, buffer);
1.17 timbl 664:      goto clean_up;
 665:    }
 666:   
1.19 timbl 667:     /* @@ Bug: The decision of whether or not to cache should also be
1.21 luotonen 668:    ** made contingent on a IP address match or non match.
 669:    */
1.19 timbl 670:     if (HTCacheHTTP) {
 671:      target = HTTee(target, HTCacheWriter(request, NULL, format_in,
1.21 luotonen 672:                         request->output_format,
 673:                         request->output_stream));
1.19 timbl 674:    }
 675:    
1.11 timbl 676: /*   Push the data down the stream
1.3 timbl 677: **   We have to remember the end of the first buffer we just read
1.2 timbl 678: */
1.17 timbl 679:    if (format_in == WWW_HTML) {
 680:      target = HTNetToText(target);    /* Pipe through CR stripper */
 681:    }
1.21 luotonen 682: 
1.17 timbl 683:    (*target->isa->put_block)(target,
1.21 luotonen 684:                 isoc->input_pointer,
 685:                 isoc->input_limit - isoc->input_pointer);
 686:    HTInputSocket_free(isoc);
1.17 timbl 687:    HTCopy(s, target);
 688:      
 689:    (*target->isa->free)(target);
 690:    status = HT_LOADED;
1.11 timbl 691:   
1.2 timbl 692: /*   Clean up
1.1 timbl 693: */
1.17 timbl 694:    
 695: clean_up: 
 696:    if (TRACE) fprintf(stderr, "HTTP: close socket %d.\n", s);
 697:    (void) NETCLOSE(s);
 698:   
 699:    return status;         /* Good return */
1.3 timbl 700:   
1.17 timbl 701:   } /* read response */
 702: } /* load HTTP */
1.1 timbl 703: 
 704: /*   Protocol descriptor
 705: */
 706: 
1.17 timbl 707: GLOBALDEF PUBLIC HTProtocol HTTP = { "http", HTLoadHTTP, 0, 0 };
1.21 luotonen 708: 

Webmaster

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