blib/lib/XML/LibXML/Proxy.pm

filter: Statementstmt 65.3% 17 / 26 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod 100% 1 / 1 totaltotal 64.8% 24 / 37 CC 2 SCAR 8.0
CC
2
Cov
61%
_loader
6.0
set
2.0
BEGIN
1.0
CRAP 2.2
↑ Summary
1 =encoding utf-8
2  
3 =head1 NAME
4  
5 XML::LibXML::Proxy - Force LibXML to use a proxy for HTTP/HTTPS external entities
6  
7 =head1 SYNOPSIS
8  
9 use XML::LibXML;
10 use XML::LibXML::Proxy;
11  
12 XML::LibXML::Proxy->set('http://127.0.0.1:8080');
13  
14 # Use XML::LibXML normally...
15  
16 =head1 DESCRIPTION
17  
18 Force L<XML::LibXML> to use a proxy using L<LWP::UserAgent> as its external
19 entity loader, instead of connecting directly to remote servers. Adds HTTPS
20 support as a bonus.
21  
22 LibXML's native HTTP client has no proxy support, and the mere parsing or
23 validating of an XML document risks making network requests to obtain
24 referenced DTDs. This causes slow performance and unnecessary strain on
25 network resources and the remote server.
26  
27 When dealing just with slow-changing standards, such as XHTML 1.1 Strict,
28 downloading the necessary DTDs locally and setting up a "catalog" for LibXML
29 to map known remote DTDs to local files is cumbersome but tolerable. Some
30 standards however, evolve more rapidly and have large numbers of versions in
31 the wild, making maintaining a complete catalog near-impossible.
32  
33 Forcing LibXML to request DTDs through a caching forward proxy is an elegant
34 solution to this problem (and is recommended by the W3 Consortium). Short of
35 LibXML doing it, now the proxy can do proper caching based on HTTP response
36 headers.
37  
38 =cut
39  
40 1 ▶use 5.6.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 package XML::LibXML::Proxy;
45  
46 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 our $VERSION = 'v0.1.1';
51 1 our $ua = undef;
52 }
53  
54 =head1 CLASS METHODS
55  
56 =over
57  
58 =item C<B<set>( I<$url> )>
59  
60 Activate use of a proxy, set to I<C<$url>>.
61  
62 =cut
63  
64 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
65 0 my ($class, $proxy) = @_;
66  
67 0 my $ua = new LWP::UserAgent;
68 0 $ua->proxy(['http', 'https'] => $proxy);
69 0 $ua->timeout(30);
70 0 $XML::LibXML::Proxy::ua = $ua;
71 0 XML::LibXML::externalEntityLoader(\&_loader);
72 }
73  
74 0 ▶sub _loader {
Subroutine _loader: not called CC=2 SCAR=17.9
75 0 my ($uri) = @_;
76 0 my $res = $XML::LibXML::Proxy::ua->get($uri);
77 0 ▶ return $res->is_success ? $res->decoded_content : '';
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
78 }
79  
80 =back
81  
82 =head1 KNOWN BUGS
83  
84 There was absolutely no way to override Libxml's built-in "nanohttp" client
85 for HTTP URIs using any mechanism described in L<XML::LibXML::InputCallback>,
86 so I had to resort to using C<XML::LibXML::externalEntityLoader()> which
87 overrides 100% of external entity loading.
88  
89 This means that B<C<file:///> and other schemes ARE NO LONGER SUPPORTED> when
90 using this proxy.
91  
92 =head1 EXAMPLES
93  
94 For a quick caching forward proxy, I'm using Nginx with the following
95 configuration:
96  
97 server {
98 listen 127.0.0.1:8080;
99 location / {
100 proxy_buffering on;
101 resolver 8.8.8.8 8.8.4.4;
102 expires max;
103 proxy_cache forward-zone;
104 proxy_cache_valid 200 302 301 1w;
105 proxy_cache_valid any 1m;
106 proxy_cache_key "$scheme:$proxy_host:$request_uri";
107 proxy_pass $scheme://$host$request_uri;
108 proxy_set_header Host $http_host;
109 proxy_set_header X-Real-IP $remote_addr;
110 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
111 proxy_ignore_headers "Set-Cookie";
112 }
113 }
114  
115 =head1 AUTHOR
116  
117 Stéphane Lavergne L<https://github.com/vphantom>
118  
119 =head1 ACKNOWLEDGEMENTS
120  
121 Graph X Design Inc. L<https://www.gxd.ca/> sponsored this project.
122  
123 =head1 COPYRIGHT & LICENSE
124  
125 Copyright (c) 2017-2018 Stéphane Lavergne L<https://github.com/vphantom>
126  
127 Permission is hereby granted, free of charge, to any person obtaining a copy
128 of this software and associated documentation files (the "Software"), to deal
129 in the Software without restriction, including without limitation the rights
130 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
131 copies of the Software, and to permit persons to whom the Software is
132 furnished to do so, subject to the following conditions:
133  
134 The above copyright notice and this permission notice shall be included in all
135 copies or substantial portions of the Software.
136  
137 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
138 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
139 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
140 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
141 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
142 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
143 SOFTWARE.
144  
145 =cut
146  
147 1 1;
↑ Summary