-
Notifications
You must be signed in to change notification settings - Fork 25
/
http_sgml_plugin.pl
137 lines (114 loc) · 4.66 KB
/
http_sgml_plugin.pl
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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2002-2014, University of Amsterdam, VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(http_sgml_plugin, []).
:- use_module(http_client, []). % library we extend
:- use_module(library(sgml)).
:- use_module(library(debug)).
:- multifile
http_client:http_convert_data/4.
:- multifile
markup_type/2. % +MimeType, -ParseOptions
/** <module> Parse of HTML and XML documents for the HTTP client libs
This module provides a plugin for the HTTP client to handle xml, html
and sgml files using the SWI-Prolog sgml-parser from library(sgml).
Using this library avoids unnecessary copying of data as the sgml-parser
reads directly from the stream that established the HTTP connection.
This is a plugin for http_get/3 and http_post/4
@see http_open/3 is now the preferred interface for client side
processing of HTTP.
*/
http_client:http_convert_data(In, Fields, Data, Options) :-
memberchk(content_type(Type), Fields),
debug(sgml_plugin, 'Content type: ~w', [Type]),
( markup_type(Type, ParseOptions)
-> true
; type_major_props(Type, Major, Props),
default_markup_type(Major, ParseOptions0),
type_props(Props, ParseOptions0, ParseOptions)
),
merge_options([ max_errors(-1),
syntax_errors(quiet)
| ParseOptions
], Options, Merged),
markup_options(Fields, Merged, MarkupOptions),
debug(sgml_plugin, 'Markup options: ~p', [MarkupOptions]),
load_structure(stream(In), Data, MarkupOptions).
type_major_props(Type0, Type, Props) :-
sub_atom(Type0, B, _, A, ;),
!,
sub_atom(Type0, 0, B, _, Major),
sub_atom(Type0, _, A, 0, Props),
normalize_space(atom(Type), Major).
type_major_props(Type, Type, '').
type_props('', L, L).
type_props(Props, L0, L) :-
sub_atom(Props, _, _, A, 'charset='),
sub_atom(Props, _, A, 0, CharSet0),
downcase_atom(CharSet0, CharSet),
known_charset(CharSet),
L = [encoding(CharSet)|L0].
type_props(_, L, L).
known_charset('iso-8859-1').
known_charset('us-ascii').
known_charset('utf-8').
%! default_markup_type(+MimeType, -ParseOptions)
%
% State that the HTTP contents should be parsed with
% load_structure/3 using the returned options. This predicate may
% be hooked using the multifile predicate markup_type/2.
default_markup_type('text/xml',
[ dialect(xmlns)
]).
default_markup_type('text/html',
[ dtd(DTD),
dialect(sgml),
shorttag(false)
]) :-
dtd(html, DTD).
default_markup_type('text/x-sgml',
[ dialect(sgml)
]).
markup_options(Fields, Opt0, Options) :-
( memberchk(content_length(Bytes), Fields)
-> Options = [content_length(Bytes)|Opt0]
; Options = Opt0
).
%! merge_options(+Defaults, +GivenOptions, -Options)
%
% If an option is not in GivenOptions, use the one from
% Defaults.
merge_options([], Options, Options).
merge_options([H|T], Options0, Options) :-
functor(H, Name, Arity),
functor(H0, Name, Arity),
memberchk(H0, Options0),
!,
merge_options(T, Options0, Options).
merge_options([H|T], Options0, Options) :-
merge_options(T, [H|Options0], Options).