my $boundary_index;
for (my @tmp = @v; @tmp;) {
my($k, $v) = splice(@tmp, 0, 2);
if ($k eq "boundary") {
$boundary = $v;
$boundary_index = @v - @tmp - 1;
last;
}
}
my @parts = map $_->as_string($CRLF), @{$self->{_parts}};
my $bno = 0;
$boundary = _boundary() unless defined $boundary;
CHECK_BOUNDARY:
{
for (@parts) {
if (index($_, $boundary) >= 0) {
# must have a better boundary
$boundary = _boundary(++$bno);
redo CHECK_BOUNDARY;
}
}
}
if ($boundary_index) {
$v[$boundary_index] = $boundary;
}
else {
push(@v, boundary => $boundary);
}
$ct = HTTP::Headers::Util::join_header_words(@v);
$self->header("Content-Type", $ct);
_set_content($self, "--$boundary$CRLF" .
join("$CRLF--$boundary$CRLF", @parts) .
"$CRLF--$boundary--$CRLF",
1);
}
sub _boundary
{
my $size = shift || return "xYzZY";
require MIME::Base64;
my $b = MIME::Base64::encode(join("", map chr(rand(256)), 1..$size*3), "");
$b =~ s/[\W]/X/g; # ensure alnum only
$b;
}
1;
__END__
=head1 NAME
HTTP::Message - HTTP style message (base class)
=head1 SYNOPSIS
use base 'HTTP::Message';
=head1 DESCRIPTION
An C<HTTP::Message> object contains some headers and a content body.
The following methods are available:
=over 4
=item $mess = HTTP::Message->new
=item $mess = HTTP::Message->new( $headers )
=item $mess = HTTP::Message->new( $headers, $content )
This constructs a new message object. Normally you would want
construct C<HTTP::Request> or C<HTTP::Response> objects instead.
The optional $header argument should be a reference to an
C<HTTP::Headers> object or a plain array reference of key/value pairs.
If an C<HTTP::Headers> object is provided then a copy of it will be
embedded into the constructed message, i.e. it will not be owned and
can be modified afterwards without affecting the message.
The optional $content argument should be a string of bytes.
=item $mess = HTTP::Message->parse( $str )
This constructs a new message object by parsing the given string.
=item $mess->headers
Returns the embedded C<HTTP::Headers> object.
=item $mess->headers_as_string
=item $mess->headers_as_string( $eol )
=7= |