<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># LWPExternEnt.pl
#
# Copyright (c) 2000 Clark Cooper
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.

package XML::Parser;

use URI;
use URI::file;
use LWP::UserAgent;

##
## Note that this external entity handler reads the entire entity into
## memory, so it will choke on huge ones. It would be really nice if
## LWP::UserAgent optionally returned us an IO::Handle.
##

sub lwp_ext_ent_handler {
  my ($xp, $base, $sys) = @_;  # We don't use public id

  my $uri;

  if (defined $base) {
    # Base may have been set by parsefile, which is agnostic about
    # whether its a file or URI.
    my $base_uri = new URI($base);
    unless (defined $base_uri-&gt;scheme) {
      $base_uri = URI-&gt;new_abs($base_uri, URI::file-&gt;cwd);
    }

    $uri = URI-&gt;new_abs($sys, $base_uri);
  }
  else {
    $uri = new URI($sys);
    unless (defined $uri-&gt;scheme) {
      $uri = URI-&gt;new_abs($uri, URI::file-&gt;cwd);
    }
  }
  
  my $ua = $xp-&gt;{_lwpagent};
  unless (defined $ua) {
    $ua = $xp-&gt;{_lwpagent} = new LWP::UserAgent();
    $ua-&gt;env_proxy();
  }

  my $req = new HTTP::Request('GET', $uri);

  my $res = $ua-&gt;request($req);
  if ($res-&gt;is_error) {
    $xp-&gt;{ErrorMessage} .= "\n" . $res-&gt;status_line . " $uri";
    return undef;
  }
  
  $xp-&gt;{_BaseStack} ||= [];
  push(@{$xp-&gt;{_BaseStack}}, $base);

  $xp-&gt;base($uri);
  
  return $res-&gt;content;
}  # End lwp_ext_ent_handler

sub lwp_ext_ent_cleanup {
  my ($xp) = @_;

  $xp-&gt;base(pop(@{$xp-&gt;{_BaseStack}}));
}  # End lwp_ext_ent_cleanup

1;
</pre></body></html>