-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.c
2491 lines (2369 loc) · 58.5 KB
/
url.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
/* $Id: url.c,v 1.100 2010/12/15 10:50:24 htrb Exp $ */
#include "fm.h"
#ifndef __MINGW32_VERSION
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#else
#include <winsock.h>
#endif /* __MINGW32_VERSION */
#include <signal.h>
#include <setjmp.h>
#include <errno.h>
#include <sys/stat.h>
#ifdef __EMX__
#include <io.h> /* ?? */
#endif /* __EMX__ */
#include "html.h"
#include "Str.h"
#include "myctype.h"
#include "regex.h"
#ifdef USE_SSL
#ifndef SSLEAY_VERSION_NUMBER
#include <openssl/crypto.h> /* SSLEAY_VERSION_NUMBER may be here */
#endif
#include <openssl/err.h>
#endif
#ifdef __WATT32__
#define write(a,b,c) write_s(a,b,c)
#endif /* __WATT32__ */
#ifdef __MINGW32_VERSION
#define write(a,b,c) send(a,b,c, 0)
#define close(fd) closesocket(fd)
#endif
#ifdef INET6
/* see rc.c, "dns_order" and dnsorders[] */
int ai_family_order_table[7][3] = {
{PF_UNSPEC, PF_UNSPEC, PF_UNSPEC}, /* 0:unspec */
{PF_INET, PF_INET6, PF_UNSPEC}, /* 1:inet inet6 */
{PF_INET6, PF_INET, PF_UNSPEC}, /* 2:inet6 inet */
{PF_UNSPEC, PF_UNSPEC, PF_UNSPEC}, /* 3: --- */
{PF_INET, PF_UNSPEC, PF_UNSPEC}, /* 4:inet */
{PF_UNSPEC, PF_UNSPEC, PF_UNSPEC}, /* 5: --- */
{PF_INET6, PF_UNSPEC, PF_UNSPEC}, /* 6:inet6 */
};
#endif /* INET6 */
static JMP_BUF AbortLoading;
/* XXX: note html.h SCM_ */
static int
DefaultPort[] = {
80, /* http */
70, /* gopher */
21, /* ftp */
21, /* ftpdir */
0, /* local - not defined */
0, /* local-CGI - not defined? */
0, /* exec - not defined? */
119, /* nntp */
119, /* nntp group */
119, /* news */
119, /* news group */
0, /* data - not defined */
0, /* mailto - not defined */
#ifdef USE_SSL
443, /* https */
#endif /* USE_SSL */
};
struct cmdtable schemetable[] = {
{"http", SCM_HTTP},
{"gopher", SCM_GOPHER},
{"ftp", SCM_FTP},
{"local", SCM_LOCAL},
{"file", SCM_LOCAL},
/* {"exec", SCM_EXEC}, */
{"nntp", SCM_NNTP},
/* {"nntp", SCM_NNTP_GROUP}, */
{"news", SCM_NEWS},
/* {"news", SCM_NEWS_GROUP}, */
{"data", SCM_DATA},
#ifndef USE_W3MMAILER
{"mailto", SCM_MAILTO},
#endif
#ifdef USE_SSL
{"https", SCM_HTTPS},
#endif /* USE_SSL */
{NULL, SCM_UNKNOWN},
};
static struct table2 DefaultGuess[] = {
{"html", "text/html"},
{"htm", "text/html"},
{"shtml", "text/html"},
{"xhtml", "application/xhtml+xml"},
{"gif", "image/gif"},
{"jpeg", "image/jpeg"},
{"jpg", "image/jpeg"},
{"png", "image/png"},
{"xbm", "image/xbm"},
{"au", "audio/basic"},
{"gz", "application/x-gzip"},
{"Z", "application/x-compress"},
{"bz2", "application/x-bzip"},
{"tar", "application/x-tar"},
{"zip", "application/x-zip"},
{"lha", "application/x-lha"},
{"lzh", "application/x-lha"},
{"ps", "application/postscript"},
{"pdf", "application/pdf"},
{NULL, NULL}
};
static void add_index_file(ParsedURL *pu, URLFile *uf);
static char * schemeNumToName(int scheme);
/* #define HTTP_DEFAULT_FILE "/index.html" */
#ifndef HTTP_DEFAULT_FILE
#define HTTP_DEFAULT_FILE "/"
#endif /* not HTTP_DEFAULT_FILE */
#ifdef SOCK_DEBUG
#include <stdarg.h>
static void
sock_log(char *message, ...)
{
FILE *f = fopen("zzzsocklog", "a");
va_list va;
if (f == NULL)
return;
va_start(va, message);
vfprintf(f, message, va);
fclose(f);
}
#endif
static TextList *mimetypes_list;
static struct table2 **UserMimeTypes;
static struct table2 *
loadMimeTypes(char *filename)
{
FILE *f;
char *d, *type;
int i, n;
Str tmp;
struct table2 *mtypes;
f = fopen(expandPath(filename), "r");
if (f == NULL)
return NULL;
n = 0;
while (tmp = Strfgets(f), tmp->length > 0) {
d = tmp->ptr;
if (d[0] != '#') {
d = strtok(d, " \t\n\r");
if (d != NULL) {
d = strtok(NULL, " \t\n\r");
for (i = 0; d != NULL; i++)
d = strtok(NULL, " \t\n\r");
n += i;
}
}
}
fseek(f, 0, 0);
mtypes = New_N(struct table2, n + 1);
i = 0;
while (tmp = Strfgets(f), tmp->length > 0) {
d = tmp->ptr;
if (d[0] == '#')
continue;
type = strtok(d, " \t\n\r");
if (type == NULL)
continue;
while (1) {
d = strtok(NULL, " \t\n\r");
if (d == NULL)
break;
mtypes[i].item1 = Strnew_charp(d)->ptr;
mtypes[i].item2 = Strnew_charp(type)->ptr;
i++;
}
}
mtypes[i].item1 = NULL;
mtypes[i].item2 = NULL;
fclose(f);
return mtypes;
}
void
initMimeTypes(void)
{
int i;
TextListItem *tl;
if (non_null(mimetypes_files))
mimetypes_list = make_domain_list(mimetypes_files);
else
mimetypes_list = NULL;
if (mimetypes_list == NULL)
return;
UserMimeTypes = New_N(struct table2 *, mimetypes_list->nitem);
for (i = 0, tl = mimetypes_list->first; tl; i++, tl = tl->next)
UserMimeTypes[i] = loadMimeTypes(tl->ptr);
}
static char *
DefaultFile(int scheme)
{
switch (scheme) {
case SCM_HTTP:
#ifdef USE_SSL
case SCM_HTTPS:
#endif /* USE_SSL */
return allocStr(HTTP_DEFAULT_FILE, -1);
#ifdef USE_GOPHER
case SCM_GOPHER:
return allocStr("1", -1);
#endif /* USE_GOPHER */
case SCM_LOCAL:
case SCM_LOCAL_CGI:
case SCM_FTP:
case SCM_FTPDIR:
return allocStr("/", -1);
}
return NULL;
}
static MySignalHandler
KeyAbort(SIGNAL_ARG)
{
LONGJMP(AbortLoading, 1);
SIGNAL_RETURN;
}
#ifdef USE_SSL
SSL_CTX *ssl_ctx = NULL;
void
free_ssl_ctx(void)
{
if (ssl_ctx != NULL)
SSL_CTX_free(ssl_ctx);
ssl_ctx = NULL;
ssl_accept_this_site(NULL);
}
#if SSLEAY_VERSION_NUMBER >= 0x00905100
#include <openssl/rand.h>
static void
init_PRNG(void)
{
char buffer[256];
const char *file;
long l;
if (RAND_status())
return;
if ((file = RAND_file_name(buffer, sizeof(buffer)))) {
#ifdef USE_EGD
if (RAND_egd(file) > 0)
return;
#endif
RAND_load_file(file, -1);
}
if (RAND_status())
goto seeded;
srand48((long)time(NULL));
while (!RAND_status()) {
l = lrand48();
RAND_seed((unsigned char *)&l, sizeof(long));
}
seeded:
if (file)
RAND_write_file(file);
}
#endif /* SSLEAY_VERSION_NUMBER >= 0x00905100 */
#ifdef SSL_CTX_set_min_proto_version
static int
str_to_ssl_version(const char *name)
{
if(!strcasecmp(name, "all"))
return 0;
if(!strcasecmp(name, "none"))
return 0;
#ifdef TLS1_3_VERSION
if (!strcasecmp(name, "TLSv1.3"))
return TLS1_3_VERSION;
#endif
#ifdef TLS1_2_VERSION
if (!strcasecmp(name, "TLSv1.2"))
return TLS1_2_VERSION;
#endif
#ifdef TLS1_1_VERSION
if (!strcasecmp(name, "TLSv1.1"))
return TLS1_1_VERSION;
#endif
if (!strcasecmp(name, "TLSv1.0"))
return TLS1_VERSION;
if (!strcasecmp(name, "TLSv1"))
return TLS1_VERSION;
if (!strcasecmp(name, "SSLv3.0"))
return SSL3_VERSION;
if (!strcasecmp(name, "SSLv3"))
return SSL3_VERSION;
return -1;
}
#endif /* SSL_CTX_set_min_proto_version */
static SSL *
openSSLHandle(int sock, char *hostname, char **p_cert)
{
SSL *handle = NULL;
static char *old_ssl_forbid_method = NULL;
#ifdef USE_SSL_VERIFY
static int old_ssl_verify_server = -1;
#endif
if (old_ssl_forbid_method != ssl_forbid_method
&& (!old_ssl_forbid_method || !ssl_forbid_method ||
strcmp(old_ssl_forbid_method, ssl_forbid_method))) {
old_ssl_forbid_method = ssl_forbid_method;
#ifdef USE_SSL_VERIFY
ssl_path_modified = 1;
#else
free_ssl_ctx();
#endif
}
#ifdef USE_SSL_VERIFY
if (old_ssl_verify_server != ssl_verify_server) {
old_ssl_verify_server = ssl_verify_server;
ssl_path_modified = 1;
}
if (ssl_path_modified) {
free_ssl_ctx();
ssl_path_modified = 0;
}
#endif /* defined(USE_SSL_VERIFY) */
if (ssl_ctx == NULL) {
int option;
#if OPENSSL_VERSION_NUMBER < 0x0800
ssl_ctx = SSL_CTX_new();
X509_set_default_verify_paths(ssl_ctx->cert);
#else /* SSLEAY_VERSION_NUMBER >= 0x0800 */
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
SSLeay_add_ssl_algorithms();
SSL_load_error_strings();
#else
OPENSSL_init_ssl(0, NULL);
#endif
if (!(ssl_ctx = SSL_CTX_new(SSLv23_client_method())))
goto eend;
#ifdef SSL_CTX_set_min_proto_version
if (ssl_min_version && *ssl_min_version != '\0') {
int sslver;
sslver = str_to_ssl_version(ssl_min_version);
if (sslver < 0
|| !SSL_CTX_set_min_proto_version(ssl_ctx, sslver)) {
free_ssl_ctx();
goto eend;
}
}
#endif
if (ssl_cipher && *ssl_cipher != '\0')
if (!SSL_CTX_set_cipher_list(ssl_ctx, ssl_cipher)) {
free_ssl_ctx();
goto eend;
}
option = SSL_OP_ALL;
if (ssl_forbid_method) {
if (strchr(ssl_forbid_method, '2'))
option |= SSL_OP_NO_SSLv2;
if (strchr(ssl_forbid_method, '3'))
option |= SSL_OP_NO_SSLv3;
if (strchr(ssl_forbid_method, 't'))
option |= SSL_OP_NO_TLSv1;
if (strchr(ssl_forbid_method, 'T'))
option |= SSL_OP_NO_TLSv1;
if (strchr(ssl_forbid_method, '4'))
option |= SSL_OP_NO_TLSv1;
#ifdef SSL_OP_NO_TLSv1_1
if (strchr(ssl_forbid_method, '5'))
option |= SSL_OP_NO_TLSv1_1;
#endif
#ifdef SSL_OP_NO_TLSv1_2
if (strchr(ssl_forbid_method, '6'))
option |= SSL_OP_NO_TLSv1_2;
#endif
#ifdef SSL_OP_NO_TLSv1_3
if (strchr(ssl_forbid_method, '7'))
option |= SSL_OP_NO_TLSv1_3;
#endif
}
#ifdef SSL_OP_NO_COMPRESSION
option |= SSL_OP_NO_COMPRESSION;
#endif
SSL_CTX_set_options(ssl_ctx, option);
#ifdef SSL_MODE_RELEASE_BUFFERS
SSL_CTX_set_mode (ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
#endif
#ifdef USE_SSL_VERIFY
/* derived from openssl-0.9.5/apps/s_{client,cb}.c */
#if 1 /* use SSL_get_verify_result() to verify cert */
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, NULL);
#else
SSL_CTX_set_verify(ssl_ctx,
ssl_verify_server ? SSL_VERIFY_PEER :
SSL_VERIFY_NONE, NULL);
#endif
if (ssl_cert_file != NULL && *ssl_cert_file != '\0') {
int ng = 1;
if (SSL_CTX_use_certificate_file
(ssl_ctx, ssl_cert_file, SSL_FILETYPE_PEM) > 0) {
char *key_file = (ssl_key_file == NULL
|| *ssl_key_file ==
'\0') ? ssl_cert_file : ssl_key_file;
if (SSL_CTX_use_PrivateKey_file
(ssl_ctx, key_file, SSL_FILETYPE_PEM) > 0)
if (SSL_CTX_check_private_key(ssl_ctx))
ng = 0;
}
if (ng) {
free_ssl_ctx();
goto eend;
}
}
if (ssl_verify_server) {
char *file = NULL, *path = NULL;
if (ssl_ca_file && *ssl_ca_file != '\0') file = ssl_ca_file;
if (ssl_ca_path && *ssl_ca_path != '\0') path = ssl_ca_path;
if ((file || path)
&& !SSL_CTX_load_verify_locations(ssl_ctx, file, path)) {
free_ssl_ctx();
goto eend;
}
if (ssl_ca_default)
SSL_CTX_set_default_verify_paths(ssl_ctx);
}
#endif /* defined(USE_SSL_VERIFY) */
#endif /* SSLEAY_VERSION_NUMBER >= 0x0800 */
}
handle = SSL_new(ssl_ctx);
SSL_set_fd(handle, sock);
#if SSLEAY_VERSION_NUMBER >= 0x00905100
init_PRNG();
#endif /* SSLEAY_VERSION_NUMBER >= 0x00905100 */
#if (SSLEAY_VERSION_NUMBER >= 0x00908070) && !defined(OPENSSL_NO_TLSEXT)
SSL_set_tlsext_host_name(handle,hostname);
#endif /* (SSLEAY_VERSION_NUMBER >= 0x00908070) && !defined(OPENSSL_NO_TLSEXT) */
if (SSL_connect(handle) > 0) {
Str serv_cert = ssl_get_certificate(handle, hostname);
if (serv_cert) {
*p_cert = serv_cert->ptr;
return handle;
}
close(sock);
SSL_free(handle);
return NULL;
}
eend:
close(sock);
if (handle)
SSL_free(handle);
/* FIXME: gettextize? */
disp_err_message(Sprintf
("SSL error: %s, a workaround might be: w3m -insecure",
ERR_error_string(ERR_get_error(), NULL))->ptr, FALSE);
return NULL;
}
static void
SSL_write_from_file(SSL * ssl, char *file)
{
FILE *fd;
int c;
char buf[1];
fd = fopen(file, "r");
if (fd != NULL) {
while ((c = fgetc(fd)) != EOF) {
buf[0] = c;
SSL_write(ssl, buf, 1);
}
fclose(fd);
}
}
#endif /* USE_SSL */
static void
write_from_file(int sock, char *file)
{
FILE *fd;
int c;
char buf[1];
fd = fopen(file, "r");
if (fd != NULL) {
while ((c = fgetc(fd)) != EOF) {
buf[0] = c;
write(sock, buf, 1);
}
fclose(fd);
}
}
ParsedURL *
baseURL(Buffer *buf)
{
if (buf->bufferprop & BP_NO_URL) {
/* no URL is defined for the buffer */
return NULL;
}
if (buf->baseURL != NULL) {
/* <BASE> tag is defined in the document */
return buf->baseURL;
}
else if (IS_EMPTY_PARSED_URL(&buf->currentURL))
return NULL;
else
return &buf->currentURL;
}
int
openSocket(char *const hostname,
char *remoteport_name, unsigned short remoteport_num)
{
volatile int sock = -1;
#ifdef INET6
int *af;
struct addrinfo hints, *res0, *res;
int error;
char *hname;
#else /* not INET6 */
struct sockaddr_in hostaddr;
struct hostent *entry;
struct protoent *proto;
unsigned short s_port;
int a1, a2, a3, a4;
unsigned long adr;
#endif /* not INET6 */
MySignalHandler(*volatile prevtrap) (SIGNAL_ARG) = NULL;
if (fmInitialized) {
/* FIXME: gettextize? */
message(Sprintf("Opening socket...")->ptr, 0, 0);
refresh();
}
if (SETJMP(AbortLoading) != 0) {
#ifdef SOCK_DEBUG
sock_log("openSocket() failed. reason: user abort\n");
#endif
if (sock >= 0)
close(sock);
goto error;
}
TRAP_ON;
if (hostname == NULL) {
#ifdef SOCK_DEBUG
sock_log("openSocket() failed. reason: Bad hostname \"%s\"\n",
hostname);
#endif
goto error;
}
#ifdef INET6
/* rfc2732 compliance */
hname = hostname;
if (hname != NULL && hname[0] == '[' && hname[strlen(hname) - 1] == ']') {
hname = allocStr(hostname + 1, -1);
hname[strlen(hname) - 1] = '\0';
if (strspn(hname, "0123456789abcdefABCDEF:.") != strlen(hname))
goto error;
}
for (af = ai_family_order_table[DNS_order];; af++) {
memset(&hints, 0, sizeof(hints));
hints.ai_family = *af;
hints.ai_socktype = SOCK_STREAM;
if (remoteport_num != 0) {
Str portbuf = Sprintf("%d", remoteport_num);
error = getaddrinfo(hname, portbuf->ptr, &hints, &res0);
}
else {
error = -1;
}
if (error && remoteport_name && remoteport_name[0] != '\0') {
/* try default port */
error = getaddrinfo(hname, remoteport_name, &hints, &res0);
}
if (error) {
if (*af == PF_UNSPEC) {
goto error;
}
/* try next ai family */
continue;
}
sock = -1;
for (res = res0; res; res = res->ai_next) {
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock < 0) {
continue;
}
if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
close(sock);
sock = -1;
continue;
}
break;
}
if (sock < 0) {
freeaddrinfo(res0);
if (*af == PF_UNSPEC) {
goto error;
}
/* try next ai family */
continue;
}
freeaddrinfo(res0);
break;
}
#else /* not INET6 */
s_port = htons(remoteport_num);
bzero((char *)&hostaddr, sizeof(struct sockaddr_in));
if ((proto = getprotobyname("tcp")) == NULL) {
/* protocol number of TCP is 6 */
proto = New(struct protoent);
proto->p_proto = 6;
}
if ((sock = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0) {
#ifdef SOCK_DEBUG
sock_log("openSocket: socket() failed. reason: %s\n", strerror(errno));
#endif
goto error;
}
regexCompile("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$", 0);
if (regexMatch(hostname, -1, 1)) {
sscanf(hostname, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
adr = htonl((a1 << 24) | (a2 << 16) | (a3 << 8) | a4);
bcopy((void *)&adr, (void *)&hostaddr.sin_addr, sizeof(long));
hostaddr.sin_family = AF_INET;
hostaddr.sin_port = s_port;
if (fmInitialized) {
message(Sprintf("Connecting to %s", hostname)->ptr, 0, 0);
refresh();
}
if (connect(sock, (struct sockaddr *)&hostaddr,
sizeof(struct sockaddr_in)) < 0) {
#ifdef SOCK_DEBUG
sock_log("openSocket: connect() failed. reason: %s\n",
strerror(errno));
#endif
goto error;
}
}
else {
char **h_addr_list;
int result = -1;
if (fmInitialized) {
message(Sprintf("Performing hostname lookup on %s", hostname)->ptr,
0, 0);
refresh();
}
if ((entry = gethostbyname(hostname)) == NULL) {
#ifdef SOCK_DEBUG
sock_log("openSocket: gethostbyname() failed. reason: %s\n",
strerror(errno));
#endif
goto error;
}
hostaddr.sin_family = AF_INET;
hostaddr.sin_port = s_port;
for (h_addr_list = entry->h_addr_list; *h_addr_list; h_addr_list++) {
bcopy((void *)h_addr_list[0], (void *)&hostaddr.sin_addr,
entry->h_length);
#ifdef SOCK_DEBUG
adr = ntohl(*(long *)&hostaddr.sin_addr);
sock_log("openSocket: connecting %d.%d.%d.%d\n",
(adr >> 24) & 0xff,
(adr >> 16) & 0xff, (adr >> 8) & 0xff, adr & 0xff);
#endif
if (fmInitialized) {
message(Sprintf("Connecting to %s", hostname)->ptr, 0, 0);
refresh();
}
if ((result = connect(sock, (struct sockaddr *)&hostaddr,
sizeof(struct sockaddr_in))) == 0) {
break;
}
#ifdef SOCK_DEBUG
else {
sock_log("openSocket: connect() failed. reason: %s\n",
strerror(errno));
}
#endif
}
if (result < 0) {
goto error;
}
}
#endif /* not INET6 */
TRAP_OFF;
return sock;
error:
TRAP_OFF;
return -1;
}
#define COPYPATH_SPC_ALLOW 0
#define COPYPATH_SPC_IGNORE 1
#define COPYPATH_SPC_REPLACE 2
#define COPYPATH_SPC_MASK 3
#define COPYPATH_LOWERCASE 4
static char *
copyPath(char *orgpath, int length, int option)
{
Str tmp = Strnew();
char ch;
while ((ch = *orgpath) != 0 && length != 0) {
if (option & COPYPATH_LOWERCASE)
ch = TOLOWER(ch);
if (IS_SPACE(ch)) {
switch (option & COPYPATH_SPC_MASK) {
case COPYPATH_SPC_ALLOW:
Strcat_char(tmp, ch);
break;
case COPYPATH_SPC_IGNORE:
/* do nothing */
break;
case COPYPATH_SPC_REPLACE:
Strcat_charp(tmp, "%20");
break;
}
}
else
Strcat_char(tmp, ch);
orgpath++;
length--;
}
return tmp->ptr;
}
void
parseURL(char *url, ParsedURL *p_url, ParsedURL *current)
{
char *p, *q, *qq;
Str tmp;
url = url_quote(url); /* quote 0x01-0x20, 0x7F-0xFF */
p = url;
copyParsedURL(p_url, NULL);
p_url->scheme = SCM_MISSING;
/* RFC1808: Relative Uniform Resource Locators
* 4. Resolving Relative URLs
*/
if (*url == '\0' || *url == '#') {
if (current)
copyParsedURL(p_url, current);
goto do_label;
}
#if defined( __EMX__ ) || defined( __CYGWIN__ )
if (!strncasecmp(url, "file://localhost/", 17)) {
p_url->scheme = SCM_LOCAL;
p += 17 - 1;
url += 17 - 1;
}
#endif
#ifdef SUPPORT_DOS_DRIVE_PREFIX
if (IS_ALPHA(*p) && (p[1] == ':' || p[1] == '|')) {
p_url->scheme = SCM_LOCAL;
goto analyze_file;
}
#endif /* SUPPORT_DOS_DRIVE_PREFIX */
/* search for scheme */
p_url->scheme = getURLScheme(&p);
if (p_url->scheme == SCM_MISSING) {
/* scheme part is not found in the url. This means either
* (a) the url is relative to the current or (b) the url
* denotes a filename (therefore the scheme is SCM_LOCAL).
*/
if (current) {
switch (current->scheme) {
case SCM_LOCAL:
case SCM_LOCAL_CGI:
p_url->scheme = SCM_LOCAL;
break;
case SCM_FTP:
case SCM_FTPDIR:
p_url->scheme = SCM_FTP;
break;
#ifdef USE_NNTP
case SCM_NNTP:
case SCM_NNTP_GROUP:
p_url->scheme = SCM_NNTP;
break;
case SCM_NEWS:
case SCM_NEWS_GROUP:
p_url->scheme = SCM_NEWS;
break;
#endif
default:
p_url->scheme = current->scheme;
break;
}
}
else
p_url->scheme = SCM_LOCAL;
p = url;
if (!strncmp(p, "//", 2)) {
/* URL begins with // */
/* it means that 'scheme:' is abbreviated */
p += 2;
goto analyze_url;
}
/* the url doesn't begin with '//' */
goto analyze_file;
}
/* scheme part has been found */
if (p_url->scheme == SCM_UNKNOWN) {
p_url->file = allocStr(url, -1);
return;
}
/* get host and port */
if (p[0] != '/' || p[1] != '/') { /* scheme:foo or scheme:/foo */
p_url->host = NULL;
if (p_url->scheme != SCM_UNKNOWN)
p_url->port = DefaultPort[p_url->scheme];
else
p_url->port = 0;
goto analyze_file;
}
/* after here, p begins with // */
if (p_url->scheme == SCM_LOCAL) { /* file://foo */
#ifdef __EMX__
p += 2;
goto analyze_file;
#else
if (p[2] == '/' || p[2] == '~'
/* <A HREF="file:///foo">file:///foo</A> or <A HREF="file://~user">file://~user</A> */
#ifdef SUPPORT_DOS_DRIVE_PREFIX
|| (IS_ALPHA(p[2]) && (p[3] == ':' || p[3] == '|'))
/* <A HREF="file://DRIVE/foo">file://DRIVE/foo</A> */
#endif /* SUPPORT_DOS_DRIVE_PREFIX */
) {
p += 2;
goto analyze_file;
}
#endif /* __EMX__ */
}
p += 2; /* scheme://foo */
/* ^p is here */
analyze_url:
q = p;
#ifdef INET6
if (*q == '[') { /* rfc2732,rfc2373 compliance */
p++;
while (IS_XDIGIT(*p) || *p == ':' || *p == '.')
p++;
if (*p != ']' || (*(p + 1) && strchr(":/?#", *(p + 1)) == NULL))
p = q;
}
#endif
while (*p && strchr(":/@?#", *p) == NULL)
p++;
switch (*p) {
case ':':
/* scheme://user:pass@host or
* scheme://host:port
*/
qq = q;
q = ++p;
while (*p && strchr("@/?#", *p) == NULL)
p++;
if (*p == '@') {
/* scheme://user:pass@... */
p_url->user = copyPath(qq, q - 1 - qq, COPYPATH_SPC_IGNORE);
p_url->pass = copyPath(q, p - q, COPYPATH_SPC_ALLOW);
p++;
goto analyze_url;
}
/* scheme://host:port/ */
p_url->host = copyPath(qq, q - 1 - qq,
COPYPATH_SPC_IGNORE | COPYPATH_LOWERCASE);
tmp = Strnew_charp_n(q, p - q);
p_url->port = atoi(tmp->ptr);
/* *p is one of ['\0', '/', '?', '#'] */
break;
case '@':
/* scheme://user@... */
p_url->user = copyPath(q, p - q, COPYPATH_SPC_IGNORE);
p++;
goto analyze_url;
case '\0':
/* scheme://host */
case '/':
case '?':
case '#':
p_url->host = copyPath(q, p - q,
COPYPATH_SPC_IGNORE | COPYPATH_LOWERCASE);
if (p_url->scheme != SCM_UNKNOWN)
p_url->port = DefaultPort[p_url->scheme];
else
p_url->port = 0;
break;
}
analyze_file:
#ifndef SUPPORT_NETBIOS_SHARE
if (p_url->scheme == SCM_LOCAL && p_url->user == NULL &&
p_url->host != NULL && *p_url->host != '\0' &&
!is_localhost(p_url->host)) {
/*
* In the environments other than CYGWIN, a URL like
* file://host/file is regarded as ftp://host/file.
* On the other hand, file://host/file on CYGWIN is
* regarded as local access to the file //host/file.
* `host' is a netbios-hostname, drive, or any other
* name; It is CYGWIN system call who interprets that.
*/
p_url->scheme = SCM_FTP; /* ftp://host/... */
if (p_url->port == 0)
p_url->port = DefaultPort[SCM_FTP];
}
#endif
if ((*p == '\0' || *p == '#' || *p == '?') && p_url->host == NULL) {
p_url->file = "";
goto do_query;
}
#ifdef SUPPORT_DOS_DRIVE_PREFIX
if (p_url->scheme == SCM_LOCAL) {
q = p;
if (*q == '/')
q++;
if (IS_ALPHA(q[0]) && (q[1] == ':' || q[1] == '|')) {
if (q[1] == '|') {
p = allocStr(q, -1);
p[1] = ':';
}
else
p = q;
}
}
#endif
q = p;
#ifdef USE_GOPHER
if (p_url->scheme == SCM_GOPHER) {
if (*q == '/')
q++;
if (*q && q[0] != '/' && q[1] != '/' && q[2] == '/')
q++;
}
#endif /* USE_GOPHER */
if (*p == '/')
p++;
if (*p == '\0' || *p == '#' || *p == '?') { /* scheme://host[:port]/ */
p_url->file = DefaultFile(p_url->scheme);
goto do_query;
}
#ifdef USE_GOPHER
if (p_url->scheme == SCM_GOPHER && *p == 'R') {
if (!*++p) {
p_url->file = "";
goto do_query;
}
tmp = Strnew();
Strcat_char(tmp, *(p++));
while (*p && *p != '/')
p++;
Strcat_charp(tmp, p);
while (*p)
p++;
p_url->file = copyPath(tmp->ptr, -1, COPYPATH_SPC_IGNORE);
}
else
#endif /* USE_GOPHER */
{
char *cgi = strchr(p, '?');
again:
while (*p && *p != '#' && p != cgi)
p++;