| 1 | package Net::Amazon::MechanicalTurk::Transport::RESTTransport; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Net::Amazon::MechanicalTurk; | ||
| 5 | use Net::Amazon::MechanicalTurk::BaseObject; | ||
| 6 | use Net::Amazon::MechanicalTurk::Transport; | ||
| 7 | use Net::Amazon::MechanicalTurk::DataStructure; | ||
| 8 | use LWP 6; | ||
| 9 | use LWP::UserAgent 6; | ||
| 10 | use LWP::Protocol::https 6; | ||
| 11 | use HTTP::Request::Common; | ||
| 12 | use Mozilla::CA; | ||
| 13 | use Net::SSLeay 1.33; | ||
| 14 | |||
| 15 | our $VERSION = '1.00'; | ||
| 16 | |||
| 17 | our @ISA = qw{ Net::Amazon::MechanicalTurk::Transport }; | ||
| 18 | |||
| 19 | Net::Amazon::MechanicalTurk::Transport::RESTTransport->attributes(qw{ | ||
| 20 | userAgent | ||
| 21 | }); | ||
| 22 | |||
| 23 | sub init { | ||
| 24 | my $self = shift; | ||
| 25 | $self->setAttributes(@_); | ||
| 26 | my $softwareName = "MTurkPerlSDK/" . $Net::Amazon::MechanicalTurk::VERSION; | ||
| 27 | if (!defined($self->userAgent)) { | ||
| 28 | $self->userAgent(LWP::UserAgent->new()); | ||
| 29 | $self->userAgent->agent( | ||
| 30 | $softwareName . | ||
| 31 | " " . | ||
| 32 | $self->userAgent->_agent | ||
| 33 | ); | ||
| 34 | } | ||
| 35 | $self->userAgent->default_headers->push_header( | ||
| 36 | 'X-Amazon-Software' => $softwareName | ||
| 37 | ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub call { | ||
| 41 | my ($self, $client, $operation, $params) = @_; | ||
| 42 | |||
| 43 | my $httpParams = Net::Amazon::MechanicalTurk::DataStructure->toProperties($params); | ||
| 44 | |||
| 45 | if ($self->debug) { | ||
| 46 | $self->debugMessage("Calling url " . $client->serviceUrl . " with parameters:"); | ||
| 47 | foreach my $key (sort keys %$httpParams) { | ||
| 48 | $self->debugMessage(" [$key] = [$httpParams->{$key}]"); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | my $hostname = 'invalid'; | ||
| 53 | if($client->serviceUrl =~ /([^:]*:\/\/)?([^\/]+\.[^\/]+)/g) { | ||
| 54 | $hostname = $2; | ||
| 55 | } | ||
| 56 | if($hostname eq 'invalid') { | ||
| 57 | die "Invalid service url" | ||
| 58 | } | ||
| 59 | |||
| 60 | my $request = POST $client->serviceUrl, $httpParams ; | ||
| 61 | $request->header("If-SSL-Cert-Subject" => "/CN=$hostname"); | ||
| 62 | my $response = $self->userAgent->request($request); | ||
| 63 | |||
| 64 | if ($self->debug) { | ||
| 65 | $self->debugMessage("HTTP Response " . $response->status_line); | ||
| 66 | $self->debugMessage($response->headers->as_string); | ||
| 67 | $self->debugMessage("HTTP Content:"); | ||
| 68 | $self->debugMessage($response->content); | ||
| 69 | } | ||
| 70 | |||
| 71 | if (!$response->is_success) { | ||
| 72 | # ServiceUnavailable errors have an XML response and come back as a 503. | ||
| 73 | if ($response->code eq "503" and $response->content =~ /<\s*Errors\s*>/s) { | ||
| 74 | return $response->content; | ||
| 75 | } | ||
| 76 | die "HTTP Error calling $operation - " . $response->status_line; | ||
| 77 | } | ||
| 78 | |||
| 79 | return $response->content; | ||
| 80 | } | ||
| 81 | |||
| 82 | return 1; |