Dillo v3.2.0-88-g47ab7c70
Loading...
Searching...
No Matches
cache.c
Go to the documentation of this file.
1/*
2 * File: cache.c
3 *
4 * Copyright 2000-2007 Jorge Arellano Cid <jcid@dillo.org>
5 * Copyright 2024-2025 Rodrigo Arias Mallo <rodarima@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13/*
14 * @file
15 * Dillo's cache module
16 */
17
18#include <sys/types.h>
19
20#include <limits.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "msg.h"
25#include "IO/Url.h"
26#include "IO/IO.h"
27#include "web.hh"
28#include "dicache.h"
29#include "nav.h"
30#include "cookies.h"
31#include "hsts.h"
32#include "misc.h"
33#include "capi.h"
34#include "decode.h"
35#include "auth.h"
36#include "domain.h"
37#include "timeout.hh"
38#include "uicmd.hh"
39#include "dlib/dlib.h"
40
42#define MAX_INIT_BUF 1024*1024
44#define HUGE_FILESIZE 15*1024*1024
45
46/*
47 * Local data types
48 */
49
50typedef struct {
51 const DilloUrl *Url;
52 char *TypeDet;
53 char *TypeHdr;
54 char *TypeMeta;
55 char *TypeNorm;
56 char *ContentDisposition;
57 Dstr *Header;
58 const DilloUrl *Location;
59 Dlist *Auth;
60 Dstr *Data;
61 Dstr *UTF8Data;
62 int DataRefcount;
63 DecodeTransfer *TransferDecoder;
64 Decode *ContentDecoder;
65 Decode *CharsetDecoder;
66 int ExpectedSize;
67 int TransferSize;
68 uint_t Flags;
69 int Hits;
70} CacheEntry_t;
71
72
73/*
74 * Local data
75 */
78
82
87
88
89/*
90 * Forward declarations
91 */
92static CacheEntry_t *Cache_process_queue(CacheEntry_t *entry);
93static void Cache_delayed_process_queue(CacheEntry_t *entry);
94static void Cache_auth_entry(CacheEntry_t *entry, BrowserWindow *bw);
95static Dstr *Cache_data(CacheEntry_t *entry);
96
100static int Cache_entry_cmp(const void *v1, const void *v2)
101{
102 const CacheEntry_t *d1 = v1, *d2 = v2;
103
104 return a_Url_cmp(d1->Url, d2->Url);
105}
106
110static int Cache_entry_by_url_cmp(const void *v1, const void *v2)
111{
112 const DilloUrl *u1 = ((CacheEntry_t*)v1)->Url;
113 const DilloUrl *u2 = v2;
114
115 return a_Url_cmp(u1, u2);
116}
117
121void a_Cache_init(void)
122{
125 CachedURLs = dList_new(256);
126
127 /* inject the splash screen in the cache */
128 {
129 DilloUrl *url = a_Url_new("about:splash", NULL);
131 a_Cache_entry_inject(url, ds);
132 dStr_free(ds, 1);
133 a_Url_free(url);
134 }
135}
136
137/* Client operations ------------------------------------------------------ */
138
144static int Cache_client_enqueue(const DilloUrl *Url, DilloWeb *Web,
145 CA_Callback_t Callback, void *CbData)
146{
147 static int ClientKey = 0; /* Provide a primary key for each client */
148 CacheClient_t *NewClient;
149
150 if (ClientKey < INT_MAX) /* check for integer overflow */
151 ClientKey++;
152 else
153 ClientKey = 1;
154
155 NewClient = dNew(CacheClient_t, 1);
156 NewClient->Key = ClientKey;
157 NewClient->Url = Url;
158 NewClient->Version = 0;
159 NewClient->Buf = NULL;
160 NewClient->BufSize = 0;
161 NewClient->Callback = Callback;
162 NewClient->CbData = CbData;
163 NewClient->Web = Web;
164
165 dList_append(ClientQueue, NewClient);
166
167 return ClientKey;
168}
169
173static int Cache_client_by_key_cmp(const void *client, const void *key)
174{
175 return ((CacheClient_t *)client)->Key - VOIDP2INT(key);
176}
177
182{
183 if (Client) {
184 dList_remove(ClientQueue, Client);
185 a_Web_free(Client->Web);
186 dFree(Client);
187 }
188}
189
190
191/* Entry operations ------------------------------------------------------- */
192
196static void Cache_entry_init(CacheEntry_t *NewEntry, const DilloUrl *Url)
197{
198 NewEntry->Url = a_Url_dup(Url);
199 NewEntry->TypeDet = NULL;
200 NewEntry->TypeHdr = NULL;
201 NewEntry->TypeMeta = NULL;
202 NewEntry->TypeNorm = NULL;
203 NewEntry->ContentDisposition = NULL;
204 NewEntry->Header = dStr_new("");
205 NewEntry->Location = NULL;
206 NewEntry->Auth = NULL;
207 NewEntry->Data = dStr_sized_new(8*1024);
208 NewEntry->UTF8Data = NULL;
209 NewEntry->DataRefcount = 0;
210 NewEntry->TransferDecoder = NULL;
211 NewEntry->ContentDecoder = NULL;
212 NewEntry->CharsetDecoder = NULL;
213 NewEntry->ExpectedSize = 0;
214 NewEntry->TransferSize = 0;
215 NewEntry->Flags = CA_IsEmpty | CA_InProgress | CA_KeepAlive;
216 NewEntry->Hits = 0;
217}
218
223static CacheEntry_t *Cache_entry_search(const DilloUrl *Url)
224{
226}
227
231static CacheEntry_t *Cache_entry_search_with_redirect(const DilloUrl *Url)
232{
233 int i;
234 CacheEntry_t *entry;
235
236 for (i = 0; (entry = Cache_entry_search(Url)); ++i) {
237
238 /* Test for a redirection loop */
239 if (entry->Flags & CA_RedirectLoop || i == 3) {
240 _MSG_WARN("Redirect loop for URL: >%s<\n", URL_STR_(Url));
241 break;
242 }
243 /* Test for a working redirection */
244 if (entry->Flags & CA_Redirect && entry->Location) {
245 Url = entry->Location;
246 } else
247 break;
248 }
249 return entry;
250}
251
255static CacheEntry_t *Cache_entry_add(const DilloUrl *Url)
256{
257 CacheEntry_t *old_entry, *new_entry;
258
259 if ((old_entry = Cache_entry_search(Url))) {
260 MSG_WARN("Cache_entry_add, leaking an entry.\n");
261 dList_remove(CachedURLs, old_entry);
262 }
263
264 new_entry = dNew(CacheEntry_t, 1);
265 Cache_entry_init(new_entry, Url); /* Set safe values */
267 return new_entry;
268}
269
274static int Cache_bufsize(CacheEntry_t *e)
275{
276 Dstr *buf = Cache_data(e);
277 if (buf)
278 return buf->len;
279 else
280 return 0;
281}
282
290void a_Cache_entry_inject(const DilloUrl *Url, Dstr *data_ds)
291{
292 CacheEntry_t *entry;
293
294 if (!(entry = Cache_entry_search(Url)))
295 entry = Cache_entry_add(Url);
296 entry->Flags = CA_GotHeader + CA_GotLength + CA_InternalUrl;
297 if (data_ds->len)
298 entry->Flags &= ~CA_IsEmpty;
299 dStr_truncate(entry->Data, 0);
300 dStr_append_l(entry->Data, data_ds->str, data_ds->len);
301 dStr_fit(entry->Data);
302 entry->ExpectedSize = entry->TransferSize = entry->Data->len;
303}
304
308static void Cache_auth_free(Dlist *auth)
309{
310 int i;
311 void *auth_field;
312 for (i = 0; (auth_field = dList_nth_data(auth, i)); ++i)
313 dFree(auth_field);
314 dList_free(auth);
315}
316
320static void Cache_entry_free(CacheEntry_t *entry)
321{
322 a_Url_free((DilloUrl *)entry->Url);
323 dFree(entry->TypeDet);
324 dFree(entry->TypeHdr);
325 dFree(entry->TypeMeta);
326 dFree(entry->TypeNorm);
327 dFree(entry->ContentDisposition);
328 dStr_free(entry->Header, TRUE);
329 a_Url_free((DilloUrl *)entry->Location);
330 Cache_auth_free(entry->Auth);
331 dStr_free(entry->Data, 1);
332 dStr_free(entry->UTF8Data, 1);
333 if (entry->CharsetDecoder)
334 a_Decode_free(entry->CharsetDecoder);
335 if (entry->TransferDecoder)
336 a_Decode_transfer_free(entry->TransferDecoder);
337 if (entry->ContentDecoder)
338 a_Decode_free(entry->ContentDecoder);
339 dFree(entry);
340}
341
347static void Cache_entry_remove(CacheEntry_t *entry, DilloUrl *url)
348{
349 int i;
350 CacheClient_t *Client;
351
352 if (!entry && !(entry = Cache_entry_search(url)))
353 return;
354 if (entry->Flags & CA_InternalUrl)
355 return;
356
357 /* remove all clients for this entry */
358 for (i = 0; (Client = dList_nth_data(ClientQueue, i)); ++i) {
359 if (Client->Url == entry->Url) {
360 a_Cache_stop_client(Client->Key);
361 --i;
362 }
363 }
364
365 /* remove from DelayedQueue */
367
368 /* remove from dicache */
369 a_Dicache_invalidate_entry(entry->Url);
370
371 /* remove from cache */
372 dList_remove(CachedURLs, entry);
373 Cache_entry_free(entry);
374}
375
380{
381 Cache_entry_remove(NULL, url);
382}
383
384/* Misc. operations ------------------------------------------------------- */
385
386static Dstr *Cache_stats(void)
387{
388 float totalKB = 0.0f;
389
390 Dstr *s = dStr_new(
391 "<!DOCTYPE HTML>\n"
392 "<html>\n"
393 "<head><title>Dillo Cache</title></head>\n"
394 "<body>\n");
395
396 int n = dList_length(CachedURLs);
397 dStr_sprintfa(s, "<h1>Cached URLs (%d)</h1>\n", n);
398
399 dStr_append(s, "<table>\n");
400 dStr_append(s, "<tr>\n");
401 dStr_append(s, "<th>Hits</th>\n");
402 dStr_append(s, "<th>Size</th>\n");
403 dStr_append(s, "<th>URL</th>\n");
404 dStr_append(s, "</tr>\n");
405 for (int i = 0; i < n; i++) {
406 CacheEntry_t *e = dList_nth_data(CachedURLs, i);
407 float sizeKB = Cache_bufsize(e) / 1024.0f;
408 const char *url = URL_STR(e->Url);
409 dStr_append(s, "<tr>\n");
410 dStr_sprintfa(s, "<td style='text-align:right'>%d</td>\n", e->Hits);
411 dStr_sprintfa(s, "<td style='text-align:right'>%.2f KiB</td>\n", sizeKB);
412 dStr_sprintfa(s, "<td><a href='%s'>", url);
413 dStr_shorten(s, url, 60);
414 dStr_append(s, "</a></td>\n");
415 dStr_append(s, "</tr>\n");
416 totalKB += sizeKB;
417 }
418 dStr_append(s, "</table>\n");
419 dStr_sprintfa(s, "<p>Total cached: %.2f MiB</p>\n", totalKB / 1024.0f);
420 dStr_append(s,
421 "</body>\n"
422 "</html>\n");
423
424 return s;
425}
426
427static int Cache_internal_url(CacheEntry_t *entry)
428{
429 Dstr *s = NULL;
430
431 if (strcmp(URL_PATH(entry->Url), "cache") == 0) {
432 s = Cache_stats();
433 } else if (strcmp(URL_PATH(entry->Url), "dicache") == 0) {
434 s = a_Dicache_stats();
435 }
436
437 if (s != NULL) {
438 a_Cache_entry_inject(entry->Url, s);
439 dStr_free(s, 1);
440 /* Remove InternalUrl */
441 entry->Flags = CA_GotHeader + CA_GotLength;
442 }
443
444 return 0;
445}
446
459int a_Cache_open_url(void *web, CA_Callback_t Call, void *CbData)
460{
461 int ClientKey;
462 CacheEntry_t *entry;
463 DilloWeb *Web = web;
464 DilloUrl *Url = Web->url;
465 int isInternal = 0;
466
467 if (dStrAsciiCasecmp(URL_SCHEME(Url), "about") == 0) {
468 _MSG("got internal URL: %s\n", URL_STR(Url));
469 isInternal = 1;
470 Cache_entry_remove(NULL, Url);
471 }
472
473 if (URL_FLAGS(Url) & URL_E2EQuery) {
474 /* remove current entry */
475 Cache_entry_remove(NULL, Url);
476 }
477
478 if ((entry = Cache_entry_search(Url))) {
479 _MSG("serving cached entry: %s\n", URL_STR(Url));
480 /* URL is cached: feed our client with cached data */
481 ClientKey = Cache_client_enqueue(entry->Url, Web, Call, CbData);
483 entry->Hits++;
484
485 } else {
486 _MSG("serving new entry: %s\n", URL_STR(Url));
487 /* URL not cached: create an entry, send our client to the queue,
488 * and open a new connection */
489 entry = Cache_entry_add(Url);
490
491 /* URL is an internal call, populate */
492 if (isInternal) {
493 _MSG("handling internal: %s\n", URL_STR(Url));
494 Cache_internal_url(entry);
495 ClientKey = Cache_client_enqueue(entry->Url, Web, Call, CbData);
497 } else {
498 ClientKey = Cache_client_enqueue(entry->Url, Web, Call, CbData);
499 }
500 }
501
502 return ClientKey;
503}
504
509{
510 CacheEntry_t *entry = Cache_entry_search(url);
511 return (entry ? entry->Flags : 0);
512}
513
518{
519 CacheEntry_t *entry = Cache_entry_search_with_redirect(url);
520 return (entry ? entry->Flags : 0);
521}
522
526static void Cache_ref_data(CacheEntry_t *entry)
527{
528 if (entry) {
529 entry->DataRefcount++;
530 _MSG("DataRefcount++: %d\n", entry->DataRefcount);
531 if (entry->CharsetDecoder &&
532 (!entry->UTF8Data || entry->DataRefcount == 1)) {
533 dStr_free(entry->UTF8Data, 1);
534 entry->UTF8Data = a_Decode_process(entry->CharsetDecoder,
535 entry->Data->str,
536 entry->Data->len);
537 }
538 }
539}
540
544static void Cache_unref_data(CacheEntry_t *entry)
545{
546 if (entry) {
547 entry->DataRefcount--;
548 _MSG("DataRefcount--: %d\n", entry->DataRefcount);
549
550 if (entry->CharsetDecoder) {
551 if (entry->DataRefcount == 0) {
552 dStr_free(entry->UTF8Data, 1);
553 entry->UTF8Data = NULL;
554 } else if (entry->DataRefcount < 0) {
555 MSG_ERR("Cache_unref_data: negative refcount\n");
556 entry->DataRefcount = 0;
557 }
558 }
559 }
560}
561
565static const char *Cache_current_content_type(CacheEntry_t *entry)
566{
567 return entry->TypeNorm ? entry->TypeNorm : entry->TypeMeta ? entry->TypeMeta
568 : entry->TypeHdr ? entry->TypeHdr : entry->TypeDet;
569}
570
574const char *a_Cache_get_content_type(const DilloUrl *url)
575{
576 CacheEntry_t *entry = Cache_entry_search_with_redirect(url);
577
578 return (entry) ? Cache_current_content_type(entry) : NULL;
579}
580
584static Dstr *Cache_data(CacheEntry_t *entry)
585{
586 return entry->UTF8Data ? entry->UTF8Data : entry->Data;
587}
588
594const char *a_Cache_set_content_type(const DilloUrl *url, const char *ctype,
595 const char *from)
596{
597 const char *curr;
598 char *major, *minor, *charset;
599 CacheEntry_t *entry = Cache_entry_search(url);
600
601 dReturn_val_if_fail (entry != NULL, NULL);
602
603 _MSG("a_Cache_set_content_type {%s} {%s}\n", ctype, URL_STR(url));
604
605 curr = Cache_current_content_type(entry);
606 if (entry->TypeMeta || (*from == 'h' && entry->TypeHdr) ) {
607 /* Type is already been set. Do nothing.
608 * BTW, META overrides TypeHdr */
609 } else {
610 if (*from == 'h') {
611 /* Content-Type from HTTP header */
612 entry->TypeHdr = dStrdup(ctype);
613 } else {
614 /* Content-Type from META */
615 entry->TypeMeta = dStrdup(ctype);
616 }
617 if (a_Misc_content_type_cmp(curr, ctype)) {
618 /* ctype gives one different from current */
619 a_Misc_parse_content_type(ctype, &major, &minor, &charset);
620 if (*from == 'm' && charset &&
621 ((!major || !*major) && (!minor || !*minor))) {
622 /* META only gives charset; use detected MIME type too */
623 entry->TypeNorm = dStrconcat(entry->TypeDet, ctype, NULL);
624 } else if (*from == 'm' &&
625 !dStrnAsciiCasecmp(ctype, "text/xhtml", 10)) {
626 /* WORKAROUND: doxygen uses "text/xhtml" in META */
627 if (charset) {
628 entry->TypeNorm = dStrconcat("application/xhtml+xml",
629 "; charset=", charset, NULL);
630 } else {
631 entry->TypeNorm = dStrdup("application/xhtml+xml");
632 }
633 }
634 if (charset) {
635 if (entry->CharsetDecoder)
636 a_Decode_free(entry->CharsetDecoder);
637 entry->CharsetDecoder = a_Decode_charset_init(charset);
638 curr = Cache_current_content_type(entry);
639
640 /* Invalidate UTF8Data */
641 dStr_free(entry->UTF8Data, 1);
642 entry->UTF8Data = NULL;
643 }
644 dFree(major); dFree(minor); dFree(charset);
645 }
646 }
647 return curr;
648}
649
654int a_Cache_get_buf(const DilloUrl *Url, char **PBuf, int *BufSize)
655{
656 CacheEntry_t *entry = Cache_entry_search_with_redirect(Url);
657 if (entry) {
658 Dstr *data;
659 Cache_ref_data(entry);
660 data = Cache_data(entry);
661 *PBuf = data->str;
662 *BufSize = data->len;
663 } else {
664 *PBuf = NULL;
665 *BufSize = 0;
666 }
667 return (entry ? 1 : 0);
668}
669
677
678
685static char *Cache_parse_field(const char *header, const char *fieldname)
686{
687 char *field;
688 uint_t i, j;
689
690 for (i = 0; header[i]; i++) {
691 /* Search fieldname */
692 for (j = 0; fieldname[j]; j++)
693 if (D_ASCII_TOLOWER(fieldname[j]) != D_ASCII_TOLOWER(header[i + j]))
694 break;
695 if (fieldname[j]) {
696 /* skip to next line */
697 for ( i += j; header[i] != '\n'; i++);
698 continue;
699 }
700
701 i += j;
702 if (header[i] == ':') {
703 /* Field found! */
704 while (header[++i] == ' ' || header[i] == '\t');
705 for (j = 0; header[i + j] != '\n'; j++);
706 while (j && (header[i + j - 1] == ' ' || header[i + j - 1] == '\t'))
707 j--;
708 field = dStrndup(header + i, j);
709 return field;
710 }
711 while (header[i] != '\n') i++;
712 }
713 return NULL;
714}
715
719static Dlist *Cache_parse_multiple_fields(const char *header,
720 const char *fieldname)
721{
722 uint_t i, j;
723 Dlist *fields = dList_new(8);
724 char *field;
725
726 for (i = 0; header[i]; i++) {
727 /* Search fieldname */
728 for (j = 0; fieldname[j]; j++)
729 if (D_ASCII_TOLOWER(fieldname[j]) != D_ASCII_TOLOWER(header[i + j]))
730 break;
731 if (fieldname[j]) {
732 /* skip to next line */
733 for (i += j; header[i] != '\n'; i++);
734 continue;
735 }
736
737 i += j;
738 if (header[i] == ':') {
739 /* Field found! */
740 while (header[++i] == ' ' || header[i] == '\t');
741 for (j = 0; header[i + j] != '\n'; j++);
742 while (j && (header[i + j - 1] == ' ' || header[i + j - 1] == '\t'))
743 j--;
744 field = dStrndup(header + i, j);
745 dList_append(fields, field);
746 } else {
747 while (header[i] != '\n') i++;
748 }
749 }
750
751 if (dList_length(fields) == 0) {
752 dList_free(fields);
753 fields = NULL;
754 }
755 return fields;
756}
757
762static void Cache_parse_header(CacheEntry_t *entry)
763{
764 char *header = entry->Header->str;
765 bool_t server1point0 = !strncmp(entry->Header->str, "HTTP/1.0", 8);
766 char *Length, *Type, *location_str, *encoding, *connection, *hsts;
767#ifndef DISABLE_COOKIES
768 Dlist *Cookies;
769#endif
770 Dlist *warnings;
771 void *data;
772 int i;
773
774 _MSG("Cache_parse_header\n");
775
776 if (entry->Header->len > 12) {
777 if (header[9] == '1' && header[10] == '0' && header[11] == '0') {
778 /* 100: Continue. The "real" header has not come yet. */
779 MSG("An actual 100 Continue header!\n");
780 entry->Flags &= ~CA_GotHeader;
781 dStr_free(entry->Header, 1);
782 entry->Header = dStr_new("");
783 return;
784 }
785 if (header[9] == '3' && header[10] == '0' &&
786 (location_str = Cache_parse_field(header, "Location"))) {
787 /* 30x: URL redirection */
788 entry->Location = a_Url_new(location_str, URL_STR_(entry->Url));
789
790 if (!a_Domain_permit(entry->Url, entry->Location) ||
791 (URL_FLAGS(entry->Location) & (URL_Post + URL_Get) &&
792 dStrAsciiCasecmp(URL_SCHEME(entry->Location), "dpi") == 0 &&
793 dStrAsciiCasecmp(URL_SCHEME(entry->Url), "dpi") != 0)) {
794 /* Domain test, and forbid dpi GET and POST from non dpi-generated
795 * urls.
796 */
797 MSG("Redirection not followed from %s to %s\n",
798 URL_HOST(entry->Url), URL_STR(entry->Location));
799 } else {
800 entry->Flags |= CA_Redirect;
801 if (header[11] == '1')
802 entry->Flags |= CA_ForceRedirect; /* 301 Moved Permanently */
803 else if (header[11] == '2')
804 entry->Flags |= CA_TempRedirect; /* 302 Temporary Redirect */
805 }
806 dFree(location_str);
807 } else if (strncmp(header + 9, "401", 3) == 0) {
808 entry->Auth =
809 Cache_parse_multiple_fields(header, "WWW-Authenticate");
810 } else if (strncmp(header + 9, "404", 3) == 0) {
811 entry->Flags |= CA_NotFound;
812 }
813 }
814
815 if ((warnings = Cache_parse_multiple_fields(header, "Warning"))) {
816 for (i = 0; (data = dList_nth_data(warnings, i)); ++i) {
817 MSG_HTTP("%s\n", (char *)data);
818 dFree(data);
819 }
820 dList_free(warnings);
821 }
822
823 if (server1point0)
824 entry->Flags &= ~CA_KeepAlive;
825
826 if ((connection = Cache_parse_field(header, "Connection"))) {
827 if (!dStrAsciiCasecmp(connection, "close"))
828 entry->Flags &= ~CA_KeepAlive;
829 else if (server1point0 && !dStrAsciiCasecmp(connection, "keep-alive"))
830 entry->Flags |= CA_KeepAlive;
831 dFree(connection);
832 }
833
835 !dStrAsciiCasecmp(URL_SCHEME(entry->Url), "https") &&
836 a_Url_host_type(URL_HOST(entry->Url)) == URL_HOST_NAME &&
837 (hsts = Cache_parse_field(header, "Strict-Transport-Security"))) {
838 a_Hsts_set(hsts, entry->Url);
839 dFree(hsts);
840 }
841
842 /*
843 * Get Transfer-Encoding and initialize decoder
844 */
845 encoding = Cache_parse_field(header, "Transfer-Encoding");
846 entry->TransferDecoder = a_Decode_transfer_init(encoding);
847
848
849 if ((Length = Cache_parse_field(header, "Content-Length")) != NULL) {
850 if (encoding) {
851 /*
852 * If Transfer-Encoding is present, Content-Length must be ignored.
853 * If the Transfer-Encoding is non-identity, it is an error.
854 */
855 if (dStrAsciiCasecmp(encoding, "identity"))
856 MSG_HTTP("Content-Length and non-identity Transfer-Encoding "
857 "headers both present.\n");
858 } else {
859 entry->Flags |= CA_GotLength;
860 entry->ExpectedSize = MAX(strtol(Length, NULL, 10), 0);
861 }
862 dFree(Length);
863 }
864
865 dFree(encoding); /* free Transfer-Encoding */
866
867#ifndef DISABLE_COOKIES
868 if ((Cookies = Cache_parse_multiple_fields(header, "Set-Cookie"))) {
869 CacheClient_t *client;
870
871 for (i = 0; (client = dList_nth_data(ClientQueue, i)); ++i) {
872 if (client->Url == entry->Url) {
873 DilloWeb *web = client->Web;
874
875 if (!web->requester ||
876 a_Url_same_organization(entry->Url, web->requester)) {
877 /* If cookies are third party, don't even consider them. */
878 char *server_date = Cache_parse_field(header, "Date");
879
880 a_Cookies_set(Cookies, entry->Url, server_date);
881 dFree(server_date);
882 break;
883 }
884 }
885 }
886 for (i = 0; (data = dList_nth_data(Cookies, i)); ++i)
887 dFree(data);
888 dList_free(Cookies);
889 }
890#endif /* !DISABLE_COOKIES */
891
892 /*
893 * Get Content-Encoding and initialize decoder
894 */
895 encoding = Cache_parse_field(header, "Content-Encoding");
896 entry->ContentDecoder = a_Decode_content_init(encoding);
897 dFree(encoding);
898
899 if (entry->ExpectedSize > 0) {
900 if (entry->ExpectedSize > HUGE_FILESIZE) {
901 entry->Flags |= CA_HugeFile;
902 }
903 /* Avoid some reallocs. With MAX_INIT_BUF we avoid a SEGFAULT
904 * with huge files (e.g. iso files).
905 * Note: the buffer grows automatically. */
906 dStr_free(entry->Data, 1);
907 entry->Data = dStr_sized_new(MIN(entry->ExpectedSize, MAX_INIT_BUF));
908 }
909
910 /* Get Content-Type */
911 if ((Type = Cache_parse_field(header, "Content-Type"))) {
912 /* This HTTP Content-Type is not trusted. It's checked against real data
913 * in Cache_process_queue(); only then CA_GotContentType becomes true. */
914 a_Cache_set_content_type(entry->Url, Type, "http");
915 _MSG("TypeHdr {%s} {%s}\n", Type, URL_STR(entry->Url));
916 _MSG("TypeMeta {%s}\n", entry->TypeMeta);
917 dFree(Type);
918 }
919
920 entry->ContentDisposition = Cache_parse_field(header, "Content-Disposition");
921
922 Cache_ref_data(entry);
923}
924
929static int Cache_get_header(CacheEntry_t *entry,
930 const char *buf, size_t buf_size)
931{
932 size_t N, i;
933 Dstr *hdr = entry->Header;
934
935 /* Header finishes when N = 2 */
936 N = (hdr->len && hdr->str[hdr->len - 1] == '\n');
937 for (i = 0; i < buf_size && N < 2; ++i) {
938 if (buf[i] == '\r' || !buf[i])
939 continue;
940 if (N == 1 && (buf[i] == ' ' || buf[i] == '\t')) {
941 /* unfold multiple-line header */
942 _MSG("Multiple-line header!\n");
943 dStr_erase(hdr, hdr->len - 1, 1);
944 }
945 N = (buf[i] == '\n') ? N + 1 : 0;
946 dStr_append_c(hdr, buf[i]);
947 }
948
949 if (N == 2) {
950 /* Got whole header */
951 _MSG("Header [buf_size=%d]\n%s", i, hdr->str);
952 entry->Flags |= CA_GotHeader;
953 dStr_fit(hdr);
954 /* Return number of header bytes in 'buf' [1 based] */
955 return i;
956 }
957 return 0;
958}
959
960static void Cache_finish_msg(CacheEntry_t *entry)
961{
962 if (!(entry->Flags & CA_InProgress)) {
963 /* already finished */
964 return;
965 }
966
967 if ((entry->ExpectedSize || entry->TransferSize) &&
968 entry->TypeHdr == NULL) {
969 MSG_HTTP("Message with a body lacked Content-Type header.\n");
970 }
971 if ((entry->Flags & CA_GotLength) &&
972 (entry->ExpectedSize != entry->TransferSize)) {
973 MSG_HTTP("Content-Length (%d) does NOT match message body (%d) for %s\n",
974 entry->ExpectedSize, entry->TransferSize, URL_STR_(entry->Url));
975 }
976 entry->Flags &= ~CA_InProgress;
977 if (entry->TransferDecoder) {
978 a_Decode_transfer_free(entry->TransferDecoder);
979 entry->TransferDecoder = NULL;
980 }
981 if (entry->ContentDecoder) {
982 a_Decode_free(entry->ContentDecoder);
983 entry->ContentDecoder = NULL;
984 }
985 dStr_fit(entry->Data); /* fit buffer size! */
986
987 if ((entry = Cache_process_queue(entry))) {
988 if (entry->Flags & CA_GotHeader) {
989 Cache_unref_data(entry);
990 }
991 }
992}
993
1002bool_t a_Cache_process_dbuf(int Op, const char *buf, size_t buf_size,
1003 const DilloUrl *Url)
1004{
1005 int offset, len;
1006 const char *str;
1007 Dstr *dstr1, *dstr2, *dstr3;
1008 bool_t done = FALSE;
1009 CacheEntry_t *entry = Cache_entry_search(Url);
1010
1011 /* Assert a valid entry (not aborted) */
1012 dReturn_val_if_fail (entry != NULL, FALSE);
1013
1014 _MSG("__a_Cache_process_dbuf__\n");
1015
1016 if (Op == IORead) {
1017 /*
1018 * Cache_get_header() will set CA_GotHeader if it has a full header, and
1019 * Cache_parse_header() will unset it if the header ends being
1020 * merely an informational response from the server (i.e., 100 Continue)
1021 */
1022 for (offset = 0; !(entry->Flags & CA_GotHeader) &&
1023 (len = Cache_get_header(entry, buf + offset, buf_size - offset));
1024 Cache_parse_header(entry) ) {
1025 offset += len;
1026 }
1027
1028 if (entry->Flags & CA_GotHeader) {
1029 str = buf + offset;
1030 len = buf_size - offset;
1031 entry->TransferSize += len;
1032 dstr1 = dstr2 = dstr3 = NULL;
1033
1034 /* Decode arrived data (<= 3 stages) */
1035 if (entry->TransferDecoder) {
1036 dstr1 = a_Decode_transfer_process(entry->TransferDecoder, str,len);
1037 done = a_Decode_transfer_finished(entry->TransferDecoder);
1038 str = dstr1->str;
1039 len = dstr1->len;
1040 }
1041 if (entry->ContentDecoder) {
1042 dstr2 = a_Decode_process(entry->ContentDecoder, str, len);
1043 str = dstr2->str;
1044 len = dstr2->len;
1045 }
1046 dStr_append_l(entry->Data, str, len);
1047 if (entry->CharsetDecoder && entry->UTF8Data) {
1048 dstr3 = a_Decode_process(entry->CharsetDecoder, str, len);
1049 dStr_append_l(entry->UTF8Data, dstr3->str, dstr3->len);
1050 }
1051 dStr_free(dstr1, 1);
1052 dStr_free(dstr2, 1);
1053 dStr_free(dstr3, 1);
1054
1055 if (entry->Data->len)
1056 entry->Flags &= ~CA_IsEmpty;
1057
1058 if ((entry->Flags & CA_GotLength) &&
1059 (entry->TransferSize >= entry->ExpectedSize)) {
1060 done = TRUE;
1061 }
1062 if (!(entry->Flags & CA_KeepAlive)) {
1063 /* Let IOClose finish it later */
1064 done = FALSE;
1065 }
1066
1067 entry = Cache_process_queue(entry);
1068
1069 if (entry && done)
1070 Cache_finish_msg(entry);
1071 }
1072 } else if (Op == IOClose) {
1073 Cache_finish_msg(entry);
1074 } else if (Op == IOAbort) {
1075 entry->Flags |= CA_Aborted;
1076 if (entry->Data->len) {
1077 MSG("Premature close for %s\n", URL_STR(entry->Url));
1078 Cache_finish_msg(entry);
1079 } else {
1080 int i;
1081 CacheClient_t *Client;
1082
1083 for (i = 0; (Client = dList_nth_data(ClientQueue, i)); ++i) {
1084 if (Client->Url == entry->Url) {
1085 DilloWeb *web = (DilloWeb *)Client->Web;
1086
1087 a_Bw_remove_client(web->bw, Client->Key);
1088 Cache_client_dequeue(Client);
1089 --i; /* Keep the index value in the next iteration */
1090 }
1091 }
1092 }
1093 }
1094 return done;
1095}
1096
1101static int Cache_redirect(CacheEntry_t *entry, int Flags, BrowserWindow *bw)
1102{
1103 DilloUrl *NewUrl;
1104
1105 _MSG(" Cache_redirect: redirect_level = %d\n", bw->redirect_level);
1106
1107 /* Don't allow redirection for SpamSafe/local URLs */
1108 if (URL_FLAGS(entry->Url) & URL_SpamSafe) {
1109 a_UIcmd_set_msg(bw, "WARNING: local URL with redirection. Aborting.");
1110 return 0;
1111 }
1112
1113 /* if there's a redirect loop, stop now */
1114 if (bw->redirect_level >= 5)
1115 entry->Flags |= CA_RedirectLoop;
1116
1117 if (entry->Flags & CA_RedirectLoop) {
1118 a_UIcmd_set_msg(bw, "ERROR: redirect loop for: %s", URL_STR_(entry->Url));
1119 bw->redirect_level = 0;
1120 return 0;
1121 }
1122
1123 if ((entry->Flags & CA_Redirect && entry->Location) &&
1124 (entry->Flags & CA_ForceRedirect || entry->Flags & CA_TempRedirect ||
1125 !entry->Data->len || entry->Data->len < 1024)) {
1126
1127 _MSG(">>>> Redirect from: %s\n to %s <<<<\n",
1128 URL_STR_(entry->Url), URL_STR_(entry->Location));
1129 _MSG("%s", entry->Header->str);
1130
1131 if (Flags & WEB_RootUrl) {
1132 /* Redirection of the main page */
1133 NewUrl = a_Url_new(URL_STR_(entry->Location), URL_STR_(entry->Url));
1134 if (entry->Flags & CA_TempRedirect)
1135 a_Url_set_flags(NewUrl, URL_FLAGS(NewUrl) | URL_E2EQuery);
1136 a_Nav_push(bw, NewUrl, entry->Url);
1137 a_Url_free(NewUrl);
1138 } else {
1139 /* Sub entity redirection (most probably an image) */
1140 if (!entry->Data->len) {
1141 _MSG(">>>> Image redirection without entity-content <<<<\n");
1142 } else {
1143 _MSG(">>>> Image redirection with entity-content <<<<\n");
1144 }
1145 }
1146 }
1147 return 0;
1148}
1149
1150typedef struct {
1151 Dlist *auth;
1152 DilloUrl *url;
1153 BrowserWindow *bw;
1154} CacheAuthData_t;
1155
1159static void Cache_auth_callback(void *vdata)
1160{
1161 CacheAuthData_t *data = (CacheAuthData_t *)vdata;
1162 if (a_Auth_do_auth(data->auth, data->url))
1163 a_Nav_reload(data->bw);
1164 Cache_auth_free(data->auth);
1165 a_Url_free(data->url);
1166 dFree(data);
1167 Cache_auth_entry(NULL, NULL);
1169}
1170
1174static void Cache_auth_entry(CacheEntry_t *entry, BrowserWindow *bw)
1175{
1176 static int busy = 0;
1177 CacheAuthData_t *data;
1178
1179 if (!entry) {
1180 busy = 0;
1181 } else if (busy) {
1182 MSG_WARN("Cache_auth_entry: caught busy!\n");
1183 } else if (entry->Auth) {
1184 busy = 1;
1185 data = dNew(CacheAuthData_t, 1);
1186 data->auth = entry->Auth;
1187 data->url = a_Url_dup(entry->Url);
1188 data->bw = bw;
1189 entry->Auth = NULL;
1191 }
1192}
1193
1199{
1200 if (!dStrAsciiCasecmp(URL_SCHEME(url), "http") ||
1201 !dStrAsciiCasecmp(URL_SCHEME(url), "https") ||
1202 !dStrAsciiCasecmp(URL_SCHEME(url), "ftp"))
1203 return 1;
1204 return 0;
1205}
1206
1212static void Cache_null_client(int Op, CacheClient_t *Client)
1213{
1214 DilloWeb *Web = Client->Web;
1215
1216 /* make the stop button insensitive when done */
1217 if (Op == CA_Close) {
1218 if (Web->flags & WEB_RootUrl) {
1219 /* Remove this client from our active list */
1220 a_Bw_close_client(Web->bw, Client->Key);
1221 }
1222 }
1223
1224 /* else ignore */
1225
1226 return;
1227}
1228
1229typedef struct {
1230 BrowserWindow *bw;
1231 DilloUrl *url;
1232 char* filename;
1233} Cache_savelink_t;
1234
1239static void Cache_savelink_cb(void *vdata)
1240{
1241 Cache_savelink_t *data = (Cache_savelink_t*) vdata;
1242
1243 a_UIcmd_save_link(data->bw, data->url, data->filename);
1244 a_Url_free(data->url);
1245 dFree(data);
1246}
1247
1251static void Cache_provide_redirection_blocked_page(CacheEntry_t *entry,
1252 CacheClient_t *client)
1253{
1254 DilloWeb *clientWeb = client->Web;
1255
1256 a_Web_dispatch_by_type("text/html", clientWeb, &client->Callback,
1257 &client->CbData);
1258 client->Buf = dStrconcat("<!doctype html><html><body>"
1259 "Dillo blocked a redirection from <a href=\"",
1260 URL_STR(entry->Url), "\">", URL_STR(entry->Url),
1261 "</a> to <a href=\"", URL_STR(entry->Location), "\">",
1262 URL_STR(entry->Location), "</a> based on your domainrc "
1263 "settings.</body></html>", NULL);
1264 client->BufSize = strlen(client->Buf);
1265 (client->Callback)(CA_Send, client);
1266 dFree(client->Buf);
1267}
1268
1281static CacheEntry_t *Cache_process_queue(CacheEntry_t *entry)
1282{
1283 uint_t i;
1284 int st;
1285 const char *Type;
1286 Dstr *data;
1287 CacheClient_t *Client;
1288 DilloWeb *ClientWeb;
1289 BrowserWindow *Client_bw = NULL;
1290 static bool_t Busy = FALSE;
1291 bool_t AbortEntry = FALSE;
1292 bool_t OfferDownload = FALSE;
1293 bool_t TypeMismatch = FALSE;
1294 char *dtype = NULL;
1295 char *dfilename = NULL;
1296
1297 if (Busy)
1298 MSG_ERR("FATAL!: >>>> Cache_process_queue Caught busy!!! <<<<\n");
1299 if (!(entry->Flags & CA_GotHeader))
1300 return entry;
1301 if (!(entry->Flags & CA_GotContentType)) {
1303 entry->Data->str, entry->Data->len, &Type);
1304 _MSG("Cache: detected Content-Type '%s'\n", Type);
1305 if (st == 0 || !(entry->Flags & CA_InProgress)) {
1306 if (a_Misc_content_type_check(entry->TypeHdr, Type) < 0) {
1307 MSG_HTTP("Content-Type '%s' doesn't match the real data.\n",
1308 entry->TypeHdr);
1309 TypeMismatch = TRUE;
1310 }
1311 entry->TypeDet = dStrdup(Type);
1312 entry->Flags |= CA_GotContentType;
1313 } else
1314 return entry; /* i.e., wait for more data */
1315 }
1316
1317 Busy = TRUE;
1318 for (i = 0; (Client = dList_nth_data(ClientQueue, i)); ++i) {
1319 if (Client->Url == entry->Url) {
1320 ClientWeb = Client->Web; /* It was a (void*) */
1321 Client_bw = ClientWeb->bw; /* 'bw' in a local var */
1322
1323 if (ClientWeb->flags & WEB_RootUrl) {
1324 /* Only parse Content-Disposition on root URLs */
1325 if (entry->ContentDisposition) {
1326 a_Misc_parse_content_disposition(entry->ContentDisposition,
1327 &dtype, &dfilename);
1328 }
1329 if (!(entry->Flags & CA_MsgErased)) {
1330 /* clear the "expecting for reply..." message */
1331 a_UIcmd_set_msg(Client_bw, "");
1332 entry->Flags |= CA_MsgErased;
1333 }
1334 if (TypeMismatch) {
1335 a_UIcmd_set_msg(Client_bw,"HTTP warning: Content-Type '%s' "
1336 "doesn't match the real data.", entry->TypeHdr);
1337 OfferDownload = TRUE;
1338 }
1339 if (entry->Flags & CA_Redirect) {
1340 if (!Client->Callback) {
1341 Client->Callback = Cache_null_client;
1342 Client_bw->redirect_level++;
1343 }
1344 } else {
1345 Client_bw->redirect_level = 0;
1346 }
1347 if (entry->Flags & CA_HugeFile) {
1348 a_UIcmd_set_msg(Client_bw, "Huge file! (%d MB)",
1349 entry->ExpectedSize / (1024*1024));
1350 AbortEntry = OfferDownload = TRUE;
1351 }
1352 } else {
1353 /* For non root URLs, ignore redirections and 404 answers */
1354 if (entry->Flags & CA_Redirect || entry->Flags & CA_NotFound)
1355 Client->Callback = Cache_null_client;
1356 }
1357
1358 /* Set the client function */
1359 if (!Client->Callback) {
1360 Client->Callback = Cache_null_client;
1361
1362 if (entry->Location && !(entry->Flags & CA_Redirect)) {
1363 /* Not following redirection, so don't display page body. */
1364 } else {
1365 if (TypeMismatch) {
1366 AbortEntry = TRUE;
1367 } else {
1368 const char *curr_type = Cache_current_content_type(entry);
1369 st = a_Web_dispatch_by_type(curr_type, ClientWeb,
1370 &Client->Callback,
1371 &Client->CbData);
1372 if (st == -1) {
1373 /* MIME type is not viewable */
1374 if (ClientWeb->flags & WEB_RootUrl) {
1375 MSG("Content-Type '%s' not viewable.\n", curr_type);
1376 /* prepare a download offer... */
1377 AbortEntry = OfferDownload = TRUE;
1378 } else {
1379 /* TODO: Resource Type not handled.
1380 * Not aborted to avoid multiple connections on the
1381 * same resource. A better idea is to abort the
1382 * connection and to keep a failed-resource flag in
1383 * the cache entry. */
1384 }
1385 } else if (dtype && dStrnAsciiCasecmp(dtype, "inline", 6) != 0) {
1386 AbortEntry = OfferDownload = TRUE;
1387 }
1388 }
1389 if (AbortEntry) {
1390 if (ClientWeb->flags & WEB_RootUrl)
1391 a_Nav_cancel_expect_if_eq(Client_bw, Client->Url);
1392 a_Bw_remove_client(Client_bw, Client->Key);
1393 Cache_client_dequeue(Client);
1394 --i; /* Keep the index value in the next iteration */
1395 continue;
1396 }
1397 }
1398 }
1399
1400 /* Send data to our client */
1401 if (ClientWeb->flags & WEB_Download) {
1402 /* for download, always provide original data, not translated */
1403 data = entry->Data;
1404 } else {
1405 data = Cache_data(entry);
1406 }
1407 if ((Client->BufSize = data->len) > 0) {
1408 Client->Buf = data->str;
1409 (Client->Callback)(CA_Send, Client);
1410 if (ClientWeb->flags & WEB_RootUrl) {
1411 /* show size of page received */
1412 a_UIcmd_set_page_prog(Client_bw, entry->Data->len, 1);
1413 }
1414 }
1415
1416 /* Remove client when done */
1417 if (!(entry->Flags & CA_InProgress)) {
1418 /* Copy flags to a local var */
1419 int flags = ClientWeb->flags;
1420
1421 if (ClientWeb->flags & WEB_RootUrl && entry->Location &&
1422 !(entry->Flags & CA_Redirect)) {
1424 }
1425 /* We finished sending data, let the client know */
1426 (Client->Callback)(CA_Close, Client);
1427 if (ClientWeb->flags & WEB_RootUrl) {
1428 if (entry->Flags & CA_Aborted) {
1429 a_UIcmd_set_msg(Client_bw, "ERROR: Connection closed early, "
1430 "read not complete.");
1431 }
1432 a_UIcmd_set_page_prog(Client_bw, 0, 0);
1433 }
1434 Cache_client_dequeue(Client);
1435 --i; /* Keep the index value in the next iteration */
1436
1437 /* we assert just one redirect call */
1438 if (entry->Flags & CA_Redirect)
1439 Cache_redirect(entry, flags, Client_bw);
1440 }
1441 }
1442 } /* for */
1443
1444 if (AbortEntry) {
1445 /* Abort the entry, remove it from cache, and maybe offer download. */
1446 DilloUrl *url = a_Url_dup(entry->Url);
1448 entry = NULL;
1449 if (OfferDownload) {
1450 /* Remove entry when 'conn' is already done */
1451 Cache_entry_remove(NULL, url);
1452 if (a_Cache_download_enabled(url)) {
1453 Cache_savelink_t *data = dNew(Cache_savelink_t, 1);
1454 data->bw = Client_bw;
1455 data->url = a_Url_dup(url);
1456 data->filename = dStrdup(dfilename);
1457 a_Timeout_add(0.0, Cache_savelink_cb, data);
1458 }
1459 }
1460 a_Url_free(url);
1461 } else if (entry->Auth && !(entry->Flags & CA_InProgress)) {
1462 Cache_auth_entry(entry, Client_bw);
1463 }
1464
1465 dFree(dtype); dFree(dfilename);
1466
1467 /* Trigger cleanup when there are no cache clients */
1468 if (dList_length(ClientQueue) == 0) {
1470 }
1471
1472 Busy = FALSE;
1473 _MSG("QueueSize ====> %d\n", dList_length(ClientQueue));
1474 return entry;
1475}
1476
1481{
1482 CacheEntry_t *entry;
1483 (void) ptr; /* Unused */
1484
1485 while ((entry = (CacheEntry_t *)dList_nth_data(DelayedQueue, 0))) {
1486 Cache_ref_data(entry);
1487 if ((entry = Cache_process_queue(entry))) {
1488 Cache_unref_data(entry);
1489 dList_remove(DelayedQueue, entry);
1490 }
1491 }
1494}
1495
1499static void Cache_delayed_process_queue(CacheEntry_t *entry)
1500{
1501 /* there's no need to repeat entries in the queue */
1502 if (!dList_find(DelayedQueue, entry))
1503 dList_append(DelayedQueue, entry);
1504
1505 if (DelayedQueueIdleId == 0) {
1506 _MSG(" Setting timeout callback\n");
1509 }
1510}
1511
1518{
1519 int i, n = 0;
1520 CacheClient_t *Client, *iClient;
1521
1522 if ((Client = dList_find_custom(ClientQueue, INT2VOIDP(Key),
1524 for (i = 0; (iClient = dList_nth_data(ClientQueue, i)); ++i) {
1525 if (iClient->Url == Client->Url) {
1526 ++n;
1527 }
1528 }
1529 }
1530 return (n == 1) ? Client : NULL;
1531}
1532
1538{
1539 CacheClient_t *Client;
1540 CacheEntry_t *entry;
1541 DICacheEntry *DicEntry;
1542
1543 /* The client can be in both queues at the same time */
1544 if ((Client = dList_find_custom(ClientQueue, INT2VOIDP(Key),
1546 /* Dicache */
1547 if ((DicEntry = a_Dicache_get_entry(Client->Url, Client->Version)))
1548 a_Dicache_unref(Client->Url, Client->Version);
1549
1550 /* DelayedQueue */
1551 if ((entry = Cache_entry_search(Client->Url)))
1552 dList_remove(DelayedQueue, entry);
1553
1554 /* Main queue */
1555 Cache_client_dequeue(Client);
1556
1557 } else {
1558 _MSG("WARNING: Cache_stop_client, nonexistent client\n");
1559 }
1560}
1561
1562
1567{
1568 CacheClient_t *Client;
1569 void *data;
1570
1571 /* free the client queue */
1572 while ((Client = dList_nth_data(ClientQueue, 0)))
1573 Cache_client_dequeue(Client);
1574
1575 /* Remove every cache entry */
1576 while ((data = dList_nth_data(CachedURLs, 0))) {
1578 Cache_entry_free(data);
1579 }
1580 /* Remove the cache list */
1582}
#define IORead
Definition IO.h:11
#define IOAbort
Definition IO.h:14
#define IOClose
Definition IO.h:13
const char *const AboutSplash
HTML text for startup screen.
Definition about.c:18
int a_Auth_do_auth(Dlist *challenges, const DilloUrl *url)
Given authentication challenge(s), prepare authorization.
Definition auth.c:671
#define _MSG(...)
Definition bookmarks.c:45
static char * Header
Definition bookmarks.c:86
#define MSG(...)
Definition bookmarks.c:46
int a_Bw_remove_client(BrowserWindow *bw, int ClientKey)
Remove the cache-client from the bw's list (client can be a image or a html page) Return: 0 if found,...
Definition bw.c:149
void a_Bw_close_client(BrowserWindow *bw, int ClientKey)
Close a cache-client upon successful retrieval.
Definition bw.c:167
int a_Cache_get_buf(const DilloUrl *Url, char **PBuf, int *BufSize)
Get the pointer to the URL document, and its size, from the cache entry.
Definition cache.c:654
CacheClient_t * a_Cache_client_get_if_unique(int Key)
Last Client for this entry?
Definition cache.c:1517
static void Cache_savelink_cb(void *vdata)
Save link from behind a timeout so that Cache_process_queue() can get on with its work.
Definition cache.c:1239
static void Cache_entry_remove(CacheEntry_t *entry, DilloUrl *url)
Remove an entry, from the cache.
Definition cache.c:347
static void Cache_delayed_process_queue(CacheEntry_t *entry)
Set a call to Cache_process_queue from the main cycle.
Definition cache.c:1499
static void Cache_finish_msg(CacheEntry_t *entry)
Definition cache.c:960
static Dlist * ClientQueue
A list for cache clients.
Definition cache.c:81
static Dstr * Cache_stats(void)
Definition cache.c:386
uint_t a_Cache_get_flags_with_redirection(const DilloUrl *url)
Get cache entry status (following redirections).
Definition cache.c:517
static void Cache_parse_header(CacheEntry_t *entry)
Scan, allocate, and set things according to header info.
Definition cache.c:762
static int Cache_entry_by_url_cmp(const void *v1, const void *v2)
Determine if two cache entries are equal, using a URL as key.
Definition cache.c:110
static void Cache_auth_entry(CacheEntry_t *entry, BrowserWindow *bw)
Set a timeout function to ask for user/password.
Definition cache.c:1174
static CacheEntry_t * Cache_entry_search_with_redirect(const DilloUrl *Url)
Given a URL, find its cache entry, following redirections.
Definition cache.c:231
static Dlist * DelayedQueue
A list for delayed clients (it holds weak pointers to cache entries, which are used to make deferred ...
Definition cache.c:85
static uint_t DelayedQueueIdleId
Definition cache.c:86
static Dlist * CachedURLs
A sorted list for cached data.
Definition cache.c:77
static int Cache_get_header(CacheEntry_t *entry, const char *buf, size_t buf_size)
Consume bytes until the whole header is got (up to a "\r\n\r\n" sequence) (Also unfold multi-line fie...
Definition cache.c:929
static void Cache_entry_init(CacheEntry_t *NewEntry, const DilloUrl *Url)
Set safe values for a new cache entry.
Definition cache.c:196
static const char * Cache_current_content_type(CacheEntry_t *entry)
Get current content type.
Definition cache.c:565
const char * a_Cache_set_content_type(const DilloUrl *url, const char *ctype, const char *from)
Change Content-Type for cache entry found by url.
Definition cache.c:594
static void Cache_client_dequeue(CacheClient_t *Client)
Remove a client from the queue.
Definition cache.c:181
static void Cache_auth_free(Dlist *auth)
Free Authentication fields.
Definition cache.c:308
void a_Cache_unref_buf(const DilloUrl *Url)
Unreference the data buffer when no longer using it.
Definition cache.c:673
static void Cache_null_client(int Op, CacheClient_t *Client)
Don't process data any further, but let the cache fill the entry.
Definition cache.c:1212
static char * Cache_parse_field(const char *header, const char *fieldname)
Extract a single field from the header, allocating and storing the value in 'field'.
Definition cache.c:685
static void Cache_auth_callback(void *vdata)
Ask for user/password and reload the page.
Definition cache.c:1159
const char * a_Cache_get_content_type(const DilloUrl *url)
Get current Content-Type for cache entry found by URL.
Definition cache.c:574
static void Cache_ref_data(CacheEntry_t *entry)
Reference the cache data.
Definition cache.c:526
static CacheEntry_t * Cache_process_queue(CacheEntry_t *entry)
Update cache clients for a single cache-entry Tasks:
Definition cache.c:1281
static void Cache_provide_redirection_blocked_page(CacheEntry_t *entry, CacheClient_t *client)
Let the client know that we're not following a redirection.
Definition cache.c:1251
void a_Cache_entry_remove_by_url(DilloUrl *url)
Wrapper for capi.
Definition cache.c:379
static CacheEntry_t * Cache_entry_search(const DilloUrl *Url)
Get the data structure for a cached URL (using 'Url' as the search key) If 'Url' isn't cached,...
Definition cache.c:223
static Dstr * Cache_data(CacheEntry_t *entry)
Get pointer to entry's data.
Definition cache.c:584
static int Cache_client_enqueue(const DilloUrl *Url, DilloWeb *Web, CA_Callback_t Callback, void *CbData)
Add a client to ClientQueue.
Definition cache.c:144
bool_t a_Cache_process_dbuf(int Op, const char *buf, size_t buf_size, const DilloUrl *Url)
Receive new data, update the reception buffer (for next read), update the cache, and service the clie...
Definition cache.c:1002
int a_Cache_download_enabled(const DilloUrl *url)
Check whether a URL scheme is downloadable.
Definition cache.c:1198
static void Cache_entry_free(CacheEntry_t *entry)
Free the components of a CacheEntry_t struct.
Definition cache.c:320
static int Cache_internal_url(CacheEntry_t *entry)
Definition cache.c:427
static void Cache_unref_data(CacheEntry_t *entry)
Unreference the cache data.
Definition cache.c:544
#define HUGE_FILESIZE
Maximum filesize for a URL, before offering a download.
Definition cache.c:44
static int Cache_entry_cmp(const void *v1, const void *v2)
Determine if two cache entries are equal (used by CachedURLs)
Definition cache.c:100
void a_Cache_init(void)
Initialize cache data.
Definition cache.c:121
static void Cache_delayed_process_queue_callback(void *ptr)
Callback function for Cache_delayed_process_queue.
Definition cache.c:1480
static CacheEntry_t * Cache_entry_add(const DilloUrl *Url)
Allocate and set a new entry in the cache list.
Definition cache.c:255
#define MAX_INIT_BUF
Maximum initial size for the automatically-growing data buffer.
Definition cache.c:42
static int Cache_client_by_key_cmp(const void *client, const void *key)
Compare function for searching a Client by its key.
Definition cache.c:173
void a_Cache_entry_inject(const DilloUrl *Url, Dstr *data_ds)
Inject full page content directly into the cache.
Definition cache.c:290
static int Cache_bufsize(CacheEntry_t *e)
Compute the actual size occupied by a cache entry.
Definition cache.c:274
static int Cache_redirect(CacheEntry_t *entry, int Flags, BrowserWindow *bw)
Process redirections (HTTP 30x answers) (This is a work in progress –not finished yet)
Definition cache.c:1101
int a_Cache_open_url(void *web, CA_Callback_t Call, void *CbData)
Try finding the url in the cache.
Definition cache.c:459
static Dlist * Cache_parse_multiple_fields(const char *header, const char *fieldname)
Extract multiple fields from the header.
Definition cache.c:719
void a_Cache_freeall(void)
Memory deallocator (only called at exit time)
Definition cache.c:1566
uint_t a_Cache_get_flags(const DilloUrl *url)
Get cache entry status.
Definition cache.c:508
void a_Cache_stop_client(int Key)
Remove a client from the client queue TODO: notify the dicache and upper layers.
Definition cache.c:1537
#define CA_Send
Definition cache.h:27
#define CA_KeepAlive
Definition cache.h:48
#define CA_NotFound
Definition cache.h:41
#define CA_InternalUrl
Definition cache.h:45
void(* CA_Callback_t)(int Op, CacheClient_t *Client)
Callback type for cache clients.
Definition cache.h:55
#define CA_Redirect
Definition cache.h:38
#define CA_ForceRedirect
Definition cache.h:39
#define CA_RedirectLoop
Definition cache.h:44
#define CA_IsEmpty
Definition cache.h:47
#define CA_GotContentType
Definition cache.h:35
#define CA_Aborted
Definition cache.h:42
#define CA_Close
Definition cache.h:28
#define CA_HugeFile
Definition cache.h:46
#define CA_MsgErased
Definition cache.h:43
#define CA_TempRedirect
Definition cache.h:40
#define CA_InProgress
Definition cache.h:37
#define CA_GotHeader
Definition cache.h:34
#define CA_GotLength
Definition cache.h:36
void a_Capi_conn_abort_by_url(const DilloUrl *url)
Abort the connection for a given url, using its CCC.
Definition capi.c:200
unsigned int uint_t
Definition d_size.h:20
unsigned char bool_t
Definition d_size.h:21
Decode * a_Decode_charset_init(const char *format)
Initialize decoder to translate from any character set known to iconv() to UTF-8.
Definition decode.c:453
Dstr * a_Decode_transfer_process(DecodeTransfer *dc, const char *instr, int inlen)
Decode 'Transfer-Encoding: chunked' data.
Definition decode.c:33
void a_Decode_transfer_free(DecodeTransfer *dc)
Definition decode.c:97
Decode * a_Decode_content_init(const char *format)
Initialize content decoder.
Definition decode.c:410
DecodeTransfer * a_Decode_transfer_init(const char *format)
Initialize transfer decoder.
Definition decode.c:374
Dstr * a_Decode_process(Decode *dc, const char *instr, int inlen)
Decode data.
Definition decode.c:480
void a_Decode_free(Decode *dc)
Definition decode.c:488
bool_t a_Decode_transfer_finished(DecodeTransfer *dc)
Definition decode.c:92
DICacheEntry * a_Dicache_get_entry(const DilloUrl *Url, int version)
Search a particular version of a URL in the Dicache.
Definition dicache.c:156
void a_Dicache_cleanup(void)
Free the imgbuf (RGB data) of unused entries.
Definition dicache.c:574
void a_Dicache_invalidate_entry(const DilloUrl *Url)
Invalidate this entry.
Definition dicache.c:246
void a_Dicache_unref(const DilloUrl *Url, int version)
Unrefs the counter of a dicache entry (it counts cache clients).
Definition dicache.c:214
Dstr * a_Dicache_stats(void)
Definition dicache.c:617
char * dStrconcat(const char *s1,...)
Concatenate a NULL-terminated list of strings.
Definition dlib.c:102
void dList_insert_sorted(Dlist *lp, void *data, dCompareFunc func)
Insert an element into a sorted list.
Definition dlib.c:797
void dFree(void *mem)
Definition dlib.c:68
int dStrAsciiCasecmp(const char *s1, const char *s2)
Definition dlib.c:203
void dStr_sprintfa(Dstr *ds, const char *format,...)
Printf-like function that appends.
Definition dlib.c:464
void dStr_append(Dstr *ds, const char *s)
Append a C string to a Dstr.
Definition dlib.c:316
char * dStrdup(const char *s)
Definition dlib.c:77
Dlist * dList_new(int size)
Create a new empty list.
Definition dlib.c:576
Dstr * dStr_sized_new(int sz)
Create a new string with a given size.
Definition dlib.c:254
int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n)
Definition dlib.c:215
void dStr_erase(Dstr *ds, int pos_0, int len)
Erase a substring.
Definition dlib.c:388
int dList_length(Dlist *lp)
For completing the ADT.
Definition dlib.c:641
void dStr_shorten(Dstr *dst, const char *src, int n)
Shorten string so it fits in n characters.
Definition dlib.c:551
void * dList_nth_data(Dlist *lp, int n0)
Return the nth data item, NULL when not found or 'n0' is out of range.
Definition dlib.c:690
void dList_remove_fast(Dlist *lp, const void *data)
Remove a data item without preserving order.
Definition dlib.c:651
void dStr_free(Dstr *ds, int all)
Free a dillo string.
Definition dlib.c:337
void dStr_append_l(Dstr *ds, const char *s, int l)
Append a C string to a Dstr (providing length).
Definition dlib.c:308
void dStr_append_c(Dstr *ds, int c)
Append one character.
Definition dlib.c:349
char * dStrndup(const char *s, size_t sz)
Definition dlib.c:88
Dstr * dStr_new(const char *s)
Create a new string.
Definition dlib.c:325
void dList_append(Dlist *lp, void *data)
Append a data item to the list.
Definition dlib.c:625
void * dList_find_sorted(Dlist *lp, const void *data, dCompareFunc func)
Search a sorted list.
Definition dlib.c:824
void dList_free(Dlist *lp)
Free a list (not its elements)
Definition dlib.c:592
void * dList_find_custom(Dlist *lp, const void *data, dCompareFunc func)
Search a data item using a custom function.
Definition dlib.c:732
void dStr_fit(Dstr *ds)
Return memory if there's too much allocated.
Definition dlib.c:269
void dStr_truncate(Dstr *ds, int len)
Truncate a Dstr to be 'len' bytes long.
Definition dlib.c:368
void dList_remove(Dlist *lp, const void *data)
Definition dlib.c:669
void * dList_find(Dlist *lp, const void *data)
Return the found data item, or NULL if not present.
Definition dlib.c:700
#define MIN(a, b)
Definition dlib.h:42
#define dReturn_val_if_fail(expr, val)
Definition dlib.h:88
#define D_ASCII_TOLOWER(c)
Definition dlib.h:49
#define VOIDP2INT(p)
Definition dlib.h:55
#define TRUE
Definition dlib.h:35
#define FALSE
Definition dlib.h:31
#define INT2VOIDP(i)
Definition dlib.h:56
#define MAX(a, b)
Definition dlib.h:39
#define dNew(type, count)
Definition dlib.h:61
bool_t a_Domain_permit(const DilloUrl *source, const DilloUrl *dest)
Is the resource at 'source' permitted to request the resource at 'dest'?
Definition domain.c:116
#define MSG_ERR(...)
Definition dpid_common.h:23
void a_Hsts_set(const char *header, const DilloUrl *url)
The response for this url had an HSTS header, so let's take action.
Definition hsts.c:201
#define _MSG_WARN(...)
Definition msg.h:15
#define MSG_WARN(...)
Definition msg.h:26
int a_Misc_content_type_check(const char *EntryType, const char *DetectedType)
Check the server-supplied 'Content-Type' against our detected type.
Definition misc.c:321
int a_Misc_content_type_cmp(const char *ct1, const char *ct2)
Compare two Content-Type strings.
Definition misc.c:274
int a_Misc_get_content_type_from_data(void *Data, size_t Size, const char **PT)
Detects 'Content-Type' from a data stream sample.
Definition misc.c:136
void a_Misc_parse_content_type(const char *type, char **major, char **minor, char **charset)
Parse Content-Type string, e.g., "text/html; charset=utf-8".
Definition misc.c:210
static void a_Misc_parse_content_disposition(const char *disposition, char **type, char **filename)
Parse Content-Disposition string, e.g., "attachment; filename="file name.jpg"".
Definition misc.h:30
void a_Nav_cancel_expect_if_eq(BrowserWindow *bw, const DilloUrl *url)
Definition nav.c:260
void a_Nav_reload(BrowserWindow *bw)
Definition nav.c:542
void a_Nav_push(BrowserWindow *bw, const DilloUrl *url, const DilloUrl *requester)
Definition nav.c:342
DilloPrefs prefs
Global Data.
Definition prefs.c:33
void a_Cookies_set(Dlist *cookie_strings, const DilloUrl *set_url, const char *date)
Set the value corresponding to the cookie string.
Definition cookies.c:142
#define MSG_HTTP(...)
Definition msg.h:25
Contains the specific data for a single window.
Definition bw.h:27
int redirect_level
Counter for the number of hops on a redirection.
Definition bw.h:63
Data structure for cache clients.
Definition cache.h:60
CA_Callback_t Callback
Client function.
Definition cache.h:66
const DilloUrl * Url
Pointer to a cache entry Url.
Definition cache.h:62
int Key
Primary Key for this client.
Definition cache.h:61
int Version
Dicache version of this Url (0 if not used)
Definition cache.h:63
void * CbData
Client function data.
Definition cache.h:67
uint_t BufSize
Valid size of cache-data.
Definition cache.h:65
void * Buf
Pointer to cache-data.
Definition cache.h:64
void * Web
Pointer to the Web structure of our client.
Definition cache.h:68
bool_t http_strict_transport_security
Definition prefs.h:105
Definition url.h:88
Definition dlib.h:144
Definition dlib.h:114
Dstr_char_t * str
Definition dlib.h:117
int len
Definition dlib.h:116
int flags
Additional info.
Definition web.hh:29
DilloUrl * url
Requested URL.
Definition web.hh:25
DilloUrl * requester
URL that caused this request, or < NULL if user-initiated.
Definition web.hh:26
BrowserWindow * bw
The requesting browser window [reference].
Definition web.hh:28
void a_Timeout_add(float t, TimeoutCb_t cb, void *cbdata)
Hook a one-time timeout function 'cb' after 't' seconds with 'cbdata" as its data.
Definition timeout.cc:25
void a_Timeout_remove()
Stop running a timeout function.
Definition timeout.cc:41
void a_UIcmd_set_msg(BrowserWindow *bw, const char *format,...)
Definition uicmd.cc:1609
void a_UIcmd_set_page_prog(BrowserWindow *bw, size_t nbytes, int cmd)
Definition uicmd.cc:1556
void a_UIcmd_save_link(BrowserWindow *bw, const DilloUrl *url, char *filename)
Definition uicmd.cc:1274
void a_Url_set_flags(DilloUrl *u, int flags)
Set DilloUrl flags.
Definition url.c:527
int a_Url_cmp(const DilloUrl *A, const DilloUrl *B)
Compare two Url's to check if they're the same, or which one is bigger.
Definition url.c:506
void a_Url_free(DilloUrl *url)
Free a DilloUrl.
Definition url.c:208
bool_t a_Url_same_organization(const DilloUrl *u1, const DilloUrl *u2)
Definition url.c:798
DilloUrl * a_Url_new(const char *url_str, const char *base_url)
Transform (and resolve) an URL string into the respective DilloURL.
Definition url.c:371
int a_Url_host_type(const char *host)
What type of host is this?
Definition url.c:683
DilloUrl * a_Url_dup(const DilloUrl *ori)
Duplicate a Url structure.
Definition url.c:477
#define URL_HOST_NAME
Definition url.h:24
#define URL_PATH(u)
Definition url.h:72
#define URL_E2EQuery
Definition url.h:35
#define URL_SpamSafe
Definition url.h:40
#define URL_FLAGS(u)
Definition url.h:79
#define URL_STR(u)
Definition url.h:76
#define URL_STR_(u)
Definition url.h:55
#define URL_SCHEME(u)
Definition url.h:70
#define URL_Post
Definition url.h:33
#define URL_Get
Definition url.h:32
#define URL_HOST(u)
Definition url.h:75
int a_Web_dispatch_by_type(const char *Type, DilloWeb *Web, CA_Callback_t *Call, void **Data)
Given the MIME content type, and a fd to read it from, this function connects the proper MIME viewer ...
Definition web.cc:50
void a_Web_free(DilloWeb *web)
Deallocate a DilloWeb structure.
Definition web.cc:152
#define WEB_RootUrl
Definition web.hh:16
#define WEB_Download
Definition web.hh:19