David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 1 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 9 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 11 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 18 | # |
| 19 | |
| 20 | require 5.6.0; |
| 21 | use strict; |
| 22 | use warnings; |
| 23 | |
| 24 | use Thrift; |
| 25 | use Thrift::Transport; |
| 26 | |
| 27 | use HTTP::Request; |
| 28 | use LWP::UserAgent; |
| 29 | use IO::String; |
| 30 | |
| 31 | package Thrift::HttpClient; |
| 32 | |
| 33 | use base('Thrift::Transport'); |
| 34 | |
| 35 | sub new |
| 36 | { |
| 37 | my $classname = shift; |
| 38 | my $url = shift || 'http://localhost:9090'; |
| 39 | my $debugHandler = shift; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 40 | |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 41 | my $out = IO::String->new; |
| 42 | binmode($out); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 43 | |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 44 | my $self = { |
| 45 | url => $url, |
| 46 | out => $out, |
| 47 | debugHandler => $debugHandler, |
| 48 | debug => 0, |
| 49 | sendTimeout => 100, |
| 50 | recvTimeout => 750, |
| 51 | handle => undef, |
| 52 | }; |
| 53 | |
| 54 | return bless($self,$classname); |
| 55 | } |
| 56 | |
| 57 | sub setSendTimeout |
| 58 | { |
| 59 | my $self = shift; |
| 60 | my $timeout = shift; |
| 61 | |
| 62 | $self->{sendTimeout} = $timeout; |
| 63 | } |
| 64 | |
| 65 | sub setRecvTimeout |
| 66 | { |
| 67 | my $self = shift; |
| 68 | my $timeout = shift; |
| 69 | |
| 70 | $self->{recvTimeout} = $timeout; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | # |
| 75 | #Sets debugging output on or off |
| 76 | # |
| 77 | # @param bool $debug |
| 78 | # |
| 79 | sub setDebug |
| 80 | { |
| 81 | my $self = shift; |
| 82 | my $debug = shift; |
| 83 | |
| 84 | $self->{debug} = $debug; |
| 85 | } |
| 86 | |
| 87 | # |
| 88 | # Tests whether this is open |
| 89 | # |
| 90 | # @return bool true if the socket is open |
| 91 | # |
| 92 | sub isOpen |
| 93 | { |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | sub open {} |
| 98 | |
| 99 | # |
| 100 | # Cleans up the buffer. |
| 101 | # |
| 102 | sub close |
| 103 | { |
| 104 | my $self = shift; |
| 105 | if (defined($self->{io})) { |
| 106 | close($self->{io}); |
| 107 | $self->{io} = undef; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | # |
| 112 | # Guarantees that the full amount of data is read. |
| 113 | # |
| 114 | # @return string The data, of exact length |
| 115 | # @throws TTransportException if cannot read data |
| 116 | # |
| 117 | sub readAll |
| 118 | { |
| 119 | my $self = shift; |
| 120 | my $len = shift; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 121 | |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 122 | my $buf = $self->read($len); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 123 | |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 124 | if (!defined($buf)) { |
| 125 | die new Thrift::TException('TSocket: Could not read '.$len.' bytes from input buffer'); |
| 126 | } |
| 127 | return $buf; |
| 128 | } |
| 129 | |
| 130 | # |
| 131 | # Read and return string |
| 132 | # |
| 133 | sub read |
| 134 | { |
| 135 | my $self = shift; |
| 136 | my $len = shift; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 137 | |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 138 | my $buf; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 139 | |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 140 | my $in = $self->{in}; |
| 141 | |
| 142 | if (!defined($in)) { |
| 143 | die new Thrift::TException("Response buffer is empty, no request."); |
| 144 | } |
| 145 | eval { |
| 146 | my $ret = sysread($in, $buf, $len); |
| 147 | if (! defined($ret)) { |
| 148 | die new Thrift::TException("No more data available."); |
| 149 | } |
| 150 | }; if($@){ |
| 151 | die new Thrift::TException($@); |
| 152 | } |
| 153 | |
| 154 | return $buf; |
| 155 | } |
| 156 | |
| 157 | # |
| 158 | # Write string |
| 159 | # |
| 160 | sub write |
| 161 | { |
| 162 | my $self = shift; |
| 163 | my $buf = shift; |
| 164 | $self->{out}->print($buf); |
| 165 | } |
| 166 | |
| 167 | # |
| 168 | # Flush output (do the actual HTTP/HTTPS request) |
| 169 | # |
| 170 | sub flush |
| 171 | { |
| 172 | my $self = shift; |
| 173 | |
David Reiss | 7502e0b | 2008-03-27 19:45:24 +0000 | [diff] [blame] | 174 | my $ua = LWP::UserAgent->new('timeout' => ($self->{sendTimeout} / 1000), |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 175 | 'agent' => 'Perl/THttpClient' |
| 176 | ); |
| 177 | $ua->default_header('Accept' => 'application/x-thrift'); |
| 178 | $ua->default_header('Content-Type' => 'application/x-thrift'); |
| 179 | $ua->cookie_jar({}); # hash to remember cookies between redirects |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 180 | |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 181 | my $out = $self->{out}; |
| 182 | $out->setpos(0); # rewind |
| 183 | my $buf = join('', <$out>); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 184 | |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 185 | my $request = new HTTP::Request(POST => $self->{url}, undef, $buf); |
| 186 | my $response = $ua->request($request); |
| 187 | my $content_ref = $response->content_ref; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 188 | |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 189 | my $in = IO::String->new($content_ref); |
| 190 | binmode($in); |
| 191 | $self->{in} = $in; |
| 192 | $in->setpos(0); # rewind |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 193 | |
David Reiss | c0c88ee | 2007-11-08 01:05:46 +0000 | [diff] [blame] | 194 | # reset write buffer |
| 195 | $out = IO::String->new; |
| 196 | binmode($out); |
| 197 | $self->{out} = $out; |
| 198 | } |
| 199 | |
| 200 | 1; |