/usr/share/perl5/DIME/Payload.pm is in libdime-tools-perl 0.03-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | # Copyright (C) 2004 Domingo Alcázar Larrea
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the version 2 of the GNU General
# Public License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307
package DIME::Payload;
use 5.008;
use strict;
use warnings;
use Data::UUID;
use DIME::Record;
use IO::Scalar;
use IO::File;
require Exporter;
our @ISA = qw(Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use DIME ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
);
our $VERSION = '0.01';
sub new
{
my $class = shift;
my @records;
my $this = {
_RECORDS => \@records,
_CHUNK_SIZE => 0,
_STREAM => undef,
_BUFFER_SIZE => 1024,
_MB => 0,
_ME => 0,
_TYPE => undef,
_TNF => 3,
_ID => undef,
_FIRST_RECORD => 1,
};
my $self = bless $this, $class;
$self->generate_uuid();
return $self;
}
sub generate_uuid
{
my $self = shift;
# Generate a new UUID to identify the record
my $duuid = new Data::UUID;
my $uuid = 'uuid:'.$duuid->create_str();
$self->id($uuid);
}
sub type
{
my $self = shift;
my $param = shift;
if(defined($param))
{
$self->{_TYPE} = $param;
}
else
{
return $self->{_TYPE};
}
}
sub mb
{
my $self = shift;
my $param = shift;
if(defined($param))
{
$self->{_MB} = $param;
}
else
{
return $self->{_MB};
}
}
sub me
{
my $self = shift;
my $param = shift;
if(defined($param))
{
$self->{_ME} = $param;
}
else
{
return $self->{_ME};
}
}
sub tnf
{
my $self = shift;
my $param = shift;
if(defined($param))
{
$self->{_TNF} = $param;
}
else
{
return $self->{_TNF};
}
}
sub id
{
my $self = shift;
my $param = shift;
if(defined($param))
{
$self->{_ID} = $param;
}
else
{
return $self->{_ID};
}
}
# Add a Record to a Payload
sub add_record
{
my $self = shift;
my $record = shift;
push(@{$self->{_RECORDS}},$record);
}
sub attach
{
my $self = shift;
my %params = @_;
my $data;
$self->{_CHUNK_SIZE} = $params{Chunked} if(defined($params{Chunked}));
if(defined($params{Path}))
{
my $file = new IO::File($params{Path},"r");
if($file)
{
# The user wants to load all the file in memory now...
if(!defined($params{Dynamic}))
{
# Load the attachment from a file
my $buf;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = $file->stat();
$file->read($buf,$size);
$file->close();
$data = \$buf;
}
else
{
# Assign the opened stream to the member variable
$self->{_STREAM} = $file;
}
}
}
if(defined($params{Data}))
{
# Get the attachment directly from memory
$data = $params{Data};
}
$self->set_mime_type($params{MIMEType}) if (defined($params{MIMEType}));
$self->set_uri_type($params{URIType}) if (defined($params{URIType}));
# If the data is data already loaded in memory...
if(defined($data))
{
if(defined($params{Chunked}))
{
my $data_stream = new IO::Scalar $data;
my $record;
for(my $i=0;$record = $self->create_chunk_record($data_stream);$i++)
{
$self->add_record($record);
}
$data_stream->close();
}
else
{
# The attachment goes in one record
my $record = new DIME::Record($self);
my $data_io = new IO::Scalar $data;
$record->data($data_io);
$self->add_record($record);
}
}
}
sub print
{
my $self = shift;
my $out = shift;
if(defined($self->{_STREAM}))
{
if($self->{_CHUNK_SIZE})
{
my $i=0;
while(my $record = $self->create_chunk_record($self->{_STREAM}))
{
$record->mb(1) if($self->mb() and $i==0);
$record->me(1) if($self->me() and $self->{_STREAM}->eof());
$record->print($out);
$i++;
}
}
else
{
my $record = new DIME::Record($self);
$record->data($self->{_STREAM});
$record->mb(1) if($self->mb());
$record->me(1) if($self->me());
$record->print($out);
}
}
else
{
my @records = @{$self->{_RECORDS}};
my $howmany = @records;
for(my $i=0;$i<$howmany;$i++)
{
$records[$i]->mb(1) if($self->mb() and $i==0);
$records[$i]->me(1) if($self->me() and $i==$howmany-1);
$records[$i]->print($out);
}
}
}
sub print_content
{
my $self = shift;
my $io = shift;
my $buf;
for my $r (@{$self->{_RECORDS}})
{
$r->print_content($io);
}
}
sub print_content_data
{
my $self = shift;
my $data;
my $io = new IO::Scalar \$data;
$self->print_content($io);
$io->close();
return \$data;
}
sub print_data
{
my $self = shift;
my $data;
my $io = new IO::Scalar \$data;
$self->print($io);
$io->close();
return \$data;
}
sub print_chunk_data
{
my $self = shift;
my $data;
my $io = new IO::Scalar \$data;
$self->print_chunk($io);
$io->close();
return \$data;
}
sub print_chunk
{
my $self = shift;
my $out = shift;
if(defined($self->{_STREAM}) and $self->{_CHUNK_SIZE})
{
my $record;
if($record = $self->create_chunk_record($self->{_STREAM}))
{
$record->print($out);
}
}
}
# This method takes data from a IO::Handle
# and returns a DIME chunked record with a max size
# of _CHUNK_SIZE bytes
sub create_chunk_record
{
my $self = shift;
my $in_stream = shift;
my $buf;
my $bytes_read;
my $record;
$bytes_read = $in_stream->read($buf,$self->{_CHUNK_SIZE});
if($bytes_read)
{
$record = new DIME::Record($self);
my $io_data = new IO::Scalar \$buf;
$record->data($io_data);
if($self->{_FIRST_RECORD})
{
$self->{_FIRST_RECORD} = 0;
$record->id($self->id());
$record->chunked(1);
}
elsif($in_stream->eof())
{
$record->id('');
$record->set_unchanged_type();
$record->chunked(0);
}
else
{
$record->id('');
$record->set_unchanged_type();
$record->chunked(1);
}
}
return $record;
}
sub set_mime_type
{
my $self = shift;
my $type = shift;
$self->type($type);
$self->{_TNF} = 0x01;
}
sub set_uri_type
{
my $self = shift;
my $type = shift;
$self->type($type);
$self->{_TNF} = 0x02;
}
1;
__END__
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
DIME::Payload - implementation of a payload of a DIME message
=head1 SYNOPSIS
# Create a standard DIME message from an existing file
# and a string
use DIME::Payload;
$payload1 = new DIME::Payload;
$payload1->attach(Path => 'existingfile.jpg',
MIMEType => 'image/jpeg',
Dynamic => 1);
$payload2 = new DIME::Payload;
my $data = 'Hello World!!!';
$payload2->attach(Data => \$data,
MIMEType => 'text/plain');
my $message = new DIME::Message;
$message->add_payload($payload1);
$message->add_payload($payload2);
=head1 DESCRIPTION
DIME::Payload represents the content of DIME message. A message is composed of one or many Payload objects.
There are two types of DIME payloads: chunked and not chunked. A DIME message that isn't chunked has only one record with all the Payload content. A chunked message is splited in several records, allowing to sender and receiver process the content without know the total size of this.
=head1 CHUNKED AND DYNAMIC CONTENT
To create a chunked message you have to specify the Chunked key:
# This create a dynamic payload with records of 16384 bytes
my $payload = new DIME::Payload;
$payload->attach(Path => 'bigfile.avi',
Chunked => 16384,
Dynamic => 1);
# You can encode all the payload at once:
my $dime_encoded_message = ${$payload->print_data()};
# Or, if you prefer, you can generate each chunk
my $ret;
do
{
$chunk = ${$payload->print_chunk_data()};
} while ($chunk ne '');
The Dynamic key is used to avoid load all the file in memory. What DIME::Payload does is to open the file and, when it need more content, read from the file. If you don't set the Dynamic key, all the data is loaded in memory.
=head1 CONTENT TYPE
To specify the type of content of a Payload, you should use the MIMEType and URIType keys:
# MIME media-type
my $payload = new DIME::Payload;
$payload->attach(Path => 'image.jpg',
MIMEType => 'image/jpeg');
# absolute URI
my $payload = new DIME::Payload;
$payload->attach(Path => 'message.xml',
URIType => 'http://schemas.xmlsoap.org/soap/envelope/');
=head1 PAYLOAD IDENTIFIER
When you create a new Payload, a unique identifier is generated automatically. You can get/set it with the id() method:
my $payload = new DIME::Payload;
print $payload->id();
=head1 AUTHOR
Domingo Alcazar Larrea, E<lt>dalcazar@cpan.orgE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2004 Domingo Alcázar Larrea
This program is free software; you can redistribute it and/or
modify it under the terms of the version 2 of the GNU General
Public License as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307
=cut
|