Annotation of libwww/Library/src/HTTP.c, revision 1.38
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.6 timbl 49: extern char * HTAppName; /* Application name: please supply */
50: extern char * HTAppVersion; /* Application version: please supply */
51:
1.19 timbl 52: PUBLIC BOOL HTCacheHTTP = YES; /* Enable caching of HTTP-retrieved files */
1.37 luotonen 53: PUBLIC long HTProxyBytes = 0; /* Number of bytes transferred thru proxy */
54: extern BOOL using_proxy; /* are we using a proxy gateway? */
55: PUBLIC char * HTProxyHeaders = NULL; /* Headers to pass as-is */
1.23 luotonen 56:
1.21 luotonen 57: PRIVATE void parse_401_headers ARGS2(HTRequest *, req,
58: HTInputSocket *, isoc)
59: {
60: HTAAScheme scheme;
61: char *line;
62: int num_schemes = 0;
63: HTList *valid_schemes = HTList_new();
64: HTAssocList **scheme_specifics = NULL;
65: char *template = NULL;
66:
67: /* Read server reply header lines */
68:
69: if (TRACE)
70: fprintf(stderr, "Server 401 reply header lines:\n");
71:
72: while (NULL != (line = HTInputSocket_getUnfoldedLine(isoc)) &&
73: *line != 0) {
74:
75: if (TRACE) fprintf(stderr, "%s\n", line);
76:
77: if (strchr(line, ':')) { /* Valid header line */
78:
79: char *p = line;
80: char *fieldname = HTNextField(&p);
81: char *arg1 = HTNextField(&p);
82: char *args = p;
83:
84: if (0==strcasecomp(fieldname, "WWW-Authenticate:")) {
85: if (HTAA_UNKNOWN != (scheme = HTAAScheme_enum(arg1))) {
86: HTList_addObject(valid_schemes, (void*)scheme);
87: if (!scheme_specifics) {
88: int i;
89: scheme_specifics = (HTAssocList**)
90: malloc(HTAA_MAX_SCHEMES * sizeof(HTAssocList*));
91: if (!scheme_specifics)
92: outofmem(__FILE__, "parse_401_headers");
93: for (i=0; i < HTAA_MAX_SCHEMES; i++)
94: scheme_specifics[i] = NULL;
95: }
96: scheme_specifics[scheme] = HTAA_parseArgList(args);
97: num_schemes++;
98: }
99: else if (TRACE) {
100: fprintf(stderr, "Unknown scheme `%s' %s\n",
101: (arg1 ? arg1 : "(null)"),
102: "in WWW-Authenticate: field");
103: }
104: }
105:
106: else if (0==strcasecomp(fieldname, "WWW-Protection-Template:")) {
107: if (TRACE)
108: fprintf(stderr, "Protection template set to `%s'\n", arg1);
109: StrAllocCopy(template, arg1);
110: }
111:
112: } /* if a valid header line */
113: else if (TRACE) {
114: fprintf(stderr, "Invalid header line `%s' ignored\n", line);
115: } /* else invalid header line */
116: } /* while header lines remain */
117:
118: req->valid_schemes = valid_schemes;
119: req->scheme_specifics = scheme_specifics;
120: req->prot_template = template;
121: }
122:
123:
124:
1.1 timbl 125: /* Load Document from HTTP Server HTLoadHTTP()
126: ** ==============================
127: **
128: ** Given a hypertext address, this routine loads a document.
129: **
130: **
131: ** On entry,
132: ** arg is the hypertext reference of the article to be loaded.
133: **
134: ** On exit,
135: ** returns >=0 If no error, a good socket number
136: ** <0 Error.
137: **
138: ** The socket must be closed by the caller after the document has been
139: ** read.
140: **
141: */
1.19 timbl 142: PUBLIC int HTLoadHTTP ARGS1 (HTRequest *, request)
1.1 timbl 143: {
1.22 luotonen 144: CONST char * arg = NULL;
1.1 timbl 145: int s; /* Socket number for returned data */
146: int status; /* tcp return */
1.10 timbl 147: char crlf[3]; /* A CR LF equivalent string */
1.3 timbl 148: HTStream * target = NULL; /* Unconverted data */
149:
1.2 timbl 150: CONST char* gate = 0; /* disable this feature */
1.1 timbl 151: SockA soc_address; /* Binary network address */
152: SockA * sin = &soc_address;
1.38 ! frystyk 153: BOOL extensions = YES; /* Assume good HTTP server
! 154: char * cache_file_name = NULL;
! 155: BOOL HTCacheHTTP_old = HTCacheHTTP; /* Save initial cache conditions */
1.36 frystyk 156:
1.37 luotonen 157: if (HTImProxy) HTProxyBytes = 0;
1.23 luotonen 158:
1.37 luotonen 159: arg = HTAnchor_physical(request->anchor);
1.22 luotonen 160:
1.1 timbl 161: if (!arg) return -3; /* Bad if no name sepcified */
162: if (!*arg) return -2; /* Bad if name had zero length */
163:
164: /* Set up defaults:
165: */
166: #ifdef DECNET
1.2 timbl 167: sin->sdn_family = AF_DECnet; /* Family = DECnet, host order */
168: sin->sdn_objnum = DNP_OBJ; /* Default: http object number */
1.1 timbl 169: #else /* Internet */
1.2 timbl 170: sin->sin_family = AF_INET; /* Family = internet, host order */
171: sin->sin_port = htons(TCP_PORT); /* Default: http port */
1.1 timbl 172: #endif
173:
1.10 timbl 174: sprintf(crlf, "%c%c", CR, LF); /* To be corect on Mac, VM, etc */
175:
1.1 timbl 176: if (TRACE) {
177: if (gate) fprintf(stderr,
178: "HTTPAccess: Using gateway %s for %s\n", gate, arg);
179: else fprintf(stderr, "HTTPAccess: Direct access for %s\n", arg);
180: }
181:
182: /* Get node name and optional port number:
183: */
184: {
185: char *p1 = HTParse(gate ? gate : arg, "", PARSE_HOST);
186: int status = HTParseInet(sin, p1); /* TBL 920622 */
187: free(p1);
188: if (status) return status; /* No such host for example */
189: }
190:
1.15 luotonen 191: /*
192: ** Compose authorization information (this was moved here
193: ** from after the making of the connection so that the connection
194: ** wouldn't have to wait while prompting username and password
195: ** from the user). -- AL 13.10.93
196: */
197: #ifdef ACCESS_AUTH
1.21 luotonen 198: HTAA_composeAuth(request);
199: if (TRACE) {
200: if (request->authorization)
201: fprintf(stderr, "HTTP: Sending Authorization: %s\n",
202: request->authorization);
203: else
204: fprintf(stderr, "HTTP: Not sending authorization (yet)\n");
1.15 luotonen 205: }
206: #endif /* ACCESS_AUTH */
1.1 timbl 207:
1.10 timbl 208: /* Now, let's get a socket set up from the server for the data:
1.1 timbl 209: */
210: #ifdef DECNET
211: s = socket(AF_DECnet, SOCK_STREAM, 0);
212: #else
213: s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
214: #endif
215: status = connect(s, (struct sockaddr*)&soc_address, sizeof(soc_address));
216: if (status < 0) {
217: if (TRACE) fprintf(stderr,
1.30 frystyk 218: "HTTP: Unable to connect to remote host for `%s' (errno = %d).\n",
219: arg, errno);
1.17 timbl 220:
1.1 timbl 221: return HTInetStatus("connect");
222: }
223:
224: if (TRACE) fprintf(stderr, "HTTP connected, socket %d\n", s);
225:
1.17 timbl 226:
227: /* Compose and send command
228: ** ------------------------
229: */
230: {
231: char *command; /* The whole command */
232:
1.1 timbl 233: /* Ask that node for the document,
234: ** omitting the host name & anchor if not gatewayed.
235: */
1.37 luotonen 236: if (gate) { /* This is no longer used, and could be thrown away */
1.17 timbl 237: command = malloc(4 + strlen(arg)+ 2 + 31);
238: if (command == NULL) outofmem(__FILE__, "HTLoadHTTP");
239: strcpy(command, "GET ");
240: strcat(command, arg);
241: } else { /* not gatewayed */
242: char * p1 = HTParse(arg, "", PARSE_PATH|PARSE_PUNCTUATION);
243: command = malloc(4 + strlen(p1)+ 2 + 31);
244: if (command == NULL) outofmem(__FILE__, "HTLoadHTTP");
1.23 luotonen 245: if (request->method != METHOD_INVALID) {
246: strcpy(command, HTMethod_name(request->method));
1.22 luotonen 247: strcat(command, " ");
248: }
249: else {
250: strcpy(command, "GET ");
251: }
1.37 luotonen 252: /* if we are using a proxy gateway don't copy in the first slash
253: ** of say: /gopher://a;lkdjfl;ajdf;lkj/;aldk/adflj
254: ** so that just gohper://.... is sent.
255: */
256: if (using_proxy)
257: strcat(command, p1+1);
258: else
259: strcat(command, p1);
260: fprintf(stderr, " ** DEBUG: arg=\"%s\" p1=\"%s\" command=\"%s\"\n",
261: arg, p1, command);
1.17 timbl 262: free(p1);
263: }
1.2 timbl 264: #ifdef HTTP2
1.17 timbl 265: if (extensions) {
266: strcat(command, " ");
267: strcat(command, HTTP_VERSION);
268: }
1.2 timbl 269: #endif
1.17 timbl 270:
271: strcat(command, crlf); /* CR LF, as in rfc 977 */
272:
1.37 luotonen 273: if (extensions && HTImProxy && HTProxyHeaders) {
274: StrAllocCat(command, HTProxyHeaders);
275: }
276: else if (extensions) {
1.21 luotonen 277:
1.17 timbl 278: int i;
279: HTAtom * present = WWW_PRESENT;
280: char line[256]; /*@@@@ */
1.21 luotonen 281: HTList *conversions[2];
282:
1.22 luotonen 283: if (!HTConversions) {
284: HTConversions = HTList_new();
1.34 frystyk 285: /* HTFormatInit(HTConversions); App may do this not us tbl940210 */
1.22 luotonen 286: }
1.28 timbl 287:
1.21 luotonen 288: conversions[0] = HTConversions;
289: conversions[1] = request->conversions;
290:
1.34 frystyk 291:
1.21 luotonen 292: for (i=0; i<2; i++) {
293: HTList *cur = conversions[i];
294: HTPresentation *pres;
295:
296: while ((pres = (HTPresentation*)HTList_nextObject(cur))) {
297: if (pres->rep_out == present) {
298: if (pres->quality != 1.0) {
1.35 frystyk 299: sprintf(line, "Accept: %s; q=%.3f%c%c",
1.21 luotonen 300: HTAtom_name(pres->rep),
301: pres->quality, CR, LF);
302: } else {
303: sprintf(line, "Accept: %s%c%c",
304: HTAtom_name(pres->rep), CR, LF);
305: }
306: StrAllocCat(command, line);
1.17 timbl 307: }
308: }
1.2 timbl 309: }
1.22 luotonen 310:
1.37 luotonen 311: sprintf(line, "User-Agent: %s/%s libwww/%s%c%c",
1.17 timbl 312: HTAppName ? HTAppName : "unknown",
313: HTAppVersion ? HTAppVersion : "0.0",
314: HTLibraryVersion, CR, LF);
1.37 luotonen 315: StrAllocCat(command, line);
316: }
1.22 luotonen 317:
1.17 timbl 318: StrAllocCat(command, crlf); /* Blank line means "end" */
319:
320: if (TRACE) fprintf(stderr, "HTTP Tx: %s\n", command);
321:
322: /* Translate into ASCII if necessary
323: */
1.4 timbl 324: #ifdef NOT_ASCII
1.17 timbl 325: {
326: char * p;
327: for(p = command; *p; p++) {
328: *p = TOASCII(*p);
329: }
1.1 timbl 330: }
1.3 timbl 331: #endif
1.17 timbl 332:
333: status = NETWRITE(s, command, (int)strlen(command));
334: free(command);
335: if (status<0) {
336: if (TRACE) fprintf(stderr,
337: "HTTPAccess: Unable to send command.\n");
1.1 timbl 338: return HTInetStatus("send");
1.17 timbl 339: }
340: } /* compose and send command */
341:
1.2 timbl 342:
1.17 timbl 343: /* Read the response
344: ** -----------------
1.11 timbl 345: **
346: ** HTTP0 servers must return ASCII style text, though it can in
347: ** principle be just text without any markup at all.
348: ** Full HTTP servers must return a response
349: ** line and RFC822 style header. The response must therefore in
350: ** either case have a CRLF somewhere soon.
351: **
352: ** This is the theory. In practice, there are (1993) unfortunately
353: ** many binary documents just served up with HTTP0.9. This
354: ** means we have to preserve the binary buffer (on the assumption that
355: ** conversion from ASCII may lose information) in case it turns
356: ** out that we want the binary original.
1.2 timbl 357: */
1.3 timbl 358:
1.37 luotonen 359: if (HTImProxy) {
1.24 luotonen 360:
1.22 luotonen 361: /*
362: ** Server as a gateway -- send body of the message
363: ** received from client (if any).
364: */
365: if (request->isoc && request->content_length > 0) {
366: int remain = request->content_length;
367: int i = remain;
368: char * buf;
369:
370: while (remain > 0 &&
371: (buf = HTInputSocket_getBlock(request->isoc, &i))) {
372: int status = NETWRITE(s, buf, i);
373: if (status < 0) {
1.27 luotonen 374: CTRACE(stderr, "HTTPAccess.. Unable to forward body\n");
1.22 luotonen 375: return HTInetStatus("send");
376: }
377: remain -= i;
378: i = remain;
379: }
380: }
1.23 luotonen 381:
382: /*
1.22 luotonen 383: ** Load results directly to client
384: */
1.37 luotonen 385: HTProxyBytes = HTCopy(s, request->output_stream);
1.25 luotonen 386: (*request->output_stream->isa->free)(request->output_stream);
1.37 luotonen 387:
1.22 luotonen 388: return HT_LOADED;
389: }
390: else { /* read response */
1.21 luotonen 391:
1.17 timbl 392: HTFormat format_in; /* Format arriving in the message */
1.21 luotonen 393: HTInputSocket *isoc = HTInputSocket_new(s);
394: char * status_line = HTInputSocket_getStatusLine(isoc);
1.2 timbl 395:
1.11 timbl 396: /* Kludge to trap binary responses from illegal HTTP0.9 servers.
397: ** First time we have enough, look at the stub in ASCII
398: ** and get out of here if it doesn't look right.
399: **
400: ** We also check for characters above 128 in the first few bytes, and
401: ** if we find them we forget the html default.
402: **
403: ** Bugs: A HTTP0.9 server returning a document starting "HTTP/"
404: ** will be taken as a HTTP 1.0 server. Failure.
405: ** An HTTP 0.9 server returning a binary document with
406: ** characters < 128 will be read as ASCII.
407: */
1.36 frystyk 408:
409: /* If HTTP 0 response, then DO NOT CACHE (Henrik 14/02-94) */
410: if (!status_line) {
1.21 luotonen 411: if (HTInputSocket_seemsBinary(isoc)) {
412: format_in = HTAtom_for("www/unknown");
413: }
414: else {
415: format_in = WWW_HTML;
416: }
1.36 frystyk 417: HTCacheHTTP = NO; /* Do not cache */
1.21 luotonen 418: goto copy;
419: } /* end kludge */
420:
421: if (status_line) { /* Decode full HTTP response */
422: /*
423: ** We now have a terminated server status line, and we have
424: ** checked that it is most probably a legal one. Parse it.
425: */
426: char server_version[VERSION_LENGTH+1];
427: int server_status;
428:
429: if (TRACE)
430: fprintf(stderr, "HTTP Status Line: Rx: %.70s\n", status_line);
1.17 timbl 431:
1.21 luotonen 432: sscanf(status_line, "%20s%d", server_version, &server_status);
1.2 timbl 433:
1.21 luotonen 434: format_in = HTAtom_for("www/mime");
1.7 timbl 435:
1.21 luotonen 436: switch (server_status / 100) {
1.2 timbl 437:
1.21 luotonen 438: default: /* bad number */
439: HTAlert("Unknown status reply from server!");
440: break;
1.17 timbl 441:
1.21 luotonen 442: case 3: /* Various forms of redirection */
443: HTAlert(
1.17 timbl 444: "Redirection response from server is not handled by this client");
1.21 luotonen 445: break;
1.17 timbl 446:
1.21 luotonen 447: case 4: /* Access Authorization problem */
1.14 luotonen 448: #ifdef ACCESS_AUTH
1.21 luotonen 449: switch (server_status) {
450: case 401:
451: parse_401_headers(request, isoc);
452:
453: if (TRACE) fprintf(stderr, "%s %d %s\n",
454: "HTTP: close socket", s,
455: "to retry with Access Authorization");
456: HTInputSocket_free(isoc);
457: (void)NETCLOSE(s);
1.24 luotonen 458: if (HTAA_retryWithAuth(request, HTLoadHTTP)) {
1.21 luotonen 459: status = HT_LOADED;/* @@ THIS ONLY WORKS ON LINEMODE */
460: goto clean_up;
461: }
462: /* else falltrough */
463: default:
1.14 luotonen 464: {
1.21 luotonen 465: char *p1 = HTParse(gate ? gate : arg, "",
466: PARSE_HOST);
467: char * message;
468:
469: if (!(message = (char*)malloc(strlen(status_line) +
470: strlen(p1) + 100)))
471: outofmem(__FILE__, "HTTP 4xx status");
1.14 luotonen 472: sprintf(message,
1.21 luotonen 473: "HTTP server at %s replies:\n%s\n\n%s\n",
474: p1, status_line,
475: ((server_status == 401)
476: ? "Access Authorization package giving up.\n"
477: : ""));
1.22 luotonen 478: status = HTLoadError(request, server_status, message);
1.14 luotonen 479: free(message);
480: free(p1);
481: goto clean_up;
482: }
1.21 luotonen 483: } /* switch */
484: goto clean_up;
485: break;
486: #else
487: /* case 4 without Access Authorization falls through */
488: /* to case 5 (previously "I think I goofed"). -- AL */
489: #endif /* ACCESS_AUTH */
490:
491: case 5: /* I think you goofed */
492: {
493: char *p1 = HTParse(gate ? gate : arg, "", PARSE_HOST);
494: char * message = (char*)malloc(strlen(status_line) +
495: strlen(p1) + 100);
496: if (!message) outofmem(__FILE__, "HTTP 5xx status");
497: sprintf(message,
498: "HTTP server at %s replies:\n%s", p1, status_line);
1.22 luotonen 499: status = HTLoadError(request, server_status, message);
1.21 luotonen 500: free(message);
501: free(p1);
502: goto clean_up;
503: }
504: break;
1.17 timbl 505:
1.21 luotonen 506: case 2: /* Good: Got MIME object */
507: break;
1.17 timbl 508:
1.21 luotonen 509: } /* switch on response code */
1.17 timbl 510:
1.21 luotonen 511: } /* Full HTTP reply */
1.17 timbl 512:
513:
1.3 timbl 514: /* Set up the stream stack to handle the body of the message
1.33 timbl 515: **
516: ** In the special case of user asking for source and the message
517: ** being in MIME, we force the MIME decoding to occur, as it is really
518: ** HTTP decoding. If the user really wants the HTTP headers, he
519: ** can ask for them as www/mime.
1.3 timbl 520: */
1.21 luotonen 521:
1.13 duns 522: copy:
1.21 luotonen 523:
1.33 timbl 524: if ((format_in == HTAtom_for("www/mime"))
525: && (request->output_format == HTAtom_for("www/source"))) {
526: target = HTMIMEConvert(request, NULL, format_in,
527: request->output_format, request->output_stream);
528: } else {
529: target = HTStreamStack(format_in, request);
530: }
531:
1.17 timbl 532: if (!target) {
533: char buffer[1024]; /* @@@@@@@@ */
534: sprintf(buffer, "Sorry, no known way of converting %s to %s.",
535: HTAtom_name(format_in), HTAtom_name(request->output_format));
536: fprintf(stderr, "HTTP: %s", buffer);
1.22 luotonen 537: status = HTLoadError(request, 501, buffer);
1.17 timbl 538: goto clean_up;
539: }
540:
1.19 timbl 541: /* @@ Bug: The decision of whether or not to cache should also be
1.21 luotonen 542: ** made contingent on a IP address match or non match.
543: */
1.19 timbl 544: if (HTCacheHTTP) {
545: target = HTTee(target, HTCacheWriter(request, NULL, format_in,
1.21 luotonen 546: request->output_format,
547: request->output_stream));
1.19 timbl 548: }
549:
1.11 timbl 550: /* Push the data down the stream
1.3 timbl 551: ** We have to remember the end of the first buffer we just read
1.2 timbl 552: */
1.30 frystyk 553: if (format_in == WWW_HTML) {
1.17 timbl 554: target = HTNetToText(target); /* Pipe through CR stripper */
555: }
1.21 luotonen 556:
1.17 timbl 557: (*target->isa->put_block)(target,
1.21 luotonen 558: isoc->input_pointer,
559: isoc->input_limit - isoc->input_pointer);
560: HTInputSocket_free(isoc);
1.17 timbl 561: HTCopy(s, target);
562:
563: (*target->isa->free)(target);
564: status = HT_LOADED;
1.11 timbl 565:
1.2 timbl 566: /* Clean up
1.1 timbl 567: */
1.17 timbl 568:
569: clean_up:
570: if (TRACE) fprintf(stderr, "HTTP: close socket %d.\n", s);
571: (void) NETCLOSE(s);
1.38 ! frystyk 572: if(status_line)
! 573: free(status_line); /* Leak fix Henrik 18/02-94 */
! 574: HTCacheHTTP = HTCacheHTTP_old; /* Reestablish cache conditions */
! 575: return status; /* Good return */
1.3 timbl 576:
1.17 timbl 577: } /* read response */
578: } /* load HTTP */
1.1 timbl 579:
580: /* Protocol descriptor
581: */
582:
1.17 timbl 583: GLOBALDEF PUBLIC HTProtocol HTTP = { "http", HTLoadHTTP, 0, 0 };
1.21 luotonen 584:
Webmaster