$eol = "\n" unless defined $eol;
# The calculation of content might update the headers
# so we need to do that first.
my $content = $self->content;
return join("", $self->{'_headers'}->as_string($eol),
$eol,
$content,
(@_ == 1 && length($content) &&
$content !~ /\n\z/) ? "\n" : "",
);
}
sub dump
{
my($self, %opt) = @_;
my $content = $self->content;
my $chopped = 0;
if (!ref($content)) {
my $maxlen = $opt{maxlength};
$maxlen = 512 unless defined($maxlen);
if ($maxlen && length($content) > $maxlen * 1.1 + 3) {
$chopped = length($content) - $maxlen;
$content = substr($content, 0, $maxlen) . "...";
}
$content =~ s/\\/\\\\/g;
$content =~ s/\t/\\t/g;
$content =~ s/\r/\\r/g;
# no need for 3 digits in escape for these
$content =~ s/([\0-\11\13-\037])(?!\d)/sprintf('\\%o',ord($1))/eg;
$content =~ s/([\0-\11\13-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg;
$content =~ s/([^\12\040-\176])/sprintf('\\x{%X}',ord($1))/eg;
# remaining whitespace
$content =~ s/( +)\n/("\\40" x length($1)) . "\n"/eg;
$content =~ s/(\n+)\n/("\\n" x length($1)) . "\n"/eg;
$content =~ s/\n\z/\\n/;
my $no_content = "(no content)";
if ($content eq $no_content) {
# escape our $no_content marker
$content =~ s/^(.)/sprintf('\\x%02X',ord($1))/eg;
}
elsif ($content eq "") {
$content = "(no content)";
}
}
my @dump;
push(@dump, $opt{preheader}) if $opt{preheader};
push(@dump, $self->{_headers}->as_string, $content);
push(@dump, "(+ $chopped more bytes not shown)") if $chopped;
my $dump = join("\n", @dump, "");
$dump =~ s/^/$opt{prefix}/gm if $opt{prefix};
print $dump unless defined wantarray;
return $dump;
}
sub parts {
my $self = shift;
if (defined(wantarray) && (!exists $self->{_parts} || ref($self->{_content}) eq "SCALAR")) {
$self->_parts;
}
my $old = $self->{_parts};
if (@_) {
my @parts = map { ref($_) eq 'ARRAY' ? @$_ : $_ } @_;
my $ct = $self->content_type || "";
if ($ct =~ m,^message/,) {
Carp::croak("Only one part allowed for $ct content")
if @parts > 1;
}
elsif ($ct !~ m,^multipart/,) {
$self->remove_content_headers;
$self->content_type("multipart/mixed");
}
$self->{_parts} = \@parts;
_stale_content($self);
}
return @$old if wantarray;
return $old->[0];
}
sub add_part {
my $self = shift;
if (($self->content_type || "") !~ m,^multipart/,) {
my $p = HTTP::Message->new($self->remove_content_headers,
$self->content(""));
$self->content_type("multipart/mixed");
$self->{_parts} = [];
if ($p->headers->header_field_names || $p->content ne "") {
push(@{$self->{_parts}}, $p);
}
=5= |