-
Notifications
You must be signed in to change notification settings - Fork 1
/
telescope.c
1254 lines (1062 loc) · 27.4 KB
/
telescope.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2021, 2024 Omar Polo <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "compat.h"
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "certs.h"
#include "cmd.h"
#include "control.h"
#include "defaults.h"
#include "ev.h"
#include "exec.h"
#include "fs.h"
#include "hist.h"
#include "imsgev.h"
#include "iri.h"
#include "keymap.h"
#include "mailcap.h"
#include "mcache.h"
#include "minibuffer.h"
#include "parser.h"
#include "parser.h"
#include "session.h"
#include "telescope.h"
#include "tofu.h"
#include "ui.h"
#include "utils.h"
#include "xwrapper.h"
static const struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"safe", no_argument, NULL, 'S'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0},
};
static const char *opts = "c:hnST:v";
static int has_url;
static char url[GEMINI_URL_LEN];
/*
* Used to know when we're finished loading.
*/
int operating;
/*
* "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
* anything to the filesystem or execute external programs.
*/
int safe_mode;
static struct imsgev *iev_net;
struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
struct tabshead ktabshead = TAILQ_HEAD_INITIALIZER(ktabshead);
struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
enum telescope_process {
PROC_UI,
PROC_NET,
};
#define CANNOT_FETCH 0
#define TOO_MANY_REDIRECTS 1
#define MALFORMED_RESPONSE 2
#define UNKNOWN_TYPE_OR_CSET 3
#define UNKNOWN_PROTOCOL 4
/* XXX: keep in sync with normalize_code */
const char *err_pages[70] = {
[CANNOT_FETCH] = "# Couldn't load the page\n",
[TOO_MANY_REDIRECTS] = "# Too many redirects\n",
[MALFORMED_RESPONSE] = "# Got a malformed response\n",
[UNKNOWN_TYPE_OR_CSET] = "# Unsupported document format or charset\n",
[UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
[10] = "# Input required\n",
[11] = "# Input required\n",
[40] = "# Temporary failure\n",
[41] = "# Server unavailable\n",
[42] = "# CGI error\n",
[43] = "# Proxy error\n",
[44] = "# Slow down\n",
[50] = "# Permanent failure\n",
[51] = "# Not found\n",
[52] = "# Gone\n",
[53] = "# Proxy request refused\n",
[59] = "# Bad request\n",
[60] = "# Client certificate required\n",
[61] = "# Certificate not authorised\n",
[62] = "# Certificate not valid\n"
};
static void die(void) __attribute__((__noreturn__));
static struct tab *tab_by_id(uint32_t);
static void handle_imsg_check_cert(struct imsg *);
static void handle_check_cert_user_choice(int, void *);
static void handle_maybe_save_new_cert(int, void *);
static void handle_maybe_save_page(int, void *);
static void handle_save_page_path(const char *, struct tab *);
static void handle_dispatch_imsg(int, int, void *);
static void load_about_url(struct tab *, const char *);
static void load_file_url(struct tab *, const char *);
static void load_finger_url(struct tab *, const char *);
static void load_gemini_url(struct tab *, const char *);
static void load_gopher_url(struct tab *, const char *);
static void load_via_proxy(struct tab *, const char *,
struct proxy *);
static void make_request(struct tab *, struct get_req *, int,
const char *);
static void do_load_url(struct tab *, const char *, const char *, int);
static pid_t start_child(enum telescope_process, const char *, int);
static void send_url(const char *);
static const struct proto {
const char *schema;
const char *port;
void (*loadfn)(struct tab *, const char *);
} protos[] = {
{"about", NULL, load_about_url},
{"file", NULL, load_file_url},
{"finger", "79", load_finger_url},
{"gemini", "1965", load_gemini_url},
{"gopher", "70", load_gopher_url},
{NULL, NULL, NULL},
};
static struct ohash certs;
static void __attribute__((__noreturn__))
die(void)
{
abort(); /* TODO */
}
static struct tab *
tab_by_id(uint32_t id)
{
struct tab *t;
TAILQ_FOREACH(t, &tabshead, tabs) {
if (t->id == id)
return t;
}
return NULL;
}
static void
handle_imsg_check_cert(struct imsg *imsg)
{
char *hash;
const char *host, *port;
int tofu_res;
struct ibuf ibuf;
struct tofu_entry *e;
struct tab *tab;
size_t datalen;
if (imsg_get_ibuf(imsg, &ibuf) == -1 ||
ibuf_borrow_str(&ibuf, &hash) == -1)
abort();
datalen = strlen(hash);
if ((tab = tab_by_id(imsg_get_id(imsg))) == NULL)
return;
if (tab->proxy != NULL) {
host = tab->proxy->host;
port = tab->proxy->port;
} else {
host = tab->iri.iri_host;
port = tab->iri.iri_portstr;
}
if ((e = tofu_lookup(&certs, host, port)) == NULL) {
/* TODO: an update in libressl/libretls changed
* significantly. Find a better approach at storing
* the certs! */
if (datalen > sizeof(e->hash))
abort();
tofu_res = 1; /* trust on first use */
e = xcalloc(1, sizeof(*e));
strlcpy(e->domain, host, sizeof(e->domain));
if (*port != '\0' && strcmp(port, "1965")) {
strlcat(e->domain, ":", sizeof(e->domain));
strlcat(e->domain, port, sizeof(e->domain));
}
strlcpy(e->hash, hash, sizeof(e->hash));
tofu_save(&certs, e);
} else
tofu_res = !strcmp(hash, e->hash);
if (tofu_res) {
if (e->verified == -1)
tab->trust = TS_TEMP_TRUSTED;
else if (e->verified == 1)
tab->trust = TS_VERIFIED;
else
tab->trust = TS_TRUSTED;
ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid, -1,
&tofu_res, sizeof(tofu_res));
} else {
tab->trust = TS_UNTRUSTED;
load_page_from_str(tab, "# Certificate mismatch\n");
tab->cert = xstrdup(hash);
ui_yornp("Certificate mismatch. Proceed?",
handle_check_cert_user_choice, tab);
}
}
static void
handle_check_cert_user_choice(int accept, void *d)
{
struct tab *tab = d;
ui_send_net(IMSG_CERT_STATUS, tab->id, -1, &accept,
sizeof(accept));
if (accept) {
const char *host, *port;
host = tab->iri.iri_host;
port = tab->iri.iri_portstr;
/*
* trust the certificate for this session only. If
* the page results in a redirect while we're asking
* the user to save, we'll end up with an invalid
* tabid (one request == one tab id). It also makes
* sense to save it for the current session if the
* user accepted it.
*/
tofu_temp_trust(&certs, host, port, tab->cert);
if (!safe_mode)
ui_yornp("Save the new certificate?",
handle_maybe_save_new_cert, tab);
else
message("Certificate temporarly trusted");
} else {
free(tab->cert);
tab->cert = NULL;
}
}
static void
handle_maybe_save_new_cert(int accept, void *data)
{
struct tab *tab = data;
struct tofu_entry *e;
const char *host, *port;
if (tab->proxy != NULL) {
host = tab->proxy->host;
port = tab->proxy->port;
} else {
host = tab->iri.iri_host;
port = tab->iri.iri_portstr;
}
if (!accept)
goto end;
e = xcalloc(1, sizeof(*e));
strlcpy(e->domain, host, sizeof(e->domain));
if (*port != '\0' && strcmp(port, "1965")) {
strlcat(e->domain, ":", sizeof(e->domain));
strlcat(e->domain, port, sizeof(e->domain));
}
strlcpy(e->hash, tab->cert, sizeof(e->hash));
tofu_update_persist(&certs, e);
tab->trust = TS_TRUSTED;
end:
free(tab->cert);
tab->cert = NULL;
}
static inline int
normalize_code(int n)
{
if (n < 20) {
if (n == 10 || n == 11)
return n;
return 10;
} else if (n < 30) {
return 20;
} else if (n < 40) {
if (n == 30 || n == 31)
return n;
return 30;
} else if (n < 50) {
if (n <= 44)
return n;
return 40;
} else if (n < 60) {
if (n <= 53 || n == 59)
return n;
return 50;
} else if (n < 70) {
if (n <= 62)
return n;
return 60;
} else
return MALFORMED_RESPONSE;
}
static void
handle_request_response(struct tab *tab)
{
char buf[128];
if (tab->code != 30 && tab->code != 31)
tab->redirect_count = 0;
if (tab->code < 10) { /* internal errors */
load_page_from_str(tab, err_pages[tab->code]);
} else if (tab->code < 20) { /* 1x */
free(tab->last_input_url);
tab->last_input_url = xstrdup(hist_cur(tab->hist));
load_page_from_str(tab, err_pages[tab->code]);
ui_require_input(tab, tab->code == 11, ir_select_gemini);
} else if (tab->code == 20) {
history_add(hist_cur(tab->hist));
if (setup_parser_for(tab)) {
ui_send_net(IMSG_PROCEED, tab->id, -1, NULL, 0);
} else if (safe_mode) {
load_page_from_str(tab,
err_pages[UNKNOWN_TYPE_OR_CSET]);
} else {
hist_prev(tab->hist);
snprintf(buf, sizeof(buf),
"Can't display \"%s\", save it?", tab->meta);
ui_yornp(buf, handle_maybe_save_page, tab);
}
} else if (tab->code < 40) { /* 3x */
tab->redirect_count++;
/* TODO: make customizable? */
if (tab->redirect_count > 5) {
load_page_from_str(tab,
err_pages[TOO_MANY_REDIRECTS]);
} else
do_load_url(tab, tab->meta, hist_cur(tab->hist),
LU_MODE_NOCACHE);
} else { /* 4x, 5x & 6x */
load_page_from_str(tab, err_pages[tab->code]);
if (tab->code >= 60) {
cmd_use_certificate(&tab->buffer);
ui_force_tab_refresh(tab);
}
}
}
static void
handle_maybe_save_page(int dosave, void *data)
{
struct tab *tab = data;
const char *f;
char input[PATH_MAX];
/* XXX: this print a message that is confusing */
ui_on_tab_loaded(tab);
if (!dosave) {
stop_tab(tab);
return;
}
if ((f = strrchr(tab->iri.iri_path, '/')) == NULL)
f = "";
else
f++;
strlcpy(input, download_path, sizeof(input));
strlcat(input, f, sizeof(input));
ui_read("Save to path", handle_save_page_path, tab, input);
}
static void
handle_save_page_path(const char *path, struct tab *tab)
{
struct download *d;
int fd;
if (path == NULL) {
stop_tab(tab);
return;
}
if ((fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, 0644)) == -1) {
message("Can't open file %s: %s", path, strerror(errno));
stop_tab(tab);
return;
}
ui_show_downloads_pane();
d = enqueue_download(tab->id, path, tab->meta);
d->fd = fd;
ui_send_net(IMSG_PROCEED, d->id, -1, NULL, 0);
/*
* Change this tab id, the old one is associated with the
* download now.
*/
tab->id = tab_new_id();
}
static void
handle_dispatch_imsg(int fd, int event, void *data)
{
struct imsgev *iev = data;
struct imsgbuf *imsgbuf = &iev->ibuf;
struct imsg imsg;
struct ibuf ibuf;
struct tab *tab;
struct download *d;
const char *h;
char *str, *page;
ssize_t n;
int code;
if (event & EV_READ) {
if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
err(1, "imsg_read");
if (n == 0)
err(1, "connection closed");
}
if (event & EV_WRITE) {
if ((n = msgbuf_write(&imsgbuf->w)) == -1 && errno != EAGAIN)
err(1, "msgbuf_write");
if (n == 0)
err(1, "connection closed");
}
for (;;) {
if ((n = imsg_get(imsgbuf, &imsg)) == -1)
err(1, "imsg_get");
if (n == 0)
break;
switch (imsg_get_type(&imsg)) {
case IMSG_ERR:
if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
break;
if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
ibuf_borrow_str(&ibuf, &str) == -1)
die();
xasprintf(&page, "# Error loading %s\n\n> %s\n",
hist_cur(tab->hist), str);
load_page_from_str(tab, page);
free(page);
break;
case IMSG_CHECK_CERT:
handle_imsg_check_cert(&imsg);
break;
case IMSG_REPLY:
if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
break;
if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
ibuf_get(&ibuf, &code, sizeof(code)) == -1 ||
ibuf_borrow_str(&ibuf, &str) == -1)
die();
if (strlcpy(tab->meta, str, sizeof(tab->meta)) >=
sizeof(tab->meta))
die();
tab->code = normalize_code(code);
handle_request_response(tab);
break;
case IMSG_BUF:
if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
return;
if (tab) {
if (!parser_parse(&tab->buffer, imsg.data,
imsg_get_len(&imsg)))
die();
ui_on_tab_refresh(tab);
} else {
size_t datalen = imsg_get_len(&imsg);
d->bytes += datalen;
write(d->fd, imsg.data, datalen);
ui_on_download_refresh();
}
break;
case IMSG_FAULTY_GEMSERVER:
if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
return;
if (tab) {
tab->faulty_gemserver = 1;
ui_on_tab_refresh(tab);
} else {
message("Download interrupted!");
}
break;
case IMSG_EOF:
if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
return;
if (tab == NULL && d != NULL) {
download_finished(d);
return;
}
if (tab != NULL) {
if (!parser_free(tab))
die();
h = hist_cur(tab->hist);
if (!strncmp(h, "gemini://", 9) ||
!strncmp(h, "gopher://", 9) ||
!strncmp(h, "finger://", 9))
mcache_tab(tab);
/*
* Gemini is handled as soon as a 2x
* reply is got.
*/
if (!strncmp(h, "finger://", 9) ||
!strncmp(h, "gopher://", 9))
history_add(h);
ui_on_tab_refresh(tab);
ui_on_tab_loaded(tab);
}
break;
default:
errx(1, "got unknown imsg %d", imsg_get_type(&imsg));
}
imsg_free(&imsg);
}
imsg_event_add(iev);
}
static void
load_about_url(struct tab *tab, const char *url)
{
tab->trust = TS_TRUSTED;
fs_load_url(tab, url);
ui_on_tab_refresh(tab);
ui_on_tab_loaded(tab);
}
static void
load_file_url(struct tab *tab, const char *url)
{
tab->trust = TS_TRUSTED;
fs_load_url(tab, url);
ui_on_tab_refresh(tab);
ui_on_tab_loaded(tab);
}
static void
load_finger_url(struct tab *tab, const char *url)
{
struct get_req req;
const char *path;
memset(&req, 0, sizeof(req));
strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
/*
* Sometimes the finger url have the user as path component
* (e.g. finger://thelambdalab.xyz/plugd), sometimes as
* userinfo (e.g. finger://[email protected]).
*/
if (tab->iri.iri_flags & IH_UINFO) {
strlcpy(req.req, tab->iri.iri_uinfo, sizeof(req.req));
} else {
path = tab->iri.iri_path;
while (*path == '/')
++path;
strlcpy(req.req, path, sizeof(req.req));
}
strlcat(req.req, "\r\n", sizeof(req.req));
parser_init(&tab->buffer, &textplain_parser);
make_request(tab, &req, PROTO_FINGER, NULL);
}
static void
load_gemini_url(struct tab *tab, const char *url)
{
struct get_req req;
memset(&req, 0, sizeof(req));
strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
make_request(tab, &req, PROTO_GEMINI, hist_cur(tab->hist));
}
static inline const char *
gopher_skip_selector(const char *path, int *ret_type)
{
*ret_type = 0;
if (!strcmp(path, "/") || *path == '\0') {
*ret_type = '1';
return path;
}
if (*path != '/')
return path;
path++;
switch (*ret_type = *path) {
case '0':
case '1':
case '7':
break;
default:
*ret_type = 0;
path -= 1;
return path;
}
return ++path;
}
static void
load_gopher_url(struct tab *tab, const char *url)
{
struct get_req req;
int type;
const char *path;
memset(&req, 0, sizeof(req));
strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
path = gopher_skip_selector(tab->iri.iri_path, &type);
switch (type) {
case '0':
parser_init(&tab->buffer, &textplain_parser);
break;
case '1':
parser_init(&tab->buffer, &gophermap_parser);
break;
case '7':
free(tab->last_input_url);
tab->last_input_url = xstrdup(url);
ui_require_input(tab, 0, ir_select_gopher);
load_page_from_str(tab, err_pages[10]);
return;
default:
load_page_from_str(tab, "Unknown gopher selector");
return;
}
if (iri_urlunescape(path, req.req, sizeof(req.req)) == -1)
strlcpy(req.req, path, sizeof(req.req));
if (tab->iri.iri_flags & IH_QUERY) {
strlcat(req.req, "?", sizeof(req.req));
strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
}
strlcat(req.req, "\r\n", sizeof(req.req));
make_request(tab, &req, PROTO_GOPHER, NULL);
}
static void
load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
{
struct get_req req;
memset(&req, 0, sizeof(req));
strlcpy(req.host, p->host, sizeof(req.host));
strlcpy(req.port, p->port, sizeof(req.port));
tab->proxy = p;
make_request(tab, &req, p->proto, hist_cur(tab->hist));
}
static void
make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
{
int use_cert = 0, fd = -1;
if (proto == PROTO_GEMINI) {
tab->client_cert = cert_for(&tab->iri, &tab->client_cert_temp);
use_cert = (tab->client_cert != NULL);
}
stop_tab(tab);
tab->id = tab_new_id();
tab->faulty_gemserver = 0;
req->proto = proto;
if (r != NULL) {
strlcpy(req->req, r, sizeof(req->req));
strlcat(req->req, "\r\n", sizeof(req->req));
}
start_loading_anim(tab);
if (!use_cert)
tab->client_cert = NULL;
if (use_cert && (fd = cert_open(tab->client_cert)) == -1) {
tab->client_cert = NULL;
message("failed to open certificate: %s", strerror(errno));
}
ui_send_net(IMSG_GET, tab->id, fd, req, sizeof(*req));
}
void
gopher_send_search_req(struct tab *tab, const char *text)
{
struct get_req req;
memset(&req, 0, sizeof(req));
strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
/* +2 to skip /7 */
strlcpy(req.req, tab->iri.iri_path+2, sizeof(req.req));
if (tab->iri.iri_flags & IH_QUERY) {
strlcat(req.req, "?", sizeof(req.req));
strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
}
strlcat(req.req, "\t", sizeof(req.req));
strlcat(req.req, text, sizeof(req.req));
strlcat(req.req, "\r\n", sizeof(req.req));
erase_buffer(&tab->buffer);
parser_init(&tab->buffer, &gophermap_parser);
make_request(tab, &req, PROTO_GOPHER, NULL);
}
void
load_page_from_str(struct tab *tab, const char *page)
{
parser_init(&tab->buffer, &gemtext_parser);
if (!parser_parse(&tab->buffer, page, strlen(page)))
abort();
if (!parser_free(tab))
abort();
ui_on_tab_refresh(tab);
ui_on_tab_loaded(tab);
}
static void
do_load_url_cmd(int res, void *d)
{
struct tab *tab = d;
const char *argv[3];
if (!res)
return;
argv[0] = DEFAULT_OPENER;
argv[1] = hist_cur(tab->hist);
argv[2] = NULL;
exec_cmd((char **)argv, EXEC_BACKGROUND);
message("Link opened with %s", DEFAULT_OPENER);
}
/*
* Effectively load the given url in the given tab.
*/
static void
do_load_url(struct tab *tab, const char *url, const char *base, int mode)
{
const struct proto *p;
struct proxy *proxy;
int nocache = mode & LU_MODE_NOCACHE;
char *t;
char buf[1025];
tab->proxy = NULL;
tab->trust = TS_UNKNOWN;
if (iri_parse(base, url, &tab->iri) == -1) {
xasprintf(&t, "# error loading %s\n>%s\n", url,
"Can't parse the IRI");
hist_set_cur(tab->hist, url);
load_page_from_str(tab, t);
free(t);
return;
}
iri_unparse(&tab->iri, buf, sizeof(buf));
hist_set_cur(tab->hist, buf);
if (!nocache && mcache_lookup(buf, tab)) {
ui_on_tab_refresh(tab);
ui_on_tab_loaded(tab);
return;
}
for (p = protos; p->schema != NULL; ++p) {
if (!strcmp(tab->iri.iri_scheme, p->schema)) {
/* patch the port */
if (*tab->iri.iri_portstr == '\0' &&
p->port != NULL)
iri_setport(&tab->iri, p->port);
p->loadfn(tab, buf);
return;
}
}
TAILQ_FOREACH(proxy, &proxies, proxies) {
if (!strcmp(tab->iri.iri_scheme, proxy->match_proto)) {
load_via_proxy(tab, url, proxy);
return;
}
}
load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
ui_yornp("Open page with "DEFAULT_OPENER"?",
do_load_url_cmd, tab);
}
/*
* Load url in tab and handle history. If a tab is marked as lazy, only
* prepare the url but don't load it.
*/
void
load_url(struct tab *tab, const char *url, const char *base, int mode)
{
size_t line_off, curr_off;
int lazy = tab->flags & TAB_LAZY;
int dohist = !(mode & LU_MODE_NOHIST);
if (operating && lazy) {
tab->flags ^= TAB_LAZY;
lazy = 0;
} else if (hist_size(tab->hist) != 0) {
get_scroll_position(tab, &line_off, &curr_off);
hist_set_offs(tab->hist, line_off, curr_off);
}
if (dohist) {
if (hist_push(tab->hist, url) == -1) {
ev_break();
return;
}
strlcpy(tab->buffer.title, url, sizeof(tab->buffer.title));
}
if (!lazy)
do_load_url(tab, url, base, mode);
}
void
load_url_in_tab(struct tab *tab, const char *url, const char *base, int mode)
{
if (operating) {
autosave_hook();
message("Loading %s...", url);
}
if (base == NULL)
base = hist_cur(tab->hist);
load_url(tab, url, base, mode);
}
int
load_previous_page(struct tab *tab)
{
const char *h;
if ((h = hist_prev(tab->hist)) == NULL)
return 0;
do_load_url(tab, h, NULL, LU_MODE_NONE);
return 1;
}
int
load_next_page(struct tab *tab)
{
const char *h;
if ((h = hist_next(tab->hist)) == NULL)
return 0;
do_load_url(tab, h, NULL, LU_MODE_NONE);
return 1;
}
void
write_buffer(const char *path, struct tab *tab)
{
FILE *fp;
if (path == NULL)
return;
if ((fp = fopen(path, "w")) == NULL)
return;
if (!parser_serialize(&tab->buffer, fp))
message("Failed to save the page.");
fclose(fp);
}
/*
* Given a user-entered URL, apply some heuristics to use it if
* load-url-use-heuristic allows it.
*
* - if it's a proper url use it
* - if it starts with a `./' or a `/' assume its a file:// url
* - assume it's a default-protocol:// url
*
* `ret' (of which len is the size) will be filled with the resulting
* url.
*/
void
humanify_url(const char *raw, const char *base, char *ret, size_t len)
{
static struct iri iri;
if (load_url_use_heuristic)
base = NULL;
if (iri_parse(base, raw, &iri) == 0) {
iri_unparse(&iri, ret, len);
return;
}
if (!strncmp(raw, "./", 2)) {
strlcpy(ret, "file://", len);
strlcat(ret, cwd, len);
strlcat(ret, "/", len);
strlcat(ret, raw+2, len);
return;
}
if (*raw == '/') {
strlcpy(ret, "file://", len);
strlcat(ret, raw, len);
return;
}
strlcpy(ret, default_protocol, len);
strlcat(ret, "://", len);
strlcat(ret, raw, len);
}
int
bookmark_page(const char *url)
{
FILE *fp;
if ((fp = fopen(bookmark_file, "a")) == NULL)
return -1;
fprintf(fp, "=> %s\n", url);
fclose(fp);
return 0;
}