/usr/share/perl5/Courriel/HeaderAttribute.pm is in libcourriel-perl 0.31-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 | package Courriel::HeaderAttribute;
{
$Courriel::HeaderAttribute::VERSION = '0.31';
}
use strict;
use warnings;
use namespace::autoclean;
use Courriel::HeaderAttribute;
use Courriel::Helpers qw( quote_and_escape_attribute_value );
use Courriel::Types qw( Maybe NonEmptyStr Str );
use Encode qw( encode );
use Moose;
use MooseX::StrictConstructor;
with 'Courriel::Role::Streams';
has name => (
is => 'ro',
isa => NonEmptyStr,
required => 1,
);
has value => (
is => 'ro',
isa => Str,
required => 1,
);
has charset => (
is => 'ro',
isa => NonEmptyStr,
default => 'us-ascii',
);
has language => (
is => 'ro',
isa => Maybe [NonEmptyStr],
default => undef,
);
override BUILDARGS => sub {
my $class = shift;
my $p = super();
return $p unless defined $p->{value};
$p->{charset} = 'UTF-8' if $p->{value} =~ /[^\p{ASCII}]/;
return $p;
};
sub _stream_to {
my $self = shift;
my $output = shift;
$output->( $self->_as_string() );
}
{
my $non_attribute_char = qr{
$Courriel::Helpers::TSPECIALS
|
[ \*\%] # space, *, %
|
[^\p{ASCII}] # anything that's not ascii
|
[\x00-\x1f\x7f] # ctrl chars
}x;
sub _as_string {
my $self = shift;
my $value = $self->value();
my $transport_method = '_simple_parameter';
if ( $value =~ /[\x00-\x1f]|\x7f|[^\p{ASCII}]/
|| defined $self->language()
|| $self->charset() ne 'us-ascii' ) {
$value = encode( 'utf-8', $value );
$value
=~ s/($non_attribute_char)/'%' . uc sprintf( '%02x', ord($1) )/eg;
$transport_method = '_encoded_parameter';
}
elsif ( $value =~ /$non_attribute_char/ ) {
$transport_method = '_quoted_parameter';
}
# XXX - hard code 78 as the max line length may not be right. Should
# this account for the length that the parameter name takes up (as
# well as encoding information, etc.)?
my @pieces;
while ( length $value ) {
my $last_percent = rindex( $value, '%', 78 );
my $size
= $last_percent >= 76 ? $last_percent
: length $value > 78 ? 78
: length $value;
push @pieces, substr( $value, 0, $size, q{} );
}
if ( @pieces == 1 ) {
return $self->$transport_method( undef, $pieces[0] );
}
else {
return join q{ },
map { $self->$transport_method( $_, $pieces[$_] ) }
0 .. $#pieces;
}
}
}
sub _simple_parameter {
my $self = shift;
my $order = shift;
my $value = shift;
my $param = $self->name();
$param .= q{*} . $order if defined $order;
$param .= q{=};
$param .= $value;
return $param;
}
sub _quoted_parameter {
my $self = shift;
my $order = shift;
my $value = shift;
my $param = $self->name();
$param .= q{*} . $order if defined $order;
$param .= q{=};
$value =~ s/\"/\\\"/g;
$param .= q{"} . $value . q{"};
return $param;
}
sub _encoded_parameter {
my $self = shift;
my $order = shift;
my $value = shift;
my $param = $self->name();
$param .= q{*} . $order if defined $order;
$param .= q{*=};
# XXX (1) - does it makes sense to just say everything is utf-8? in theory
# someone could pass through binary data in another encoding.
unless ($order) {
$param .= 'UTF-8' . q{'}
. ( $self->language() // q{} ) . q{'};
}
$param .= $value;
return $param;
}
__PACKAGE__->meta()->make_immutable();
1;
# ABSTRACT: A single attribute belonging to a header
__END__
=pod
=head1 NAME
Courriel::HeaderAttribute - A single attribute belonging to a header
=head1 VERSION
version 0.31
=head1 SYNOPSIS
my $ct = $headers->get('Content-Type');
print $ct->get_attribute('charset')->value();
=head1 DESCRIPTION
This class represents a single attribute belonging to a header. An attribute
consists of a name and value, with optional charset and language information.
=head1 API
This class supports the following methods:
=head1 Courriel::HeaderAttribute->new( ... )
This method creates a new object. It accepts the following parameters:
=over 4
=item * name
The name of the attribute. This should be a non-empty string.
=item * value
The value of the attribute. This can be empty.
=item * charset
The charset for the value. If the value contains any non-ASCII data, this will
always be "UTF-8", otherwise the default is "us-ascii".
=item * language
The language for the attribute's value. It should be a valid ISO language code
like "en-us" or "zh". This is optional.
=back
=head2 $attribute->name()
The attribute name as passed to the constructor.
=head2 $attribute->value()
The attribute value as passed to the constructor.
=head2 $attribute->charset()
The attribute's charset.
=head2 $attribute->language()
The attribute's language.
=head2 $attribute->as_string()
This returns the attribute in a form suitable for putting in an email. This
may involve escaping, quoting, splitting up, and otherwise messing with the
value.
If the value needs to be split across continuations, each name/value pair is
returned separate by a space, but not folded across multiple lines.
=head2 $attribute->stream_to( output => $output )
This method will send the stringified attribute to the specified output. The
output can be a subroutine reference, a filehandle, or an object with a
C<print()> method. The output may be sent as a single string, as a list of
strings, or via multiple calls to the output.
=head1 ROLES
This class does the C<Courriel::Role::Streams> role.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 CONTRIBUTOR
Zbigniew Ĺukasiak <zzbbyy@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2013 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut
|