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