| 1 | =pod | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | HTTP::Handle - HTTP Class designed for streaming | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use HTTP::Handle; | ||
| 10 | |||
| 11 | my $http = HTTP::Handle->new( uri => "http://www.google.com/" ); | ||
| 12 | $http->connect(); | ||
| 13 | |||
| 14 | my $fd = $http->fd(); | ||
| 15 | |||
| 16 | while (<$fd>) { | ||
| 17 | print "--> $_"; | ||
| 18 | } | ||
| 19 | |||
| 20 | =head1 VERSION | ||
| 21 | |||
| 22 | Version: 0.2 | ||
| 23 | |||
| 24 | $Id: Handle.pm,v 1.6 2004/07/03 08:44:53 psionic Exp $ | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | The C<HTTP::Handle> module allows you to make HTTP requests and handle | ||
| 29 | the data yourself. The general ideas is that you use this module to make | ||
| 30 | a HTTP request and handle non-header data yourself. I needed such a | ||
| 31 | feature for my mp3 player to listen to icecast streams. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | package HTTP::Handle; | ||
| 36 | |||
| 37 | use strict; | ||
| 38 | use IO::Handle; | ||
| 39 | use Socket; | ||
| 40 | use URI; | ||
| 41 | |||
| 42 | my $VERSION = "0.2"; | ||
| 43 | |||
| 44 | =pod | ||
| 45 | |||
| 46 | =over 4 | ||
| 47 | |||
| 48 | =item HTTP::Handle->new() | ||
| 49 | |||
| 50 | Create a new HTTP::Handle object thingy. | ||
| 51 | |||
| 52 | Arguments possible: | ||
| 53 | |||
| 54 | =over 4 | ||
| 55 | |||
| 56 | =item url => "http://www.google.com/" | ||
| 57 | |||
| 58 | Sets the initial URL to connect to. | ||
| 59 | |||
| 60 | =item follow_redirects => [ 0 | 1 ] | ||
| 61 | |||
| 62 | Automatically follow HTTP redirects. This defaults to true (1). Set to 0 to disable this. | ||
| 63 | |||
| 64 | =item http_request => HASHREF | ||
| 65 | |||
| 66 | Any thing put in here will be sent as "key: value" in the http request string. | ||
| 67 | |||
| 68 | =back | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub new { | ||
| 73 | my $class = shift; | ||
| 74 | |||
| 75 | my $self = { | ||
| 76 | data_timeout => 5, | ||
| 77 | follow_redirects => 1, | ||
| 78 | http_request => { | ||
| 79 | "User-Agent" => "HTTP-Handle/$VERSION", | ||
| 80 | }, | ||
| 81 | }; | ||
| 82 | |||
| 83 | my %args = @_; | ||
| 84 | |||
| 85 | while (my ($key, $val) = each(%args)) { | ||
| 86 | $self->{$key} = $val; | ||
| 87 | } | ||
| 88 | |||
| 89 | $self->url($self->{"uri"}) if (defined($self->{"uri"})); | ||
| 90 | bless $self, $class; | ||
| 91 | |||
| 92 | return $self; | ||
| 93 | } | ||
| 94 | |||
| 95 | =pod | ||
| 96 | |||
| 97 | =item $http->connect() | ||
| 98 | |||
| 99 | Connect, send the http request, and process the response headers. | ||
| 100 | |||
| 101 | This function returns -1 on failure, undef otherwise. The reason for failure will be printed to STDERR. | ||
| 102 | |||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub connect($) { | ||
| 107 | my $self = shift; | ||
| 108 | |||
| 109 | my $sock; | ||
| 110 | |||
| 111 | CONNECT: | ||
| 112 | |||
| 113 | socket($sock, PF_INET, SOCK_STREAM, getprotobyname('tcp')) | ||
| 114 | or _fatal("Failed creating socket.") and return -1; | ||
| 115 | |||
| 116 | $self->{"socket"} = $sock; | ||
| 117 | |||
| 118 | _debug("Looking up hostname for " . $self->{"host"}); | ||
| 119 | my $inetnum = inet_aton($self->{"host"}); | ||
| 120 | _debug("Done!\n"); | ||
| 121 | |||
| 122 | if (!defined($inetnum)) { | ||
| 123 | _fatal("Unable to resolve hostname: " . $self->{"host"}); | ||
| 124 | return -1; | ||
| 125 | } | ||
| 126 | |||
| 127 | _debug("Connecting to " . $self->{"host"} . ":" . $self->{"port"}); | ||
| 128 | |||
| 129 | connect($sock, sockaddr_in($self->{"port"}, $inetnum)) | ||
| 130 | or _fatal("Failed connecting to " . $self->{"host"} . ":" . $self->{"port"}) and return -1; | ||
| 131 | _debug("Connected"); | ||
| 132 | |||
| 133 | $sock->autoflush(1); | ||
| 134 | _debug("Sending HTTP Request"); | ||
| 135 | print $sock $self->http_request_string(); | ||
| 136 | _debug($self->http_request_string()); | ||
| 137 | |||
| 138 | _debug("Data Sent"); | ||
| 139 | my $sel = new IO::Select($sock); | ||
| 140 | my $count = 0; | ||
| 141 | my $data; | ||
| 142 | |||
| 143 | while (1) { | ||
| 144 | return -1 if ($count > $self->{"data_timeout"}); | ||
| 145 | if ($sel->can_read(1)) { | ||
| 146 | my $ret = read($sock,$data,1,length($data)); | ||
| 147 | if (!defined($ret)) { | ||
| 148 | _debug("Failed on read: $!"); | ||
| 149 | _debug("Buffer: $data"); | ||
| 150 | exit; | ||
| 151 | } elsif ($ret == 0) { | ||
| 152 | _debug("EOF Hit..."); | ||
| 153 | _debug("Buffer: $data"); | ||
| 154 | last; | ||
| 155 | } | ||
| 156 | while ($data =~ s!(.*)\r\n!!s) { | ||
| 157 | chomp(my $f = $1); | ||
| 158 | _debug("HEADER- '$f'"); | ||
| 159 | unless (defined($self->{"code"})) { | ||
| 160 | $self->{"code"} = $f; | ||
| 161 | next; | ||
| 162 | } | ||
| 163 | goto DONEHEADERS if ($f =~ m/^$/); | ||
| 164 | $f =~ m/(\S+):\s*(.+)\s*/; | ||
| 165 | $self->{"http_response"}->{$1} = $2; | ||
| 166 | } | ||
| 167 | #print STDERR "Waiting on data... $count\n"; | ||
| 168 | } else { | ||
| 169 | $count++; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | DONEHEADERS: | ||
| 173 | |||
| 174 | |||
| 175 | if ($self->{"follow_redirects"} && ($self->{"code"} =~ m/^\S+\s+302/)) { | ||
| 176 | close($sock); | ||
| 177 | $self->url($self->{"http_response"}->{"Location"}); | ||
| 178 | _debug("Redirecting to " . $self->{"http_response"}->{"Location"}); | ||
| 179 | goto CONNECT; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | =pod | ||
| 184 | |||
| 185 | =item $http->fd() | ||
| 186 | |||
| 187 | Get the file descriptor (socket) we're using to connect. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub fd($) { | ||
| 192 | my $self = shift; | ||
| 193 | |||
| 194 | return $self->{"socket"}; | ||
| 195 | } | ||
| 196 | |||
| 197 | =pod | ||
| 198 | |||
| 199 | =item $http->url( [ url_string ]) | ||
| 200 | |||
| 201 | Get or set the URL. If a url string is passed, you will change the url | ||
| 202 | that is requested. If no parameter is passed, a L<URI> object will be | ||
| 203 | returned containing the | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | sub url($;$) { | ||
| 208 | my $self = shift; | ||
| 209 | my $url = shift; | ||
| 210 | |||
| 211 | if (defined($url)) { | ||
| 212 | my $uri = URI->new($url); | ||
| 213 | |||
| 214 | $self->{"host"} = $uri->host(); | ||
| 215 | $self->{"http_path"} = $uri->path() || "/"; | ||
| 216 | $self->{"port"} = $uri->port(); | ||
| 217 | $self->{"uri"} = $uri; | ||
| 218 | |||
| 219 | $self->{"http_request"}->{"Host"} = $self->{"host"}; | ||
| 220 | $self->{"http_action"} |= "GET"; | ||
| 221 | |||
| 222 | } else { | ||
| 223 | return $self->{"url"}; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | =pod | ||
| 228 | |||
| 229 | =item $http->follow_redirects( [ 0 | 1 ] ) | ||
| 230 | |||
| 231 | If a value is passed then you will set whether or not we will | ||
| 232 | automatically follow HTTP 302 Redirects. If no value is passed, then | ||
| 233 | we will return whatever the current option is. | ||
| 234 | |||
| 235 | Defaults to 1 (will follow redirects). | ||
| 236 | |||
| 237 | =cut | ||
| 238 | |||
| 239 | sub follow_redirects($;$) { | ||
| 240 | my $self = shift; | ||
| 241 | my $opt = shift; | ||
| 242 | |||
| 243 | if (defined($opt)) { | ||
| 244 | $self->{"follow_redirects"} = $opt; | ||
| 245 | } else { | ||
| 246 | return $self->{"follow_redirects"}; | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | =pod | ||
| 251 | |||
| 252 | =item $http->http_request_string() | ||
| 253 | |||
| 254 | Returns a string containing the HTTP request and headers, this is used | ||
| 255 | when $http->connect() is called. | ||
| 256 | |||
| 257 | =cut | ||
| 258 | |||
| 259 | sub http_request_string($) { | ||
| 260 | my $self = shift; | ||
| 261 | |||
| 262 | my $ret = sprintf("%s %s HTTP/1.0\r\n", $self->{"http_action"}, $self->{"http_path"}); | ||
| 263 | map { $ret .= sprintf("%s: %s\r\n", $_, $self->{"http_request"}->{$_}) } keys(%{$self->{"http_request"}}); | ||
| 264 | |||
| 265 | $ret .= "\r\n\r\n"; | ||
| 266 | |||
| 267 | return $ret; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub _fatal { | ||
| 271 | print STDERR @_, "\n", "ERR: $!\n"; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub _debug { | ||
| 275 | #print STDERR map { "$_\n" } @_; | ||
| 276 | } | ||
| 277 | |||
| 278 | =pod | ||
| 279 | |||
| 280 | =back | ||
| 281 | |||
| 282 | =head1 AUTHOR | ||
| 283 | |||
| 284 | Jordan Sissel <psionic@csh.rit.edu> | ||
| 285 | |||
| 286 | =cut | ||
| 287 | |||
| 288 | 1; | ||
| 289 | |||
| 290 |