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