Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
#
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
9
#
10
#   http://www.apache.org/licenses/LICENSE-2.0
11
#
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.
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;
40
 
41
    my $out = IO::String->new;
42
    binmode($out);
43
 
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;
121
 
122
    my $buf = $self->read($len);
123
 
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;
137
 
138
    my $buf;
139
 
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
 
174
    my $ua = LWP::UserAgent->new('timeout' => ($self->{sendTimeout} / 1000),
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
180
 
181
    my $out = $self->{out};
182
    $out->setpos(0); # rewind
183
    my $buf = join('', <$out>);
184
 
185
    my $request = new HTTP::Request(POST => $self->{url}, undef, $buf);
186
    my $response = $ua->request($request);
187
    my $content_ref = $response->content_ref;
188
 
189
    my $in = IO::String->new($content_ref);
190
    binmode($in);
191
    $self->{in} = $in;
192
    $in->setpos(0); # rewind
193
 
194
    # reset write buffer
195
    $out = IO::String->new;
196
    binmode($out);
197
    $self->{out} = $out;
198
}
199
 
200
1;