Annotation of libwww/Library/src/HTTP.c, revision 1.16
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.14 luotonen 44: #include "HTAABrow.h" /* Access Authorization */
1.1 timbl 45:
1.2 timbl 46: struct _HTStream {
47: HTStreamClass * isa; /* all we need to know */
48: };
49:
50:
1.6 timbl 51: extern char * HTAppName; /* Application name: please supply */
52: extern char * HTAppVersion; /* Application version: please supply */
53:
1.1 timbl 54: /* Load Document from HTTP Server HTLoadHTTP()
55: ** ==============================
56: **
57: ** Given a hypertext address, this routine loads a document.
58: **
59: **
60: ** On entry,
61: ** arg is the hypertext reference of the article to be loaded.
62: ** gate is nill if no gateway, else the gateway address.
63: **
64: ** On exit,
65: ** returns >=0 If no error, a good socket number
66: ** <0 Error.
67: **
68: ** The socket must be closed by the caller after the document has been
69: ** read.
70: **
71: */
1.2 timbl 72: PUBLIC int HTLoadHTTP ARGS4 (
73: CONST char *, arg,
74: /* CONST char *, gate, */
75: HTParentAnchor *, anAnchor,
76: HTFormat, format_out,
77: HTStream*, sink)
1.1 timbl 78: {
79: int s; /* Socket number for returned data */
80: char *command; /* The whole command */
1.3 timbl 81: char * eol = 0; /* End of line if found */
1.7 timbl 82: char * start_of_data; /* Start of body of reply */
1.11 timbl 83: int length; /* Number of valid bytes in buffer */
1.1 timbl 84: int status; /* tcp return */
1.10 timbl 85: char crlf[3]; /* A CR LF equivalent string */
1.3 timbl 86: HTStream * target = NULL; /* Unconverted data */
87: HTFormat format_in; /* Format arriving in the message */
1.15 luotonen 88: char *auth = NULL; /* Authorization information */
1.3 timbl 89:
1.2 timbl 90: CONST char* gate = 0; /* disable this feature */
1.1 timbl 91: SockA soc_address; /* Binary network address */
92: SockA * sin = &soc_address;
1.2 timbl 93: BOOL had_header = NO; /* Have we had at least one header? */
1.11 timbl 94: char * text_buffer = NULL;
95: char * binary_buffer = NULL;
1.2 timbl 96: BOOL extensions = YES; /* Assume good HTTP server */
1.1 timbl 97: if (!arg) return -3; /* Bad if no name sepcified */
98: if (!*arg) return -2; /* Bad if name had zero length */
99:
100: /* Set up defaults:
101: */
102: #ifdef DECNET
1.2 timbl 103: sin->sdn_family = AF_DECnet; /* Family = DECnet, host order */
104: sin->sdn_objnum = DNP_OBJ; /* Default: http object number */
1.1 timbl 105: #else /* Internet */
1.2 timbl 106: sin->sin_family = AF_INET; /* Family = internet, host order */
107: sin->sin_port = htons(TCP_PORT); /* Default: http port */
1.1 timbl 108: #endif
109:
1.10 timbl 110: sprintf(crlf, "%c%c", CR, LF); /* To be corect on Mac, VM, etc */
111:
1.1 timbl 112: if (TRACE) {
113: if (gate) fprintf(stderr,
114: "HTTPAccess: Using gateway %s for %s\n", gate, arg);
115: else fprintf(stderr, "HTTPAccess: Direct access for %s\n", arg);
116: }
117:
118: /* Get node name and optional port number:
119: */
120: {
121: char *p1 = HTParse(gate ? gate : arg, "", PARSE_HOST);
122: int status = HTParseInet(sin, p1); /* TBL 920622 */
123: free(p1);
124: if (status) return status; /* No such host for example */
125: }
126:
1.2 timbl 127: retry:
1.15 luotonen 128:
129: /*
130: ** Compose authorization information (this was moved here
131: ** from after the making of the connection so that the connection
132: ** wouldn't have to wait while prompting username and password
133: ** from the user). -- AL 13.10.93
134: */
135: #ifdef ACCESS_AUTH
136: #define FREE(x) if (x) {free(x); x=NULL;}
137: {
138: char *docname;
139: char *hostname;
140: char *colon;
141: int portnumber;
142:
143: docname = HTParse(arg, "", PARSE_PATH);
144: hostname = HTParse((gate ? gate : arg), "", PARSE_HOST);
145: if (hostname &&
146: NULL != (colon = strchr(hostname, ':'))) {
1.16 ! duns 147: *(colon++) = '0円'; /* Chop off port number */
1.15 luotonen 148: portnumber = atoi(colon);
149: }
150: else portnumber = 80;
151:
152: auth = HTAA_composeAuth(hostname, portnumber, docname);
153:
154: if (TRACE) {
155: if (auth)
156: fprintf(stderr, "HTTP: Sending authorization: %s\n", auth);
157: else
158: fprintf(stderr, "HTTP: Not sending authorization (yet)\n");
159: }
160: FREE(hostname);
161: FREE(docname);
162: }
163: #endif /* ACCESS_AUTH */
1.1 timbl 164:
1.10 timbl 165: /* Now, let's get a socket set up from the server for the data:
1.1 timbl 166: */
167: #ifdef DECNET
168: s = socket(AF_DECnet, SOCK_STREAM, 0);
169: #else
170: s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
171: #endif
172: status = connect(s, (struct sockaddr*)&soc_address, sizeof(soc_address));
173: if (status < 0) {
174: if (TRACE) fprintf(stderr,
175: "HTTP: Unable to connect to remote host for `%s' (errno = %d).\n", arg, errno);
176: /* free(command); BUG OUT TBL 921121 */
177: return HTInetStatus("connect");
178: }
179:
180: if (TRACE) fprintf(stderr, "HTTP connected, socket %d\n", s);
181:
182: /* Ask that node for the document,
183: ** omitting the host name & anchor if not gatewayed.
184: */
185: if (gate) {
1.2 timbl 186: command = malloc(4 + strlen(arg)+ 2 + 31);
1.1 timbl 187: if (command == NULL) outofmem(__FILE__, "HTLoadHTTP");
188: strcpy(command, "GET ");
189: strcat(command, arg);
190: } else { /* not gatewayed */
191: char * p1 = HTParse(arg, "", PARSE_PATH|PARSE_PUNCTUATION);
1.2 timbl 192: command = malloc(4 + strlen(p1)+ 2 + 31);
1.1 timbl 193: if (command == NULL) outofmem(__FILE__, "HTLoadHTTP");
194: strcpy(command, "GET ");
195: strcat(command, p1);
196: free(p1);
197: }
1.2 timbl 198: #ifdef HTTP2
199: if (extensions) {
200: strcat(command, " ");
201: strcat(command, HTTP_VERSION);
202: }
203: #endif
1.10 timbl 204:
205: strcat(command, crlf); /* CR LF, as in rfc 977 */
1.1 timbl 206:
1.2 timbl 207: if (extensions) {
208:
209: int n;
210: int i;
211: HTAtom * present = WWW_PRESENT;
212: char line[256]; /*@@@@ */
213:
214: if (!HTPresentations) HTFormatInit();
215: n = HTList_count(HTPresentations);
216:
217: for(i=0; i<n; i++) {
218: HTPresentation * pres = HTList_objectAt(HTPresentations, i);
219: if (pres->rep_out == present) {
220: if (pres->quality != 1.0) {
1.3 timbl 221: sprintf(line, "Accept: %s q=%.3f%c%c",
222: HTAtom_name(pres->rep), pres->quality, CR, LF);
1.2 timbl 223: } else {
1.3 timbl 224: sprintf(line, "Accept: %s%c%c",
225: HTAtom_name(pres->rep), CR, LF);
1.2 timbl 226: }
227: StrAllocCat(command, line);
228:
229: }
230: }
1.6 timbl 231:
232: sprintf(line, "User-Agent: %s/%s libwww/%s%c%c",
233: HTAppName ? HTAppName : "unknown",
234: HTAppVersion ? HTAppVersion : "0.0",
235: HTLibraryVersion, CR, LF);
236: StrAllocCat(command, line);
1.14 luotonen 237:
238: #ifdef ACCESS_AUTH
1.15 luotonen 239: if (auth != NULL) {
240: sprintf(line, "%s%c%c", auth, CR, LF);
241: StrAllocCat(command, line);
1.14 luotonen 242: }
243: #endif /* ACCESS_AUTH */
1.2 timbl 244: }
1.14 luotonen 245:
1.10 timbl 246: StrAllocCat(command, crlf); /* Blank line means "end" */
247:
248: if (TRACE) fprintf(stderr, "HTTP Tx: %s\n", command);
249:
250: /* Translate into ASCII if necessary
251: */
1.4 timbl 252: #ifdef NOT_ASCII
1.1 timbl 253: {
254: char * p;
255: for(p = command; *p; p++) {
256: *p = TOASCII(*p);
257: }
1.4 timbl 258: }
1.3 timbl 259: #endif
1.1 timbl 260:
261: status = NETWRITE(s, command, (int)strlen(command));
262: free(command);
263: if (status<0) {
264: if (TRACE) fprintf(stderr, "HTTPAccess: Unable to send command.\n");
265: return HTInetStatus("send");
266: }
267:
1.2 timbl 268:
1.7 timbl 269: /* Read the first line of the response
270: ** -----------------------------------
1.11 timbl 271: **
272: ** HTTP0 servers must return ASCII style text, though it can in
273: ** principle be just text without any markup at all.
274: ** Full HTTP servers must return a response
275: ** line and RFC822 style header. The response must therefore in
276: ** either case have a CRLF somewhere soon.
277: **
278: ** This is the theory. In practice, there are (1993) unfortunately
279: ** many binary documents just served up with HTTP0.9. This
280: ** means we have to preserve the binary buffer (on the assumption that
281: ** conversion from ASCII may lose information) in case it turns
282: ** out that we want the binary original.
1.2 timbl 283: */
1.3 timbl 284:
1.2 timbl 285: {
286:
287: /* Get numeric status etc */
288:
289: BOOL end_of_file = NO;
290: HTAtom * encoding = HTAtom_for("7bit");
291: int buffer_length = INIT_LINE_SIZE; /* Why not? */
292:
1.11 timbl 293: binary_buffer = (char *) malloc(buffer_length * sizeof(char));
294: if (!binary_buffer) outofmem(__FILE__, "HTLoadHTTP");
295: text_buffer = (char *) malloc(buffer_length * sizeof(char));
296: if (!text_buffer) outofmem(__FILE__, "HTLoadHTTP");
297: length = 0;
1.2 timbl 298:
1.7 timbl 299: do { /* Loop to read in the first line */
1.2 timbl 300:
301: /* Extend line buffer if necessary for those crazy WAIS URLs ;-) */
302:
303: if (buffer_length - length < LINE_EXTEND_THRESH) {
304: buffer_length = buffer_length + buffer_length;
1.11 timbl 305: binary_buffer = (char *) realloc(
306: binary_buffer, buffer_length * sizeof(char));
307: if (!binary_buffer) outofmem(__FILE__, "HTLoadHTTP");
308: text_buffer = (char *) realloc(
309: text_buffer, buffer_length * sizeof(char));
310: if (!text_buffer) outofmem(__FILE__, "HTLoadHTTP");
1.2 timbl 311: }
1.11 timbl 312: status = NETREAD(s, binary_buffer + length,
1.2 timbl 313: buffer_length - length -1);
314: if (status < 0) {
315: HTAlert("Unexpected network read error on response");
1.9 timbl 316: NETCLOSE(s);
1.2 timbl 317: return status;
318: }
1.10 timbl 319:
320: if (TRACE) fprintf(stderr, "HTTP: read returned %d bytes.\n",
321: status);
322:
1.2 timbl 323: if (status == 0) {
324: end_of_file = YES;
325: break;
326: }
1.11 timbl 327: binary_buffer[length+status] = 0;
328:
329:
330: /* Make an ASCII *copy* of the buffer
331: */
1.2 timbl 332: #ifdef NOT_ASCII
1.10 timbl 333: if (TRACE) fprintf(stderr, "Local codes CR=%d, LF=%d\n", CR, LF);
1.11 timbl 334: #endif
1.2 timbl 335: {
336: char * p;
1.11 timbl 337: char * q;
338: for(p = binary_buffer+length, q=text_buffer+length;
339: *p; p++, q++) {
340: *q = FROMASCII(*p);
341: }
342:
343: *q++ = 0;
344: }
345:
346: /* Kludge to trap binary responses from illegal HTTP0.9 servers.
347: ** First time we have enough, look at the stub in ASCII
348: ** and get out of here if it doesn't look right.
349: **
350: ** We also check for characters above 128 in the first few bytes, and
351: ** if we find them we forget the html default.
352: **
353: ** Bugs: A HTTP0.9 server returning a document starting "HTTP/"
354: ** will be taken as a HTTP 1.0 server. Failure.
355: ** An HTTP 0.9 server returning a binary document with
356: ** characters < 128 will be read as ASCII.
357: */
358: #define STUB_LENGTH 20
359: if (length < STUB_LENGTH && length+status >= STUB_LENGTH) {
360: if(strncmp("HTTP/", text_buffer, 5)!=0) {
361: char *p;
362: start_of_data = text_buffer; /* reparse whole reply */
363: for(p=binary_buffer; p <binary_buffer+STUB_LENGTH;p++) {
1.13 duns 364: if (((int)*p)&128) {
1.11 timbl 365: format_in = HTAtom_for("www/unknown");
1.13 duns 366: length = length + status;
367: goto copy; /* out of while loop */
1.11 timbl 368: }
369: }
1.2 timbl 370: }
371: }
1.11 timbl 372: /* end kludge */
373:
374:
375: eol = strchr(text_buffer + length, 10);
376: if (eol) {
377: *eol = 0; /* Terminate the line */
1.14 luotonen 378: if (eol[-1] == CR) eol[-1] = 0; /* Chop trailing CR */
379: /* = corrected to == -- AL */
1.11 timbl 380: }
1.2 timbl 381:
382: length = length + status;
383:
1.7 timbl 384: } while (!eol && !end_of_file); /* No LF */
385:
386: } /* Scope of loop variables */
1.2 timbl 387:
1.7 timbl 388:
389: /* We now have a terminated unfolded line. Parse it.
390: ** -------------------------------------------------
1.2 timbl 391: */
392:
1.11 timbl 393: if (TRACE)fprintf(stderr, "HTTP: Rx: %.70s\n", text_buffer);
1.7 timbl 394:
395: {
396: int fields;
397: char server_version [VERSION_LENGTH+1];
398: int server_status;
399:
1.2 timbl 400:
401: /* Kludge to work with old buggy servers. They can't handle the third word
402: ** so we try again without it.
403: */
1.7 timbl 404: if (extensions &&
1.11 timbl 405: 0==strcmp(text_buffer, /* Old buggy server? */
1.7 timbl 406: "Document address invalid or access not authorised")) {
407: extensions = NO;
1.11 timbl 408: if (binary_buffer) free(binary_buffer);
409: if (text_buffer) free(text_buffer);
1.7 timbl 410: if (TRACE) fprintf(stderr,
411: "HTTP: close socket %d to retry with HTTP0\n", s);
412: NETCLOSE(s);
413: goto retry; /* @@@@@@@@@@ */
414: }
1.11 timbl 415: /* end kludge */
1.2 timbl 416:
1.11 timbl 417: fields = sscanf(text_buffer, "%20s%d",
1.7 timbl 418: server_version,
419: &server_status);
420:
1.11 timbl 421: if (fields < 2 ||
422: strncmp(server_version, "HTTP/", 5)!=0) { /* HTTP0 reply */
1.7 timbl 423: format_in = WWW_HTML;
1.11 timbl 424: start_of_data = text_buffer; /* reread whole reply */
1.9 timbl 425: if (eol) *eol = '\n'; /* Reconstitute buffer */
1.2 timbl 426:
1.11 timbl 427: } else { /* Full HTTP reply */
1.7 timbl 428:
429: /* Decode full HTTP response */
430:
1.3 timbl 431: format_in = HTAtom_for("www/mime");
1.11 timbl 432: start_of_data = eol ? eol + 1 : text_buffer + length;
1.3 timbl 433:
1.2 timbl 434: switch (server_status / 100) {
435:
1.3 timbl 436: default: /* bad number */
437: HTAlert("Unknown status reply from server!");
438: break;
439:
1.2 timbl 440: case 3: /* Various forms of redirection */
1.7 timbl 441: HTAlert(
1.3 timbl 442: "Redirection response from server is not handled by this client");
443: break;
444:
1.14 luotonen 445: case 4: /* Access Authorization problem */
446: #ifdef ACCESS_AUTH
447: switch (server_status) {
448: case 401:
449: length -= start_of_data - text_buffer;
450: if (HTAA_shouldRetryWithAuth(start_of_data, length, s)) {
451: /* Clean up before retrying */
452: if (binary_buffer) free(binary_buffer);
453: if (text_buffer) free(text_buffer);
454: if (TRACE)
455: fprintf(stderr, "%s %d %s\n",
456: "HTTP: close socket", s,
457: "to retry with Access Authorization");
458: (void)NETCLOSE(s);
459: goto retry;
460: break;
461: }
462: else {
463: /* FALL THROUGH */
464: }
465: default:
466: {
467: char *p1 = HTParse(gate ? gate : arg, "", PARSE_HOST);
468: char * message;
469:
470: if (!(message = (char*)malloc(strlen(text_buffer) +
471: strlen(p1) + 100)))
472: outofmem(__FILE__, "HTTP 4xx status");
473: sprintf(message,
474: "HTTP server at %s replies:\n%s\n\n%s\n",
475: p1, text_buffer,
476: ((server_status == 401)
477: ? "Access Authorization package giving up.\n"
478: : ""));
479: status = HTLoadError(sink, server_status, message);
480: free(message);
481: free(p1);
482: goto clean_up;
483: }
484: } /* switch */
485: goto clean_up;
486: break;
487: #else
488: /* case 4 without Access Authorization falls through */
489: /* to case 5 (previously "I think I goofed"). -- AL */
490: #endif /* ACCESS_AUTH */
491:
1.2 timbl 492: case 5: /* I think you goofed */
1.6 timbl 493: {
494: char *p1 = HTParse(gate ? gate : arg, "", PARSE_HOST);
495: char * message = (char*)malloc(
1.11 timbl 496: strlen(text_buffer)+strlen(p1) + 100);
1.6 timbl 497: if (!message) outofmem(__FILE__, "HTTP 5xx status");
498: sprintf(message,
1.11 timbl 499: "HTTP server at %s replies:\n%s", p1, text_buffer);
1.8 timbl 500: status = HTLoadError(sink, server_status, message);
1.6 timbl 501: free(message);
502: free(p1);
503: goto clean_up;
504: }
1.3 timbl 505: break;
1.2 timbl 506:
507: case 2: /* Good: Got MIME object */
508: break;
509:
1.7 timbl 510: } /* switch on response code */
511:
512: } /* Full HTTP reply */
513:
514: } /* scope of fields */
1.2 timbl 515:
1.3 timbl 516: /* Set up the stream stack to handle the body of the message
517: */
518:
1.13 duns 519: copy:
520:
1.3 timbl 521: target = HTStreamStack(format_in,
522: format_out,
523: sink , anAnchor);
524:
525: if (!target) {
526: char buffer[1024]; /* @@@@@@@@ */
1.11 timbl 527: if (binary_buffer) free(binary_buffer);
528: if (text_buffer) free(text_buffer);
1.3 timbl 529: sprintf(buffer, "Sorry, no known way of converting %s to %s.",
530: HTAtom_name(format_in), HTAtom_name(format_out));
531: fprintf(stderr, "HTTP: %s", buffer);
1.6 timbl 532: status = HTLoadError(sink, 501, buffer);
533: goto clean_up;
1.3 timbl 534: }
535:
536:
1.11 timbl 537: /* Push the data down the stream
1.3 timbl 538: ** We have to remember the end of the first buffer we just read
1.2 timbl 539: */
1.11 timbl 540: if (format_in == WWW_HTML) {
541: target = HTNetToText(target); /* Pipe through CR stripper */
542: }
543:
544: (*target->isa->put_block)(target,
545: binary_buffer + (start_of_data - text_buffer),
546: length - (start_of_data - text_buffer));
547: HTCopy(s, target);
1.3 timbl 548:
549: (*target->isa->free)(target);
1.8 timbl 550: status = HT_LOADED;
1.2 timbl 551:
552: /* Clean up
1.1 timbl 553: */
1.3 timbl 554:
1.6 timbl 555: clean_up:
1.11 timbl 556: if (binary_buffer) free(binary_buffer);
557: if (text_buffer) free(text_buffer);
1.3 timbl 558:
1.1 timbl 559: if (TRACE) fprintf(stderr, "HTTP: close socket %d.\n", s);
1.6 timbl 560: (void) NETCLOSE(s);
1.1 timbl 561:
1.8 timbl 562: return status; /* Good return */
1.3 timbl 563:
1.1 timbl 564: }
1.7 timbl 565:
1.1 timbl 566:
567: /* Protocol descriptor
568: */
569:
1.13 duns 570: GLOBALDEF PUBLIC HTProtocol HTTP = { "http", HTLoadHTTP, 0 };
Webmaster