SPVM-Mojolicious

 view release on metacpan or  search on metacpan

lib/SPVM/Mojo/UserAgent/Proxy.spvm  view on Meta::CPAN

# Copyright (c) 2025 Yuki Kimoto
# MIT License

class Mojo::UserAgent::Proxy {
  version_from Mojolicious;
  
  use Mojo::URL;
  use Mojo::Transaction::HTTP;
  
  use Sys;
  use Re;
  
  # Fields
  has http : rw string;
  
  has https : rw string;
  
  has not : rw string[];
  
  # Class Methods
  static method new : Mojo::UserAgent::Proxy () {
    
    my $self = new Mojo::UserAgent::Proxy;
    
    return $self;
  }
  
  # Instance Methods
  method detect : void () {
    
    if (length (my $_ = Sys->env("HTTP_PROXY"))) {
      $self->{http} = $_;
    }
    elsif (length (my $_ = Sys->env("http_proxy"))) {
      $self->{http} = $_;
    }
    
    if (length (my $_ = Sys->env("HTTPS_PROXY"))) {
      $self->{https} = $_;
    }
    elsif (length (my $_ = Sys->env("https_proxy"))) {
      $self->{https} = $_;
    }
    
    my $no_proxy = (string)undef;
    if (length (my $_ = Sys->env("NO_PROXY"))) {
      $no_proxy = $_;
    }
    elsif (length (my $_ = Sys->env("no_proxy"))) {
      $no_proxy = $_;
    }
    if ($no_proxy) {
      $self->{not} = Fn->split(",", $no_proxy);
    }
    
  }
  
  method is_needed : int ($domain : string)  {
    
    unless ($domain) {
      die "The domain \$domain must be defined";
    }
    
    my $not_needed = 0;
    for my $not_domain (@{$self->not // new string[0]}) {
      if (Re->m($domain, "\Q$not_domain\E$")) {
        $not_needed = 1;
        last;
      }
    }
    
    return !$not_needed;
  }
  
  method prepare : void ($tx : Mojo::Transaction::HTTP) {
    
    my $env_mojo_proxy = length(my $_ = Sys->env("SPVM_MOJO_PROXY")) ? (int)$_ : 0;
    
    if ($env_mojo_proxy) {
      $self->detect;
    }
    
    my $req = $tx->req;
    my $url = $req->url;
    unless ($self->is_needed($url->host)) {
      return;
    }
    # HTTP proxy
    my $proto = $url->protocol;
    my $http  = $self->http;
    if ($http && $proto eq "http") {



( run in 1.485 second using v1.01-cache-2.11-cpan-71847e10f99 )