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
|
package Mail::AuthenticationResults::FoldableHeader;
# ABSTRACT: Class for modelling a foldable header string
require 5.008;
use strict;
use warnings;
our $VERSION = '2.20210112'; # VERSION
use Carp;
use Mail::AuthenticationResults::Token::String;
use Mail::AuthenticationResults::Token::Space;
use Mail::AuthenticationResults::Token::Separator;
use Mail::AuthenticationResults::Token::Comment;
use Mail::AuthenticationResults::Token::Assignment;
sub new {
my ( $class, $args ) = @_;
my $self = {};
bless $self, $class;
$self->{ 'string' } = [];
return $self;
}
sub eol {
my ( $self ) = @_;
return $self->{ 'eol' } if exists ( $self->{ 'eol' } );
return "\n";
}
sub set_eol {
my ( $self, $eol ) = @_;
$self->{ 'eol' } = $eol;
return $self;
}
sub indent {
my ( $self ) = @_;
return $self->{ 'indent' } if exists ( $self->{ 'indent' } );
return ' ';
}
sub set_indent {
my ( $self, $indent ) = @_;
$self->{ 'indent' } = $indent;
return $self;
}
sub sub_indent {
my ( $self ) = @_;
return $self->{ 'sub_indent' } if exists ( $self->{ 'sub_indent' } );
return ' ';
}
sub set_sub_indent {
my ( $self, $indent ) = @_;
$self->{ 'sub_indent' } = $indent;
return $self;
}
sub try_fold_at {
my ( $self ) = @_;
return $self->{ 'try_fold_at' } if exists ( $self->{ 'try_fold_at' } );
return 800;
}
sub set_try_fold_at {
my ( $self, $length ) = @_;
$self->{ 'try_fold_at' } = $length;
return $self;
}
sub force_fold_at {
my ( $self ) = @_;
return $self->{ 'force_fold_at' } if exists ( $self->{ 'force_fold_at' } );
return 900;
}
sub set_force_fold_at {
my ( $self, $length ) = @_;
$self->{ 'force_fold_at' } = $length;
return $self;
}
sub string {
my( $self, $string ) = @_;
push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::String->new_from_value( $string );
return $self;
}
sub space {
my ( $self, $string ) = @_;
push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Space->new_from_value( $string );
return $self;
}
sub separator {
my ( $self, $string ) = @_;
push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Separator->new_from_value( $string );
return $self;
}
sub comment {
my ( $self, $string ) = @_;
push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Comment->new_from_value( $string );
return $self;
}
sub assignment {
my ( $self, $string ) = @_;
push @{ $self->{ 'string' } }, Mail::AuthenticationResults::Token::Assignment->new_from_value( $string );
return $self;
}
sub as_string {
my ( $self ) = @_;
my $string = q{};
my $string_length = 0;
my $content_added = 0;
my $sections = [];
my $stack = [];
my $last_type;
foreach my $part ( @{ $self->{ 'string' } } ) {
if ( $part->is() eq 'space' && $last_type ne 'space' ) {
# We have a folding space
push @$sections, $stack if @$stack;
$stack = [];
}
push @$stack, $part;
$last_type = $part->is();
}
push @$sections, $stack if @$stack;
my $eol = $self->eol();;
my $indent = $self->indent();
my $sub_indent = $self->sub_indent();
my $fold_length = 0;
SECTION:
while ( my $section = shift @$sections ) {
if ( $section->[0]->is() eq 'space' && $section->[0]->value() eq $eol ) {
# This section starts a new line
$fold_length = 0;
if ( ! exists( $section->[0]->{ '_folded' } ) ) {
if ( $section->[1]->is() eq 'space' ) {
# Take the last indent value for the fold indent
$indent = $section->[1]->value();
}
}
}
my $section_string = join( q{}, map { $_->value() } @$section );
my $section_length = length( $section_string );
if ( $fold_length + $section_length > $self->try_fold_at() ) {
if ( $fold_length > 0 ) {
# Remove whitespace tokens at beginning of section
while ( $section->[0]->is() eq 'space' ) {
shift @$section;
}
# Insert new folding whitespace at beginning of section
unshift @$section, Mail::AuthenticationResults::Token::Space->new_from_value( $indent . $sub_indent );
unshift @$section, Mail::AuthenticationResults::Token::Space->new_from_value( $eol );
$section->[0]->{ '_folded' } = 1;
unshift @$sections, $section;
next SECTION;
}
else {
# ToDo:
# This section alone is over the line limit
# It already starts with a fold, so we need to remove
# some of it to a new line if we can.
# Strategy 1: Fold at a relevant token boundary
my $first_section = [];
my $second_section = [];
push @$second_section, Mail::AuthenticationResults::Token::Space->new_from_value( $eol );
push @$second_section, Mail::AuthenticationResults::Token::Space->new_from_value( $indent . $sub_indent );
$second_section->[0]->{ '_folded' } = 1;
my $first_section_length = 0;
foreach my $part ( @$section ) {
my $part_length = length $part->value();
if ( $part_length + $first_section_length < $self->try_fold_at() ) {
push @$first_section, $part;
$first_section_length += $part_length;
}
else {
push @$second_section, $part;
$first_section_length = $self->try_fold_at() + 1; # everything from this point goes onto second
}
}
# Do we have a first and second section with actual content?
if ( ( grep { $_->is() ne 'space' } @$first_section ) &&
( grep { $_->is() ne 'space' } @$second_section ) ) {
unshift @$sections, $second_section;
unshift @$sections, $first_section;
next SECTION;
}
# We MUST fold at $self->force_fold_at();
# Strategy 2: Force fold at a space within a string
# Strategy 3: Force fold anywhere
# We assume that force fold is greater than try fold
}
}
$string .= $section_string;
$fold_length += $section_length;
}
return $string;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Mail::AuthenticationResults::FoldableHeader - Class for modelling a foldable header string
=head1 VERSION
version 2.20210112
=head1 DESCRIPTION
Class representing a foldable Authentication Results header string
=head1 METHODS
=head2 new( $args )
Return a new instance of this class
=head2 eol()
Return the current eol marker.
=head2 set_eol( $eol )
Set the current eol marker.
=head2 indent()
Return the current base indent string.
Defaults to 4 spaces.
=head2 set_indent( $indent )
Set the current base indent string.
=head2 sub_indent()
Return the current fold indent string.
This is added to the current indent for folded headers.
Defaults to 2 spaces.
=head2 set_sub_indent( $indent )
Set the current fold indent string.
=head2 try_fold_at()
Return the length of header line for triggering a fold attempt
=head2 set_try_fold_at( $length )
Set the length of header line for triggering a fold attempt.
Defaults to 800.
=head2 force_fold_at()
Return the length of header line for forcing a fold.
=head2 set_force_fold_at( $length )
Set the length of header line for forcing a fold.
Defaults to 900.
=head2 string( $string )
Add $string to this header string
In this context, string can include a quoted string, or a string with assignment operators embedded within it.
A string is a unit of data which we do not want to break with a fold.
=head2 space( $string )
Add a space $string to this header string
In this context, a space can be a single space, multiple spaces, or a folding space.
A space is a unit of data which would be an ideal spot to insert a fold.
=head2 separator( $string )
Add a separator $string to this header string
In this context, a separator is the ; string or the / string.
=head2 comment( $string )
Add a comment $string to this header string
In this context, a comment is a comment string. A comment is a unit of data which we do not want to break with a fold.
=head2 assignment( $string )
Add an assignment $string to this header string
In this context, as assignment is the = string.
=head2 as_string()
Return the current header string
=head1 AUTHOR
Marc Bradshaw <marc@marcbradshaw.net>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2020 by Marc Bradshaw.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|