XML-LibXML-Proxy

 view release on metacpan or  search on metacpan

lib/XML/LibXML/Proxy.pm  view on Meta::CPAN

	our $VERSION = 'v0.1.1';
	our $ua = undef;
}

=head1 CLASS METHODS

=over

=item C<B<set>( I<$url> )>

Activate use of a proxy, set to I<C<$url>>.

=cut

sub set {
	my ($class, $proxy) = @_;

	my $ua = new LWP::UserAgent;
	$ua->proxy(['http', 'https'] => $proxy);
	$ua->timeout(30);
	$XML::LibXML::Proxy::ua = $ua;
	XML::LibXML::externalEntityLoader(\&_loader);
}

sub _loader {
	my ($uri) = @_;
	my $res = $XML::LibXML::Proxy::ua->get($uri);
	return $res->is_success ? $res->decoded_content : '';
}

=back

=head1 KNOWN BUGS

There was absolutely no way to override Libxml's built-in "nanohttp" client
for HTTP URIs using any mechanism described in L<XML::LibXML::InputCallback>,
so I had to resort to using C<XML::LibXML::externalEntityLoader()> which
overrides 100% of external entity loading.

This means that B<C<file:///> and other schemes ARE NO LONGER SUPPORTED> when
using this proxy.

=head1 EXAMPLES

For a quick caching forward proxy, I'm using Nginx with the following
configuration:

	server {
		listen 127.0.0.1:8080;
		location / {
			proxy_buffering on;
			resolver 8.8.8.8 8.8.4.4;
			expires max;
			proxy_cache forward-zone;
			proxy_cache_valid 200 302 301 1w;
			proxy_cache_valid any 1m;
			proxy_cache_key "$scheme:$proxy_host:$request_uri";
			proxy_pass $scheme://$host$request_uri;
			proxy_set_header Host $http_host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_ignore_headers "Set-Cookie";
		}
	}

=head1 AUTHOR

Stéphane Lavergne L<https://github.com/vphantom>

=head1 ACKNOWLEDGEMENTS

Graph X Design Inc. L<https://www.gxd.ca/> sponsored this project.

=head1 COPYRIGHT & LICENSE

Copyright (c) 2017-2018 Stéphane Lavergne L<https://github.com/vphantom>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=cut

1;



( run in 0.568 second using v1.01-cache-2.11-cpan-39bf76dae61 )