url encoding data; certificate verify failed when posting as non-root

Terrence Brannon <[email protected]> Fri, 9 Jul 2010 17:35:41 -0400
Newsgroups gmane.comp.lang.perl.modules.lwp
Message-ID <[email protected]>
1 - we have a homegrown URL encoding routine. I am certain there must be
something more standard in the LWP distro and would appreciate a pointer to
it:

   foreach my $field ( keys %$data ) {
        next if ( $data->{$field} eq '' );

        $data->{$field} =~ s/([^a-zA-Z0-9_\-.])/uc
sprintf("%%%02x",ord($1))/eg;

        if ( $field =~ /^setFeature/ ) {
            # $field interpolates to setFeature=parameter
            $post_string .= "$field:$data->{$field}&";
        }
        else {
            $post_string .= "$field=$data->{$field}&";
        }
    }
    $post_string =~ s/\&$//;


2 - the _Post() routine below works fine when the post is made via the root
user. But it fails when run as a different user. I would appreciate any
pointers on debugging this problem.

sub _Post {
    my $self = shift;    # Class reference
    my $data = shift;

    # Path to local client cert and key
    $ENV{HTTPS_CERT_FILE} = APID_CERT_PATH . $self->_GetCertificate;
    $ENV{HTTPS_KEY_FILE}  = APID_CERT_PATH . $self->_GetKey;

    # Path to certificate authority file, for comparing the server's
certificate.
    $ENV{HTTPS_CA_FILE} = APID_CERT_PATH . APID_CA_FILE;

    if ( ( !defined($data) ) ) {
        return (undef);
    }

    my $ua = LWP::UserAgent->new();

    my $port = APID_POST_PORT;

    my $req = HTTP::Request->new( POST => "https://" . $self->_GetUrl .
":$port" . $self->_GetPage );

    $req->content( $self->_UrlEncodeData($data) );
    $req->content_type("application/x-www-form-urlencoded");

    my $res = $ua->request($req);

    # Remove the Environment Variables, holding cert files.  Was causing
problems with SOAP, when
    # these happened to still be there, using the wrong cert and SOAP
bombing out after a DIY call.

    delete $ENV{HTTPS_CERT_FILE};
    delete $ENV{HTTPS_KEY_FILE};
    delete $ENV{HTTPS_CA_FILE};

    if ( $res->is_success ) {
        return $res->content;
    }
    else {
        return "ERROR: " . $res->status_line . "\n";
    }

}    # END _Post