| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Connector::RequestHandle; | ||
| 4 | |||
| 5 | use base qw(IO::Handle); | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Servlet::Util::Exception (); | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $self = shift; | ||
| 13 | my $request = shift; | ||
| 14 | |||
| 15 | $self = $self->SUPER::new(@_); | ||
| 16 | |||
| 17 | ${*self}->{wombat_connector_requesthandle_closed} = undef; | ||
| 18 | ${*self}->{wombat_connector_requesthandle_count} = 0; | ||
| 19 | ${*self}->{wombat_connector_requesthandle_length} = | ||
| 20 | $request->getContentLength(); | ||
| 21 | ${*self}->{wombat_connector_requesthandle_handle} = $request->getHandle(); | ||
| 22 | ${*self}->{wombat_connector_requesthandle_request} = $request; | ||
| 23 | |||
| 24 | return $self; | ||
| 25 | } | ||
| 26 | |||
| 27 | # overridden IO::Handle methods | ||
| 28 | |||
| 29 | sub close { | ||
| 30 | my $self = shift; | ||
| 31 | |||
| 32 | if (${*self}->{wombat_connector_requesthandle_closed}) { | ||
| 33 | my $msg = "close attempted on closed input handle"; | ||
| 34 | Servlet::Util::IOException->throw($msg); | ||
| 35 | } | ||
| 36 | |||
| 37 | ${*self}->{wombat_connector_requesthandle_closed} = 1; | ||
| 38 | |||
| 39 | return 1; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub read { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | if (${*self}->{wombat_connector_requesthandle_closed}) { | ||
| 46 | my $msg = "read attempted on closed input handle"; | ||
| 47 | Servlet::Util::IOException->throw($msg); | ||
| 48 | } | ||
| 49 | |||
| 50 | # return EOF if we've read the entire Content-Length bytes | ||
| 51 | if (${*self}->{wombat_connector_requesthandle_length} && | ||
| 52 | ${*self}->{wombat_connector_requesthandle_count} >= | ||
| 53 | ${*self}->{wombat_connector_requesthandle_length}) { | ||
| 54 | return undef; | ||
| 55 | } | ||
| 56 | |||
| 57 | my $read; | ||
| 58 | eval { | ||
| 59 | $read = ${*self}->{wombat_connector_requesthandle_handle}->read(@_); | ||
| 60 | ${*self}->{wombat_connector_requesthandle_count} += $read; | ||
| 61 | }; | ||
| 62 | if ($@) { | ||
| 63 | Servlet::Util::IOException->throw($@); | ||
| 64 | } | ||
| 65 | |||
| 66 | return $read; | ||
| 67 | } | ||
| 68 | |||
| 69 | # package methods | ||
| 70 | |||
| 71 | sub closed { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | return ${*self}->{wombat_connector_requesthandle_closed}; | ||
| 75 | } | ||
| 76 | |||
| 77 | 1; | ||
| 78 | __END__ |