2012/08/16

SOAP via WSDL

#####
/home/michael/localhost/cgi-bin/server.cgi:

#!/usr/bin/env perl

use SOAP::Transport::HTTP;

SOAP::Transport::HTTP::CGI
    -> dispatch_to( '/home/michael/localhost/lib', 'Hi' )
    -> handle;

#####
/home/michael/localhost/lib/Hi.pm:

package Hi;

use base 'SOAP::Server::Parameters';

my $AUTH = {
    'user' => 'admin',
    'pass' => 'temp123',
};

sub hi {
    my ($self, $name) = @_;

    unless ( $ENV{'HTTPS'} ) {
        return 'https only, please';
    }

    unless ( $self->check_auth( pop ) ) {
        return 'Hello, unauthenticated';
    }

    unless ( $name ) {
        $name = 'world';
    }

    return "Hello, $name";
}

sub check_auth {
    my ($self, $envelope) = @_;

    my $header = $envelope->{'_content'}[-3]{'Header'};

    my $authenticated = 0;

    if (
        $header->{'Username'}
        &&
        $header->{'Password'}
        &&
        $header->{'Username'} eq $AUTH->{'user'}
        &&
        $header->{'Password'} eq $AUTH->{'pass'}
    ) {
        $authenticated = 1;
    }

    return $authenticated;
}

#sub debug {
#   use Data::Dumper;
#   my $message = shift;
#   print STDERR Dumper( $message );
#}

1;

#####
/home/michael/localhost/bin/client.pl:

#!/usr/bin/env perl

use SOAP::Lite
    on_fault => sub {
        my ($soap, $res) = @_;
        eval { die ref $res ? $res->faultstring : $soap->transport->status };
        return ref $res ? $res : SOAP::SOM->new();
    };

use Data::Dumper;

my $user = 'admin';
my $pass = 'temp123';

my $header = SOAP::Header
    -> name('Security')
    -> value(
        SOAP::Header
        -> name('UsernameToken')
        -> value(
            SOAP::Header->name('Username' => $user),
            SOAP::Header->name('Password' => $pass),
        ),
    );

my $soap = SOAP::Lite
    -> service('http://192.168.4.46/wsdl');

my $greeting = $soap->hi('York', $header);
print "$greeting\n";

#####
/home/michael/localhost/www/wsdl:

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="Hi"
   targetNamespace="http://192.168.4.46/wsdl"
   xmlns="http://schemas.xmlsoap.org/wsdl/"
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:tns="http://192.168.4.46/wsdl"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">

   <message name="HiRequest">
      <part name="name" type="xsd:string"/>
   </message>
   <message name="HiResponse">
      <part name="greeting" type="xsd:string"/>
   </message>

   <portType name="HiPortType">
      <operation name="hi">
         <input message="tns:HiRequest"/>
         <output message="tns:HiResponse"/>
      </operation>
   </portType>
  
   <binding name="HiBinding" type="tns:HiPortType">
      <soap:binding style="rpc"
         transport="http://schemas.xmlsoap.org/soap/http"/>
      <operation name="hi">
         <soap:operation soapAction="urn:Hi#hi"/>
         <input>
            <soap:body
               encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
               namespace="urn:Hi"
               use="encoded"/>
         </input>
         <output>
            <soap:body
               encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
               namespace="urn:Hi"
               use="encoded"/>
         </output>
      </operation>
   </binding>

   <service name="HiService">
      <documentation>WSDL File for HiService</documentation>
      <port binding="tns:HiBinding" name="HiPort">
         <soap:address
            location="https://192.168.4.46/cgi-bin/server.cgi"/>
      </port>
   </service>
</definitions>

2012/08/13

SOAP

cgi-bin/server.cgi:

#!/usr/bin/env perl

use SOAP::Transport::HTTP;

SOAP::Transport::HTTP::CGI
    ->dispatch_to('/home/michael/localhost/lib', 'Demo')
    ->handle;

lib/Demo.pm:

package Demo;

sub hi {
    return 'Hello, world';
}

sub bye {
    return 'Goodbye, cruel world';
}

1;

somewhere/client.pl:

#!/usr/bin/env perl

use SOAP::Lite;

my $hi = SOAP::Lite
    ->uri('http://localhost/Demo')
    ->proxy('http://localhost/cgi-bin/server.cgi')
    ->hi()
    ->result();

print "hi: [$hi]\n";

my $bye = SOAP::Lite
    ->uri('http://localhost/Demo')
    ->proxy('http://localhost/cgi-bin/server.cgi')
    ->bye()
    ->result();

print "bye: [$bye]\n";


2012/08/09

sub inside sub

bar() is only visible inside foo() and it does not leak
(as opposed to: my $bar = sub {}; $bar->(); but that is not tested -- see "http://www.perlmonks.org/index.pl?node=833941" for explanation)

sub foo {
    print "in foo\n";
    local *bar = sub {
        print "in bar\n";
    };
    bar();
}

foo();

2012/08/03

vim syntax

:set syn=perl
:set syn=cpp
:set syn=javascript

full list:
/usr/share/vim/vim72/syntax