blob: d6fc8be34aea813b94ea5bbc1c65dc504c0304a7 [file] [log] [blame]
David Reissc0c88ee2007-11-08 01:05:46 +00001#
David Reissea2cba82009-03-30 21:35:00 +00002# 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 Reissc0c88ee2007-11-08 01:05:46 +00009#
David Reissea2cba82009-03-30 21:35:00 +000010# http://www.apache.org/licenses/LICENSE-2.0
David Reissc0c88ee2007-11-08 01:05:46 +000011#
David Reissea2cba82009-03-30 21:35:00 +000012# 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 Reissc0c88ee2007-11-08 01:05:46 +000018#
19
20require 5.6.0;
21use strict;
22use warnings;
23
24use Thrift;
25use Thrift::Transport;
26
27use HTTP::Request;
28use LWP::UserAgent;
29use IO::String;
30
31package Thrift::HttpClient;
32
33use base('Thrift::Transport');
34
35sub new
36{
37 my $classname = shift;
38 my $url = shift || 'http://localhost:9090';
39 my $debugHandler = shift;
David Reiss0c90f6f2008-02-06 22:18:40 +000040
David Reissc0c88ee2007-11-08 01:05:46 +000041 my $out = IO::String->new;
42 binmode($out);
David Reiss0c90f6f2008-02-06 22:18:40 +000043
David Reissc0c88ee2007-11-08 01:05:46 +000044 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
57sub setSendTimeout
58{
59 my $self = shift;
60 my $timeout = shift;
61
62 $self->{sendTimeout} = $timeout;
63}
64
65sub 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#
79sub 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#
92sub isOpen
93{
94 return 1;
95}
96
97sub open {}
98
99#
100# Cleans up the buffer.
101#
102sub 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#
117sub readAll
118{
119 my $self = shift;
120 my $len = shift;
David Reiss0c90f6f2008-02-06 22:18:40 +0000121
David Reissc0c88ee2007-11-08 01:05:46 +0000122 my $buf = $self->read($len);
David Reiss0c90f6f2008-02-06 22:18:40 +0000123
David Reissc0c88ee2007-11-08 01:05:46 +0000124 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#
133sub read
134{
135 my $self = shift;
136 my $len = shift;
David Reiss0c90f6f2008-02-06 22:18:40 +0000137
David Reissc0c88ee2007-11-08 01:05:46 +0000138 my $buf;
David Reiss0c90f6f2008-02-06 22:18:40 +0000139
David Reissc0c88ee2007-11-08 01:05:46 +0000140 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#
160sub 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#
170sub flush
171{
172 my $self = shift;
173
David Reiss7502e0b2008-03-27 19:45:24 +0000174 my $ua = LWP::UserAgent->new('timeout' => ($self->{sendTimeout} / 1000),
David Reissc0c88ee2007-11-08 01:05:46 +0000175 '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 Reiss0c90f6f2008-02-06 22:18:40 +0000180
David Reissc0c88ee2007-11-08 01:05:46 +0000181 my $out = $self->{out};
182 $out->setpos(0); # rewind
183 my $buf = join('', <$out>);
David Reiss0c90f6f2008-02-06 22:18:40 +0000184
David Reissc0c88ee2007-11-08 01:05:46 +0000185 my $request = new HTTP::Request(POST => $self->{url}, undef, $buf);
186 my $response = $ua->request($request);
187 my $content_ref = $response->content_ref;
David Reiss0c90f6f2008-02-06 22:18:40 +0000188
David Reissc0c88ee2007-11-08 01:05:46 +0000189 my $in = IO::String->new($content_ref);
190 binmode($in);
191 $self->{in} = $in;
192 $in->setpos(0); # rewind
David Reiss0c90f6f2008-02-06 22:18:40 +0000193
David Reissc0c88ee2007-11-08 01:05:46 +0000194 # reset write buffer
195 $out = IO::String->new;
196 binmode($out);
197 $self->{out} = $out;
198}
199
2001;