diff options
author | Thomas E. Dickey <dickey@invisible-island.net> | 1998-11-06 16:18:09 -0500 |
---|---|---|
committer | Thomas E. Dickey <dickey@invisible-island.net> | 1998-11-06 16:18:09 -0500 |
commit | 18024037b515bfff83e0230b35151babe6005e18 (patch) | |
tree | b8c80055282a00883284722a3dd6bb25234e07f0 /WWW/Library/Implementation/HTTP.c | |
parent | 3d8ecbe48af249fa0d77ce4d273e32ce4b7e6e18 (diff) | |
download | lynx-snapshots-18024037b515bfff83e0230b35151babe6005e18.tar.gz |
snapshot of project "lynx", label v2-8-1dev_4
Diffstat (limited to 'WWW/Library/Implementation/HTTP.c')
-rw-r--r-- | WWW/Library/Implementation/HTTP.c | 197 |
1 files changed, 140 insertions, 57 deletions
diff --git a/WWW/Library/Implementation/HTTP.c b/WWW/Library/Implementation/HTTP.c index 4d128c81..996ebd4b 100644 --- a/WWW/Library/Implementation/HTTP.c +++ b/WWW/Library/Implementation/HTTP.c @@ -7,8 +7,9 @@ */ #include <HTUtils.h> +#include <tcp.h> + #include <HTTP.h> -#include <LYUtils.h> #define HTTP_VERSION "HTTP/1.0" @@ -24,18 +25,18 @@ #include <HTTCP.h> #include <HTFormat.h> #include <HTFile.h> +#include <ctype.h> #include <HTAlert.h> #include <HTMIME.h> #include <HTML.h> #include <HTInit.h> #include <HTAABrow.h> -#include <LYCookie.h> #include <LYGlobalDefs.h> -#include <GridText.h> -#include <LYStrings.h> #include <LYLeaks.h> +/* #define TRACE 1 */ + struct _HTStream { HTStreamClass * isa; @@ -64,6 +65,18 @@ extern char *http_error_file; /* Store HTTP status code in this file */ extern BOOL traversal; /* TRUE if we are doing a traversal */ extern BOOL dump_output_immediately; /* TRUE if no interactive user */ +extern char * HTLoadedDocumentURL NOPARAMS; +extern int HTCheckForInterrupt NOPARAMS; +extern void LYSetCookie PARAMS(( + CONST char * SetCookie, + CONST char * SetCookie2, + CONST char * address)); +extern char * LYCookie PARAMS(( + CONST char * hostname, + CONST char * path, + int port, + BOOL secure)); + #define HTTP_NETREAD(a, b, c, d) NETREAD(a, b, c) #define HTTP_NETWRITE(a, b, c, d) NETWRITE(a, b, c) #define HTTP_NETCLOSE(a, b) (void)NETCLOSE(a) @@ -152,6 +165,7 @@ try_again: ** so we can start over here... */ eol = 0; + bytes_already_read = 0; had_header = NO; length = 0; doing_redirect = FALSE; @@ -172,13 +186,17 @@ try_again: /* ** Interrupt cleanly. */ - CTRACE (tfp, "HTTP: Interrupted on connect; recovering cleanly.\n"); - _HTProgress ("Connection interrupted."); - status = HT_NOT_LOADED; - goto done; - } - if (status < 0) { - CTRACE(tfp, "HTTP: Unable to connect to remote host for `%s' (errno = %d).\n", + if (TRACE) + fprintf (stderr, + "HTTP: Interrupted on connect; recovering cleanly.\n"); + _HTProgress ("Connection interrupted."); + status = HT_NOT_LOADED; + goto done; + } + if (status < 0) { + if (TRACE) + fprintf(stderr, + "HTTP: Unable to connect to remote host for `%s' (errno = %d).\n", url, SOCKET_ERRNO); HTAlert("Unable to connect to remote host."); status = HT_NOT_LOADED; @@ -295,7 +313,8 @@ try_again: strcpy(line, pref_charset); if (line[strlen(line)-1] == ',') line[strlen(line)-1] = '\0'; - LYLowerCase(line); + for (i = 0; line[i]; i++) + line[i] = TOLOWER(line[i]); if (strstr(line, "iso-8859-1") == NULL) strcat(line, ", iso-8859-1;q=0.01"); if (strstr(line, "us-ascii") == NULL) @@ -422,7 +441,8 @@ try_again: */ sprintf(line, "%s%c%c", auth, CR, LF); StrAllocCat(command, line); - CTRACE(tfp, "HTTP: Sending authorization: %s\n", auth); + if (TRACE) + fprintf(stderr, "HTTP: Sending authorization: %s\n", auth); } else if (auth && *auth == '\0') { /* ** If auth is a zero-length string, the user either @@ -446,7 +466,9 @@ try_again: goto done; } } else { - CTRACE(tfp, "HTTP: Not sending authorization (yet).\n"); + if (TRACE) + fprintf(stderr, + "HTTP: Not sending authorization (yet).\n"); } /* ** Add 'Cookie:' header, if it's HTTP or HTTPS @@ -479,7 +501,9 @@ try_again: */ StrAllocCat(command, "Cookie2: $Version=\"1\""); StrAllocCat(command, crlf); - CTRACE(tfp, "HTTP: Sending Cookie2: $Version =\"1\"\n"); + if (TRACE) + fprintf(stderr, + "HTTP: Sending Cookie2: $Version =\"1\"\n"); } if (*cookie != '\0') { /* @@ -490,7 +514,8 @@ try_again: StrAllocCat(command, "Cookie: "); StrAllocCat(command, cookie); StrAllocCat(command, crlf); - CTRACE(tfp, "HTTP: Sending Cookie: %s\n", cookie); + if (TRACE) + fprintf(stderr, "HTTP: Sending Cookie: %s\n", cookie); } FREE(cookie); } @@ -515,7 +540,9 @@ try_again: */ sprintf(line, "%s%c%c", auth, CR, LF); StrAllocCat(command, line); - CTRACE(tfp, (auth_proxy ? + if (TRACE) + fprintf(stderr, + (auth_proxy ? "HTTP: Sending proxy authorization: %s\n" : "HTTP: Sending authorization: %s\n"), auth); @@ -542,7 +569,9 @@ try_again: goto done; } } else { - CTRACE(tfp, (auth_proxy ? + if (TRACE) + fprintf(stderr, + (auth_proxy ? "HTTP: Not sending proxy authorization (yet).\n" : "HTTP: Not sending authorization (yet).\n")); } @@ -552,10 +581,12 @@ try_again: auth_proxy = NO; } - if (do_post) { - CTRACE (tfp, "HTTP: Doing post, content-type '%s'\n", - anAnchor->post_content_type ? anAnchor->post_content_type - : "lose"); + if (do_post) + { + if (TRACE) + fprintf (stderr, "HTTP: Doing post, content-type '%s'\n", + anAnchor->post_content_type ? anAnchor->post_content_type + : "lose"); sprintf (line, "Content-type: %s%c%c", anAnchor->post_content_type ? anAnchor->post_content_type : "lose", CR, LF); @@ -578,9 +609,12 @@ try_again: else StrAllocCat(command, crlf); /* Blank line means "end" of headers */ - CTRACE (tfp, "Writing:\n%s%s----------------------------------\n", + if (TRACE) { + fprintf (stderr, + "Writing:\n%s%s----------------------------------\n", command, (anAnchor->post_data ? crlf : "")); + } _HTProgress ("Sending HTTP request."); @@ -588,7 +622,8 @@ try_again: FREE(command); if (status <= 0) { if (status == 0) { - CTRACE (tfp, "HTTP: Got status 0 in initial write\n"); + if (TRACE) + fprintf (stderr, "HTTP: Got status 0 in initial write\n"); /* Do nothing. */ } else if ((SOCKET_ERRNO == ENOTCONN || SOCKET_ERRNO == ECONNRESET || @@ -598,14 +633,18 @@ try_again: /* ** Arrrrgh, HTTP 0/1 compability problem, maybe. */ - CTRACE (tfp, "HTTP: BONZO ON WRITE Trying again with HTTP0 request.\n"); + if (TRACE) + fprintf (stderr, + "HTTP: BONZO ON WRITE Trying again with HTTP0 request.\n"); _HTProgress ("Retrying as HTTP0 request."); HTTP_NETCLOSE(s, handle); extensions = NO; already_retrying = TRUE; goto try_again; } else { - CTRACE (tfp, "HTTP: Hit unexpected network WRITE error; aborting connection.\n"); + if (TRACE) + fprintf (stderr, + "HTTP: Hit unexpected network WRITE error; aborting connection.\n"); HTTP_NETCLOSE(s, handle); status = -1; HTAlert("Unexpected network write error; connection aborted."); @@ -613,7 +652,8 @@ try_again: } } - CTRACE (tfp, "HTTP: WRITE delivered OK\n"); + if (TRACE) + fprintf (stderr, "HTTP: WRITE delivered OK\n"); _HTProgress ("HTTP request sent; waiting for response."); /* Read the first line of the response @@ -626,7 +666,6 @@ try_again: line_buffer = (char *)calloc(1, (buffer_length * sizeof(char))); - HTReadProgress (bytes_already_read = 0, 0); do {/* Loop to read in the first line */ /* ** Extend line buffer if necessary for those crazy WAIS URLs ;-) @@ -636,18 +675,21 @@ try_again: line_buffer = (char *)realloc(line_buffer, (buffer_length * sizeof(char))); } - CTRACE (tfp, "HTTP: Trying to read %d\n", + if (TRACE) + fprintf (stderr, "HTTP: Trying to read %d\n", buffer_length - length - 1); status = HTTP_NETREAD(s, line_buffer + length, buffer_length - length - 1, handle); - CTRACE (tfp, "HTTP: Read %d\n", status); + if (TRACE) + fprintf (stderr, "HTTP: Read %d\n", status); if (status <= 0) { /* * Retry if we get nothing back too. * Bomb out if we get nothing twice. */ if (status == HT_INTERRUPTED) { - CTRACE (tfp, "HTTP: Interrupted initial read.\n"); + if (TRACE) + fprintf (stderr, "HTTP: Interrupted initial read.\n"); _HTProgress ("Connection interrupted."); HTTP_NETCLOSE(s, handle); status = HT_NO_DATA; @@ -660,7 +702,9 @@ try_again: /* ** Arrrrgh, HTTP 0/1 compability problem, maybe. */ - CTRACE (tfp, "HTTP: BONZO Trying again with HTTP0 request.\n"); + if (TRACE) + fprintf (stderr, + "HTTP: BONZO Trying again with HTTP0 request.\n"); HTTP_NETCLOSE(s, handle); FREE(line_buffer); FREE(line_kept_clean); @@ -670,7 +714,9 @@ try_again: _HTProgress ("Retrying as HTTP0 request."); goto try_again; } else { - CTRACE (tfp, "HTTP: Hit unexpected network read error; aborting connection; status %d.\n", + if (TRACE) + fprintf (stderr, + "HTTP: Hit unexpected network read error; aborting connection; status %d.\n", status); HTAlert("Unexpected network read error; connection aborted."); HTTP_NETCLOSE(s, handle); @@ -680,7 +726,8 @@ try_again: } bytes_already_read += status; - HTReadProgress (bytes_already_read, 0); + sprintf (line, "Read %d bytes of data.", bytes_already_read); + HTProgress (line); #ifdef UCX /* UCX returns -1 on EOF */ if (status == 0 || status == -1) @@ -724,7 +771,8 @@ try_again: /* We now have a terminated unfolded line. Parse it. ** ------------------------------------------------- */ - CTRACE(tfp, "HTTP: Rx: %s\n", line_buffer); + if (TRACE) + fprintf(stderr, "HTTP: Rx: %s\n", line_buffer); /* ** Kludge to work with old buggy servers and the VMS Help gateway. @@ -740,7 +788,8 @@ try_again: FREE(line_kept_clean); extensions = NO; already_retrying = TRUE; - CTRACE(tfp, "HTTP: close socket %d to retry with HTTP0\n", s); + if (TRACE) + fprintf(stderr, "HTTP: close socket %d to retry with HTTP0\n", s); HTTP_NETCLOSE(s, handle); /* print a progress message */ _HTProgress ("Retrying as HTTP0 request."); @@ -759,7 +808,8 @@ try_again: server_version, &server_status); - CTRACE (tfp, "HTTP: Scanned %d fields from line_buffer\n", fields); + if (TRACE) + fprintf (stderr, "HTTP: Scanned %d fields from line_buffer\n", fields); if (http_error_file) { /* Make the status code externally available */ FILE *error_file; @@ -791,7 +841,8 @@ try_again: */ HTAtom * encoding; - CTRACE (tfp, "--- Talking HTTP0.\n"); + if (TRACE) + fprintf (stderr, "--- Talking HTTP0.\n"); format_in = HTFileFormat(url, &encoding, NULL); /* @@ -800,19 +851,27 @@ try_again: ** without looking at content. */ if (!strncmp(HTAtom_name(format_in), "text/plain",10)) { - CTRACE(tfp, "HTTP: format_in being changed to text/HTML\n"); + if (TRACE) + fprintf(stderr, + "HTTP: format_in being changed to text/HTML\n"); format_in = WWW_HTML; } if (!IsUnityEnc(encoding)) { /* ** Change the format to that for "www/compressed". */ - CTRACE(tfp, "HTTP: format_in is '%s',\n", HTAtom_name(format_in)); + if (TRACE) { + fprintf(stderr, + "HTTP: format_in is '%s',\n", HTAtom_name(format_in)); + } StrAllocCopy(anAnchor->content_type, HTAtom_name(format_in)); StrAllocCopy(anAnchor->content_encoding, HTAtom_name(encoding)); format_in = HTAtom_for("www/compressed"); - CTRACE(tfp, " Treating as '%s' with encoding '%s'\n", + if (TRACE) { + fprintf(stderr, + " Treating as '%s' with encoding '%s'\n", "www/compressed", HTAtom_name(encoding)); + } } start_of_data = line_kept_clean; @@ -821,7 +880,8 @@ try_again: ** Set up to decode full HTTP/1.n response. - FM */ format_in = HTAtom_for("www/mime"); - CTRACE (tfp, "--- Talking HTTP1.\n"); + if (TRACE) + fprintf (stderr, "--- Talking HTTP1.\n"); /* ** We set start_of_data to "" when !eol here because there @@ -881,6 +941,7 @@ try_again: HTTP_NETCLOSE(s, handle); status = HT_NO_DATA; goto clean_up; + break; case 205: /* @@ -894,6 +955,7 @@ try_again: HTTP_NETCLOSE(s, handle); status = HT_NO_DATA; goto clean_up; + break; case 206: /* @@ -906,6 +968,7 @@ try_again: HTTP_NETCLOSE(s, handle); status = HT_NO_DATA; goto clean_up; + break; default: /* @@ -1070,7 +1133,8 @@ try_again: /* * Impatient user. - FM */ - CTRACE (tfp, "HTTP: Interrupted followup read.\n"); + if (TRACE) + fprintf (stderr, "HTTP: Interrupted followup read.\n"); _HTProgress ("Connection interrupted."); status = HT_INTERRUPTED; goto clean_up; @@ -1083,7 +1147,9 @@ try_again: * Don't make the redirection permanent * if we have POST content. - FM */ - CTRACE(tfp, "HTTP: Have POST content. Treating 301 (Permanent) as Temporary.\n"); + if (TRACE) + fprintf(stderr, + "HTTP: Have POST content. Treating 301 (Permanent) as Temporary.\n"); HTAlert( "Have POST content. Treating Permanent Redirection as Temporary.\n"); } else { @@ -1301,7 +1367,9 @@ Cookie2_continuation: * thus is probably something in the body, so * we'll show the user what was returned. - FM */ - CTRACE(tfp, "HTTP: 'Location:' is zero-length!\n"); + if (TRACE) + fprintf(stderr, + "HTTP: 'Location:' is zero-length!\n"); if (cp1) *cp1 = LF; if (cp2) @@ -1320,7 +1388,9 @@ Cookie2_continuation: * seek the document at that Location. - FM */ HTProgress(line_buffer); - CTRACE(tfp, "HTTP: Picked up location '%s'\n", + if (TRACE) + fprintf(stderr, + "HTTP: Picked up location '%s'\n", redirecting_url); if (cp1) *cp1 = LF; @@ -1338,7 +1408,9 @@ Cookie2_continuation: * Append our URL. - FM */ StrAllocCat(redirecting_url, anAnchor->address); - CTRACE(tfp, "HTTP: Proxy URL is '%s'\n", + if (TRACE) + fprintf(stderr, + "HTTP: Proxy URL is '%s'\n", redirecting_url); } if (!do_post || @@ -1402,7 +1474,8 @@ Cookie2_continuation: * header, so we'll show the user what we got, if * anything. - FM */ - CTRACE (tfp, "HTTP: Failed to pick up location.\n"); + if (TRACE) + fprintf (stderr, "HTTP: Failed to pick up location.\n"); doing_redirect = FALSE; permanent_redirection = FALSE; start_of_data = line_kept_clean; @@ -1446,7 +1519,8 @@ Cookie2_continuation: */ if (show_401) break; - if (HTAA_shouldRetryWithAuth(start_of_data, length, s, NO)) { + if (HTAA_shouldRetryWithAuth(start_of_data, length, + (void *)handle, s, NO)) { HTTP_NETCLOSE(s, handle); if (dump_output_immediately && !authentication_info[0]) { @@ -1458,15 +1532,17 @@ Cookie2_continuation: goto clean_up; } - CTRACE(tfp, "%s %d %s\n", - "HTTP: close socket", s, - "to retry with Access Authorization"); + if (TRACE) + fprintf(stderr, "%s %d %s\n", + "HTTP: close socket", s, + "to retry with Access Authorization"); _HTProgress ( "Retrying with access authorization information."); FREE(line_buffer); FREE(line_kept_clean); goto try_again; + break; } else if (!(traversal || dump_output_immediately) && HTConfirm("Show the 401 message body?")) { break; @@ -1478,6 +1554,7 @@ Cookie2_continuation: status = -1; goto clean_up; } + break; case 407: /* @@ -1493,7 +1570,8 @@ Cookie2_continuation: */ if (!using_proxy || show_407) break; - if (HTAA_shouldRetryWithAuth(start_of_data, length, s, YES)) { + if (HTAA_shouldRetryWithAuth(start_of_data, length, + (void *)handle, s, YES)) { HTTP_NETCLOSE(s, handle); if (dump_output_immediately && !proxyauth_info[0]) { @@ -1505,15 +1583,17 @@ Cookie2_continuation: goto clean_up; } - CTRACE(tfp, "%s %d %s\n", - "HTTP: close socket", s, - "to retry with Proxy Authorization"); + if (TRACE) + fprintf(stderr, "%s %d %s\n", + "HTTP: close socket", s, + "to retry with Proxy Authorization"); _HTProgress ( "Retrying with proxy authorization information."); FREE(line_buffer); FREE(line_kept_clean); goto try_again; + break; } else if (!(traversal || dump_output_immediately) && HTConfirm("Show the 407 message body?")) { if (!dump_output_immediately && @@ -1534,6 +1614,7 @@ Cookie2_continuation: status = -1; goto clean_up; } + break; case 408: /* @@ -1544,6 +1625,7 @@ Cookie2_continuation: HTTP_NETCLOSE(s, handle); status = HT_NO_DATA; goto done; + break; default: /* @@ -1697,7 +1779,8 @@ Cookie2_continuation: (*target->isa->_abort)(target, NULL); HTTP_NETCLOSE(s, handle); if (!already_retrying && !do_post) { - CTRACE (tfp, "HTTP: Trying again with HTTP0 request.\n"); + if (TRACE) + fprintf (stderr, "HTTP: Trying again with HTTP0 request.\n"); /* ** May as well consider it an interrupt -- right? */ |