-
Notifications
You must be signed in to change notification settings - Fork 10
/
file.c
8466 lines (8001 loc) · 199 KB
/
file.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: file.c,v 1.265 2010/12/15 10:50:24 htrb Exp $ */
#include "fm.h"
#include <sys/types.h>
#include "myctype.h"
#include <signal.h>
#include <setjmp.h>
#if defined(HAVE_WAITPID) || defined(HAVE_WAIT3)
#include <sys/wait.h>
#endif
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <utime.h>
/* foo */
#include "html.h"
#include "parsetagx.h"
#include "local.h"
#include "regex.h"
#ifndef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#endif /* not max */
#ifndef min
#define min(a,b) ((a) > (b) ? (b) : (a))
#endif /* not min */
static int frame_source = 0;
static char *guess_filename(char *file);
static int _MoveFile(char *path1, char *path2);
static void uncompress_stream(URLFile *uf, char **src);
static FILE *lessopen_stream(char *path);
static Buffer *loadcmdout(char *cmd,
Buffer *(*loadproc) (URLFile *, Buffer *),
Buffer *defaultbuf);
#ifndef USE_ANSI_COLOR
#define addnewline(a,b,c,d,e,f,g) _addnewline(a,b,c,e,f,g)
#endif
static void addnewline(Buffer *buf, char *line, Lineprop *prop,
Linecolor *color, int pos, int width, int nlines);
static void addLink(Buffer *buf, struct parsed_tag *tag);
static JMP_BUF AbortLoading;
static struct table *tables[MAX_TABLE];
static struct table_mode table_mode[MAX_TABLE];
#ifdef USE_IMAGE
static ParsedURL *cur_baseURL = NULL;
#ifdef USE_M17N
static char cur_document_charset;
#endif
#endif
static Str cur_title;
static Str cur_select;
static Str select_str;
static int select_is_multiple;
static int n_selectitem;
static Str cur_option;
static Str cur_option_value;
static Str cur_option_label;
static int cur_option_selected;
static int cur_status;
#ifdef MENU_SELECT
/* menu based <select> */
FormSelectOption *select_option;
static int max_select = MAX_SELECT;
static int n_select;
static int cur_option_maxwidth;
#endif /* MENU_SELECT */
static Str cur_textarea;
Str *textarea_str;
static int cur_textarea_size;
static int cur_textarea_rows;
static int cur_textarea_readonly;
static int n_textarea;
static int ignore_nl_textarea;
static int max_textarea = MAX_TEXTAREA;
static int http_response_code;
#ifdef USE_M17N
static wc_ces content_charset = 0;
static wc_ces meta_charset = 0;
static char *check_charset(char *p);
static char *check_accept_charset(char *p);
#endif
#define set_prevchar(x,y,n) Strcopy_charp_n((x),(y),(n))
#define set_space_to_prevchar(x) Strcopy_charp_n((x)," ",1)
struct link_stack {
int cmd;
short offset;
short pos;
struct link_stack *next;
};
static struct link_stack *link_stack = NULL;
#define FORMSTACK_SIZE 10
#define FRAMESTACK_SIZE 10
#ifdef USE_NNTP
#define Str_news_endline(s) ((s)->ptr[0]=='.'&&((s)->ptr[1]=='\n'||(s)->ptr[1]=='\r'||(s)->ptr[1]=='\0'))
#endif /* USE_NNTP */
#define INITIAL_FORM_SIZE 10
static FormList **forms;
static int *form_stack;
static int form_max = -1;
static int forms_size = 0;
#define cur_form_id ((form_sp >= 0)? form_stack[form_sp] : -1)
static int form_sp = 0;
static clen_t current_content_length;
static int cur_hseq;
#ifdef USE_IMAGE
static int cur_iseq;
#endif
#define MAX_UL_LEVEL 9
#define UL_SYMBOL(x) (N_GRAPH_SYMBOL + (x))
#define UL_SYMBOL_DISC UL_SYMBOL(9)
#define UL_SYMBOL_CIRCLE UL_SYMBOL(10)
#define UL_SYMBOL_SQUARE UL_SYMBOL(11)
#define IMG_SYMBOL UL_SYMBOL(12)
#define HR_SYMBOL 26
#ifdef USE_COOKIE
/* This array should be somewhere else */
/* FIXME: gettextize? */
char *violations[COO_EMAX] = {
"internal error",
"tail match failed",
"wrong number of dots",
"RFC 2109 4.3.2 rule 1",
"RFC 2109 4.3.2 rule 2.1",
"RFC 2109 4.3.2 rule 2.2",
"RFC 2109 4.3.2 rule 3",
"RFC 2109 4.3.2 rule 4",
"RFC XXXX 4.3.2 rule 5"
};
#endif
/* *INDENT-OFF* */
static struct compression_decoder {
int type;
char *ext;
char *mime_type;
int auxbin_p;
char *cmd;
char *name;
char *encoding;
char *encodings[4];
} compression_decoders[] = {
{ CMP_COMPRESS, ".gz", "application/x-gzip",
0, GUNZIP_CMDNAME, GUNZIP_NAME, "gzip",
{"gzip", "x-gzip", NULL} },
{ CMP_COMPRESS, ".Z", "application/x-compress",
0, GUNZIP_CMDNAME, GUNZIP_NAME, "compress",
{"compress", "x-compress", NULL} },
{ CMP_BZIP2, ".bz2", "application/x-bzip",
0, BUNZIP2_CMDNAME, BUNZIP2_NAME, "bzip, bzip2",
{"x-bzip", "bzip", "bzip2", NULL} },
{ CMP_DEFLATE, ".deflate", "application/x-deflate",
1, INFLATE_CMDNAME, INFLATE_NAME, "deflate",
{"deflate", "x-deflate", NULL} },
{ CMP_NOCOMPRESS, NULL, NULL, 0, NULL, NULL, NULL, {NULL}},
};
/* *INDENT-ON* */
#define SAVE_BUF_SIZE 1536
static MySignalHandler
KeyAbort(SIGNAL_ARG)
{
LONGJMP(AbortLoading, 1);
SIGNAL_RETURN;
}
static void
UFhalfclose(URLFile *f)
{
switch (f->scheme) {
case SCM_FTP:
closeFTP();
break;
#ifdef USE_NNTP
case SCM_NEWS:
case SCM_NNTP:
closeNews();
break;
#endif
default:
UFclose(f);
break;
}
}
int
currentLn(Buffer *buf)
{
if (buf->currentLine)
/* return buf->currentLine->real_linenumber + 1; */
return buf->currentLine->linenumber + 1;
else
return 1;
}
static Buffer *
loadSomething(URLFile *f,
char *path,
Buffer *(*loadproc) (URLFile *, Buffer *), Buffer *defaultbuf)
{
Buffer *buf;
if ((buf = loadproc(f, defaultbuf)) == NULL)
return NULL;
buf->filename = path;
if (buf->buffername == NULL || buf->buffername[0] == '\0') {
buf->buffername = checkHeader(buf, "Subject:");
if (buf->buffername == NULL)
buf->buffername = conv_from_system(lastFileName(path));
}
if (buf->currentURL.scheme == SCM_UNKNOWN)
buf->currentURL.scheme = f->scheme;
buf->real_scheme = f->scheme;
if (f->scheme == SCM_LOCAL && buf->sourcefile == NULL)
buf->sourcefile = path;
return buf;
}
int
dir_exist(char *path)
{
struct stat stbuf;
if (path == NULL || *path == '\0')
return 0;
if (stat(path, &stbuf) == -1)
return 0;
return IS_DIRECTORY(stbuf.st_mode);
}
static int
is_dump_text_type(char *type)
{
struct mailcap *mcap;
return (type && (mcap = searchExtViewer(type)) &&
(mcap->flags & (MAILCAP_HTMLOUTPUT | MAILCAP_COPIOUSOUTPUT)));
}
static int
is_text_type(char *type)
{
return (type == NULL || type[0] == '\0' ||
strncasecmp(type, "text/", 5) == 0 ||
(strncasecmp(type, "application/", 12) == 0 &&
strstr(type, "xhtml") != NULL) ||
strncasecmp(type, "message/", sizeof("message/") - 1) == 0);
}
static int
is_plain_text_type(char *type)
{
return ((type && strcasecmp(type, "text/plain") == 0) ||
(is_text_type(type) && !is_dump_text_type(type)));
}
int
is_html_type(char *type)
{
return (type && (strcasecmp(type, "text/html") == 0 ||
strcasecmp(type, "application/xhtml+xml") == 0));
}
static void
check_compression(char *path, URLFile *uf)
{
int len;
struct compression_decoder *d;
if (path == NULL)
return;
len = strlen(path);
uf->compression = CMP_NOCOMPRESS;
for (d = compression_decoders; d->type != CMP_NOCOMPRESS; d++) {
int elen;
if (d->ext == NULL)
continue;
elen = strlen(d->ext);
if (len > elen && strcasecmp(&path[len - elen], d->ext) == 0) {
uf->compression = d->type;
uf->guess_type = d->mime_type;
break;
}
}
}
static char *
compress_application_type(int compression)
{
struct compression_decoder *d;
for (d = compression_decoders; d->type != CMP_NOCOMPRESS; d++) {
if (d->type == compression)
return d->mime_type;
}
return NULL;
}
static char *
uncompressed_file_type(char *path, char **ext)
{
int len, slen;
Str fn;
char *t0;
struct compression_decoder *d;
if (path == NULL)
return NULL;
slen = 0;
len = strlen(path);
for (d = compression_decoders; d->type != CMP_NOCOMPRESS; d++) {
if (d->ext == NULL)
continue;
slen = strlen(d->ext);
if (len > slen && strcasecmp(&path[len - slen], d->ext) == 0)
break;
}
if (d->type == CMP_NOCOMPRESS)
return NULL;
fn = Strnew_charp(path);
Strshrink(fn, slen);
if (ext)
*ext = filename_extension(fn->ptr, 0);
t0 = guessContentType(fn->ptr);
if (t0 == NULL)
t0 = "text/plain";
return t0;
}
static int
setModtime(char *path, time_t modtime)
{
struct utimbuf t;
struct stat st;
if (stat(path, &st) == 0)
t.actime = st.st_atime;
else
t.actime = time(NULL);
t.modtime = modtime;
return utime(path, &t);
}
void
examineFile(char *path, URLFile *uf)
{
struct stat stbuf;
uf->guess_type = NULL;
if (path == NULL || *path == '\0' ||
stat(path, &stbuf) == -1 || NOT_REGULAR(stbuf.st_mode)) {
uf->stream = NULL;
return;
}
uf->stream = openIS(path);
if (!do_download) {
if (use_lessopen && getenv("LESSOPEN") != NULL) {
FILE *fp;
uf->guess_type = guessContentType(path);
if (uf->guess_type == NULL)
uf->guess_type = "text/plain";
if (is_html_type(uf->guess_type))
return;
if ((fp = lessopen_stream(path))) {
UFclose(uf);
uf->stream = newFileStream(fp, (void (*)())pclose);
uf->guess_type = "text/plain";
return;
}
}
check_compression(path, uf);
if (uf->compression != CMP_NOCOMPRESS) {
char *ext = uf->ext;
char *t0 = uncompressed_file_type(path, &ext);
uf->guess_type = t0;
uf->ext = ext;
uncompress_stream(uf, NULL);
return;
}
}
}
#define S_IXANY (S_IXUSR|S_IXGRP|S_IXOTH)
int
check_command(char *cmd, int auxbin_p)
{
static char *path = NULL;
Str dirs;
char *p, *np;
Str pathname;
struct stat st;
if (path == NULL)
path = getenv("PATH");
if (auxbin_p)
dirs = Strnew_charp(w3m_auxbin_dir());
else
dirs = Strnew_charp(path);
for (p = dirs->ptr; p != NULL; p = np) {
np = strchr(p, PATH_SEPARATOR);
if (np)
*np++ = '\0';
pathname = Strnew();
Strcat_charp(pathname, p);
Strcat_char(pathname, '/');
Strcat_charp(pathname, cmd);
if (stat(pathname->ptr, &st) == 0 && S_ISREG(st.st_mode)
&& (st.st_mode & S_IXANY) != 0)
return 1;
}
return 0;
}
char *
acceptableEncoding()
{
static Str encodings = NULL;
struct compression_decoder *d;
TextList *l;
char *p;
if (encodings != NULL)
return encodings->ptr;
l = newTextList();
for (d = compression_decoders; d->type != CMP_NOCOMPRESS; d++) {
if (check_command(d->cmd, d->auxbin_p)) {
pushText(l, d->encoding);
}
}
encodings = Strnew();
while ((p = popText(l)) != NULL) {
if (encodings->length)
Strcat_charp(encodings, ", ");
Strcat_charp(encodings, p);
}
return encodings->ptr;
}
/*
* convert line
*/
#ifdef USE_M17N
Str
convertLine(URLFile *uf, Str line, int mode, wc_ces * charset,
wc_ces doc_charset)
#else
Str
convertLine0(URLFile *uf, Str line, int mode)
#endif
{
#ifdef USE_M17N
line = wc_Str_conv_with_detect(line, charset, doc_charset, InnerCharset);
#endif
if (mode != RAW_MODE)
cleanup_line(line, mode);
#ifdef USE_NNTP
if (uf && uf->scheme == SCM_NEWS)
Strchop(line);
#endif /* USE_NNTP */
return line;
}
/*
* loadFile: load file to buffer
*/
Buffer *
loadFile(char *path)
{
Buffer *buf;
URLFile uf;
init_stream(&uf, SCM_LOCAL, NULL);
examineFile(path, &uf);
if (uf.stream == NULL)
return NULL;
buf = newBuffer(INIT_BUFFER_WIDTH);
current_content_length = 0;
#ifdef USE_M17N
content_charset = 0;
#endif
buf = loadSomething(&uf, path, loadBuffer, buf);
UFclose(&uf);
return buf;
}
int
matchattr(char *p, char *attr, int len, Str *value)
{
int quoted;
char *q = NULL;
if (strncasecmp(p, attr, len) == 0) {
p += len;
SKIP_BLANKS(p);
if (value) {
*value = Strnew();
if (*p == '=') {
p++;
SKIP_BLANKS(p);
quoted = 0;
while (!IS_ENDL(*p) && (quoted || *p != ';')) {
if (!IS_SPACE(*p))
q = p;
if (*p == '"')
quoted = (quoted) ? 0 : 1;
else
Strcat_char(*value, *p);
p++;
}
if (q)
Strshrink(*value, p - q - 1);
}
return 1;
}
else {
if (IS_ENDT(*p)) {
return 1;
}
}
}
return 0;
}
#ifdef USE_IMAGE
#ifdef USE_XFACE
static char *
xface2xpm(char *xface)
{
Image image;
ImageCache *cache;
FILE *f;
struct stat st;
SKIP_BLANKS(xface);
image.url = xface;
image.ext = ".xpm";
image.width = 48;
image.height = 48;
image.cache = NULL;
cache = getImage(&image, NULL, IMG_FLAG_AUTO);
if (cache->loaded & IMG_FLAG_LOADED && !stat(cache->file, &st))
return cache->file;
cache->loaded = IMG_FLAG_ERROR;
f = popen(Sprintf("%s > %s", shell_quote(auxbinFile(XFACE2XPM)),
shell_quote(cache->file))->ptr, "w");
if (!f)
return NULL;
fputs(xface, f);
pclose(f);
if (stat(cache->file, &st) || !st.st_size)
return NULL;
cache->loaded = IMG_FLAG_LOADED | IMG_FLAG_DONT_REMOVE;
cache->index = 0;
return cache->file;
}
#endif
#endif
void
readHeader(URLFile *uf, Buffer *newBuf, int thru, ParsedURL *pu)
{
char *p, *q;
#ifdef USE_COOKIE
char *emsg;
#endif
char c;
Str lineBuf2 = NULL;
Str tmp;
TextList *headerlist;
#ifdef USE_M17N
wc_ces charset = WC_CES_US_ASCII, mime_charset;
#endif
char *tmpf;
FILE *src = NULL;
Lineprop *propBuffer;
headerlist = newBuf->document_header = newTextList();
if (uf->scheme == SCM_HTTP
#ifdef USE_SSL
|| uf->scheme == SCM_HTTPS
#endif /* USE_SSL */
)
http_response_code = -1;
else
http_response_code = 0;
if (thru && !newBuf->header_source
#ifdef USE_IMAGE
&& !image_source
#endif
) {
tmpf = tmpfname(TMPF_DFL, NULL)->ptr;
src = fopen(tmpf, "w");
if (src)
newBuf->header_source = tmpf;
}
while ((tmp = StrmyUFgets(uf))->length) {
#ifdef USE_NNTP
if (uf->scheme == SCM_NEWS && tmp->ptr[0] == '.')
Strshrinkfirst(tmp, 1);
#endif
if(w3m_reqlog){
FILE *ff;
ff = fopen(w3m_reqlog, "a");
Strfputs(tmp, ff);
fclose(ff);
}
if (src)
Strfputs(tmp, src);
cleanup_line(tmp, HEADER_MODE);
if (tmp->ptr[0] == '\n' || tmp->ptr[0] == '\r' || tmp->ptr[0] == '\0') {
if (!lineBuf2)
/* there is no header */
break;
/* last header */
}
else if (!(w3m_dump & DUMP_HEAD)) {
if (lineBuf2) {
Strcat(lineBuf2, tmp);
}
else {
lineBuf2 = tmp;
}
c = UFgetc(uf);
UFundogetc(uf);
if (c == ' ' || c == '\t')
/* header line is continued */
continue;
lineBuf2 = decodeMIME(lineBuf2, &mime_charset);
lineBuf2 = convertLine(NULL, lineBuf2, RAW_MODE,
mime_charset ? &mime_charset : &charset,
mime_charset ? mime_charset
: DocumentCharset);
/* separated with line and stored */
tmp = Strnew_size(lineBuf2->length);
for (p = lineBuf2->ptr; *p; p = q) {
for (q = p; *q && *q != '\r' && *q != '\n'; q++) ;
lineBuf2 = checkType(Strnew_charp_n(p, q - p), &propBuffer,
NULL);
Strcat(tmp, lineBuf2);
if (thru)
addnewline(newBuf, lineBuf2->ptr, propBuffer, NULL,
lineBuf2->length, FOLD_BUFFER_WIDTH, -1);
for (; *q && (*q == '\r' || *q == '\n'); q++) ;
}
#ifdef USE_IMAGE
if (thru && activeImage && displayImage) {
Str src = NULL;
if (!strncasecmp(tmp->ptr, "X-Image-URL:", 12)) {
tmpf = &tmp->ptr[12];
SKIP_BLANKS(tmpf);
src = Strnew_m_charp("<img src=\"", html_quote(tmpf),
"\" alt=\"X-Image-URL\">", NULL);
}
#ifdef USE_XFACE
else if (!strncasecmp(tmp->ptr, "X-Face:", 7)) {
tmpf = xface2xpm(&tmp->ptr[7]);
if (tmpf)
src = Strnew_m_charp("<img src=\"file:",
html_quote(tmpf),
"\" alt=\"X-Face\"",
" width=48 height=48>", NULL);
}
#endif
if (src) {
URLFile f;
Line *l;
#ifdef USE_M17N
wc_ces old_charset = newBuf->document_charset;
#endif
init_stream(&f, SCM_LOCAL, newStrStream(src));
loadHTMLstream(&f, newBuf, NULL, TRUE);
for (l = newBuf->lastLine; l && l->real_linenumber;
l = l->prev)
l->real_linenumber = 0;
#ifdef USE_M17N
newBuf->document_charset = old_charset;
#endif
}
}
#endif
lineBuf2 = tmp;
}
else {
lineBuf2 = tmp;
}
if ((uf->scheme == SCM_HTTP
#ifdef USE_SSL
|| uf->scheme == SCM_HTTPS
#endif /* USE_SSL */
) && http_response_code == -1) {
p = lineBuf2->ptr;
while (*p && !IS_SPACE(*p))
p++;
while (*p && IS_SPACE(*p))
p++;
http_response_code = atoi(p);
if (fmInitialized) {
message(lineBuf2->ptr, 0, 0);
refresh();
}
}
if (!strncasecmp(lineBuf2->ptr, "content-transfer-encoding:", 26)) {
p = lineBuf2->ptr + 26;
while (IS_SPACE(*p))
p++;
if (!strncasecmp(p, "base64", 6))
uf->encoding = ENC_BASE64;
else if (!strncasecmp(p, "quoted-printable", 16))
uf->encoding = ENC_QUOTE;
else if (!strncasecmp(p, "uuencode", 8) ||
!strncasecmp(p, "x-uuencode", 10))
uf->encoding = ENC_UUENCODE;
else
uf->encoding = ENC_7BIT;
}
else if (!strncasecmp(lineBuf2->ptr, "content-encoding:", 17)) {
struct compression_decoder *d;
p = lineBuf2->ptr + 17;
while (IS_SPACE(*p))
p++;
uf->compression = CMP_NOCOMPRESS;
for (d = compression_decoders; d->type != CMP_NOCOMPRESS; d++) {
char **e;
for (e = d->encodings; *e != NULL; e++) {
if (strncasecmp(p, *e, strlen(*e)) == 0) {
uf->compression = d->type;
break;
}
}
if (uf->compression != CMP_NOCOMPRESS)
break;
}
uf->content_encoding = uf->compression;
}
#ifdef USE_COOKIE
else if (use_cookie && accept_cookie &&
pu && check_cookie_accept_domain(pu->host) &&
(!strncasecmp(lineBuf2->ptr, "Set-Cookie:", 11) ||
!strncasecmp(lineBuf2->ptr, "Set-Cookie2:", 12))) {
Str name = Strnew(), value = Strnew(), domain = NULL, path = NULL,
comment = NULL, commentURL = NULL, port = NULL, tmp2;
int version, quoted, flag = 0;
time_t expires = (time_t) - 1;
q = NULL;
if (lineBuf2->ptr[10] == '2') {
p = lineBuf2->ptr + 12;
version = 1;
}
else {
p = lineBuf2->ptr + 11;
version = 0;
}
#ifdef DEBUG
fprintf(stderr, "Set-Cookie: [%s]\n", p);
#endif /* DEBUG */
SKIP_BLANKS(p);
while (*p != '=' && !IS_ENDT(*p))
Strcat_char(name, *(p++));
Strremovetrailingspaces(name);
if (*p == '=') {
p++;
SKIP_BLANKS(p);
quoted = 0;
while (!IS_ENDL(*p) && (quoted || *p != ';')) {
if (!IS_SPACE(*p))
q = p;
if (*p == '"')
quoted = (quoted) ? 0 : 1;
Strcat_char(value, *(p++));
}
if (q)
Strshrink(value, p - q - 1);
}
while (*p == ';') {
p++;
SKIP_BLANKS(p);
if (matchattr(p, "expires", 7, &tmp2)) {
/* version 0 */
expires = mymktime(tmp2->ptr);
}
else if (matchattr(p, "max-age", 7, &tmp2)) {
/* XXX Is there any problem with max-age=0? (RFC 2109 ss. 4.2.1, 4.2.2 */
expires = time(NULL) + atol(tmp2->ptr);
}
else if (matchattr(p, "domain", 6, &tmp2)) {
domain = tmp2;
}
else if (matchattr(p, "path", 4, &tmp2)) {
path = tmp2;
}
else if (matchattr(p, "secure", 6, NULL)) {
flag |= COO_SECURE;
}
else if (matchattr(p, "comment", 7, &tmp2)) {
comment = tmp2;
}
else if (matchattr(p, "version", 7, &tmp2)) {
version = atoi(tmp2->ptr);
}
else if (matchattr(p, "port", 4, &tmp2)) {
/* version 1, Set-Cookie2 */
port = tmp2;
}
else if (matchattr(p, "commentURL", 10, &tmp2)) {
/* version 1, Set-Cookie2 */
commentURL = tmp2;
}
else if (matchattr(p, "discard", 7, NULL)) {
/* version 1, Set-Cookie2 */
flag |= COO_DISCARD;
}
quoted = 0;
while (!IS_ENDL(*p) && (quoted || *p != ';')) {
if (*p == '"')
quoted = (quoted) ? 0 : 1;
p++;
}
}
if (pu && name->length > 0) {
int err;
if (show_cookie) {
if (flag & COO_SECURE)
disp_message_nsec("Received a secured cookie", FALSE, 1,
TRUE, FALSE);
else
disp_message_nsec(Sprintf("Received cookie: %s=%s",
name->ptr, value->ptr)->ptr,
FALSE, 1, TRUE, FALSE);
}
err =
add_cookie(pu, name, value, expires, domain, path, flag,
comment, version, port, commentURL);
if (err) {
char *ans = (accept_bad_cookie == ACCEPT_BAD_COOKIE_ACCEPT)
? "y" : NULL;
if (fmInitialized && (err & COO_OVERRIDE_OK) &&
accept_bad_cookie == ACCEPT_BAD_COOKIE_ASK) {
Str msg = Sprintf("Accept bad cookie from %s for %s?",
pu->host,
((domain && domain->ptr)
? domain->ptr : "<localdomain>"));
if (msg->length > COLS - 10)
Strshrink(msg, msg->length - (COLS - 10));
Strcat_charp(msg, " (y/n)");
ans = inputAnswer(msg->ptr);
}
if (ans == NULL || TOLOWER(*ans) != 'y' ||
(err =
add_cookie(pu, name, value, expires, domain, path,
flag | COO_OVERRIDE, comment, version,
port, commentURL))) {
err = (err & ~COO_OVERRIDE_OK) - 1;
if (err >= 0 && err < COO_EMAX)
emsg = Sprintf("This cookie was rejected "
"to prevent security violation. [%s]",
violations[err])->ptr;
else
emsg =
"This cookie was rejected to prevent security violation.";
record_err_message(emsg);
if (show_cookie)
disp_message_nsec(emsg, FALSE, 1, TRUE, FALSE);
}
else
if (show_cookie)
disp_message_nsec(Sprintf
("Accepting invalid cookie: %s=%s",
name->ptr, value->ptr)->ptr, FALSE,
1, TRUE, FALSE);
}
}
}
#endif /* USE_COOKIE */
else if (!strncasecmp(lineBuf2->ptr, "w3m-control:", 12) &&
uf->scheme == SCM_LOCAL_CGI) {
Str funcname = Strnew();
int f;
p = lineBuf2->ptr + 12;
SKIP_BLANKS(p);
while (*p && !IS_SPACE(*p))
Strcat_char(funcname, *(p++));
SKIP_BLANKS(p);
f = getFuncList(funcname->ptr);
if (f >= 0) {
tmp = Strnew_charp(p);
Strchop(tmp);
pushEvent(f, tmp->ptr);
}
}
if (headerlist)
pushText(headerlist, lineBuf2->ptr);
Strfree(lineBuf2);
lineBuf2 = NULL;
}
if (thru)
addnewline(newBuf, "", propBuffer, NULL, 0, -1, -1);
if (src)
fclose(src);
}
char *
checkHeader(Buffer *buf, char *field)
{
int len;
TextListItem *i;
char *p;
if (buf == NULL || field == NULL || buf->document_header == NULL)
return NULL;
len = strlen(field);
for (i = buf->document_header->first; i != NULL; i = i->next) {
if (!strncasecmp(i->ptr, field, len)) {
p = i->ptr + len;
return remove_space(p);
}
}
return NULL;
}
char *
checkContentType(Buffer *buf)
{
char *p;
Str r;
p = checkHeader(buf, "Content-Type:");
if (p == NULL)
return NULL;
r = Strnew();
while (*p && *p != ';' && !IS_SPACE(*p))
Strcat_char(r, *p++);
#ifdef USE_M17N
if ((p = strcasestr(p, "charset")) != NULL) {
p += 7;
SKIP_BLANKS(p);
if (*p == '=') {
p++;
SKIP_BLANKS(p);
if (*p == '"')
p++;
content_charset = wc_guess_charset(p, 0);
}
}
#endif
return r->ptr;
}
struct auth_param {
char *name;
Str val;
};
struct http_auth {
int pri;
char *scheme;
struct auth_param *param;
Str (*cred) (struct http_auth * ha, Str uname, Str pw, ParsedURL *pu,
HRequest *hr, FormList *request);
};
enum {
AUTHCHR_NUL,
AUTHCHR_SEP,
AUTHCHR_TOKEN,
};
static int
skip_auth_token(char **pp)
{
char *p;
int first = AUTHCHR_NUL, typ;
for (p = *pp ;; ++p) {
switch (*p) {