forked from xinzsky/libutils-c
/
htmlextract.c
1588 lines (1401 loc) · 49.4 KB
/
htmlextract.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) 2008 Liu xing zhi.
@2010.11.12: added support utf-8.
@2011.04.10: added reserved interesting tag attributes.
@2012.09.18: added extract list url.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "comn.h"
#include "utils.h"
#include "htmlparse.h"
#include "htmlextract.h"
#include "fsfhash.h"
#include "md5.h"
#include "xmalloc.h"
#include "charset.h"
#include "xstring.h"
#include "url.h"
#include "crc32.h"
const char * extract_text_mapfun(struct taginfo *taginfo, void *maparg);
const char * extract_interesting_tag_attr_mapfun(struct taginfo *taginfo, void *maparg);
// tag_attr_list不能为空,为空表示所有属性解析过程中都会返回,可以杜撰一个不存在的属性。
void extract_text_init(spec_tag_t *spec_tag_list, int tag_list_size, struct hash_table ** spec_tag_ht_pp,
tag_attr_t *tag_attr_list, int attr_list_size, struct hash_table ** tag_attr_ht_pp)
{
int i;
struct hash_table * ht;
tag_attr_t * tagattr;
ht = make_nocase_string_hash_table(tag_list_size);
for (i = 0; i < tag_list_size; i++)
hash_table_put(ht, spec_tag_list[i].tag_name, spec_tag_list + i);
*spec_tag_ht_pp = ht;
ht = make_nocase_string_hash_table(attr_list_size);
for (i = 0; i < attr_list_size; i++) {
// 先检查属性是否存在...
tagattr = hash_table_get(ht,tag_attr_list[i].tag_attr);
if(!tagattr) {
// 不存在,插入为首节点
tag_attr_list[i].tail = tag_attr_list + i;
hash_table_put(ht, tag_attr_list[i].tag_attr, tag_attr_list + i);
} else {
// 存在,插入到链表的末尾
tagattr->tail->next = tag_attr_list + i;
tagattr->tail = tag_attr_list + i;
}
}
*tag_attr_ht_pp = ht;
}
void extract_text_free(struct hash_table *spec_tag_ht, struct hash_table * tag_attr_ht)
{
if (spec_tag_ht)
hash_table_destroy(spec_tag_ht);
if (tag_attr_ht)
hash_table_destroy(tag_attr_ht);
}
/*
pure text, text <tag>,<tag> text
*/
void extract_text(const char *html_content, int content_len, buffer_t *text_buffer_p, const struct hash_table *spec_tag_ht, const struct hash_table *tag_attr_ht, int is_reserve_indent,int codetype,int xssfilter)
{
int len;
map_arg_t maparg;
maparg.html_content_end_position = html_content + content_len;
maparg.last_tag_end_position = html_content;/* text <tag> */
maparg.spec_tag_ht = spec_tag_ht;
maparg.tag_attr_ht = tag_attr_ht;
maparg.text_buffer_p = text_buffer_p;
maparg.text_buffer_p->reserve1 = 1;
maparg.is_reserve_indent = is_reserve_indent;
maparg.codetype = codetype;
maparg.xssfilter = xssfilter;
//注意: 感兴趣标签列表为NULL,表示对所有标签都感兴趣
map_html_tags(html_content, content_len, extract_text_mapfun, &maparg, MHT_TRIM_VALUES, NULL, tag_attr_ht);
/* pure text */
if (maparg.last_tag_end_position == html_content && text_buffer_p->free >= content_len)
{
memcpy(text_buffer_p->p + text_buffer_p->pos, html_content, content_len);
text_buffer_p->free -= content_len;
text_buffer_p->pos += content_len;
return;
}
/* <tag> text */
len = maparg.html_content_end_position - maparg.last_tag_end_position;
if (maparg.last_tag_end_position != maparg.html_content_end_position && text_buffer_p->free >= len)
{
memcpy(text_buffer_p->p + text_buffer_p->pos, maparg.last_tag_end_position, len);
text_buffer_p->free -= len;
text_buffer_p->pos += len;
}
return;
}
/* return NULL -- error,html page parser is end.
return (taginfo->end_position - 1) -- normal.
return (skip_close_tag_end_position - 1) -- html parse goto skip_close_tag_end_position - 1.
arg->text_buffer_p->reserve1 //为1:表示该buffer最后一个字节是空格字符
*/
const char * extract_text_mapfun(struct taginfo *taginfo, void *maparg)
{
const char *p;
spec_tag_t *spec_tag = NULL;
map_arg_t *arg = (map_arg_t *)maparg;
/* process between last tag and this tag text */
/* <last tag>text<this tag> */
/* ^ ^ */
/* src_beg src_end */
{
const char *src_beg, *src_end;
char *dst;
int i, char_cnt;
if (arg->last_tag_end_position == NULL)
src_beg = taginfo->start_position;
else
src_beg = arg->last_tag_end_position;
src_end = taginfo->start_position;
dst = arg->text_buffer_p->p + arg->text_buffer_p->pos;
char_cnt = src_end - src_beg;
for (i = 0;i < char_cnt;i++)
{
/* whitespace char */
if (*(src_beg + i) < ' ' && *(src_beg + i) >= 0)
continue;
// xssfilter时标签之间的特殊字符需要encode
if(arg->xssfilter && (*(src_beg + i) == '<' || *(src_beg + i) == '>')) { // < >
if (arg->text_buffer_p->free <= 5) {
arg->last_tag_end_position = taginfo->end_position;
return NULL; /* html page parser is end */
}
if(*(src_beg + i) == '<') memcpy(dst,"<",4);
if(*(src_beg + i) == '>') memcpy(dst,">",4);
dst += 4;
arg->text_buffer_p->reserve1 = 0;
arg->text_buffer_p->pos += 4;
arg->text_buffer_p->free -= 4;
if (arg->text_buffer_p->free <= 1) {
arg->last_tag_end_position = taginfo->end_position;
return NULL; /* html page parser is end */
}
continue;
}
/* space */
if (*(src_beg + i) == ' ') {
if (arg->text_buffer_p->reserve1 == 1)
continue;
*dst = ' ';
arg->text_buffer_p->reserve1 = 1;
goto dst_add;
}
/* decode entities only*/
if (!arg->xssfilter && *(src_beg + i) == '&' && *(src_beg + i + 1) == 'n'
&& *(src_beg + i + 2) == 'b' && *(src_beg + i + 3) == 's'
&& *(src_beg + i + 4) == 'p' && *(src_beg + i + 5) == ';')
{
const char *tp = src_beg + i;
int ret;
ret = decode_entity(&tp, src_end);
if (ret != -1)
{
if (ret == ' ')
{
if (arg->text_buffer_p->reserve1 == 1)
{
/* */
/* ^ ^tp */
i = tp - src_beg - 1;
continue;
}
*dst = ' ';
arg->text_buffer_p->reserve1 = 1;
i = tp - src_beg - 1;
goto dst_add;
}
*dst = ret;
arg->text_buffer_p->reserve1 = 0;
i = tp - src_beg - 1;
goto dst_add;
}
}
if (*((unsigned char*)(src_beg + i)) < 0x80)
{
*dst = *(src_beg + i);
arg->text_buffer_p->reserve1 = 0;
goto dst_add;
}
else //multi-byte char
{
int chlen,cn;
if(arg->codetype == CODETYPE_GBK)
chlen = GBKCHLEN((unsigned char)src_beg[i]);
else if(arg->codetype == CODETYPE_UTF8)
chlen = UTF8CHLEN((unsigned char)src_beg[i]);
else
chlen = 2;
if (i + chlen - 1 < char_cnt) //防止*(src_beg+i+1)= '<'
{
//GBK中中文空格为A1A1,UTF-8中中文空格为E38080
if (arg->is_reserve_indent == 0 && ( \
(arg->codetype == CODETYPE_GBK && chlen == 2 && *(src_beg + i) == '\xa1' && *(src_beg + i + 1) == '\xa1')\
||(arg->codetype == CODETYPE_UTF8 && chlen == 3 \
&& *(src_beg + i) == '\xe3' && *(src_beg + i + 1) == '\x80' && *(src_beg + i + 2) == '\x80') \
)) {
if (arg->text_buffer_p->reserve1 == 1) {
i += (chlen-1);
continue;
} else {
*dst = ' ';
arg->text_buffer_p->reserve1 = 1;
i += (chlen -1);
goto dst_add;
}
} else {
for(cn=0;cn<chlen;++cn) {
*dst = *(src_beg + i + cn);
++dst;
arg->text_buffer_p->pos++;
arg->text_buffer_p->free--;
if (arg->text_buffer_p->free <= 1) {
arg->last_tag_end_position = taginfo->end_position;
return NULL; /* html page parser is end */
}
}
arg->text_buffer_p->reserve1 = 0;
i += (chlen -1);
continue;
}
} else { //this multi byte char error.
break;
}
}
dst_add:
++dst;
arg->text_buffer_p->pos++;
arg->text_buffer_p->free--;
if (arg->text_buffer_p->free <= 1) {
arg->last_tag_end_position = taginfo->end_position;
return NULL; /* html page parser is end */
}
}
} /* end of process between last tag and this tag text */
/* comment tag or declare tag or close tag ?*/
if (taginfo->end_tag_p == DECLARE_TAG /*|| taginfo->end_tag_p == CLOSE_TAG*/)
{
arg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
/* this tag is special tag ? */
spec_tag = hash_table_get(arg->spec_tag_ht, taginfo->name);
if (!spec_tag) /* not a speical tag */
{
arg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
switch (spec_tag->type)
{
case SKIP_TAG:
if (taginfo->end_tag_p == CLOSE_TAG)
{
arg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
if (spec_tag->handler == NULL)
{
/* process block tag */
/* <block >....block....</blcok> */
/* ^ ^ ^ */
/* block_beg block_end p */
const char *block_beg, *block_end;
block_beg = taginfo->end_position;
block_end = stristrb(block_beg, arg->html_content_end_position - block_beg, spec_tag->end_str, strlen(spec_tag->end_str));
if (block_end == NULL)
{
arg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
else
{
p = memchr(block_end, '>', arg->html_content_end_position - block_end);
if (p == NULL)
{
arg->last_tag_end_position = taginfo->end_position;
return NULL;
}
arg->last_tag_end_position = p + 1;
return p;
}
}
else
{
return spec_tag->handler(taginfo, arg, spec_tag);
}
case RESERVE_TAG:
if (spec_tag->handler == NULL)
{
int tag_len,k, blen, bpos;;
const char *tag_ptr;
char tagbuf[1024];
tag_attr_t * tagattr;
// 保留标签,但需要过滤掉属性,只保留指定的属性,added @2011.4.10
if(taginfo->end_tag_p == START_TAG) {
tagbuf[0] = 0;
bpos = 0;
blen = snprintf(tagbuf+bpos,sizeof(tagbuf)-bpos,"<%s",taginfo->name);
bpos += blen;
for(k=0;k<taginfo->nattrs;k++) {
tagattr = hash_table_get(arg->tag_attr_ht, taginfo->attrs[k].name);
if(tagattr)
{
do {
if(strcasecmp(taginfo->name, tagattr->tag_name) == 0)
break;
else
tagattr=tagattr->next;
} while(tagattr);
if(!tagattr)
continue;
if(arg->xssfilter) {
if( (strcasecmp(taginfo->name,"img") == 0 && strcasecmp(taginfo->attrs[k].name, "src") == 0)
||(strcasecmp(taginfo->name,"a") == 0 && strcasecmp(taginfo->attrs[k].name, "href") == 0)) {
if( taginfo->attrs[k].value \
&& !strcasebeginwith("http://",taginfo->attrs[k].value) \
&& !strcasebeginwith("https://",taginfo->attrs[k].value) )
continue;
}
}
if(taginfo->attrs[k].value)
blen = snprintf(tagbuf+bpos,sizeof(tagbuf)-bpos," %s=\"%s\"",taginfo->attrs[k].name,taginfo->attrs[k].value);
else
blen = snprintf(tagbuf+bpos,sizeof(tagbuf)-bpos," %s=\"%s\"",taginfo->attrs[k].name,taginfo->attrs[k].name);
if(blen < 0 || blen >= sizeof(tagbuf)-bpos) {
arg->last_tag_end_position = taginfo->end_position;
return NULL;
}
bpos += blen;
}
}
if(strcasecmp(taginfo->name,"br") == 0 || strcasecmp(taginfo->name,"img") == 0)
blen = snprintf(tagbuf + bpos, sizeof(tagbuf) - bpos, " />");
else if(arg->xssfilter && strcasecmp(taginfo->name,"a") == 0)
blen = snprintf(tagbuf + bpos, sizeof(tagbuf) - bpos, " target=\"_blank\" >");
else
blen = snprintf(tagbuf + bpos, sizeof(tagbuf) - bpos, " >");
bpos += blen;
tag_len = bpos;
tag_ptr = tagbuf;
} else {
tag_len = taginfo->end_position - taginfo->start_position;
tag_ptr = taginfo->start_position;
}
if (arg->text_buffer_p->free >= tag_len) {
memcpy(arg->text_buffer_p->p + arg->text_buffer_p->pos, tag_ptr, tag_len);
arg->text_buffer_p->pos += tag_len;
arg->text_buffer_p->free -= tag_len;
arg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
} else {
arg->last_tag_end_position = taginfo->end_position;
return NULL;
}
}
else
{
return spec_tag->handler(taginfo, arg, spec_tag);
}
case EXTRACT_TAG:
if (taginfo->end_tag_p == CLOSE_TAG)
{
arg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
if (spec_tag->handler == NULL)
{
arg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
else
{
return spec_tag->handler(taginfo, arg, spec_tag);
}
default:
arg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
}
/* process block tag */
/* <block >....block....</blcok> */
/* ^ ^ ^ */
/* block_beg block_end p */
const char *tag_handler_title(struct taginfo *taginfo, map_arg_t* maparg, spec_tag_t *spec_tag)
{
const char *block_beg, *block_end, *p;
buffer_t *title_buffer = (buffer_t *)spec_tag->handler_arg;
int no_title_len = strlen("no title");
block_beg = taginfo->end_position;
block_end = stristrb(block_beg, maparg->html_content_end_position - block_beg, spec_tag->end_str, strlen(spec_tag->end_str));
if (title_buffer->reserve1 >= 1) //表示已经提取了一个title,不再提取了
{
if (block_end == NULL)
{
maparg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
else
{
p = memchr(block_end, '>', maparg->html_content_end_position - block_end);
if (p == NULL)
{
maparg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
maparg->last_tag_end_position = p + 1;
return p;
}
}
if (block_end == NULL)
{
if (title_buffer->free > no_title_len)
{
strcpy(title_buffer->p + title_buffer->pos, "no title");
title_buffer->pos += no_title_len;
title_buffer->free -= no_title_len;
}
title_buffer->reserve1 = 1;
maparg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
else
{
int title_len = block_end - block_beg;
if (title_len == 0)
{
if (title_buffer->free > no_title_len)
{
strcpy(title_buffer->p + title_buffer->pos, "no title");
title_buffer->pos += no_title_len;
title_buffer->free -= no_title_len;
}
}
else
{
if (title_len < title_buffer->free)
{
memcpy(title_buffer->p + title_buffer->pos, block_beg, title_len);
title_buffer->pos += title_len;
title_buffer->free -= title_len;
*(title_buffer->p + title_buffer->pos) = 0;
}
else
{
memcpy(title_buffer->p + title_buffer->pos, block_beg, title_buffer->free - 1);
title_buffer->pos += (title_buffer->free - 1);
title_buffer->free -= (title_buffer->free - 1);
*(title_buffer->p + title_buffer->pos) = 0;
}
trim(title_buffer->p);
title_buffer->pos = strlen(title_buffer->p);
if (title_buffer->pos == 0)
{
if (title_buffer->size > no_title_len)
{
strcpy(title_buffer->p, "no title");
title_buffer->pos = no_title_len;
title_buffer->free = title_buffer->size - no_title_len;
}
else
{
title_buffer->pos = 0;
title_buffer->free = title_buffer->size;
}
}
}
title_buffer->reserve1 = 1;
p = memchr(block_end, '>', maparg->html_content_end_position - block_end);
if (p == NULL)
{
maparg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
maparg->last_tag_end_position = p + 1;
return p;
}
}
const char *tag_handler_anchor(struct taginfo *taginfo, map_arg_t* maparg, spec_tag_t *spec_tag)
{
const char *block_beg, *block_end, *p;
buffer_t *anchor_text_buffer = spec_tag->handler_arg;
block_beg = taginfo->end_position;
block_end = stristrb(block_beg, maparg->html_content_end_position - block_beg, spec_tag->end_str, strlen(spec_tag->end_str));
if (block_end == NULL)
{
maparg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
else
{
/* copy anchor text to anchor_text_buffer */
int n = 0;
char * ank_dst;
ank_dst = anchor_text_buffer->p + anchor_text_buffer->pos;
while (block_beg + n != block_end)
{
/* whitespace char */
if (*(block_beg + n) < ' ' && *(block_beg + n) >= 0)
{
n++;
continue;
}
//space
if (*(block_beg + n) == ' ')
{
if (anchor_text_buffer->reserve1 == 1)
{
n++;
continue;
}
else
{
*ank_dst = ' ';
anchor_text_buffer->reserve1 = 1;
n++;
goto ank_dst_add;
}
}
/* decode entities only */
if (*(block_beg + n) == '&' && *(block_beg + n + 1) == 'n' &&
*(block_beg + n + 2) == 'b' && *(block_beg + n + 3) == 's' &&
*(block_beg + n + 4) == 'p' && *(block_beg + n + 5) == ';')
{
const char *tp = block_beg + n;
int ret;
ret = decode_entity(&tp, block_end);
if (ret != -1)
{
if (ret == ' ')
{
if (anchor_text_buffer->reserve1 == 1)
{
/* */
/* ^ ^tp */
n = tp - block_beg;
continue;
}
*ank_dst = ' ';
anchor_text_buffer->reserve1 = 1;
n = tp - block_beg;
goto ank_dst_add;
}
*ank_dst = ret;
anchor_text_buffer->reserve1 = 0;
n = tp - block_beg;
goto ank_dst_add;
}
}
/* skip html tag between <a>...</a> */
if (*(block_beg + n) == '<')
{
char *close_tag;
close_tag = (char *)memchr(block_beg + n + 1, '>', block_end - (block_beg + n + 1));
if (close_tag == NULL) /* error html tag,skip this anchor tag */
{
p = memchr(block_end, '>', maparg->html_content_end_position - block_end);
if (p == NULL)
{
maparg->last_tag_end_position = taginfo->end_position;
return NULL;
}
maparg->last_tag_end_position = p + 1;
return p;
}
n += (close_tag - (block_beg + n) + 1);
continue;
}
/* ascii char */
if (*((unsigned char *)(block_beg + n)) < 0x80)
{
*ank_dst = *(block_beg + n);
anchor_text_buffer->reserve1 = 0;
n++;
goto ank_dst_add;
}
else /* multi-byte char */
{
int chlen,cn;
if(maparg->codetype == CODETYPE_GBK)
chlen = GBKCHLEN((unsigned char)block_beg[n]);
else if(maparg->codetype == CODETYPE_UTF8)
chlen = UTF8CHLEN((unsigned char)block_beg[n]);
else
chlen = 2;
if (block_beg + n + chlen <= block_end) {
//GBK中中文空格为A1A1,UTF-8中中文空格为E38080
if (maparg->is_reserve_indent == 0 && ( \
(maparg->codetype == CODETYPE_GBK && chlen == 2 && *(block_beg + n) == '\xa1' && *(block_beg + n + 1) == '\xa1')\
||(maparg->codetype == CODETYPE_UTF8 && chlen == 3 \
&& *(block_beg + n) == '\xe3' && *(block_beg + n + 1) == '\x80' && *(block_beg + n + 2) == '\x80')))
{
if (anchor_text_buffer->reserve1 == 1)
{
n += chlen;
continue;
}
else
{
*ank_dst = ' ';
anchor_text_buffer->reserve1 = 1;
n += chlen;
goto ank_dst_add;
}
} else {
for(cn=0;cn<chlen;++cn) {
*ank_dst = *(block_beg + n + cn);
++ank_dst;
anchor_text_buffer->pos++;
anchor_text_buffer->free--;
if (anchor_text_buffer->free <= 1)
{
maparg->last_tag_end_position = taginfo->end_position;
return NULL; /* html page parser is end */
}
}
anchor_text_buffer->reserve1 = 0;
n += chlen;
continue;
}
} else { //this multi byte char error.
break;
}
}
ank_dst_add:
++ank_dst;
anchor_text_buffer->pos++;
anchor_text_buffer->free--;
if (anchor_text_buffer->free <= 1)
{
maparg->last_tag_end_position = taginfo->end_position;
return NULL; /* html page parser is end */
}
}/* end for while */
if (anchor_text_buffer->reserve1 == 0)
{
*ank_dst = ' ';
anchor_text_buffer->reserve1 = 1;
++ank_dst;
anchor_text_buffer->pos++;
anchor_text_buffer->free--;
if (anchor_text_buffer->free == 0)
{
maparg->last_tag_end_position = taginfo->end_position;
return NULL; /* html page parser is end */
}
}
p = memchr(block_end, '>', maparg->html_content_end_position - block_end);
if (p == NULL)
{
maparg->last_tag_end_position = taginfo->end_position;
return NULL;
}
maparg->last_tag_end_position = p + 1;
return p;
}
}
/*
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta name="description" content="中国造ARJ21-700飞机将于明年上半年首飞">
<meta name="keywords" content="飞机 支线">
*/
const char *tag_handler_meta(struct taginfo *taginfo, map_arg_t* maparg, spec_tag_t *spec_tag)
{
struct _meta_text_buffer * b = (struct _meta_text_buffer *)spec_tag->handler_arg;
buffer_t *meta_keyword_buffer = b->meta_keyword_buffer_p;
buffer_t *meta_desc_buffer = b->meta_desc_buffer_p;
int i, j, len;
for (i = 0;i < taginfo->nattrs;i++)
{
if (strcasecmp(taginfo->attrs[i].name, "name") == 0 && strcasecmp(taginfo->attrs[i].value, "description") == 0)
{
for (j = 0;j < taginfo->nattrs;j++)
{
if (strcasecmp(taginfo->attrs[j].name, "content") == 0)
{
len = strlen(taginfo->attrs[j].value);
if (meta_desc_buffer->free >= len) {
memcpy(meta_desc_buffer->p + meta_desc_buffer->pos, taginfo->attrs[j].value, len);
meta_desc_buffer->pos += len;
meta_desc_buffer->free -= len;
}
maparg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
}
}
if (strcasecmp(taginfo->attrs[i].name, "name") == 0 && strcasecmp(taginfo->attrs[i].value, "keywords") == 0)
{
for (j = 0;j < taginfo->nattrs;j++)
{
if (strcasecmp(taginfo->attrs[j].name, "content") == 0)
{
len = strlen(taginfo->attrs[j].value);
if (meta_keyword_buffer->free >= len) {
memcpy(meta_keyword_buffer->p + meta_keyword_buffer->pos, taginfo->attrs[j].value, len);
meta_keyword_buffer->pos += len;
meta_keyword_buffer->free -= len;
}
maparg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
}
}
}
maparg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
/*
<img src=http://counter.csdn.net/pv.aspx?id=23 border=0 width=0 height=0>
*/
const char *tag_handler_img(struct taginfo *taginfo, map_arg_t* maparg, spec_tag_t *spec_tag)
{
struct _img_arg *img_arg = (struct _img_arg *)spec_tag->handler_arg;
buffer_t *text_buffer = maparg->text_buffer_p;
buffer_t *url_buffer = img_arg->url_buffer_p;
int i, j, k, url_len, len, rpos, *w, imgextnum;
char *file, *ext, *p, *alt;
char url_md5[33], url[MAX_URL_LEN+1], rpath[MAX_PATH_LEN+1];
const char *imgext[] = {".jpg", ".png", ".jpeg", ".gif", ".bmp"};
switch (img_arg->op)
{
case IMG_OP_FILTER:
goto end;
case IMG_OP_RESERVE:
for (i=0;i<taginfo->nattrs;i++) {
if (strcasecmp(taginfo->attrs[i].name, "src") == 0) {
if(!taginfo->attrs[i].value || !taginfo->attrs[i].value[0])
goto end;
p = url_mergenormalize(taginfo->attrs[i].value, img_arg->base, img_arg->referer, img_arg->pageisutf8, img_arg->cd,url, MAX_URL_LEN + 1);
if (!p) goto end;
APPEND_BUFFER((*text_buffer),"<img ",5);
APPEND_BUFFER((*text_buffer),"src=\"",5);
APPEND_BUFFER((*text_buffer),url,strlen(url));
APPEND_BUFFER((*text_buffer),"\" ",2);
for (j=0;j<taginfo->nattrs;j++) {
if (strcasecmp(taginfo->attrs[j].name, "src") != 0) {
if(!taginfo->attrs[j].value || !taginfo->attrs[j].value[0])
continue;
APPEND_BUFFER((*text_buffer),taginfo->attrs[j].name,strlen(taginfo->attrs[j].name));
APPEND_BUFFER((*text_buffer),"=\"",2);
APPEND_BUFFER((*text_buffer),taginfo->attrs[j].value,strlen(taginfo->attrs[j].value));
APPEND_BUFFER((*text_buffer),"\" ",2);
}
}
APPEND_BUFFER((*text_buffer)," />",3);
goto end;
}
}
break;
/* tag_len = taginfo->end_position - taginfo->start_position;
if (text_buffer->free >= tag_len) {
memcpy(text_buffer->p + text_buffer->pos, taginfo->start_position, tag_len);
text_buffer->pos += tag_len;
text_buffer->free -= tag_len;
goto end;
} else {
maparg->last_tag_end_position = taginfo->end_position;
return NULL;
} */
case IMG_OP_REPLACE:
for (i = 0;i < taginfo->nattrs;i++) {
if (strcasecmp(taginfo->attrs[i].name, "src") == 0) {
if(!taginfo->attrs[i].value || !taginfo->attrs[i].value[0])
goto end;
p = url_mergenormalize(taginfo->attrs[i].value, img_arg->base, img_arg->referer, img_arg->pageisutf8, img_arg->cd,url, MAX_URL_LEN + 1);
if (!p) goto end;
file = strchr(url + 7, '/');
if (!file) goto end;
url_len = strlen(url);
md5s((unsigned char *)url, url_len, url_md5);
rpos = 0;
rpath[0] = 0;
if (img_arg->image_option_dir) {
strcpy(rpath, img_arg->image_option_dir);
rpos += strlen(img_arg->image_option_dir);
if (rpath[rpos-1] != PATH_SEPARATOR) {
rpath[rpos] = PATH_SEPARATOR;
rpath[++rpos] = '\0';
}
}
if (img_arg->image_subdir_num) {
p = url_md5;
w = img_arg->image_subdirname_width;
for (k = 0;k < img_arg->image_subdir_num;k++) {
memcpy(rpath + rpos, p, w[k]);
rpos += w[k];
p += w[k];
rpath[rpos++] = '/';
rpath[rpos] = 0;
}
}
memcpy(rpath + rpos, url_md5, 32);
rpos += 32;
rpath[rpos] = 0;
ext = strrchr(file, '.');
if (ext) {
imgextnum = countof(imgext);
for (k = 0;k < imgextnum;k++) {
if (strcasecmp(ext, imgext[k]) == 0)
break;
}
if (k != imgextnum) {
len = strlen(ext);
memcpy(rpath + rpos, ext, len);
rpos += len;
rpath[rpos] = 0;
}
}
//xhtml中每个图片标签都必须有ALT说明文字。
//<img src="ball.jpg" alt="large red ball" />
//保留原来img中alt文本。
alt = "";
for (j=0;j<taginfo->nattrs;j++){
if (strcasecmp(taginfo->attrs[j].name, "alt") == 0 && taginfo->attrs[j].value) {
alt = taginfo->attrs[j].value;
break;
}
}
APPEND_BUFFER((*text_buffer),"<img ",5);
APPEND_BUFFER((*text_buffer),"src=\"",5);
if(img_arg->image_base_url) {
APPEND_BUFFER((*text_buffer),img_arg->image_base_url,strlen(img_arg->image_base_url));
}
APPEND_BUFFER((*text_buffer),rpath,strlen(rpath));
APPEND_BUFFER((*text_buffer),"\" ",2);
APPEND_BUFFER((*text_buffer),"alt=\"",5);
APPEND_BUFFER((*text_buffer),alt,strlen(alt));
APPEND_BUFFER((*text_buffer),"\" ",2);
for (j=0;j<taginfo->nattrs;j++) {
if (strcasecmp(taginfo->attrs[j].name, "src") != 0 && \
strcasecmp(taginfo->attrs[j].name, "alt") != 0) {
if(!taginfo->attrs[j].value || !taginfo->attrs[j].value[0])
continue;
APPEND_BUFFER((*text_buffer),taginfo->attrs[j].name,strlen(taginfo->attrs[j].name));
APPEND_BUFFER((*text_buffer),"=\"",2);
APPEND_BUFFER((*text_buffer),taginfo->attrs[j].value,strlen(taginfo->attrs[j].value));
APPEND_BUFFER((*text_buffer),"\" ",2);
}
}
APPEND_BUFFER((*text_buffer)," />",3);
len = strlen(img_arg->url_prefix);
if (url_buffer->free < len + url_len + 1) { /* '\n' */
RESIZE_BUFFER((*url_buffer), url_buffer->size, (len + url_len + 1)*2)
}
memcpy(url_buffer->p + url_buffer->pos, img_arg->url_prefix, len);
url_buffer->pos += len;
url_buffer->free -= len;
memcpy(url_buffer->p + url_buffer->pos, url, url_len);
url_buffer->pos += url_len;
url_buffer->free -= url_len;
*(url_buffer->p + url_buffer->pos) = '\n';
url_buffer->pos ++;
url_buffer->free --;
url_buffer->reserve2++;
goto end;
}
}
break;
default:
break;
}
end:
maparg->last_tag_end_position = taginfo->end_position;
return (taginfo->end_position - 1);
}
struct interesting_tag_attr
{
char *interesting_tag_attr;
buffer_t *interesting_tag_attr_buf_p;
int count;
int is_ok;
};
/* <BASE HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/"/>
<a href=""> text </a>
<img src="" >
return:buffer:Url\nUrl\...\n
*/
int extract_interesting_tag_attr(char *html_content, int content_len,
char *interesting_tag, char *interesting_tag_attr,
buffer_t *interesting_tag_attr_buf_p, int count)
{
struct interesting_tag_attr maparg;
struct hash_table * interesting_tag_ht, *interesting_attr_ht;
if (count == 0)
return -1;
interesting_tag_ht = make_nocase_string_hash_table(1);
interesting_attr_ht = make_nocase_string_hash_table(1);
hash_table_put(interesting_tag_ht, interesting_tag, interesting_tag_attr);
hash_table_put(interesting_attr_ht, interesting_tag_attr, interesting_tag);
maparg.interesting_tag_attr = interesting_tag_attr;
maparg.interesting_tag_attr_buf_p = interesting_tag_attr_buf_p;
maparg.is_ok = 0;
maparg.count = count;
map_html_tags(html_content, content_len, extract_interesting_tag_attr_mapfun, &maparg, MHT_TRIM_VALUES, interesting_tag_ht, interesting_attr_ht);
if (interesting_tag_ht)
hash_table_destroy(interesting_tag_ht);
if (interesting_attr_ht)
hash_table_destroy(interesting_attr_ht);
if (maparg.is_ok)
return 0;
else
return -1;
}