Debian Codegraph @

WWW/Library/Implementation/HTTP.c

raw
1 /*
2 * $LynxId: HTTP.c,v 1.208 2025/07/22 00:33:24 tom Exp $
3 *
4 * HyperText Transfer Protocol - Client implementation HTTP.c
5 * ===========================
6 * Modified:
7 * 27 Jan 1994 PDM Added Ari Luotonen's Fix for Reload when using proxy
8 * servers.
9 * 28 Apr 1997 AJL,FM Do Proxy Authorisation.
10 * 04 Jul 2025 Nate Choe, Use newly-made TLS API
11 */
12
13 #include <HTNews.h>
14 #include <HTTP.h>
15 #include <LYUtils.h>
16
17 #define HTTP_PORT 80
18 #define HTTPS_PORT 443
19 #define SNEWS_PORT 563
20
21 #define INIT_LINE_SIZE 1536 /* Start with line buffer this big */
22 #define LINE_EXTEND_THRESH 256 /* Minimum read size */
23 #define VERSION_LENGTH 20 /* for returned protocol version */
24
25 #include <HTParse.h>
26 #include <HTTCP.h>
27 #include <HTFile.h>
28 #include <HTAlert.h>
29 #include <HTAABrow.h>
30
31 #include <LYGlobalDefs.h>
32 #include <GridText.h>
33 #include <LYStrings.h>
34 #include <LYrcFile.h>
35 #include <LYLeaks.h>
36
37 BOOLEAN reloading = FALSE; /* Reloading => send no-cache pragma to proxy */
38 char *redirecting_url = NULL; /* Location: value. */
39 BOOL permanent_redirection = FALSE; /* Got 301 status? */
40 BOOL redirect_post_content = FALSE; /* Don't convert to GET? */
41
42 #ifdef USE_SSL
43
44 SSL * SSL_handle = NULL;
45
46 #define HTTP_NETREAD(sock, buff, size, handle) \
47 (handle \
48 ? SSL_read(handle, buff, size) \
49 : NETREAD(sock, buff, size))
50
51 #define HTTP_NETWRITE(sock, buff, size, handle) \
52 (handle \
53 ? SSL_write(handle, buff, size) \
54 : NETWRITE(sock, buff, size))
55
56 #define HTTP_NETCLOSE(sock, handle) \
57 { (void)NETCLOSE(sock); \
58 if (handle) \
59 SSL_free(handle); \
60 SSL_handle = handle = NULL; \
61 }
62
63 #else
64
65 #define HTTP_NETREAD(a, b, c, d) NETREAD(a, b, c)
66 #define HTTP_NETWRITE(a, b, c, d) NETWRITE(a, b, c)
67 #define HTTP_NETCLOSE(a, b) (void)NETCLOSE(a)
68
69 #endif /* USE_SSL */
70
71 #ifdef _WINDOWS /* 1997/11/06 (Thu) 13:00:08 */
72
73 #define BOX_TITLE "Lynx " __FILE__
74 #define BOX_FLAG (MB_ICONINFORMATION | MB_SETFOREGROUND)
75
76 typedef struct {
77 int fd;
78 char *buf;
79 int len;
80 } recv_data_t;
81
82 int ws_read_per_sec = 0;
83 static int ws_errno = 0;
84
85 static DWORD g_total_times = 0;
86 static DWORD g_total_bytes = 0;
87
88 /* The same like read, but takes care of EINTR and uses select to
89 timeout the stale connections. */
90
91 static int ws_read(int fd, char *buf, int len)
92 {
93 int res;
94 int retry = 3;
95
96 do {
97 res = recv(fd, buf, len, 0);
98 if (WSAEWOULDBLOCK == WSAGetLastError()) {
99 Sleep(100);
100 if (retry-- > 0)
101 continue;
102 }
103 } while (res == SOCKET_ERROR && SOCKET_ERRNO == EINTR);
104
105 return res;
106 }
107
108 #define DWORD_ERR ((DWORD)-1)
109
110 static DWORD __stdcall _thread_func(void *p)
111 {
112 DWORD result;
113 int i, val;
114 recv_data_t *q = (recv_data_t *) p;
115
116 i = 0;
117 i++;
118 val = ws_read(q->fd, q->buf, q->len);
119
120 if (val == SOCKET_ERROR) {
121 ws_errno = WSAGetLastError();
122 #if 0
123 char buff[256];
124
125 sprintf(buff, "Thread read: %d, error (%ld), fd = %d, len = %d",
126 i, ws_errno, q->fd, q->len);
127 MessageBox(NULL, buff, BOX_TITLE, BOX_FLAG);
128 #endif
129 result = DWORD_ERR;
130 } else {
131 result = val;
132 }
133
134 return result;
135 }
136
137 /* The same like read, but takes care of EINTR and uses select to
138 timeout the stale connections. */
139
140 int ws_netread(int fd, char *buf, int len)
141 {
142 int i;
143 char buff[256];
144
145 /* 1998/03/30 (Mon) 09:01:21 */
146 HANDLE hThread;
147 DWORD dwThreadID;
148 DWORD exitcode = 0;
149 DWORD ret_val = DWORD_ERR;
150 DWORD val, process_time, now_TickCount, save_TickCount;
151
152 static recv_data_t para;
153
154 #define TICK 5
155 #define STACK_SIZE 0x2000uL
156
157 EnterCriticalSection(&critSec_READ);
158
159 para.fd = fd;
160 para.buf = buf;
161 para.len = len;
162
163 ws_read_per_sec = 0;
164 save_TickCount = GetTickCount();
165
166 hThread = CreateThread(NULL, STACK_SIZE,
167 _thread_func,
168 (void *) &para, 0UL, &dwThreadID);
169
170 if (hThread == 0) {
171 HTInfoMsg("CreateThread Failed (read)");
172 goto read_exit;
173 }
174
175 i = 0;
176 while (1) {
177 val = WaitForSingleObject(hThread, 1000 / TICK);
178 i++;
179 if (val == WAIT_FAILED) {
180 HTInfoMsg("Wait Failed");
181 ret_val = DWORD_ERR;
182 break;
183 } else if (val == WAIT_TIMEOUT) {
184 i++;
185 if (i / TICK > (AlertSecs + 2)) {
186 sprintf(buff, "Read Waiting (%2d.%01d) for %d Bytes",
187 i / TICK, (i % TICK) * 10 / TICK, len);
188 SetConsoleTitle(buff);
189 }
190 if (win32_check_interrupt() || ((i / TICK) > lynx_timeout)) {
191 if (CloseHandle(hThread) == FALSE) {
192 HTInfoMsg("Thread terminate Failed");
193 }
194 WSASetLastError(ETIMEDOUT);
195 ret_val = HT_INTERRUPTED;
196 break;
197 }
198 } else if (val == WAIT_OBJECT_0) {
199 if (GetExitCodeThread(hThread, &exitcode) == FALSE) {
200 exitcode = DWORD_ERR;
201 }
202 if (CloseHandle(hThread) == FALSE) {
203 HTInfoMsg("Thread terminate Failed");
204 }
205 now_TickCount = GetTickCount();
206 if (now_TickCount >= save_TickCount)
207 process_time = now_TickCount - save_TickCount;
208 else
209 process_time = now_TickCount + (0xffffffff - save_TickCount);
210
211 if (process_time == 0)
212 process_time = 1;
213 g_total_times += process_time;
214
215 /*
216 * DWORD is unsigned, and could be an error code which is signed.
217 */
218 if ((long) exitcode > 0)
219 g_total_bytes += exitcode;
220
221 ws_read_per_sec = g_total_bytes;
222 if (ws_read_per_sec > 2000000) {
223 if (g_total_times > 1000)
224 ws_read_per_sec /= (g_total_times / 1000);
225 } else {
226 ws_read_per_sec *= 1000;
227 ws_read_per_sec /= g_total_times;
228 }
229
230 ret_val = exitcode;
231 break;
232 }
233 } /* end while(1) */
234
235 read_exit:
236 LeaveCriticalSection(&critSec_READ);
237 return ret_val;
238 }
239 #endif /* _WINDOWS */
240
241 /*
242 * RFC-1738 says we can have user/password using these ASCII characters
243 * safe = "$" | "-" | "_" | "." | "+"
244 * extra = "!" | "*" | "'" | "(" | ")" | ","
245 * hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
246 * "a" | "b" | "c" | "d" | "e" | "f"
247 * escape = "%" hex hex
248 * unreserved = alpha | digit | safe | extra
249 * uchar = unreserved | escape
250 * user = *[ uchar | ";" | "?" | "&" | "=" ]
251 * password = *[ uchar | ";" | "?" | "&" | "=" ]
252 * and we cannot have a password without user, i.e., no leading ":"
253 * and ":", "@", "/" must be encoded, i.e., will not appear as such.
254 *
255 * However, in a URL
256 * //<user>:<password>@<host>:<port>/<url-path>
257 * valid characters in the host are different, not allowing most of those
258 * punctuation characters.
259 *
260 * RFC-3986 amends this, using
261 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
262 * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
263 * reserved = gen-delims / sub-delims
264 * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
265 * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
266 * / "*" / "+" / "," / ";" / "="
267 * and
268 * host = IP-literal / IPv4address / reg-name
269 * reg-name = *( unreserved / pct-encoded / sub-delims )
270 */
271 char *HTSkipToAt(char *host, int *gen_delims)
272 {
273 char *result = NULL;
274 char *s = host;
275 int pass = 0;
276 int ch;
277 int last = -1;
278
279 *gen_delims = 0;
280 while ((ch = UCH(*s)) != '\0') {
281 if (ch == ':') {
282 if (pass++)
283 break;
284 } else if (ch == '@') {
285 if (s != host && last != ':')
286 result = s;
287 break;
288 } else if (RFC_3986_GEN_DELIMS(ch)) {
289 *gen_delims += 1;
290 if (!RFC_3986_GEN_DELIMS(s[1]))
291 break;
292 } else if (ch == '%') {
293 if (!(isxdigit(UCH(s[1])) && isxdigit(UCH(s[2]))))
294 break;
295 } else if (!(RFC_3986_UNRESERVED(ch) ||
296 RFC_3986_SUB_DELIMS(ch))) {
297 break;
298 }
299 ++s;
300 last = ch;
301 }
302 return result;
303 }
304
305 static char *fake_hostname(char *auth)
306 {
307 char *result = NULL;
308 char *colon = NULL;
309
310 StrAllocCopy(result, auth);
311 if ((colon = strchr(result, ':')) != NULL)
312 *colon = '\0';
313 if (strchr(result, '.') == NULL)
314 FREE(result);
315 return result;
316 }
317
318 /*
319 * Strip any username from the given string so we retain only the host.
320 */
321 void strip_userid(char *host, int parse_only)
322 {
323 int gen_delims = 0;
324 char *p1 = host;
325 char *p2 = HTSkipToAt(host, &gen_delims);
326
327 if (p2 != NULL) {
328 char *msg = NULL;
329 char *auth = NULL;
330 char *fake = NULL;
331 char *p3 = p2;
332 int sub_delims = 0;
333 int my_delimit = UCH(*p2);
334 int do_trimming = (my_delimit == '@');
335
336 *p2++ = '\0';
337
338 StrAllocCopy(auth, host);
339
340 /*
341 * Trailing "gen-delims" demonstrates that there is no user/password.
342 */
343 while ((p3 != host) && RFC_3986_GEN_DELIMS(p3[-1])) {
344 *(--p3) = '\0';
345 }
346 /*
347 * While legal, punctuation-only user/password is questionable.
348 */
349 while ((p3 != host) && RFC_3986_SUB_DELIMS(p3[-1])) {
350 ++sub_delims;
351 *(--p3) = '\0';
352 }
353 /*
354 * Trim trailing "gen-delims" from the real hostname.
355 */
356 for (p3 = p2; *p3 != '\0'; ++p3) {
357 if (RFC_3986_GEN_DELIMS(*p3)) {
358 *p3 = '\0';
359 break;
360 }
361 }
362 CTRACE((tfp, "trim auth: result:`%s'\n", host));
363
364 if (gen_delims || strcmp(host, auth)) {
365 do_trimming = !gen_delims;
366 }
367 if (*host == '\0' && sub_delims) {
368 HTSprintf0(&msg,
369 LY_MSG("User/password contains only punctuation: %s"),
370 auth);
371 } else if ((fake = fake_hostname(host)) != NULL) {
372 HTSprintf0(&msg,
373 LY_MSG("User/password may be confused with hostname: '%s' (e.g, '%s')"),
374 auth, fake);
375 }
376 if (msg != NULL && !parse_only)
377 HTAlert(msg);
378 if (do_trimming) {
379 while ((*p1++ = *p2++) != '\0') {
380 ;
381 }
382 CTRACE((tfp, "trim host: result:`%s'\n", host));
383 }
384 FREE(fake);
385 FREE(auth);
386 FREE(msg);
387 }
388 }
389
390 /*
391 * Check if the user's options specified to use the given encoding. Normally
392 * all encodings with compiled-in support are specified (encodingALL).
393 */
394 static BOOL acceptEncoding(int code)
395 {
396 BOOL result = FALSE;
397
398 if ((code & LYAcceptEncoding) != 0) {
399 const char *program = NULL;
400
401 switch (code) {
402 case encodingGZIP:
403 program = HTGetProgramPath(ppGZIP);
404 break;
405 case encodingDEFLATE:
406 program = HTGetProgramPath(ppINFLATE);
407 break;
408 case encodingCOMPRESS:
409 program = HTGetProgramPath(ppCOMPRESS);
410 break;
411 case encodingBZIP2:
412 program = HTGetProgramPath(ppBZIP2);
413 break;
414 case encodingBROTLI:
415 program = HTGetProgramPath(ppBROTLI);
416 break;
417 case encodingZSTD:
418 program = HTGetProgramPath(ppZSTD);
419 break;
420 default:
421 break;
422 }
423 result = (BOOL) (program != NULL);
424 }
425 return result;
426 }
427
428 /* Load Document from HTTP Server HTLoadHTTP()
429 * ==============================
430 *
431 * Given a hypertext address, this routine loads a document.
432 *
433 *
434 * On entry,
435 * arg is the hypertext reference of the article to be loaded.
436 *
437 * On exit,
438 * returns >=0 If no error, a good socket number
439 * <0 Error.
440 *
441 * The socket must be closed by the caller after the document has been
442 * read.
443 *
444 */
445 static int HTLoadHTTP(const char *arg,
446 HTParentAnchor *anAnchor,
447 HTFormat format_out,
448 HTStream *sink)
449 {
450 static char empty[1];
451 int s; /* Socket number for returned data */
452 const char *url = arg; /* The URL which get_physical() returned */
453 bstring *command = NULL; /* The whole command */
454 char *eol; /* End of line if found */
455 char *start_of_data; /* Start of body of reply */
456 int status; /* tcp return */
457 off_t bytes_already_read;
458 char crlf[3]; /* A CR LF equivalent string */
459 HTStream *target; /* Unconverted data */
460 HTFormat format_in; /* Format arriving in the message */
461 BOOL do_head = FALSE; /* Whether or not we should do a head */
462 BOOL do_post = FALSE; /* ARE WE posting ? */
463 const char *METHOD;
464
465 char *line_buffer = NULL;
466 char *line_kept_clean = NULL;
467
468 #ifdef SH_EX /* FIX BUG by [email protected] */
469 int real_length_of_line = 0;
470 #endif
471 BOOL extensions; /* Assume good HTTP server */
472 char *linebuf = NULL;
473 char temp[80];
474 BOOL first_Accept = TRUE;
475 BOOL show_401 = FALSE;
476 BOOL show_407 = FALSE;
477 BOOL auth_proxy = NO; /* Generate a proxy authorization. - AJL */
478
479 int length, rawlength, rv;
480 int server_status = 0;
481 BOOL doing_redirect, already_retrying = FALSE;
482 int len = 0;
483
484 #ifdef USE_SSL
485 SSL *handle = NULL; /* The SSL handle */
486
487 unsigned long SSLerror;
488 BOOL do_connect = FALSE; /* ARE WE going to use a proxy tunnel ? */
489 BOOL did_connect = FALSE; /* ARE WE actually using a proxy tunnel ? */
490 const char *connect_url = NULL; /* The URL being proxied */
491 char *connect_host = NULL; /* The host being proxied */
492 BOOL try_tls = TRUE;
493 #else
494 void *handle = NULL;
495 #endif /* USE_SSL */
496
497 if (anAnchor->isHEAD)
498 do_head = TRUE;
499 else if (anAnchor->post_data)
500 do_post = TRUE;
501
502 if (!url) {
503 status = -3;
504 _HTProgress(BAD_REQUEST);
505 goto done;
506 }
507 if (!*url) {
508 status = -2;
509 _HTProgress(BAD_REQUEST);
510 goto done;
511 }
512 #ifdef USE_SSL
513 if (using_proxy && !StrNCmp(url, "http://", 7)) {
514 int portnumber;
515
516 if ((connect_url = strstr((url + 7), "https://"))) {
517 do_connect = TRUE;
518 connect_host = HTParse(connect_url, "https", PARSE_HOST);
519 if (!HTParsePort(connect_host, &portnumber)) {
520 sprintf(temp, ":%d", HTTPS_PORT);
521 StrAllocCat(connect_host, temp);
522 }
523 CTRACE((tfp, "HTTP: connect_url = '%s'\n", connect_url));
524 CTRACE((tfp, "HTTP: connect_host = '%s'\n", connect_host));
525 } else if ((connect_url = strstr((url + 7), "snews://"))) {
526 do_connect = TRUE;
527 connect_host = HTParse(connect_url, "snews", PARSE_HOST);
528 if (!HTParsePort(connect_host, &portnumber)) {
529 sprintf(temp, ":%d", SNEWS_PORT);
530 StrAllocCat(connect_host, temp);
531 }
532 CTRACE((tfp, "HTTP: connect_url = '%s'\n", connect_url));
533 CTRACE((tfp, "HTTP: connect_host = '%s'\n", connect_host));
534 }
535 }
536 #endif /* USE_SSL */
537
538 sprintf(crlf, "%c%c", CR, LF);
539
540 /*
541 * At this point, we're talking HTTP/1.0.
542 */
543 extensions = YES;
544
545 try_again:
546 /*
547 * All initializations are moved down here from up above, so we can start
548 * over here...
549 */
550 eol = NULL;
551 length = 0;
552 doing_redirect = FALSE;
553 permanent_redirection = FALSE;
554 redirect_post_content = FALSE;
555 target = NULL;
556 line_buffer = NULL;
557 line_kept_clean = NULL;
558
559 #ifdef USE_SSL
560 if (!StrNCmp(url, "https", 5))
561 status = HTDoConnect(url, "HTTPS", HTTPS_PORT, &s);
562 else
563 status = HTDoConnect(url, "HTTP", HTTP_PORT, &s);
564 #else
565 if (!StrNCmp(url, "https", 5)) {
566 HTAlert(gettext("This client does not contain support for HTTPS URLs."));
567 status = HT_NOT_LOADED;
568 goto done;
569 }
570 status = HTDoConnect(arg, "HTTP", HTTP_PORT, &s);
571 #endif /* USE_SSL */
572 if (status == HT_INTERRUPTED) {
573 /*
574 * Interrupt cleanly.
575 */
576 CTRACE((tfp, "HTTP: Interrupted on connect; recovering cleanly.\n"));
577 _HTProgress(CONNECTION_INTERRUPTED);
578 status = HT_NOT_LOADED;
579 goto done;
580 }
581 if (status < 0) {
582 #ifdef _WINDOWS
583 CTRACE((tfp, "HTTP: Unable to connect to remote host for `%s'\n"
584 " (status = %d, sock_errno = %d).\n",
585 url, status, SOCKET_ERRNO));
586 #else
587 CTRACE((tfp,
588 "HTTP: Unable to connect to remote host for `%s' (errno = %d).\n",
589 url, SOCKET_ERRNO));
590 #endif
591 HTAlert(gettext("Unable to connect to remote host."));
592 status = HT_NOT_LOADED;
593 goto done;
594 }
595 #ifdef USE_SSL
596 use_tunnel:
597 /*
598 * If this is an https document, then do the SSL stuff here.
599 */
600 if (did_connect || !StrNCmp(url, "https", 5)) {
601 int max_version;
602
603 /* If TLS failed, we try again with just SSL */
604 max_version = try_tls ? HTTLS_ANY_VERSION : HTSSL_MAX_VERSION;
605
606 SSL_handle = handle = HTSSLConnect(s, url,
607 HTTLS_ANY_VERSION, max_version);
608
609 if (handle == NULL) {
610 if (did_connect)
611 HTTP_NETCLOSE(s, handle);
612 if (try_tls) {
613 _HTProgress(gettext("Retrying connection without TLS."));
614 try_tls = FALSE;
615 goto try_again;
616 }
617 status = HT_NOT_LOADED;
618 goto done;
619 }
620 }
621 #endif /* USE_SSL */
622
623 /* Ask that node for the document, omitting the host name & anchor
624 */
625 {
626 char *p1 = (HTParse(url, "", PARSE_PATH | PARSE_PUNCTUATION));
627
628 #ifdef USE_SSL
629 if (do_connect) {
630 METHOD = "CONNECT";
631 BStrCopy0(command, "CONNECT ");
632 } else
633 #endif /* USE_SSL */
634 if (do_post) {
635 METHOD = "POST";
636 BStrCopy0(command, "POST ");
637 } else if (do_head) {
638 METHOD = "HEAD";
639 BStrCopy0(command, "HEAD ");
640 } else {
641 METHOD = "GET";
642 BStrCopy0(command, "GET ");
643 }
644
645 /*
646 * If we are using a proxy gateway don't copy in the first slash of
647 * say: /gopher://a;lkdjfl;ajdf;lkj/;aldk/adflj so that just
648 * gopher://.... is sent.
649 */
650 #ifdef USE_SSL
651 if (using_proxy && !did_connect) {
652 if (do_connect)
653 BStrCat0(command, connect_host);
654 else
655 BStrCat0(command, p1 + 1);
656 }
657 #else
658 if (using_proxy)
659 BStrCat0(command, p1 + 1);
660 #endif /* USE_SSL */
661 else
662 BStrCat0(command, p1);
663 FREE(p1);
664 }
665 if (extensions) {
666 BStrCat0(command, " ");
667 BStrCat0(command, ((HTprotocolLevel == HTTP_1_0)
668 ? "HTTP/1.0"
669 : "HTTP/1.1"));
670 }
671
672 BStrCat0(command, crlf); /* CR LF, as in rfc 977 */
673
674 if (extensions) {
675 int n, i;
676 char *host = NULL;
677
678 if ((host = HTParse(anAnchor->address, "", PARSE_HOST)) != NULL) {
679 strip_userid(host, TRUE);
680 HTBprintf(&command, "Host: %s%c%c", host, CR, LF);
681 FREE(host);
682 }
683 if (HTprotocolLevel >= HTTP_1_1) {
684 HTBprintf(&command, "Connection: close%c%c", CR, LF);
685 }
686
687 if (!HTPresentations)
688 HTFormatInit();
689 n = HTList_count(HTPresentations);
690
691 first_Accept = TRUE;
692 len = 0;
693 for (i = 0; i < n; i++) {
694 HTPresentation *pres =
695 (HTPresentation *) HTList_objectAt(HTPresentations, i);
696
697 if (pres->get_accept) {
698 if (pres->quality < 1.0) {
699 if (pres->maxbytes > 0) {
700 sprintf(temp, ";q=%4.3f;mxb=%" PRI_off_t "",
701 pres->quality, CAST_off_t (pres->maxbytes));
702 } else {
703 sprintf(temp, ";q=%4.3f", pres->quality);
704 }
705 } else if (pres->maxbytes > 0) {
706 sprintf(temp, ";mxb=%" PRI_off_t "", CAST_off_t (pres->maxbytes));
707 } else {
708 temp[0] = '\0';
709 }
710 HTSprintf0(&linebuf, "%s%s%s",
711 (first_Accept ?
712 "Accept: " : ", "),
713 HTAtom_name(pres->rep),
714 temp);
715 len += (int) strlen(linebuf);
716 if (len > 252 && !first_Accept) {
717 BStrCat0(command, crlf);
718 HTSprintf0(&linebuf, "Accept: %s%s",
719 HTAtom_name(pres->rep),
720 temp);
721 len = (int) strlen(linebuf);
722 }
723 BStrCat0(command, linebuf);
724 first_Accept = FALSE;
725 }
726 }
727 HTBprintf(&command, "%s*/*;q=0.01%c%c",
728 (first_Accept ?
729 "Accept: " : ", "), CR, LF);
730
731 /*
732 * FIXME: suppressing the "Accept-Encoding" in this case is done to
733 * work around limitations of the presentation logic used for the
734 * command-line "-base" option. The remote site may transmit the
735 * document gzip'd, but the ensuing logic in HTSaveToFile() would see
736 * the mime-type as gzip rather than text/html, and not prepend the
737 * base URL. This is less efficient than accepting the compressed data
738 * and uncompressing it, adding the base URL but is simpler than
739 * augmenting the dump's presentation logic -TD
740 */
741 if (LYPrependBaseToSource && dump_output_immediately) {
742 CTRACE((tfp,
743 "omit Accept-Encoding to work-around interaction with -source\n"));
744 } else {
745 char *list = NULL;
746 int j, k;
747
748 for (j = 1; j < encodingALL; j <<= 1) {
749 if (acceptEncoding(j)) {
750 for (k = 0; tbl_preferred_encoding[k].name != NULL; ++k) {
751 if (tbl_preferred_encoding[k].value == j) {
752 if (list != NULL)
753 StrAllocCat(list, ", ");
754 StrAllocCat(list, tbl_preferred_encoding[k].name);
755 break;
756 }
757 }
758 }
759 }
760
761 if (list != NULL) {
762 HTBprintf(&command, "Accept-Encoding: %s%c%c", list, CR, LF);
763 free(list);
764 }
765 }
766
767 if (non_empty(language)) {
768 HTBprintf(&command, "Accept-Language: %s%c%c", language, CR, LF);
769 }
770
771 if (non_empty(pref_charset)) {
772 BStrCat0(command, "Accept-Charset: ");
773 StrAllocCopy(linebuf, pref_charset);
774 if (linebuf[strlen(linebuf) - 1] == ',')
775 linebuf[strlen(linebuf) - 1] = '\0';
776 LYLowerCase(linebuf);
777 if (strstr(linebuf, "iso-8859-1") == NULL)
778 StrAllocCat(linebuf, ", iso-8859-1;q=0.01");
779 if (strstr(linebuf, "us-ascii") == NULL)
780 StrAllocCat(linebuf, ", us-ascii;q=0.01");
781 BStrCat0(command, linebuf);
782 HTBprintf(&command, "%c%c", CR, LF);
783 }
784 #if 0
785 /*
786 * Promote 300 (Multiple Choices) replies, if supported, over 406 (Not
787 * Acceptable) replies. - FM
788 *
789 * This used to be done in versions 2.7 and 2.8*, but violates the
790 * specs for transparent content negotiation and has the effect that
791 * servers supporting those specs will send 300 (Multiple Choices)
792 * instead of a normal response (e.g. 200 OK), since they will assume
793 * that the client wants to make the choice. It is not clear whether
794 * there are any servers or sites for which sending this header really
795 * improves anything.
796 *
797 * If there ever is a need to send "Negotiate: trans" and really mean
798 * it, we should send "Negotiate: trans,trans" or similar, since that
799 * is semantically equivalent and some servers may ignore "Negotiate:
800 * trans" as a special case when it comes from Lynx (to work around the
801 * old faulty behavior). - kw
802 *
803 * References:
804 * RFC 2295 (see also RFC 2296), and mail to lynx-dev and
805 * [email protected] from Koen Holtman, Jan 1999.
806 */
807 if (!do_post) {
808 HTBprintf(&command, "Negotiate: trans%c%c", CR, LF);
809 }
810 #endif /* 0 */
811
812 /*
813 * When reloading give no-cache pragma to proxy server to make it
814 * refresh its cache. -- Ari L. <[email protected]>
815 *
816 * Also send it as a Cache-Control header for HTTP/1.1. - FM
817 */
818 if (reloading) {
819 HTBprintf(&command, "Pragma: no-cache%c%c", CR, LF);
820 HTBprintf(&command, "Cache-Control: no-cache%c%c", CR, LF);
821 }
822
823 if (LYSendUserAgent || no_useragent) {
824 if (non_empty(LYUserAgent)) {
825 char *cp = LYSkipBlanks(LYUserAgent);
826
827 /* Won't send it at all if all blank - kw */
828 if (*cp != '\0')
829 HTBprintf(&command, "User-Agent: %.*s%c%c",
830 INIT_LINE_SIZE - 15, LYUserAgent, CR, LF);
831 } else {
832 HTBprintf(&command, "User-Agent: %s/%s libwww-FM/%s%c%c",
833 HTAppName ? HTAppName : "unknown",
834 HTAppVersion ? HTAppVersion : "0.0",
835 HTLibraryVersion, CR, LF);
836 }
837 }
838
839 if (non_empty(personal_mail_address) && !LYNoFromHeader) {
840 HTBprintf(&command, "From: %s%c%c", personal_mail_address, CR, LF);
841 }
842
843 if (!(LYUserSpecifiedURL ||
844 LYNoRefererHeader || LYNoRefererForThis) &&
845 strcmp(HTLoadedDocumentURL(), "")) {
846 const char *cp = LYRequestReferer;
847
848 if (!cp)
849 cp = HTLoadedDocumentURL(); /* @@@ Try both? - kw */
850 BStrCat0(command, "Referer: ");
851 if (isLYNXIMGMAP(cp)) {
852 char *pound = findPoundSelector(cp);
853 int nn = (pound ? (int) (pound - cp) : (int) strlen(cp));
854
855 HTSABCat(&command, cp + LEN_LYNXIMGMAP, nn);
856 } else {
857 BStrCat0(command, cp);
858 }
859 HTBprintf(&command, "%c%c", CR, LF);
860 } {
861 char *abspath;
862 char *docname;
863 char *hostname;
864 char *colon;
865 int portnumber;
866 char *auth, *cookie = NULL;
867 BOOL secure = (BOOL) (StrNCmp(anAnchor->address, "https", 5)
868 ? FALSE
869 : TRUE);
870
871 abspath = HTParse(arg, "", PARSE_PATH | PARSE_PUNCTUATION);
872 docname = HTParse(arg, "", PARSE_PATH);
873 hostname = HTParse(arg, "", PARSE_HOST);
874 if (hostname &&
875 NULL != (colon = HTParsePort(hostname, &portnumber))) {
876 *colon = '\0'; /* Chop off port number */
877 } else if (!StrNCmp(arg, "https", 5)) {
878 portnumber = HTTPS_PORT;
879 } else {
880 portnumber = HTTP_PORT;
881 }
882
883 /*
884 * Add Authorization, Proxy-Authorization, and/or Cookie headers,
885 * if applicable.
886 */
887 if (using_proxy) {
888 /*
889 * If we are using a proxy, first determine if we should
890 * include an Authorization header and/or Cookie header for the
891 * ultimate target of this request. - FM & AJL
892 */
893 char *host2 = NULL, *path2 = NULL;
894 int port2 = (StrNCmp(docname, "https", 5) ?
895 HTTP_PORT : HTTPS_PORT);
896
897 host2 = HTParse(docname, "", PARSE_HOST);
898 path2 = HTParse(docname, "", PARSE_PATH | PARSE_PUNCTUATION);
899 if ((colon = HTParsePort(host2, &port2)) != NULL) {
900 /* Use non-default port number */
901 *colon = '\0';
902 }
903
904 /*
905 * This composeAuth() does file access, i.e., for the ultimate
906 * target of the request. - AJL
907 */
908 auth_proxy = NO;
909 auth = HTAA_composeAuth(host2, port2, path2, auth_proxy);
910 if (auth == NULL) {
911 CTRACE((tfp, "HTTP: Not sending authorization (yet).\n"));
912 } else if (*auth != '\0') {
913 /*
914 * We have an Authorization header to be included.
915 */
916 HTBprintf(&command, "%s%c%c", auth, CR, LF);
917 CTRACE((tfp, "HTTP: Sending authorization: %s\n", auth));
918 } else {
919 /*
920 * The user either cancelled or made a mistake with the
921 * username and password prompt.
922 */
923 if (!(traversal || dump_output_immediately) &&
924 HTConfirm(CONFIRM_WO_PASSWORD)) {
925 show_401 = TRUE;
926 } else {
927 if (traversal || dump_output_immediately)
928 HTAlert(FAILED_NEED_PASSWD);
929 #ifdef USE_SSL
930 if (did_connect)
931 HTTP_NETCLOSE(s, handle);
932 #endif /* USE_SSL */
933 BStrFree(command);
934 FREE(hostname);
935 FREE(docname);
936 FREE(abspath);
937 FREE(host2);
938 FREE(path2);
939 status = HT_NOT_LOADED;
940 goto done;
941 }
942 }
943 /*
944 * Add 'Cookie:' header, if it's HTTP or HTTPS document being
945 * proxied.
946 */
947 if (!StrNCmp(docname, "http", 4)) {
948 cookie = LYAddCookieHeader(host2, path2, port2, secure);
949 }
950 FREE(host2);
951 FREE(path2);
952 /*
953 * The next composeAuth() will be for the proxy. - AJL
954 */
955 auth_proxy = YES;
956 } else {
957 /*
958 * Add cookie for a non-proxied request. - FM
959 */
960 cookie = LYAddCookieHeader(hostname, abspath, portnumber, secure);
961 auth_proxy = NO;
962 }
963 /*
964 * If we do have a cookie set, add it to the request buffer. - FM
965 */
966 if (cookie != NULL) {
967 if (*cookie != '$' && USE_RFC_2965) {
968 /*
969 * It's a historical cookie, so signal to the server that
970 * we support modern cookies. - FM
971 */
972 BStrCat0(command, "Cookie2: $Version=\"1\"");
973 BStrCat0(command, crlf);
974 CTRACE((tfp, "HTTP: Sending Cookie2: $Version =\"1\"\n"));
975 }
976 if (*cookie != '\0') {
977 /*
978 * It's not a zero-length string, so add the header. Note
979 * that any folding of long strings has been done already
980 * in LYCookie.c. - FM
981 */
982 BStrCat0(command, "Cookie: ");
983 BStrCat0(command, cookie);
984 BStrCat0(command, crlf);
985 CTRACE((tfp, "HTTP: Sending Cookie: %s\n", cookie));
986 }
987 FREE(cookie);
988 }
989 FREE(abspath);
990
991 /*
992 * If we are using a proxy, auth_proxy should be YES, and we check
993 * here whether we want a Proxy-Authorization header for it. If we
994 * are not using a proxy, auth_proxy should still be NO, and we
995 * check here for whether we want an Authorization header. - FM &
996 * AJL
997 */
998 if ((auth = HTAA_composeAuth(hostname,
999 portnumber,
1000 docname,
1001 auth_proxy)) != NULL &&
1002 *auth != '\0') {
1003 /*
1004 * If auth is not NULL nor zero-length, it's an Authorization
1005 * or Proxy-Authorization header to be included. - FM
1006 */
1007 HTBprintf(&command, "%s%c%c", auth, CR, LF);
1008 CTRACE((tfp, (auth_proxy ?
1009 "HTTP: Sending proxy authorization: %s\n" :
1010 "HTTP: Sending authorization: %s\n"),
1011 auth));
1012 } else if (auth && *auth == '\0') {
1013 /*
1014 * If auth is a zero-length string, the user either cancelled
1015 * or goofed at the username and password prompt. - FM
1016 */
1017 if (!(traversal || dump_output_immediately) && HTConfirm(CONFIRM_WO_PASSWORD)) {
1018 if (auth_proxy == TRUE) {
1019 show_407 = TRUE;
1020 } else {
1021 show_401 = TRUE;
1022 }
1023 } else {
1024 if (traversal || dump_output_immediately)
1025 HTAlert(FAILED_NEED_PASSWD);
1026 BStrFree(command);
1027 FREE(hostname);
1028 FREE(docname);
1029 status = HT_NOT_LOADED;
1030 goto done;
1031 }
1032 } else {
1033 CTRACE((tfp, (auth_proxy ?
1034 "HTTP: Not sending proxy authorization (yet).\n" :
1035 "HTTP: Not sending authorization (yet).\n")));
1036 }
1037 FREE(hostname);
1038 FREE(docname);
1039 }
1040 }
1041
1042 if (
1043 #ifdef USE_SSL
1044 !do_connect &&
1045 #endif /* USE_SSL */
1046 do_post) {
1047 CTRACE((tfp, "HTTP: Doing post, content-type '%s'\n",
1048 anAnchor->post_content_type
1049 ? anAnchor->post_content_type
1050 : "lose"));
1051 HTBprintf(&command, "Content-Type: %s%c%c",
1052 anAnchor->post_content_type
1053 ? anAnchor->post_content_type
1054 : "lose",
1055 CR, LF);
1056
1057 HTBprintf(&command, "Content-Length: %d%c%c",
1058 !isBEmpty(anAnchor->post_data)
1059 ? BStrLen(anAnchor->post_data)
1060 : 0,
1061 CR, LF);
1062
1063 BStrCat0(command, crlf); /* Blank line means "end" of headers */
1064
1065 BStrCat(command, anAnchor->post_data);
1066 } else
1067 BStrCat0(command, crlf); /* Blank line means "end" of headers */
1068
1069 if (TRACE) {
1070 CTRACE((tfp, "Writing:\n"));
1071 trace_bstring(command);
1072 #ifdef USE_SSL
1073 CTRACE((tfp, "%s",
1074 (anAnchor->post_data && !do_connect ? crlf : "")));
1075 #else
1076 CTRACE((tfp, "%s",
1077 (anAnchor->post_data ? crlf : "")));
1078 #endif /* USE_SSL */
1079 CTRACE((tfp, "----------------------------------\n"));
1080 }
1081
1082 _HTProgress(gettext("Sending HTTP request."));
1083
1084 #ifdef NOT_ASCII /* S/390 -- gil -- 0548 */
1085 {
1086 char *p2;
1087
1088 for (p2 = BStrData(command);
1089 p2 < BStrData(command) + BStrLen(command);
1090 p2++)
1091 *p2 = TOASCII(*p2);
1092 }
1093 #endif /* NOT_ASCII */
1094 status = (int) HTTP_NETWRITE(s,
1095 BStrData(command),
1096 BStrLen(command),
1097 handle);
1098 BStrFree(command);
1099 FREE(linebuf);
1100 if (status <= 0) {
1101 if (status == 0) {
1102 CTRACE((tfp, "HTTP: Got status 0 in initial write\n"));
1103 /* Do nothing. */
1104 } else if ((SOCKET_ERRNO == ENOTCONN ||
1105 SOCKET_ERRNO == ECONNRESET ||
1106 SOCKET_ERRNO == EPIPE) &&
1107 !already_retrying &&
1108 /* Don't retry if we're posting. */ !do_post) {
1109 /*
1110 * Arrrrgh, HTTP 0/1 compatibility problem, maybe.
1111 */
1112 CTRACE((tfp,
1113 "HTTP: BONZO ON WRITE Trying again with HTTP0 request.\n"));
1114 _HTProgress(RETRYING_AS_HTTP0);
1115 HTTP_NETCLOSE(s, handle);
1116 extensions = NO;
1117 already_retrying = TRUE;
1118 goto try_again;
1119 } else {
1120 CTRACE((tfp,
1121 "HTTP: Hit unexpected network WRITE error; aborting connection.\n"));
1122 HTTP_NETCLOSE(s, handle);
1123 status = -1;
1124 HTAlert(gettext("Unexpected network write error; connection aborted."));
1125 goto done;
1126 }
1127 }
1128
1129 CTRACE((tfp, "HTTP: WRITE delivered OK\n"));
1130 _HTProgress(gettext("HTTP request sent; waiting for response."));
1131
1132 /* Read the first line of the response
1133 * -----------------------------------
1134 */
1135 {
1136 /* Get numeric status etc */
1137 BOOL end_of_file = NO;
1138 int buffer_length = INIT_LINE_SIZE;
1139
1140 line_buffer = typecallocn(char, (size_t) buffer_length);
1141
1142 if (line_buffer == NULL)
1143 outofmem(__FILE__, "HTLoadHTTP");
1144
1145 HTReadProgress(bytes_already_read = 0, (off_t) 0);
1146 do { /* Loop to read in the first line */
1147 /*
1148 * Extend line buffer if necessary for those crazy WAIS URLs ;-)
1149 */
1150 if (buffer_length - length < LINE_EXTEND_THRESH) {
1151 buffer_length = buffer_length + buffer_length;
1152 line_buffer =
1153 (char *) realloc(line_buffer, ((unsigned) buffer_length *
1154 sizeof(char)));
1155
1156 if (line_buffer == NULL)
1157 outofmem(__FILE__, "HTLoadHTTP");
1158 }
1159 CTRACE((tfp, "HTTP: Trying to read %d\n", buffer_length - length - 1));
1160 status = HTTP_NETREAD(s,
1161 line_buffer + length,
1162 (buffer_length - length - 1),
1163 handle);
1164 CTRACE((tfp, "HTTP: Read %d\n", status));
1165 if (status <= 0) {
1166 /*
1167 * Retry if we get nothing back too.
1168 * Bomb out if we get nothing twice.
1169 */
1170 if (status == HT_INTERRUPTED) {
1171 CTRACE((tfp, "HTTP: Interrupted initial read.\n"));
1172 _HTProgress(CONNECTION_INTERRUPTED);
1173 HTTP_NETCLOSE(s, handle);
1174 status = HT_NO_DATA;
1175 goto clean_up;
1176 } else if (status < 0 &&
1177 (SOCKET_ERRNO == ENOTCONN ||
1178 #ifdef _WINDOWS /* 1997/11/09 (Sun) 16:59:58 */
1179 SOCKET_ERRNO == ETIMEDOUT ||
1180 #endif
1181 SOCKET_ERRNO == ECONNRESET ||
1182 SOCKET_ERRNO == EPIPE) &&
1183 !already_retrying && !do_post) {
1184 /*
1185 * Arrrrgh, HTTP 0/1 compatibility problem, maybe.
1186 */
1187 CTRACE((tfp,
1188 "HTTP: BONZO Trying again with HTTP0 request.\n"));
1189 HTTP_NETCLOSE(s, handle);
1190 FREE(line_buffer);
1191 FREE(line_kept_clean);
1192
1193 extensions = NO;
1194 already_retrying = TRUE;
1195 _HTProgress(RETRYING_AS_HTTP0);
1196 goto try_again;
1197 }
1198 #ifdef USE_SSL
1199 else if ((SSLerror = ERR_get_error()) != 0) {
1200 CTRACE((tfp,
1201 "HTTP: Hit unexpected network read error; aborting connection; status %d:%s.\n",
1202 status, ERR_error_string(SSLerror, NULL)));
1203 HTAlert(gettext("Unexpected network read error; connection aborted."));
1204 HTTP_NETCLOSE(s, handle);
1205 status = -1;
1206 goto clean_up;
1207 }
1208 #endif
1209 else {
1210 CTRACE((tfp,
1211 "HTTP: Hit unexpected network read error; aborting connection; status %d.\n",
1212 status));
1213 HTAlert(gettext("Unexpected network read error; connection aborted."));
1214 HTTP_NETCLOSE(s, handle);
1215 status = -1;
1216 goto clean_up;
1217 }
1218 }
1219 #ifdef NOT_ASCII /* S/390 -- gil -- 0564 */
1220 {
1221 char *p2;
1222
1223 for (p2 = line_buffer + length;
1224 p2 < line_buffer + length + status;
1225 p2++)
1226 *p2 = FROMASCII(*p2);
1227 }
1228 #endif /* NOT_ASCII */
1229
1230 bytes_already_read += status;
1231 HTReadProgress(bytes_already_read, (off_t) 0);
1232
1233 #ifdef UCX /* UCX returns -1 on EOF */
1234 if (status == 0 || status == -1)
1235 #else
1236 if (status == 0)
1237 #endif
1238 {
1239 break;
1240 }
1241 line_buffer[length + status] = 0;
1242
1243 if (line_buffer) {
1244 FREE(line_kept_clean);
1245 line_kept_clean = (char *) malloc((unsigned) buffer_length *
1246 sizeof(char));
1247
1248 if (line_kept_clean == NULL)
1249 outofmem(__FILE__, "HTLoadHTTP");
1250 MemCpy(line_kept_clean, line_buffer, buffer_length);
1251 #ifdef SH_EX /* FIX BUG by [email protected] */
1252 real_length_of_line = length + status;
1253 #endif
1254 }
1255
1256 eol = StrChr(line_buffer + length, LF);
1257 /* Do we *really* want to do this? */
1258 if (eol && eol != line_buffer && *(eol - 1) == CR)
1259 *(eol - 1) = ' ';
1260
1261 length = length + status;
1262
1263 /* Do we really want to do *this*? */
1264 if (eol)
1265 *eol = 0; /* Terminate the line */
1266 }
1267 /* All we need is the first line of the response. If it's a HTTP/1.0
1268 * response, then the first line will be absurdly short and therefore
1269 * we can safely gate the number of bytes read through this code (as
1270 * opposed to below) to ~1000.
1271 *
1272 * Well, let's try 100.
1273 */
1274 while (!eol && !end_of_file && bytes_already_read < 100);
1275 } /* Scope of loop variables */
1276
1277 /* save total length, in case we decide later to show it all - kw */
1278 rawlength = length;
1279
1280 /* We now have a terminated unfolded line. Parse it.
1281 * --------------------------------------------------
1282 */
1283 CTRACE((tfp, "HTTP: Rx: %s\n", line_buffer));
1284
1285 /*
1286 * Kludge to work with old buggy servers and the VMS Help gateway. They
1287 * can't handle the third word, so we try again without it.
1288 */
1289 if (extensions && /* Old buggy server or Help gateway? */
1290 (0 == StrNCmp(line_buffer, "<TITLE>Bad File Request</TITLE>", 31) ||
1291 0 == StrNCmp(line_buffer, "Address should begin with", 25) ||
1292 0 == StrNCmp(line_buffer, "<TITLE>Help ", 12) ||
1293 0 == strcmp(line_buffer,
1294 "Document address invalid or access not authorised"))) {
1295 FREE(line_buffer);
1296 FREE(line_kept_clean);
1297 extensions = NO;
1298 already_retrying = TRUE;
1299 CTRACE((tfp, "HTTP: close socket %d to retry with HTTP0\n", s));
1300 HTTP_NETCLOSE(s, handle);
1301 /* print a progress message */
1302 _HTProgress(RETRYING_AS_HTTP0);
1303 goto try_again;
1304 } {
1305 int fields;
1306 char server_version[VERSION_LENGTH + 1];
1307
1308 server_version[0] = 0;
1309
1310 fields = sscanf(line_buffer, "%20s %d",
1311 server_version,
1312 &server_status);
1313
1314 CTRACE((tfp, "HTTP: Scanned %d fields from line_buffer\n", fields));
1315
1316 if (non_empty(http_error_file)) {
1317 /* Make the status code externally available */
1318 FILE *error_file;
1319
1320 #ifdef SERVER_STATUS_ONLY
1321 error_file = fopen(http_error_file, TXT_W);
1322 if (error_file) { /* Managed to open the file */
1323 fprintf(error_file, "error=%d\n", server_status);
1324 fclose(error_file);
1325 }
1326 #else
1327 error_file = fopen(http_error_file, TXT_A);
1328 if (error_file) { /* Managed to open the file */
1329 fprintf(error_file, " URL=%s (%s)\n", url, METHOD);
1330 fprintf(error_file, "STATUS=%s\n", line_buffer);
1331 fclose(error_file);
1332 }
1333 #endif /* SERVER_STATUS_ONLY */
1334 }
1335
1336 /*
1337 * Rule out a non-HTTP/1.n reply as best we can.
1338 */
1339 if (fields < 2 || !server_version[0] || server_version[0] != 'H' ||
1340 server_version[1] != 'T' || server_version[2] != 'T' ||
1341 server_version[3] != 'P' || server_version[4] != '/' ||
1342 server_version[6] != '.') {
1343 /*
1344 * Ugh! An HTTP0 reply,
1345 */
1346 HTAtom *encoding;
1347
1348 CTRACE((tfp, "--- Talking HTTP0.\n"));
1349
1350 format_in = HTFileFormat(url, &encoding, NULL);
1351 /*
1352 * Treat all plain text as HTML. This sucks but its the only
1353 * solution without without looking at content.
1354 */
1355 if (!StrNCmp(HTAtom_name(format_in), STR_PLAINTEXT, 10)) {
1356 CTRACE((tfp, "HTTP: format_in being changed to text/HTML\n"));
1357 format_in = WWW_HTML;
1358 }
1359 if (!IsUnityEnc(encoding)) {
1360 /*
1361 * Change the format to that for "www/compressed".
1362 */
1363 CTRACE((tfp, "HTTP: format_in is '%s',\n", HTAtom_name(format_in)));
1364 StrAllocCopy(anAnchor->content_type, HTAtom_name(format_in));
1365 StrAllocCopy(anAnchor->content_encoding, HTAtom_name(encoding));
1366 format_in = HTAtom_for("www/compressed");
1367 CTRACE((tfp, " Treating as '%s' with encoding '%s'\n",
1368 "www/compressed", HTAtom_name(encoding)));
1369 }
1370
1371 start_of_data = line_kept_clean;
1372 } else {
1373 /*
1374 * Set up to decode full HTTP/1.n response. - FM
1375 */
1376 format_in = HTAtom_for("www/mime");
1377 CTRACE((tfp, "--- Talking HTTP1.\n"));
1378
1379 /*
1380 * We set start_of_data to "" when !eol here because there will be
1381 * a put_block done below; we do *not* use the value of
1382 * start_of_data (as a pointer) in the computation of length (or
1383 * anything else) when !eol. Otherwise, set the value of length to
1384 * what we have beyond eol (i.e., beyond the status line). - FM
1385 */
1386 if (eol != NULL) {
1387 start_of_data = (eol + 1);
1388 } else {
1389 start_of_data = empty;
1390 }
1391 length = (eol
1392 ? length - (int) (start_of_data - line_buffer)
1393 : 0);
1394
1395 /*
1396 * Trim trailing spaces in line_buffer so that we can use it in
1397 * messages which include the status line. - FM
1398 */
1399 while (line_buffer[strlen(line_buffer) - 1] == ' ')
1400 line_buffer[strlen(line_buffer) - 1] = '\0';
1401
1402 /*
1403 * Take appropriate actions based on the status. - FM
1404 */
1405 switch (server_status / 100) {
1406 case 1:
1407 /*
1408 * HTTP/1.1 Informational statuses.
1409 * 100 Continue.
1410 * 101 Switching Protocols.
1411 * > 101 is unknown.
1412 * We should never get these, and they have only the status
1413 * line and possibly other headers, so we'll deal with them by
1414 * showing the full header to the user as text/plain. - FM
1415 */
1416 HTAlert(gettext("Got unexpected Informational Status."));
1417 do_head = TRUE;
1418 break;
1419
1420 case 2:
1421 /*
1422 * Good: Got MIME object! (Successful) - FM
1423 */
1424 if (do_head) {
1425 /*
1426 * If HEAD was requested, show headers (and possibly bogus
1427 * body) for all 2xx status codes as text/plain - KW
1428 */
1429 HTProgress(line_buffer);
1430 break;
1431 }
1432 switch (server_status) {
1433 case 204:
1434 /*
1435 * No Content.
1436 */
1437 HTAlert(line_buffer);
1438 HTTP_NETCLOSE(s, handle);
1439 HTNoDataOK = 1;
1440 status = HT_NO_DATA;
1441 goto clean_up;
1442
1443 case 205:
1444 /*
1445 * Reset Content. The server has fulfilled the request but
1446 * nothing is returned and we should reset any form
1447 * content. We'll instruct the user to do that, and
1448 * restore the current document. - FM
1449 */
1450 HTAlert(gettext("Request fulfilled. Reset Content."));
1451 HTTP_NETCLOSE(s, handle);
1452 status = HT_NO_DATA;
1453 goto clean_up;
1454
1455 case 206:
1456 /*
1457 * Partial Content. We didn't send a Range so something
1458 * went wrong somewhere. Show the status message and
1459 * restore the current document. - FM
1460 */
1461 HTAlert(line_buffer);
1462 HTTP_NETCLOSE(s, handle);
1463 status = HT_NO_DATA;
1464 goto clean_up;
1465
1466 default:
1467 /*
1468 * 200 OK.
1469 * 201 Created.
1470 * 202 Accepted.
1471 * 203 Non-Authoritative Information.
1472 * > 206 is unknown.
1473 * All should return something to display.
1474 */
1475 #if defined(USE_SSL) /* && !defined(DISABLE_NEWS) _H */
1476 if (do_connect) {
1477 CTRACE((tfp,
1478 "HTTP: Proxy tunnel to '%s' established.\n",
1479 connect_host));
1480 do_connect = FALSE;
1481 url = connect_url;
1482 FREE(line_buffer);
1483 FREE(line_kept_clean);
1484 #ifndef DISABLE_NEWS
1485 if (!StrNCmp(connect_url, "snews", 5)) {
1486 CTRACE((tfp,
1487 " Will attempt handshake and snews connection.\n"));
1488 status = HTNewsProxyConnect(s, url, anAnchor,
1489 format_out, sink);
1490 goto done;
1491 }
1492 #endif /* DISABLE_NEWS */
1493 did_connect = TRUE;
1494 already_retrying = TRUE;
1495 eol = NULL;
1496 length = 0;
1497 doing_redirect = FALSE;
1498 permanent_redirection = FALSE;
1499 target = NULL;
1500 CTRACE((tfp,
1501 " Will attempt handshake and resubmit headers.\n"));
1502 goto use_tunnel;
1503 }
1504 #endif /* USE_SSL */
1505 HTProgress(line_buffer);
1506 } /* case 2 switch */
1507 break;
1508
1509 case 3:
1510 /*
1511 * Various forms of Redirection. - FM
1512 * 300 Multiple Choices.
1513 * 301 Moved Permanently.
1514 * 302 Found (temporary; we can, and do, use GET).
1515 * 303 See Other (temporary; always use GET).
1516 * 304 Not Modified.
1517 * 305 Use Proxy.
1518 * 306 Set Proxy.
1519 * 307 Temporary Redirect with method retained.
1520 * 308 Permanent Redirect
1521 * > 309 is unknown.
1522 */
1523 if (no_url_redirection || do_head || keep_mime_headers) {
1524 /*
1525 * If any of these flags are set, we do not redirect, but
1526 * instead show what was returned to the user as
1527 * text/plain. - FM
1528 */
1529 HTProgress(line_buffer);
1530 break;
1531 }
1532
1533 if (server_status == 300) { /* Multiple Choices */
1534 /*
1535 * For client driven content negotiation. The server
1536 * should be sending some way for the user-agent to make a
1537 * selection, so we'll show the user whatever the server
1538 * returns. There might be a Location: header with the
1539 * server's preference present, but the choice should be up
1540 * to the user, someday based on an Alternates: header,
1541 * and a body always should be present with descriptions
1542 * and links for the choices (i.e., we use the latter, for
1543 * now). - FM
1544 */
1545 HTAlert(line_buffer);
1546 if (traversal) {
1547 HTTP_NETCLOSE(s, handle);
1548 status = -1;
1549 goto clean_up;
1550 }
1551 if (!dump_output_immediately &&
1552 format_out == WWW_DOWNLOAD) {
1553 /*
1554 * Convert a download request to a presentation request
1555 * for interactive users. - FM
1556 */
1557 format_out = WWW_PRESENT;
1558 }
1559 break;
1560 }
1561
1562 if (server_status == 304) { /* Not Modified */
1563 /*
1564 * We didn't send an "If-Modified-Since" header, so this
1565 * status is inappropriate. We'll deal with it by showing
1566 * the full header to the user as text/plain. - FM
1567 */
1568 HTAlert(gettext("Got unexpected 304 Not Modified status."));
1569 do_head = TRUE;
1570 break;
1571 }
1572
1573 if (server_status == 305 ||
1574 server_status == 306 ||
1575 server_status > 308) {
1576 /*
1577 * Show user the content, if any, for 305, 306, or unknown
1578 * status. - FM
1579 */
1580 HTAlert(line_buffer);
1581 if (traversal) {
1582 HTTP_NETCLOSE(s, handle);
1583 status = -1;
1584 goto clean_up;
1585 }
1586 if (!dump_output_immediately &&
1587 format_out == WWW_DOWNLOAD) {
1588 /*
1589 * Convert a download request to a presentation request
1590 * for interactive users. - FM
1591 */
1592 format_out = WWW_PRESENT;
1593 }
1594 break;
1595 }
1596
1597 /*
1598 * We do not load the file, but read the headers for the
1599 * "Location:", check out that redirecting_url and if it's
1600 * acceptable (e.g., not a telnet URL when we have that
1601 * disabled), initiate a new fetch. If that's another
1602 * redirecting_url, we'll repeat the checks, and fetch
1603 * initiations if acceptable, until we reach the actual URL, or
1604 * the redirection limit set in HTAccess.c is exceeded. If the
1605 * status was 301 indicating that the relocation is permanent,
1606 * we set the permanent_redirection flag to make it permanent
1607 * for the current anchor tree (i.e., will persist until the
1608 * tree is freed or the client exits). If the redirection
1609 * would include POST content, we seek confirmation from an
1610 * interactive user, with option to use 303 for 301 (but not
1611 * for 307), and otherwise refuse the redirection. We also
1612 * don't allow permanent redirection if we keep POST content.
1613 * If we don't find the Location header or its value is
1614 * zero-length, we display whatever the server returned, and
1615 * the user should RELOAD that to try again, or make a
1616 * selection from it if it contains links, or Left-Arrow to the
1617 * previous document. - FM
1618 */
1619 {
1620 if ((dump_output_immediately || traversal) &&
1621 do_post &&
1622 server_status != 303 &&
1623 server_status != 302 &&
1624 server_status != 301) {
1625 /*
1626 * Don't redirect POST content without approval from an
1627 * interactive user. - FM
1628 */
1629 HTTP_NETCLOSE(s, handle);
1630 status = -1;
1631 HTAlert(gettext("Redirection of POST content requires user approval."));
1632 if (traversal)
1633 HTProgress(line_buffer);
1634 goto clean_up;
1635 }
1636
1637 HTProgress(line_buffer);
1638 if ((server_status == 301) || /* Moved Permanently */
1639 (server_status == 308)) { /* Permanent Redirect */
1640 if (do_post) {
1641 /*
1642 * Don't make the redirection permanent if we have
1643 * POST content. - FM
1644 */
1645 CTRACE((tfp,
1646 "HTTP: Have POST content. Treating 301 (Permanent) as Temporary.\n"));
1647 HTAlert(gettext("Have POST content. Treating Permanent Redirection as Temporary.\n"));
1648 } else {
1649 permanent_redirection = TRUE;
1650 }
1651 }
1652 doing_redirect = TRUE;
1653
1654 break;
1655 }
1656
1657 case 4:
1658 /*
1659 * "I think I goofed!" (Client Error) - FM
1660 */
1661 switch (server_status) {
1662 case 401: /* Unauthorized */
1663 /*
1664 * Authorization for origin server required. If show_401
1665 * is set, proceed to showing the 401 body. Otherwise, if
1666 * we can set up authorization based on the
1667 * WWW-Authenticate header, and the user provides a
1668 * username and password, try again. Otherwise, check
1669 * whether to show the 401 body or restore the current
1670 * document - FM
1671 */
1672 if (show_401)
1673 break;
1674 if (HTAA_shouldRetryWithAuth(start_of_data, (size_t)
1675 length, s, NO)) {
1676
1677 HTTP_NETCLOSE(s, handle);
1678 if (dump_output_immediately &&
1679 !HTAA_HaveUserinfo(HTParse(arg, "", PARSE_HOST)) &&
1680 !authentication_info[0]) {
1681 fprintf(stderr,
1682 "HTTP: Access authorization required.\n");
1683 fprintf(stderr,
1684 " Use the -auth=id:pw parameter.\n");
1685 status = HT_NO_DATA;
1686 goto clean_up;
1687 }
1688
1689 CTRACE((tfp, "%s %d %s\n",
1690 "HTTP: close socket", s,
1691 "to retry with Access Authorization"));
1692
1693 _HTProgress(gettext("Retrying with access authorization information."));
1694 FREE(line_buffer);
1695 FREE(line_kept_clean);
1696 #ifdef USE_SSL
1697 if (using_proxy && !StrNCmp(url, "https://", 8)) {
1698 url = arg;
1699 do_connect = TRUE;
1700 did_connect = FALSE;
1701 }
1702 #endif /* USE_SSL */
1703 goto try_again;
1704 } else if (!(traversal || dump_output_immediately) &&
1705 HTConfirm(gettext("Show the 401 message body?"))) {
1706 break;
1707 } else {
1708 if (traversal || dump_output_immediately)
1709 HTAlert(FAILED_RETRY_WITH_AUTH);
1710 HTTP_NETCLOSE(s, handle);
1711 status = -1;
1712 goto clean_up;
1713 }
1714
1715 case 407:
1716 /*
1717 * Authorization for proxy server required. If we are not
1718 * in fact using a proxy, or show_407 is set, proceed to
1719 * showing the 407 body. Otherwise, if we can set up
1720 * authorization based on the Proxy-Authenticate header,
1721 * and the user provides a username and password, try
1722 * again. Otherwise, check whether to show the 401 body or
1723 * restore the current document. - FM & AJL
1724 */
1725 if (!using_proxy || show_407)
1726 break;
1727 if (HTAA_shouldRetryWithAuth(start_of_data, (size_t)
1728 length, s, YES)) {
1729
1730 HTTP_NETCLOSE(s, handle);
1731 if (dump_output_immediately && !proxyauth_info[0]) {
1732 fprintf(stderr,
1733 "HTTP: Proxy authorization required.\n");
1734 fprintf(stderr,
1735 " Use the -pauth=id:pw parameter.\n");
1736 status = HT_NO_DATA;
1737 goto clean_up;
1738 }
1739
1740 CTRACE((tfp, "%s %d %s\n",
1741 "HTTP: close socket", s,
1742 "to retry with Proxy Authorization"));
1743
1744 _HTProgress(HTTP_RETRY_WITH_PROXY);
1745 FREE(line_buffer);
1746 FREE(line_kept_clean);
1747 goto try_again;
1748 } else if (!(traversal || dump_output_immediately) &&
1749 HTConfirm(gettext("Show the 407 message body?"))) {
1750 if (!dump_output_immediately &&
1751 format_out == WWW_DOWNLOAD) {
1752 /*
1753 * Convert a download request to a presentation
1754 * request for interactive users. - FM
1755 */
1756 format_out = WWW_PRESENT;
1757 }
1758 break;
1759 } else {
1760 if (traversal || dump_output_immediately)
1761 HTAlert(FAILED_RETRY_WITH_PROXY);
1762 HTTP_NETCLOSE(s, handle);
1763 status = -1;
1764 goto clean_up;
1765 }
1766
1767 case 408:
1768 /*
1769 * Request Timeout. Show the status message and restore
1770 * the current document. - FM
1771 */
1772 HTAlert(line_buffer);
1773 HTTP_NETCLOSE(s, handle);
1774 status = HT_NO_DATA;
1775 goto clean_up;
1776
1777 default:
1778 /*
1779 * 400 Bad Request.
1780 * 402 Payment Required.
1781 * 403 Forbidden.
1782 * 404 Not Found.
1783 * 405 Method Not Allowed.
1784 * 406 Not Acceptable.
1785 * 409 Conflict.
1786 * 410 Gone.
1787 * 411 Length Required.
1788 * 412 Precondition Failed.
1789 * 413 Request Entity Too Large.
1790 * 414 Request-URI Too Long.
1791 * 415 Unsupported Media Type.
1792 * 416 List Response (for content negotiation).
1793 * > 416 is unknown.
1794 * Show the status message, and display the returned text
1795 * if we are not doing a traversal. - FM
1796 */
1797 HTAlert(line_buffer);
1798 if (traversal) {
1799 HTTP_NETCLOSE(s, handle);
1800 status = -1;
1801 goto clean_up;
1802 }
1803 if (!dump_output_immediately &&
1804 format_out == WWW_DOWNLOAD) {
1805 /*
1806 * Convert a download request to a presentation request
1807 * for interactive users. - FM
1808 */
1809 format_out = WWW_PRESENT;
1810 }
1811 break;
1812 } /* case 4 switch */
1813 break;
1814
1815 case 5:
1816 /*
1817 * "I think YOU goofed!" (server error)
1818 * 500 Internal Server Error
1819 * 501 Not Implemented
1820 * 502 Bad Gateway
1821 * 503 Service Unavailable
1822 * 504 Gateway Timeout
1823 * 505 HTTP Version Not Supported
1824 * > 505 is unknown.
1825 * Should always include a message, which we always should
1826 * display. - FM
1827 */
1828 HTAlert(line_buffer);
1829 if (traversal) {
1830 HTTP_NETCLOSE(s, handle);
1831 status = -1;
1832 goto clean_up;
1833 }
1834 if (!dump_output_immediately &&
1835 format_out == WWW_DOWNLOAD) {
1836 /*
1837 * Convert a download request to a presentation request for
1838 * interactive users. - FM
1839 */
1840 format_out = WWW_PRESENT;
1841 }
1842 break;
1843
1844 default:
1845 /*
1846 * Bad or unknown server_status number. Take a chance and hope
1847 * there is something to display. - FM
1848 */
1849 HTAlert(gettext("Unknown status reply from server!"));
1850 HTAlert(line_buffer);
1851 if (traversal) {
1852 HTTP_NETCLOSE(s, handle);
1853 status = -1;
1854 goto clean_up;
1855 }
1856 if (!dump_output_immediately &&
1857 format_out == WWW_DOWNLOAD) {
1858 /*
1859 * Convert a download request to a presentation request for
1860 * interactive users. - FM
1861 */
1862 format_out = WWW_PRESENT;
1863 }
1864 break;
1865 } /* Switch on server_status/100 */
1866
1867 } /* Full HTTP reply */
1868 } /* scope of fields */
1869
1870 /*
1871 * The user may have pressed the 'z'ap key during the pause caused by one
1872 * of the HTAlerts above if the server reported an error, to skip loading
1873 * of the error response page. Checking here before setting up the stream
1874 * stack and feeding it data avoids doing unnecessary work, it also can
1875 * avoid unnecessarily pushing a loaded document out of the cache to make
1876 * room for the unwanted error page. - kw
1877 */
1878 if (HTCheckForInterrupt()) {
1879 HTTP_NETCLOSE(s, handle);
1880 if (doing_redirect) {
1881 /*
1882 * Impatient user. - FM
1883 */
1884 CTRACE((tfp, "HTTP: Interrupted followup read.\n"));
1885 _HTProgress(CONNECTION_INTERRUPTED);
1886 }
1887 status = HT_INTERRUPTED;
1888 goto clean_up;
1889 }
1890 /*
1891 * Set up the stream stack to handle the body of the message.
1892 */
1893 if (do_head || keep_mime_headers) {
1894 /*
1895 * It was a HEAD request, or we want the headers and source.
1896 */
1897 start_of_data = line_kept_clean;
1898 #ifdef SH_EX /* FIX BUG by [email protected] */
1899 /* GIF file contains \0, so strlen does not return the data length */
1900 length = real_length_of_line;
1901 #else
1902 length = rawlength;
1903 #endif
1904 format_in = HTAtom_for(STR_PLAINTEXT);
1905
1906 } else if (doing_redirect) {
1907
1908 format_in = HTAtom_for("message/x-http-redirection");
1909 StrAllocCopy(anAnchor->content_type, HTAtom_name(format_in));
1910 if (traversal) {
1911 format_out = WWW_DEBUG;
1912 if (!sink)
1913 sink = HTErrorStream();
1914 } else if (!dump_output_immediately &&
1915 format_out == WWW_DOWNLOAD) {
1916 /*
1917 * Convert a download request to a presentation request for
1918 * interactive users. - FM
1919 */
1920 format_out = WWW_PRESENT;
1921 }
1922 }
1923
1924 target = HTStreamStack(format_in,
1925 format_out,
1926 sink, anAnchor);
1927
1928 if (target == NULL) {
1929 char *buffer = NULL;
1930
1931 HTTP_NETCLOSE(s, handle);
1932 HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O,
1933 HTAtom_name(format_in), HTAtom_name(format_out));
1934 _HTProgress(buffer);
1935 FREE(buffer);
1936 status = -1;
1937 goto clean_up;
1938 }
1939
1940 /*
1941 * Recycle the first chunk of data, in all cases.
1942 */
1943 (*target->isa->put_block) (target, start_of_data, length);
1944
1945 /*
1946 * Go pull the bulk of the data down.
1947 */
1948 rv = HTCopy(anAnchor, s, (void *) handle, target);
1949
1950 /*
1951 * If we get here with doing_redirect set, it means that we were looking
1952 * for a Location header. We either have got it now in redirecting_url -
1953 * in that case the stream should not have loaded any data. Or we didn't
1954 * get it, in that case the stream may have presented the message body
1955 * normally. - kw
1956 */
1957
1958 if (rv == -1) {
1959 /*
1960 * Intentional interrupt before data were received, not an error
1961 */
1962 if (doing_redirect && traversal)
1963 status = -1;
1964 else
1965 status = HT_INTERRUPTED;
1966 HTTP_NETCLOSE(s, handle);
1967 goto clean_up;
1968 }
1969
1970 if (rv == -2) {
1971 /*
1972 * Aw hell, a REAL error, maybe cuz it's a dumb HTTP0 server
1973 */
1974 (*target->isa->_abort) (target, NULL);
1975 if (doing_redirect && redirecting_url) {
1976 /*
1977 * Got a location before the error occurred? Then consider it an
1978 * interrupt but proceed below as normal. - kw
1979 */
1980 /* do nothing here */
1981 } else {
1982 HTTP_NETCLOSE(s, handle);
1983 if (!doing_redirect && !already_retrying && !do_post) {
1984 CTRACE((tfp, "HTTP: Trying again with HTTP0 request.\n"));
1985 /*
1986 * May as well consider it an interrupt -- right?
1987 */
1988 FREE(line_buffer);
1989 FREE(line_kept_clean);
1990 extensions = NO;
1991 already_retrying = TRUE;
1992 _HTProgress(RETRYING_AS_HTTP0);
1993 goto try_again;
1994 } else {
1995 status = HT_NOT_LOADED;
1996 goto clean_up;
1997 }
1998 }
1999 }
2000
2001 /*
2002 * Free if complete transmission (socket was closed before return). Close
2003 * socket if partial transmission (was freed on abort).
2004 */
2005 if (rv != HT_INTERRUPTED && rv != -2) {
2006 (*target->isa->_free) (target);
2007 } else {
2008 HTTP_NETCLOSE(s, handle);
2009 }
2010
2011 if (doing_redirect) {
2012 if (redirecting_url) {
2013 /*
2014 * Set up for checking redirecting_url in LYGetFile.c for
2015 * restrictions before we seek the document at that Location. - FM
2016 */
2017 CTRACE((tfp, "HTTP: Picked up location '%s'\n",
2018 redirecting_url));
2019 if (rv == HT_INTERRUPTED) {
2020 /*
2021 * Intentional interrupt after data were received, not an error
2022 * (probably). We take it as a user request to abandon the
2023 * redirection chain.
2024 *
2025 * This could reasonably be changed (by just removing this
2026 * block), it would make sense if there are redirecting
2027 * resources that "hang" after sending the headers. - kw
2028 */
2029 FREE(redirecting_url);
2030 CTRACE((tfp, "HTTP: Interrupted followup read.\n"));
2031 status = HT_INTERRUPTED;
2032 goto clean_up;
2033 }
2034 HTProgress(line_buffer);
2035 if (server_status == 305) { /* Use Proxy */
2036 /*
2037 * Make sure the proxy field ends with a slash. - FM
2038 */
2039 if (redirecting_url[strlen(redirecting_url) - 1]
2040 != '/')
2041 StrAllocCat(redirecting_url, "/");
2042 /*
2043 * Append our URL. - FM
2044 */
2045 StrAllocCat(redirecting_url, anAnchor->address);
2046 CTRACE((tfp, "HTTP: Proxy URL is '%s'\n",
2047 redirecting_url));
2048 }
2049 if (!do_post ||
2050 server_status == 303 ||
2051 server_status == 302) {
2052 /*
2053 * We don't have POST content (nor support PUT or DELETE), or
2054 * the status is "See Other" or "General Redirection" and we
2055 * can convert to GET, so go back and check out the new URL. -
2056 * FM
2057 */
2058 status = HT_REDIRECTING;
2059 goto clean_up;
2060 }
2061 /*
2062 * Make sure the user wants to redirect the POST content, or treat
2063 * as GET - FM & DK
2064 */
2065 switch (HTConfirmPostRedirect(redirecting_url,
2066 server_status)) {
2067 /*
2068 * User failed to confirm. Abort the fetch.
2069 */
2070 case 0:
2071 FREE(redirecting_url);
2072 status = HT_NO_DATA;
2073 goto clean_up;
2074
2075 /*
2076 * User wants to treat as GET with no content. Go back to
2077 * check out the URL.
2078 */
2079 case 303:
2080 break;
2081
2082 /*
2083 * Set the flag to retain the POST content and go back to check
2084 * out the URL. - FM
2085 */
2086 default:
2087 redirect_post_content = TRUE;
2088 }
2089
2090 /* Lou's old comment: - FM */
2091 /* OK, now we've got the redirection URL temporarily stored
2092 in external variable redirecting_url, exported from HTMIME.c,
2093 since there's no straightforward way to do this in the library
2094 currently. Do the right thing. */
2095
2096 status = HT_REDIRECTING;
2097
2098 } else {
2099 status = traversal ? -1 : HT_LOADED;
2100 }
2101
2102 } else {
2103 /*
2104 * If any data were received, treat as a complete transmission
2105 */
2106 status = HT_LOADED;
2107 }
2108
2109 /*
2110 * Clean up
2111 */
2112 clean_up:
2113 FREE(line_buffer);
2114 FREE(line_kept_clean);
2115
2116 done:
2117 /*
2118 * Clear out on exit, just in case.
2119 */
2120 reloading = FALSE;
2121 #ifdef USE_SSL
2122 FREE(connect_host);
2123 if (handle) {
2124 SSL_free(handle);
2125 SSL_handle = handle = NULL;
2126 }
2127 #endif /* USE_SSL */
2128 dump_server_status = server_status;
2129 return status;
2130 }
2131
2132 /* Protocol descriptor
2133 */
2134 #ifdef GLOBALDEF_IS_MACRO
2135 #define _HTTP_C_GLOBALDEF_1_INIT { "http", HTLoadHTTP, 0}
2136 GLOBALDEF(HTProtocol, HTTP, _HTTP_C_GLOBALDEF_1_INIT);
2137 #define _HTTP_C_GLOBALDEF_2_INIT { "https", HTLoadHTTP, 0}
2138 GLOBALDEF(HTProtocol, HTTPS, _HTTP_C_GLOBALDEF_2_INIT);
2139 #else
2140 GLOBALDEF HTProtocol HTTP =
2141 {"http", HTLoadHTTP, NULL};
2142 GLOBALDEF HTProtocol HTTPS =
2143 {"https", HTLoadHTTP, NULL};
2144 #endif /* GLOBALDEF_IS_MACRO */