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