/[suikacvs]/messaging/manakai/lib/Message/Field/Params.pm
Suika

Contents of /messaging/manakai/lib/Message/Field/Params.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Sat Mar 23 10:41:33 2002 UTC (22 years, 8 months ago) by wakaba
Branch: MAIN
2002-03-23  wakaba <w@suika.fam.cx>

	* Params.pm, ContentType.pm, ContentDisposition.pm:
	New files.

1 wakaba 1.1
2     =head1 NAME
3    
4     Message::Field::Params Perl module
5    
6     =head1 DESCRIPTION
7    
8     Perl module for parameters field body (such as C<Content-Type:>).
9    
10     =cut
11    
12     package Message::Field::Params;
13     use strict;
14     require 5.6.0;
15     use re 'eval';
16     use vars qw(%DEFAULT %REG $VERSION);
17     $VERSION=do{my @r=(q$Revision: 1.1 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
18    
19     use Carp;
20     use overload '@{}' => sub {shift->_delete_empty()->{param}},
21     '""' => sub {shift->stringify};
22    
23     $REG{WSP} = qr/[\x09\x20]/;
24     $REG{FWS} = qr/[\x09\x20]*/;
25    
26     $REG{comment} = qr/\x28(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x27\x2A-\x5B\x5D-\xFF]+|(??{$REG{comment}}))*\x29/;
27     $REG{quoted_string} = qr/\x22(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x21\x23-\x5B\x5D-\xFF])*\x22/;
28     $REG{domain_literal} = qr/\x5B(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x5A\x5E-\xFF])*\x5D/;
29     $REG{atext} = qr/[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]+/;
30     $REG{atext_dot} = qr/[\x21\x23-\x27\x2A\x2B\x2D-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]+/;
31     $REG{token} = qr/[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+/;
32     $REG{attribute_char} = qr/[\x21\x23-\x24\x26\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+/;
33    
34     $REG{param} = qr/(?:$REG{atext_dot}|$REG{quoted_string})(?:$REG{atext_dot}|$REG{quoted_string}|$REG{WSP}|,)*/;
35     ## more naive C<parameter>. (Comma is allowed for RFC 1049)
36     $REG{parameter} = qr/$REG{token}=(?:$REG{token}|$REG{quoted_string})?/;
37     ## as defined by RFC 2045, not RFC 2231.
38    
39     $REG{M_quoted_string} = qr/\x22((?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x21\x23-\x5B\x5D-\xFF])*)\x22/;
40     $REG{M_parameter} = qr/($REG{token})=($REG{token}|$REG{quoted_string})?/;
41     ## as defined by RFC 2045, not RFC 2231.
42     $REG{M_parameter_name} = qr/($REG{attribute_char}+)(?:\*([0-9]+)(\*)?|(\*))/;
43     ## as defined by RFC 2231.
44     $REG{M_parameter_extended_value} = qr/([^']*)'([^']*)'($REG{token}*)/;
45     ## as defined by RFC 2231, but more naive.
46    
47     $REG{NON_atext} = qr/[^\x21\x23-\x27\x2A\x2B\x2D\x2F\x30-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]/;
48     $REG{NON_atext_dot} = qr/[^\x21\x23-\x27\x2A\x2B\x2D-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]/;
49     $REG{NON_token} = qr/[^\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]/;
50     $REG{NON_attribute_char} = qr/[^\x21\x23-\x24\x26\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]/;
51    
52    
53     %DEFAULT = (
54     parameter_value_max => 78,
55     use_parameter_extension => -1,
56     );
57    
58     =head2 Message::Field::Params->new ([%option])
59    
60     Returns new Message::Field::Params. Some options can be given as hash.
61    
62     =cut
63    
64     sub new ($;%) {
65     my $class = shift;
66     my $self = bless {option => {@_}}, $class;
67     $self->_initialize_new ();
68     for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}}
69     $self;
70     }
71    
72     ## Initialization for new () method.
73     sub _initialize_new ($;%) {
74     my $self = shift;
75     #for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}}
76     }
77    
78     =head2 Message::Field::Params->parse ($nantara, [%option])
79    
80     Parse Message::Field::Params and new ContentType instance.
81     Some options can be given as hash.
82    
83     =cut
84    
85     sub parse ($$;%) {
86     my $class = shift;
87     my $body = shift;
88     my $self = bless {option => {@_}}, $class;
89     $self->_initialize_parse ();
90     for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}}
91     $body = $self->_delete_fws ($self->_delete_comment ($body));
92     my @b = ();
93     $body =~ s{$REG{FWS}($REG{param})$REG{FWS}(?:;$REG{FWS}|$)}{
94     my $param = $1;
95     push @b, $self->_parse_param ($param);
96     }goex;
97     @b = $self->_restore_param (@b);
98     $self->_save_param (@b);
99     $self;
100     }
101    
102     ## Initialization for parse () method.
103     sub _initialize_parse ($;%) {
104     my $self = shift;
105     #for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}}
106     }
107    
108     sub _parse_param ($$) {
109     my $self = shift;
110     my $param = shift;
111     if ($param =~ /^$REG{M_parameter}$/) {
112     my ($name, $value) = (lc $1, $2);
113     my ($seq, $isencoded, $charset, $lang) = (-1, 0, '', '');
114     if ($name =~ /^$REG{M_parameter_name}$/) {
115     ($name, $seq, $isencoded) = ($1, $4?-1:$2, ($3||$4)?1:0);
116     }
117     if ($isencoded && $value =~ /^$REG{M_parameter_extended_value}$/) {
118     ($charset, $lang, $value) = ($1, $2, $3);
119     }
120     return [$name, {value => $value, seq => $seq, is_encoded => $isencoded,
121     charset => $charset, language => $lang, is_parameter => 1}];
122     } else {
123     return [$param, {is_parameter => 0}];
124     }
125     }
126    
127     sub _restore_param ($@) {
128     my $self = shift;
129     my @p = @_;
130     my @ret;
131     my %part;
132     for my $i (@p) {
133     if ($i->[1]->{is_parameter}) {
134     my $p = $i->[1];
135     if ($p->{seq}<0) {
136     my $s = $p->{value};
137     if ($p->{is_encoded}) {
138     $s =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr(hex($1))/eg;
139     } else {
140     $s = $self->_unquote_if_quoted_string ($p->{value});
141     }
142     push @ret, [$i->[0], {value => $s, language => $p->{language},
143     charset => $p->{charset}, is_parameter => 1}];
144     } else {
145     $part{$i->[0]}->[$p->{seq}] = {
146     value => $self->_unquote_if_quoted_string ($p->{value}),
147     language => $p->{language}, charset => $p->{charset},
148     is_encoded => $p->{is_encoded}};
149     }
150     } else {push @ret, $i}
151     }
152     for my $name (keys %part) {
153     my $t = join '', map {
154     my $v = $_;
155     my $s = $v->{value};
156     $s =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr(hex($1))/eg if $v->{is_encoded};
157     $s;
158     } @{$part{$name}};
159     push @ret, [$name, {value => $t, charset => $part{$name}->[0]->{charset},
160     language => $part{$name}->[0]->{language},
161     is_parameter => 1}];
162     }
163     @ret;
164     }
165    
166     sub _save_param ($@) {
167     my $self = shift;
168     my @p = @_;
169     $self->{param} = \@p;
170     $self;
171     }
172    
173     =head2 $self->add ($name, [$value]. [%option]
174    
175     Adds parameter name=value pair.
176    
177     Example:
178     $self->add (title => 'foo of bar'); ## title="foo of bar"
179     $self->add (subject => 'hogehoge, foo'); ## subject*=''hogehoge%2C%20foo
180     $self->add (foo => 'bar', language => 'en') ## foo*='en'bar
181     $self->add ('text/plain', '', value => 1) ## text/plain
182    
183     This method returns array reference of (name, {value => value, attribute...}).
184    
185     Available options: charset (charset name), language (language tag),
186     value (1/0, see example above).
187    
188     =cut
189    
190     sub add ($$;$%) {
191     my $self = shift;
192     my ($name, $value, %option) = (lc shift, shift, @_);
193     my $p = [$name, {value => $value, charset => $option{charset},
194     is_parameter => 1, language => $option{language}}];
195     $p->[1]->{is_parameter} = 0 if !$value && $option{value}>0;
196     croak "add: \$name contains of non-attribute-char: $name"
197     if $p->[1]->{is_parameter} && $name =~ /$REG{NON_attribute_char}/;
198     $p->[1]->{value} = $self->_param_value ($name => $p->[1]->{value});
199     if ($option{prepend}) {
200     unshift @{$self->{param}}, $p;
201     } else {
202     push @{$self->{param}}, $p;
203     }
204     $p;
205     }
206     sub replace ($$;$%) {
207     my $self = shift;
208     my ($name, $value, %option) = (lc shift, shift, @_);
209     for my $param (@{$self->{param}}) {
210     if ($param->[0] eq $name) {
211     $param->[1] = {value => $value, charset => $option{charset},
212     is_parameter => 1, language => $option{language}};
213     $param->[1]->{is_parameter} = 0 if !$value && $option{value}>0;
214     $param->[1]->{value} = $self->_param_value ($name => $param->[1]->{value});
215     return $param;
216     }
217     }
218     my $p = [$name, {value => $value, charset => $option{charset},
219     is_parameter => 1, language => $option{language}}];
220     $p->[1]->{is_parameter} = 0 if !$value && $option{value}>0;
221     croak "replace: \$name contains of non-attribute-char: $name"
222     if $p->[1]->{is_parameter} && $name =~ /$REG{NON_attribute_char}/;
223     $p->[1]->{value} = $self->_param_value ($name => $p->[1]->{value});
224     push @{$self->{param}}, $p;
225     $p;
226     }
227    
228     sub delete ($$;%) {
229     my $self = shift;
230     my ($name, $index) = (lc shift, shift);
231     my $i = 0;
232     for my $param (@{$self->{param}}) {
233     if ($param->[0] eq $name) {
234     $i++;
235     if ($index == 0 || $i == $index) {
236     undef $param;
237     return $self if $i == $index;
238     }
239     }
240     }
241     $self;
242     }
243    
244     sub count ($;$%) {
245     my $self = shift;
246     my ($name) = (lc shift);
247     unless ($name) {
248     $self->_delete_empty ();
249     return $#{$self->{param}}+1;
250     }
251     my $count = 0;
252     for my $param (@{$self->{param}}) {
253     if ($param->[0] eq $name) {
254     $count++;
255     }
256     }
257     $count;
258     }
259    
260    
261     sub parameter ($$;$) {
262     my $self = shift;
263     my $name = lc shift;
264     my $newvalue = shift;
265     return $self->replace ($name => $newvalue,@_)->[1]->{value} if defined $newvalue;
266     my @ret;
267     for my $param (@{$self->{param}}) {
268     if ($param->[0] eq $name) {
269     unless (wantarray) {
270     $self->{param}->[1]->{value}
271     = $self->_param_value ($name => $self->{param}->[1]->{value});
272     return $param->[1]->{value};
273     } else {
274     $self->{param}->[1]->{value}
275     = $self->_param_value ($name => $self->{param}->[1]->{value});
276     push @ret, $param->[1]->{value};
277     }
278     }
279     }
280     @ret;
281     }
282    
283     sub parameter_name ($$;$) {
284     my $self = shift;
285     my $i = shift;
286     my $newname = shift;
287     if ($newname) {
288     return 0 if $newname =~ /$REG{NON_attribute_char}/;
289     $self->{param}->[$i]->[0] = $newname;
290     }
291     $self->{param}->[$i]->[0];
292     }
293     sub parameter_value ($$;$) {
294     my $self = shift;
295     my $i = shift;
296     my $newvalue = shift;
297     if ($newvalue) {
298     $newvalue = $self->_param_value ($self->{param}->[$i]->[0] => $newvalue);
299     $self->{param}->[$i]->[1]->{value} = $newvalue;
300     }
301     $self->{param}->[$i]->[1]->{value}
302     = $self->_param_value
303     ($self->{param}->[$i]->[0] => $self->{param}->[$i]->[1]->{value});
304     $self->{param}->[$i]->[1]->{value};
305     }
306    
307     ## Hook called before returning C<value>.
308     ## $self->_param_value ($name, $value);
309     sub _param_value ($$$) {$_[2]}
310    
311     sub _delete_empty ($) {
312     my $self = shift;
313     my @ret;
314     for my $param (@{$self->{param}}) {
315     push @ret, $param if $param->[0];
316     }
317     $self->{param} = \@ret;
318     $self;
319     }
320    
321    
322     =head2 $self->stringify ([%option])
323    
324     Returns Message::Field::Params as a string.
325    
326     =head2 $self->as_string ([%option])
327    
328     An alias of C<stringify>.
329    
330     =cut
331    
332     sub stringify ($;%) {
333     my $self = shift;
334     my %option = @_;
335     my $use_xparam = $option{use_parameter_extension}
336     || $self->{option}->{use_parameter_extension};
337     $option{parameter_value_max}
338     ||= $self->{option}->{parameter_value_max};
339     $self->_delete_empty ();
340     join '; ',
341     map {
342     my $v = $_->[1];
343     my $new = '';
344     if ($v->{is_parameter}) {
345     my ($encoded, @value) = (0, '');
346     if ($use_xparam>0 && ($v->{charset} || $v->{language})) {
347     my ($charset, $lang);
348     $encoded = 1;
349     ($charset, $lang) = ($v->{charset}, $v->{language});
350     ## Note: %-quoting for charset and for language is not allowed.
351     ## But charset name can be included non-sttribute-char such as "'".
352     ## How can we treat this?
353     $charset =~ s/($REG{NON_attribute_char})/sprintf('%%%02X', ord $1)/ge;
354     $lang =~ s/($REG{NON_attribute_char})/sprintf('%%%02X', ord $1)/ge;
355     if (length $v->{value} > $option{parameter_value_max}) {
356     for my $i (0..length ($v->{value})/$option{parameter_value_max}) {
357     $value[$i] = substr ($v->{value}, $i*$option{parameter_value_max},
358     $option{parameter_value_max});
359     }
360     } else {$value[0] = $v->{value}}
361     for my $i (0..$#value) {
362     $value[$i] =~ s/($REG{NON_attribute_char})/sprintf('%%%02X', ord $1)/ge;
363     }
364     $value[0] = "${charset}'${lang}'".$value[0];
365     } elsif (length $v->{value} == 0) {
366     $value[0] = '""';
367     } else {
368     if ($use_xparam>0 && length $v->{value} > $option{parameter_value_max}) {
369     for my $i (0..length ($v->{value})/$option{parameter_value_max}) {
370     $value[$i] = $self->_quote_unsafe_string
371     (substr ($v->{value}, $i*$option{parameter_value_max},
372     $option{parameter_value_max}), unsafe => 'NON_attribute_char');
373     }
374     } else {
375     $value[0] = $self->_quote_unsafe_string
376     ($v->{value}, unsafe => 'NON_attribute_char');
377     }
378     }
379     ## Note: quoted-string for parameter name is not allowed.
380     ## But it is better than output bare non-atext.
381     if ($#value == 0) {
382     $new =
383     $self->_quote_unsafe_string ($_->[0], unsafe => 'NON_attribute_char')
384     .($encoded?'*':'').'='.$value[0];
385     } else {
386     my @new;
387     my $name = $self->_quote_unsafe_string
388     ($_->[0], unsafe => 'NON_attribute_char');
389     for my $i (0..$#value) {
390     push @new, $name.'*'.$i.($encoded?'*':'').'='.$value[$i];
391     }
392     $new = join '; ', @new;
393     }
394     } else {
395     $new = $self->_quote_unsafe_string ($_->[0], unsafe => 'NON_token');
396     }
397     $new;
398     } @{$self->{param}}
399     ;
400     }
401     sub as_string ($;%) {shift->stringify (@_)}
402    
403     =head2 $self->option ($option_name)
404    
405     Returns/set (new) value of the option.
406    
407     =cut
408    
409     sub option ($$;$) {
410     my $self = shift;
411     my ($name, $newval) = @_;
412     if ($newval) {
413     $self->{option}->{$name} = $newval;
414     }
415     $self->{option}->{$name};
416     }
417    
418     sub _quote_unsafe_string ($$;%) {
419     my $self = shift;
420     my $string = shift;
421     my %option = @_;
422     $option{unsafe} ||= 'NON_atext_dot';
423     if ($string =~ /$REG{$option{unsafe}}/ || $string =~ /$REG{WSP}$REG{WSP}+/) {
424     $string =~ s/([\x22\x5C])/\x5C$1/g;
425     $string = '"'.$string.'"';
426     }
427     $string;
428     }
429    
430     =head2 $self->_unquote_quoted_string ($string)
431    
432     Unquote C<quoted-string>. Get rid of C<DQUOTE>s and
433     C<REVERSED SOLIDUS> included in C<quoted-pair>.
434     This method is intended for internal use.
435    
436     =cut
437    
438     sub _unquote_quoted_string ($$) {
439     my $self = shift;
440     my $quoted_string = shift;
441     $quoted_string =~ s{$REG{M_quoted_string}}{
442     my $qtext = $1;
443     $qtext =~ s/\x5C([\x00-\xFF])/$1/g;
444     $qtext;
445     }goex;
446     $quoted_string;
447     }
448    
449     sub _unquote_if_quoted_string ($$) {
450     my $self = shift;
451     my $quoted_string = shift;
452     $quoted_string =~ s{^$REG{M_quoted_string}$}{
453     my $qtext = $1;
454     $qtext =~ s/\x5C([\x00-\xFF])/$1/g;
455     $qtext;
456     }goex;
457     $quoted_string;
458     }
459    
460     =head2 $self->_delete_comment ($field_body)
461    
462     Remove all C<comment> in given strictured C<field-body>.
463     This method is intended for internal use.
464    
465     =cut
466    
467     sub _delete_comment ($$) {
468     my $self = shift;
469     my $body = shift;
470     $body =~ s{($REG{quoted_string}|$REG{domain_literal})|$REG{comment}}{
471     my $o = $1; $o? $o : ' ';
472     }gex;
473     $body;
474     }
475    
476     sub _delete_fws ($$) {
477     my $self = shift;
478     my $body = shift;
479     $body =~ s{($REG{quoted_string}|$REG{domain_literal})|$REG{WSP}+}{
480     my $o = $1; $o? $o : '';
481     }gex;
482     $body;
483     }
484    
485     =head1 LICENSE
486    
487     Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
488    
489     This program is free software; you can redistribute it and/or modify
490     it under the terms of the GNU General Public License as published by
491     the Free Software Foundation; either version 2 of the License, or
492     (at your option) any later version.
493    
494     This program is distributed in the hope that it will be useful,
495     but WITHOUT ANY WARRANTY; without even the implied warranty of
496     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
497     GNU General Public License for more details.
498    
499     You should have received a copy of the GNU General Public License
500     along with this program; see the file COPYING. If not, write to
501     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
502     Boston, MA 02111-1307, USA.
503    
504     =head1 CHANGE
505    
506     See F<ChangeLog>.
507     $Date: 2002/03/21 04:33:44 $
508    
509     =cut
510    
511     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24